// Set vars;

var img_root = '/images';
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;


function setUploadPrevImage(string,image) {
   document.getElementById(image).src = 'File:\/\/' + string;
   document.getElementById(image).height=70;
}

function textMx(field, countfield, maxlimit) {
	var fc = field.value.length;
	if (fc > maxlimit) { // if too long...trim it!
	  field.value = field.value.substring(0, maxlimit);
	  // otherwise, update 'characters left' counter
        } else { 
	  countfield.innerHTML = maxlimit - fc;
	}
}


if(document.layers){
  document.captureEvents(Event.KEYPRESS)
}


var waitForTimeOut = 0;

function blinkExecute(form_name,field_name,color_on,color_off){
   
   var divName = 'form_' + field_name;

   waitForTimeOut = 2502;
   
   if (document.all) {

      setTimeout('document.all.'+form_name+'.'+field_name+'.focus();',2501);
      setTimeout('document.all.'+form_name+'.'+field_name+'.className=\''+color_on+'\';',0);
      setTimeout('document.all.'+form_name+'.'+field_name+'.className=\''+color_off+'\';',500);
      setTimeout('document.all.'+form_name+'.'+field_name+'.className=\''+color_on+'\';',1000);
      setTimeout('document.all.'+form_name+'.'+field_name+'.className=\''+color_off+'\';',1500);             
      setTimeout('document.all.'+form_name+'.'+field_name+'.className=\''+color_on+'\';',2000);
      setTimeout('document.all.'+form_name+'.'+field_name+'.className=\''+color_off+'\';',2500);
      
   } else  {
      setTimeout('window.document.'+form_name+'.'+field_name+'.focus();',2501);
      setTimeout('window.document.'+form_name+'.'+field_name+'.className=\''+color_on+'\';',0);
      setTimeout('window.document.'+form_name+'.'+field_name+'.className=\''+color_off+'\';',500);
      setTimeout('window.document.'+form_name+'.'+field_name+'.className=\''+color_on+'\';',1000);
      setTimeout('window.document.'+form_name+'.'+field_name+'.className=\''+color_off+'\';',1500);             
      setTimeout('window.document.'+form_name+'.'+field_name+'.className=\''+color_on+'\';',2000);
      setTimeout('window.document.'+form_name+'.'+field_name+'.className=\''+color_off+'\';',2500);
      
   }

   //setTimeout('showDiv('+divName+',\'absolute\');',2500);


}


function menuOver(color,i) {
   
   var lineOne = 'td'+(i-1);
   var lineTwo = 'td'+i;
   var link    = 'a'+i;
   var button  = 'b'+i;

   var bg_color = color + '_bg';
   var link_color = color + '_link';

   if (document.getElementById) {
      window.document.getElementById(lineOne).className=bg_color;
      window.document.getElementById(lineTwo).className=bg_color;
      window.document.getElementById(button).className=bg_color;
      window.document.getElementById(link).className=link_color;
   } else if (document.all) {
      document.all.lineOne.className=bg_color;
      document.all.lineTwo.className=bg_color;
      document.all.button.className=bg_color;
      document.all.link.className=link_color;
   } else if (document.layers) {
      window.document.layers[lineOne].className=bg_color;
      window.document.layers[lineTwo].className=bg_color;
      window.document.layers[button].className=bg_color;
      window.document.layers[link].className=link_color;
   }
}

function colorTR(tr,cName) {
   
   var tr2 = tr+'-2';

   if (document.getElementById) {
      window.document.getElementById(tr).className=cName;
      window.document.getElementById(tr2).className=cName;
   } else if (document.all) {
      document.all.tr.className=cName;
      document.all.tr2.className=cName;
   } else if (document.layers) {
      window.document.layers[tr].className=cName;
      window.document.layers[tr2].className=cName;
   }
}


var count = new Array();

function showHiddenDiv(id,bg,bg_on) {
   
   
   if ( count[id]==1 ) {
      
   } else {
      count[id]=0;
   }


   for (var i in count) {
      //alert(i+'-'+count[i]);
      var td = i+'_td';
      if (id!=i) {
         count[i]=1;
      }
      if (count[i]==0 ) {
         count[i]++;
         if (document.getElementById) { 
           document.getElementById(td).className=bg_on;
           //document.getElementById(i).style.visibility = 'visible'; 
           document.getElementById(i).style.display = 'block'; 
           document.getElementById(i).style.position = 'static'; 
         } else if (document.layers) {  
           document.all.td.className=bg_on;
           //document.i.visibility = 'visible'; 
           document.i.display = 'block'; 
           document.i.position = 'static'; 
         } else { 
           document.layers[td].className=bg_on;
           //document.all.i.style.visibility = 'visible'; 
           document.all.i.style.display = 'block'; 
           document.all.i.style.position = 'static'; 
         }
      } else {
         count[i]=0;
         if (document.getElementById) { 
           document.getElementById(td).className=bg;
           document.getElementById(i).style.display = 'none'; 
           //document.getElementById(i).style.visibility = 'hidden'; 
           document.getElementById(i).style.position = 'absolute'; 
         } else if (document.layers) {  
           document.all.td.className=bg;
           document.i.display = 'none'; 
           //document.i.visibility = 'hidden'; 
           document.i.position = 'absolute'; 
         } else { 
           document.layers[td].className=bg;
           document.all.i.style.display = 'none'; 
           //document.all.i.style.visibility = 'hidden'; 
           document.all.i.style.position = 'absolute'; 
         }
      }
   }
   
    
}

