// JavaScript Document
var emailException=',!#$%&\'*+-/=?^_`{|}~.@';
var phoneException='()-';
var zipException='-';
var letternumberChar='abcdefghijklmnopqrstuvwxyz0123456789';
var numberonlyChar='0123456789';
var currencyonlyChar=numberonlyChar+'.';

function restrictChar(e,type,exception){
  var key;
  var keychar;
  var normalChar;
  if(type=='letternumber'){
    normalChar=letternumberChar;
  } else if (type=='numberonly') {
    normalChar=numberonlyChar;
  } else if (type=='currency'){
    normalChar=currencyonlyChar;  
  }
  if(exception){
    normalChar+=exception  
  }
  if (window.event) {
    key = window.event.keyCode;
  } else if (e) {
    key = e.which;
  } else {
    return true;
  }
  keychar = String.fromCharCode(key);
  keychar = keychar.toLowerCase();
  // control keys
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) {
    return true;
  // alphas and numbers
  } else if (((normalChar).indexOf(keychar) > -1)) {
    return true;
  } else {
    return false;
  }
}

function checkSSN(index) {
  stripped =  index.value.replace(/[^0-9]/g,'');
  if (stripped.length == 9) {
    index.value = stripped.substr(0,3)+'-'+stripped.substr(3,2)+'-'+stripped.substr(5,4);
  }
}

function selectImage(index,ref,target,hideTarget,showTarget){
  var f;
  var g=document.getElementById(target);
  var h=document.getElementById('finalImage');
  if(index!='reset'){
    if(index.firstChild.tagName){
      f=index.firstChild;
    } else {
      f=index.previousSibling;
      while(f.innerHTML == null){
        f = f.previousSibling;
      }
      f=f.firstChild;
    }
    h.src=f.src;
  }
  g.value=ref;
  document.getElementById(hideTarget).className+=' hide';
  document.getElementById(showTarget).className=document.getElementById(showTarget).className.replace(/hide/g,'');
}

function checkImage(index,showTarget,hideTarget){
  if(document.getElementById(index).value!=-1){
    var tempName = "img"+document.getElementById(index).value;
    var f=document.getElementById(tempName);
    document.getElementById('finalImage').src=f.src;
    document.getElementById(hideTarget).className+=' hide';
    document.getElementById(showTarget).className=document.getElementById(showTarget).className.replace(/hide/g,'');
  }
}

function hoverBorder(index,color){
  index.style.borderColor=color;
}

function enableButton(index,target,disableTarget){
  var f,g,h;
  if(index.id){f=index}else{f=document.getElementById(index)};
  if(target.id){g=target}else{g=document.getElementById(target)};
  if(disableTarget.id){h=disableTarget}else{h=document.getElementById(disableTarget)};
  if(f.checked){
    g.className=g.className.replace(/hide/g,'');
    h.className=h.className.replace(/hide/g,'')+' hide';
  } else {
    h.className=h.className.replace(/hide/g,'');
    g.className=g.className.replace(/hide/g,'')+' hide';
  }
}

function helpDisplay(index){
  var myRE = new RegExp("helpText", "i");
  var newRE = new RegExp("hide","i");
  var f=index.nextSibling;
  while(f.innerHTML == null){
    f = f.nextSibling;
  }
  if (f.className.match(myRE)){
    if(f.className.match(newRE)){
      f.className=f.className.replace(newRE,'');
    } else {
      f.className=f.className.replace(newRE,'')+' hide';
    }
  } else {
    var f=index.parentNode.nextSibling;
    while(f.innerHTML == null){
      f = f.nextSibling;
    }
    if (f.className.match(myRE)){
      if(f.className.match(newRE)){
        f.className=f.className.replace(newRE,'');
      } else {
        f.className=f.className.replace(newRE,'')+' hide';
      }
    }
  }
}

function showHide(index,target){
  document.getElementById(index).className=document.getElementById(index).className.replace(/hide/g,'')+' hide';
  document.getElementById(target).className=document.getElementById(target).className.replace(/hide/g,'');
}

function writeField(target,index,other){
  if(target!=-1){
    document.getElementById(target).value=index;
  }
  var f=document.getElementById(other).getElementsByTagName('input');
  for(var i=0;i<f.length;i++){
    f[i].value='';
  }
}

function checkAccount(index){
  var f=document.getElementById(index).value;
  document.getElementById(f).className=document.getElementById(f).className.replace(/hide/g,'');
  for(var i=0;i<typeArray.length;i++){
    if(typeArray[i]!=f){
      document.getElementById(typeArray[i]).className=document.getElementById(typeArray[i]).className.replace(/hide/g,'')+' hide';
    }
  }
}

