
/* This file contains JavaScript that is common among pages in the Shipley Credit Application */

var debug = false;


//Uncomment the following line to hide javascript errors from users 
//window.onerror=null;

/* Code to identify incompatible browser */
if (!document.all) {
    if (!document.getElementById) {
        window.location = "/browser.html"
    }
}

browserName=navigator.appName;

/* Code to identify the browser */
function getBrowser() {
    
    if (document.all) {
        return "IE";
    }
    return "NS";
}

function windowOpen(noteurl) {
    if (document.all) {
       XLS=window.open(noteurl,"displayWindow","toolbar=no,menubar=no,resizable=yes,width=790,height=540,scrollbars=yes,left=10,top=10");
    } else {
       XLS=window.open(noteurl,"displayWindow","toolbar=no,menubar=no,resizable=yes,width=790,height=540,scrollbars=yes,screenX=10,screenY=10");
       XLS.focus();
    }
}

function windowOpenSmall(noteurl) {
    if (document.all) {
       XLS=window.open(noteurl,"displayWindow","toolbar=no,menubar=no,resizable=yes,width=430,height=300,scrollbars=yes,left=10,top=10");
    } else {
       XLS=window.open(noteurl,"displayWindow","toolbar=no,menubar=no,resizable=yes,width=430,height=300,scrollbars=yes,screenX=10,screenY=10");
       XLS.focus();
    }
}

function openFullSize(URL){

    var win = window.open(URL);
    win.moveTo(0,0);
    win.resizeTo(screen.availWidth,screen.availHeight);
}

/* This came from Header.jsp.... not sure what its for */
function openURL(URL,queryStr) {
    if(queryStr !="") {
        myHelp=window.location(URL + queryStr);
    }   
}   

/* Directs user to logoutConfirm.jsp */
function logout() {
    var URL = "/cw/logoutConfirm.jsp?";
    var qstring = document.location.toString();
    document.location = URL + qstring;
}

/* Function to check the length of a textbox, shows a message and truncates if it is too long */
function CheckLength(length, tbox) {

    if (tbox.value.length > length) {
        alert('No more text can be entered.'); 
        tbox.value = tbox.value.substring(0,length);       
        return false; 
    }
    return true;    
}

/* Sets the value within a selectbox */
function setSelectBoxValue(selectbox, value) {
	if(selectbox && selectbox.options) {
	    for (i = 0; i < selectbox.options.length; i++) {
		if (selectbox.options[i].value == value) {
		    selectbox.selectedIndex=[i];
		    break;
		}
	    }
	}
}

function setInputValue(input, value) {
    
    if (input.type == "select-one") {
        setSelectBoxValue(selectbox, value);
    } else if (input.type == "hidden" || 
                input.type == "text" || 
                input.type == "textarea") {
        input.value = value;
    } else if (input.type == "checkbox") {
        input.checked = value;
    } else if (button[0]) {
        setRadioButtonValue(input, value);
    } else {
        alert("Attempting to set value of undefined type: " + input + " : " + value);
    }
}

/* Sets the value within a radio button set */
function setRadioButtonValue(button, value) {
    
    for (i = 0; i < button.length; i++) {
        if (value == "")  {
            button[i].checked = false;
        }
        if (button[i].value == value) {
            button[i].checked = true;
            break;
        }
    }
}

/* Sets text within a named <SPAN> to the specified html */
function setInnerHtml(name, html) {

    if (getBrowser() == "IE") { 
      if (document.all[name] == null) {
    if (debug){
          alert(name + " not found");
    }
      } else {
        document.all[name].innerHTML = html;
      }
    } else { 
        document.getElementById(name).innerHTML = html;
    }  
}

/* Formats a float value to have exactly two decimal places, as in currency */
function twoDecimalPlaces(pmt){

    pmt = ((Math.round(pmt * 100)) / 100);
    var pmtStr = pmt.toString();
    if (pmtStr.indexOf(".") == -1) {
        pmtStr = pmtStr + ".00";
    } else if ((pmtStr.indexOf(".")) == (pmtStr.length - 2)) {
        pmtStr = pmtStr + "0";
    }
    return pmtStr;
}

function writeError(message){

        var html = '<table width="100%" border="0" cellpadding="0"  cellspacing="0" align="center">';
        html = html + '<tr><td width="100%" valign="top" class="errortxt"><b><ul><br>';
        html = html + '<li>' + message + '</li>';
        html = html + '</ul></b></td></tr></table>';
        setInnerHtml("errorBlock",html);
}

function NewWindow(mypage,myname,w,h,scroll,pos){
  if(pos=="random") {
    LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
    TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
  }
  if(pos=="center"){
    LeftPosition=(screen.width)?(screen.width-w)/2:100;
    TopPosition=(screen.height)?(screen.height-h)/2:100;
  } else if((pos!="center" && pos!="random") || pos==null){
    LeftPosition=0;
    TopPosition=20;
  }

  settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
  win=window.open(mypage,myname,settings);
}

function getHTTPObject() {
  var xmlhttp;
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return xmlhttp;
}

function setFocus(formElement) {
  formElement.focus();
  formElement.select();
}