function showDiv(i,pos) {
   if (document.getElementById) { 
     document.getElementById(i).style.display = 'block'; 
     document.getElementById(i).style.position = pos; 
   } else if (document.layers) {  
     document.i.display = 'block'; 
     document.i.position = pos; 
   } else { 
     document.all.i.style.display = 'block'; 
     document.all.i.style.position = pos; 
   }
} 
 
function hideDiv(i){
   if (document.getElementById) { 
     document.getElementById(i).style.display = 'none'; 
     //document.getElementById(i).style.visibility = 'hidden'; 
     document.getElementById(i).style.position = 'absolute'; 
   } else if (document.layers) {  
     document.i.display = 'none'; 
     //document.i.visibility = 'hidden'; 
     document.i.position = 'absolute'; 
   } else { 
     document.all.i.style.display = 'none'; 
     //document.all.i.style.visibility = 'hidden'; 
     document.all.i.style.position = 'absolute'; 
   }


}

function checkShowDiv(checkbox,divName) {
   if (checkbox.checked==true) {
      showDiv(divName,'static');
   } else {
      hideDiv(divName);
   }
}


function phoneNum(field,evt) {

//function changeKey (textControl, evt, keyChecker) {
  var keyCode = evt.keyCode ? evt.keyCode :
                evt.charCode ? evt.charCode :
		          evt.which ? evt.which : void 0;
  var key;


  if (keyCode) {
    key = String.fromCharCode(keyCode);
  }
  
  if (!isIE && (keyCode==37 || keyCode==39 || keyCode==9 || keyCode==46 || keyCode==8 || keyCode==undefined)) {
     //alert(keyCode);
     return true;
  }

  if (isIE && (key=='' || key==undefined)) {
     return true;
  }

  if (field.value.length > 17) {
     return false;
  }

  var newChar = convertNum(key);

  var rc;

  //if(typeof clipboardData != 'undefined' && isIE) {
  //   cbLength = clipboardData.getData("Text").length;
  //}

  if (keyCode && window.event && !window.opera) {
    if (newChar=='') {
      rc = false;
    } else  {
      window.event.keyCode = newChar.charCodeAt();
      if (window.event.preventDefault) {
        window.event.preventDefault();
      }
      rc = true;
    } 
  } else if (typeof field.setSelectionRange != 'undefined') {
    if (newChar=='') {
      if (evt.preventDefault) {

        evt.preventDefault();
      }
      rc = false;
    } else {
      
      var oldSelectionStart = field.selectionStart;
      var oldSelectionEnd = field.selectionEnd;
      var selectedText = field.value.substring(oldSelectionStart, oldSelectionEnd);

      phPos = field.value.length - oldSelectionEnd;

      if (selectedText.length>0 && !isIE) {
         return true;
      } else if (evt.preventDefault) {
	     evt.preventDefault();
      }

      field.value = field.value.substring(0, oldSelectionStart) +
                    newChar +
                    field.value.substring(oldSelectionEnd);
   
      rc = false;

      field.setSelectionRange(oldSelectionStart + newChar.length, oldSelectionStart + newChar.length);
    }
  } else if (newChar=='') {
    if (evt.preventDefault) {
      evt.preventDefault();
    }
    rc = false;
  } else {
    rc = true;
  }

  if (newChar.length>0) {
     makeKeySound(img_root);
  }


  return rc;

}

var phPos;
var phField = '#%~';
function getPos (field) {
	  	//save off the current value to restore it later,
	  	var sOldText = field.value;
      phField = field.value;
		var position=0;
      var position2;
		//create a range object and save off it's text
		if (document.getSelection) {
	  		// the Navigator 4.0x code	  		
	  		position = field.selectionStart;
	  		position2 = field.selectionEnd;
		} else if (document.selection && document.selection.createRange) {
	  		// the Internet Explorer 4.0x code
	  		var objRange = document.selection.createRange();  
	  		var sOldRange = objRange.text;

         if (sOldRange.length==field.value.length) {
            //alert(sOldRange.length+'=='+field.value.length);
            phPos = 0;
            return;
         }
	
			//set this string to a small string that will not normally be encountered
	  		var sWeirdString = '#%~';
	
			//insert the weirdstring where the cursor is at
	  		objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));
	
			//save off the new string with the weirdstring in it
	 		var sNewText = field.value;
	
			//set the actual text value back to how it was
	  		objRange.text = sOldRange;
	
			//look through the new string we saved off and find the location of
			//the weirdstring that was inserted and return that value
	  		for (i=0; i <= sNewText.length; i++) {
	    		var sTemp = sNewText.substring(i, i + sWeirdString.length);
	    		if (sTemp == sWeirdString) {
	      			position = (i - sOldRange.length);
	      			position2 = (position + sOldRange.length);
	    		}
	  		}
		}
      phPos = sOldText.length - position2;
		
}