function checkFocusValue(index){
  if(index.value=='mm/dd/yyyy'){
    index.value='';  
  }
}

function loadFocus(index){
  if((index)&&(index!='')){
    document.getElementById(index).focus();
  } else {
    var f=0;
    var g=document.forms[0];
    while((f>=0)&&(f<g.length)){
      if((g.elements[f].type!='hidden')&&((g.elements[f].tagName.toLowerCase()=='input')||(g.elements[f].tagName.toLowerCase()=='select')||(g.elements[f].tagName.toLowerCase()=='textarea'))){
        g.elements[f].focus();
        f=-1;
      } else {
        f++;  
      }
    }
  }
}

function tooltip(elem, which) {

  // elem = a reference to the element (in this case an <a>) that fired the event handler;
  // which = the class name of the div.tooltip that should be displayed to the user;
  
  if (!document.getElementById) return false;
  if (!document.getElementsByTagName) return false;

  //make sure that any tooltip that was previously shown is hidden;
  if (document.getElementById("tooltip")) {
    document.getElementById("tooltip").style.display = "none";
  }

  //remove id="tooltip" from the div that currently has it;
  var divs = document.getElementsByTagName("div");
  for (var i = 0; i < divs.length; i++) {
    if (divs[i].getAttribute("id") == "tooltip") {
      divs[i].setAttribute("id", "");
    }
  }

  //locate the div.tooltip that has been pressed into service;
  var divs = document.getElementsByTagName("div");
  for (var i = 0; i < divs.length; i++) {
    
  if (divs[i].getAttribute("class")) { //modern browsers;
      if (divs[i].getAttribute("class") == which) {
    var tipDiv = divs[i];
    var hrefs = tipDiv.getElementsByTagName("a"); //find the 'Close' button w/in div.tooltip;
    for (var j = 0; j < hrefs.length; j++) {
      if (hrefs[j].getAttribute("class") == "tooltipClose") { //attach the event handler;
      hrefs[j].onclick = function() {
        tipDiv.style.display = "none";
          return false;
      }
      }
    }
    }
    
  } else if (divs[i].className) { //IE;
    if (divs[i].className == which) {
    var tipDiv = divs[i];
    var hrefs = tipDiv.getElementsByTagName("a"); //find the 'Close' button w/in div.tooltip;
    for (var j = 0; j < hrefs.length; j++) {
      if (hrefs[j].className == "tooltipClose") { //attach the event handler;
      hrefs[j].onclick = function() {
          tipDiv.style.display = "none";
        return false;
      }
      }
    }
    }
  }
  }
  
  //get the x/y coordinates;
  var coords = new Array();
  coords = findPosition(elem);
  
  //attach the id (which in turn styles the div), position the div, and then finally display it;
  tipDiv.setAttribute("id", "tooltip");
  tipDiv.style.left = coords[0] + "px"; //x coord;
  tipDiv.style.top = coords[1] + "px"; //y coord;
  tipDiv.style.display = "block";

}

function findPosition(obj) {
  var x = y = 0;
  if (obj.offsetParent) {
    x = obj.offsetLeft
    y = obj.offsetTop
    while (obj = obj.offsetParent) {
    x += obj.offsetLeft
    y += obj.offsetTop
    }
  }
  return [x, y];
}

function checkInput(index){
  var checkButton=document.getElementById('checkButton');
  var checkValue=index.value.replace(/ /g,'');
  if( checkValue===null || checkValue.length < 1){
    checkButton.savedOnClick=checkButton.onclick;
    checkButton.savedHREF=checkButton.href;
    checkButton.href="";
    checkButton.onclick=function(){
      return false;
    };
    checkButton.className=checkButton.className.replace(/[ ]*btnDisabled/g,'')+' btnDisabled';
  } else {
    checkButton.className=checkButton.className.replace(/[ ]*btnDisabled/g,'');
    checkButton.onclick=checkButton.savedOnClick;
    checkButton.href=checkButton.savedHREF;
  }
}

function addLoadEvent(func) {
  var oldOnload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldOnload();
      func();
    }
  }
}

function showPasswordReset() {
  document.getElementsByTagName('body')[0].className='lastReset';
}

function loadingPage(button) {
  button.innerHTML = "Loading...";
  button.onclick = function () {
    return false;	  
  }
}
