/*
 * (C) Copyright New Zealand Law Society 2007
 *
 * Project: Registration Database
 * File: nzls-common.js
 *
 * Created on 02 October 2007, 16:31 by SeungHyunK
 */

function isMozilla() {
  return navigator.appName=="Netscape";
}

function isIE() {
  return navigator.appName=="Microsoft Internet Explorer";
}

function setModified() {
  if (document.getElementById("form1:hiddenField_modified") != null) {
    document.getElementById("form1:hiddenField_modified").value = "true";
  }
}

function checkModified_hyperlink(hyperlink) {
  if (document.getElementById("form1:hiddenField_modified") != null) {
      var modified = document.getElementById("form1:hiddenField_modified").value;
      if (modified) {
        var agree = confirm("You have not saved changes to this screen. Please confirm you want to discard changes and continue.");
        if (agree)
           return hyperlink_submit(hyperlink, 'form1', null);
        else
            return false;
      }
  }
  return hyperlink_submit(hyperlink, 'form1', null);
}

function checkModified() {
  if (document.getElementById("form1:hiddenField_modified") != null) {
      var modified = document.getElementById("form1:hiddenField_modified").value;
      if (modified) {
        var agree = confirm("You have not saved changes to this screen. Please confirm you want to discard changes and continue.");
        if (agree)
           return true;
        else
            return false;
      }
  }
  return true;
}

function launchPDF(elementId) {
    var pdfPath=document.getElementById(elementId);
    if(pdfPath.value != '') {
        window.open(pdfPath.value, 'mywindow','width=600,height=600,location=1,resizable=1,scrollbars=1,toolbar=1');
    } 
}

function autoSubmit(){
  document.getElementById('form1:button_autoSubmit').click();
}

function openFileOpener(pathToPage){
    if(pathToPage.value != '') {
        return window.open(pathToPage, 'mywindow','width=600,height=600,location=1,resizable=1,scrollbars=1,toolbar=1');
    }
}

function doPopup() {
    attachmentFileOpener=window.open('', 'attachmentFileOpener','width=600,height=600,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,top=0,left=0,bottom=0');
    attachmentFileOpener.focus();
    attachmentFileOpener.close();
}

// This function is used to set the value for a hidden field bound to an Integer 
// value in search criteria objects.  At the point of writing, this is only used
// for search screens where the ID is being entered.  See Defect NZLS00000282.
function setToValueIfFromValueIsNumeric(fromElementId, toElementId){
    // Regular expression matches non-digit characters.
    var regEx = new RegExp('[^0-9]');
    var fromElement = document.getElementById(fromElementId);
    
    // If we don't find a non-digit character, then the value is numeric, and
    // we can set the to value
    if (!(regEx.test(fromElement.value))){
        document.getElementById(toElementId).value = fromElement.value;
        
    // If we found a non-digit character, set the to value to a negative number,
    // which will return no results in the search screens
    } else {
        
        document.getElementById(toElementId).value = '-1';
    }
}

function showDivAndSelectDivContents(id, display) {
    var div, style;
    if(document.getElementById) {
        div = document.getElementById(id);
    }
    if(div && (style = div.style)) {
        style.display = display;
    }
    if (display != 'none'){
        var childId = div.firstChild.id;
        var t=setTimeout('selectNode(document.getElementById(\'' + childId + '\'))', 10);
    }
}

function selectNode (node) {
//This is a third party function written by Martin Honnen
//In comp.lang.javascript
//http://groups-beta.google.com/group/comp.lang.javascript/browse_thread/thread/2b389e61c7b951f2/99b5f1bee9922c39?lnk=gst&q=(doc+%3D+node.ownerDocument)+%26%26+(win+%3D+doc .defaultView)&rnum=1&hl=en#99b5f1bee9922c39
    var selection, range, doc, win;
    if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof
        win.getSelection != 'undefined' && typeof doc.createRange != 'undefined'
        && (selection = window.getSelection()) && typeof
        selection.removeAllRanges != 'undefined') {
        range = doc.createRange();
        range.selectNode(node);
        selection.removeAllRanges();
        selection.addRange(range);
    } else if (document.body && typeof document.body.createTextRange !=
            'undefined' && (range = document.body.createTextRange())) {
        range.moveToElementText(node);
        range.select();
    }
}


function toggleVisibility(id){
    var div, display, newDisplay;
    if(document.getElementById) {
        div = document.getElementById(id);
    }
    
    display = div.style.display;
    
    if (display == 'none' || display == ''){
        newDisplay = 'block';
    } else {
        newDisplay = 'none';
    }
    showDivAndSelectDivContents(id, newDisplay);
}

function openWindow(url){
    window.open(url,'mywindow','width=700,height=600,resizable=1');
}

function setFocus(id) {
    var element = document.getElementById(id);
    if (element && element.focus) {
        element.focus();
    }
}

function setDisabledWhenNotValue(elementToDisableId, elementWithValueId, triggerValue){
    if (document.getElementById(elementWithValueId).value != triggerValue){
        elementToDisable = document.getElementById(elementToDisableId);
        elementToDisable.disabled = true;
        elementToDisable.className = elementToDisable.className + " TxtFldDis";
    }
}

function transferValue(fromId, toId){
    var fromVal = document.getElementById(fromId).value;
    document.getElementById(toId).value = fromVal;

}