var field_max = 18;

function numChange(field) {
   if (field.value.substring(0,1)=='-' || field.value.substring(0,1)==' ') {
      field.value= field.value.substring(1)
      if (field.setSelectionRange) {   
         field.focus();   
         field.setSelectionRange(0, 0);
      } else if (field.createTextRange) {   
         var range = field.createTextRange();   
         range.collapse(true);   
         range.moveEnd('character', 0);   
         range.moveStart('character', 0);   
         range.select();  
      }
   }
   if (field.value.substring(field.value.length-1,field.value.length)=='.' || 
       field.value.substring(field.value.length-1,field.value.length)==' ' ) {
      field.value= field.value.substring(0,field.value.length-1)
   }
   if (parseInt(field.value)!=field.value ) {
      var oldVal = field.value;
      var newVal = '';
      for (i=0;i<field.value.length;i++) {
         var temp = field.value.substring(i,(i+1));
         var cVal =  convertNum(temp);
         newVal=newVal + cVal;
      }
      field.value= newVal;
      if (oldVal!=newVal) {
        newPos = field.value.length-phPos;
        if (field.setSelectionRange) {   
           field.focus();   
           field.setSelectionRange(newPos, newPos);
        } else if (field.createTextRange) {   
           var range = field.createTextRange();   
           range.collapse(true);   
           range.moveEnd('character', newPos);   
           range.moveStart('character', newPos);   
           range.select();  
        }
      }
   }
   phPos=0;
   if (field.value.length>field_max) {
      field.value= field.value.substring(0,field_max);
   }
   
   if (field.value.length > phField.length && !(phField=='#%~')) {
      makeKeySound(img_root);
   }
   phField = field.value;
}

function convertNum(keychar) {


   keychar=keychar.toLowerCase();

   if (keychar=="a" || keychar=="b" || keychar=="c") {
         return '2';
   } else if (keychar=="d" || keychar=="e" || keychar=="f") {
         return '3';
   } else if (keychar=="g" || keychar=="h" || keychar=="i") {
         return '4';
   } else if (keychar=="j" || keychar=="k" || keychar=="l") {
         return '5';
   } else if (keychar=="m" || keychar=="n" || keychar=="o") {
         return '6';
   } else if (keychar=="p" || keychar=="q" || keychar=="r" || keychar=="s") {
         return '7';
   } else if (keychar=="t" || keychar=="u" || keychar=="v") {
         return '8';
   } else if (keychar=="w" ||keychar=="x" || keychar=="y" || keychar=="z") {
         return '9';
   } else if (keychar=="1" || 
              keychar=="2" || 
              keychar=="3" || 
              keychar=="4" || 
              keychar=="5" || 
              keychar=="6" || 
              keychar=="7" || 
              keychar=="8" || 
              keychar=="9" || 
              keychar=="0"
              ) {
         return keychar;
   } else {
      return '';
   }


}

var keySound = new Array();

for (i=0;i<10;i++) {
   keySound[i] = i;
}

function makeKeySound(img_root) {
   return;
   var randomnumber=Math.floor(Math.random()*11)-1;
   alert(randomnumber);
   if (isIE) {
      document.getElementById("sound").src = img_root+'/dtmf-'+randomnumber+'.wav';
   } else {
    var thissound = eval("document.sound"+randomnumber);
    thissound.Play();
   }

}

function writeSound() {
   if (isIE) {
    
      var myDiv = '<bgsound name=sound id=sound src=\"#\" loop=false autostart=false autorewind=true hidden=true width=0 height=0><\/bgsound>';
      
      document.write('<bgsound name=sound id=sound src=\"#\" loop=false autostart=false autorewind=true hidden=true width=0 height=0><\/bgsound>');
   
      setTimeout('document.getElementById(\"sound_span\").innerHTML = (!document.getElementById(\"sound_span\").innerHTML ) ? \''+myDiv+'\' : \' \';',waitForTimeOut);


   } else {
    var myDiv;
    myDiv = '';
    for (i=0;i<10;i++) {
      myDiv+='<embed src=\\"'+img_root+'\\/dtmf-'+i+'\\.wav" autostart=false volume=100 width=0 height=0 name=\\"sound'+i+'\\" enablejavascript=true>\\n';
    }
    //var x = document.getElementById("sound_span");
    setTimeout('document.getElementById(\"sound_span\").innerHTML = (!document.getElementById(\"sound_span\").innerHTML ) ? \''+myDiv+'\' : \' \';',waitForTimeOut);
    //alert(document.getElementById("sound_span").innerHTML);
   }
}


function tabIfNeeded(field,max,next_field,form_name) {
if (field.value.length==max) {
   setTimeout('document.'+form_name+'.'+next_field+'.focus();',1);
}

}

