// reusable even handler code used to parse query string on page load.
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
    obj.addEventListener(evType, fn, true); 
    return true; 
 } else if (obj.attachEvent){ 
    var r = obj.attachEvent("on"+evType, fn); 
    return r; 
 } else { 
    return false; 
 } 
} 

// QueryString 
// Call function by x = querystring("variable") returns variable=x 
function QueryString(key) 
{
QueryString_parse(); 
var value = null; 
for (var i=0;i<QueryString.keys.length;i++) 
{ 
if (QueryString.keys[i]==key) 
{ 
value = QueryString.values[i]; 
break; 
} 
} 
return value; 
} 


function QueryString_parse() 
{ 
QueryString.keys = new Array(); 
QueryString.values = new Array();

var query = window.location.search.substring(1); 
var pairs = query.split("&"); 

for (var i=0;i<pairs.length;i++) 
{ 
var pos = pairs[i].indexOf('='); 
if (pos >= 0) 
{ 
var argname = pairs[i].substring(0,pos); 
var value = pairs[i].substring(pos+1); 
QueryString.keys[QueryString.keys.length] = argname; 
QueryString.values[QueryString.values.length] = value; 
} 
} 
} 


function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
   
function MaxLength(sText, iMax)
{
   var IsValid=true;
   
   
      if (sText.length > iMax) 
         {
         IsValid = false;
         }
      
   return IsValid;
   
}

function RangeLimit(sText, iMin, iMax)
{
   var IsValid=false;
   
   
      if (sText.length >= iMin && sText.length <= iMax) 
         {
         IsValid = true;
         }
      
   return IsValid;
   
}

function Mod10(ccNumb) {  // v2.0
var valid = "0123456789"  // Valid digits in a credit card number
var len = ccNumb.length;  // The length of the submitted cc number
var iCCN = parseInt(ccNumb);  // integer of ccNumb
var sCCN = ccNumb.toString();  // string of ccNumb
sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
var iTotal = 0;  // integer total set at zero
var bNum = true;  // by default assume it is a number
var bResult = false;  // by default assume it is NOT a valid cc
var temp;  // temp variable for parsing string
var calc;  // used for calculation of each digit

// Determine if the ccNumb is in fact all numbers
for (var j=0; j<len; j++) {
  temp = "" + sCCN.substring(j, j+1);
  if (valid.indexOf(temp) == "-1"){bNum = false;}
}

// if it is NOT a number, you can either alert to the fact, or just pass a failure
if(!bNum){
  /*alert("Not a Number");*/bResult = false;
}

// Determine if it is the proper length 
if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
  bResult = false;
} else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
  if(len >= 15){  // 15 or 16 for Amex or V/MC
    for(var i=len;i>0;i--){  // LOOP throught the digits of the card
      calc = parseInt(iCCN) % 10;  // right most digit
      calc = parseInt(calc);  // assure it is an integer
      iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
      i--;  // decrement the count - move to the next digit in the card
      iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
      calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
      calc = calc *2;                                 // multiply the digit by two
      // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
      // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
      switch(calc){
        case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
        case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
        case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
        case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
        case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
        default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
      }                                               
    iCCN = iCCN / 10;  // subtracts right most digit from ccNum
    iTotal += calc;  // running total of the card number as we loop
  }  // END OF LOOP
  if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
    bResult = true;  // This IS (or could be) a valid credit card number.
  } else {
    bResult = false;  // This could NOT be a valid credit card number
    }
  }
}
// change alert to on-page display or other indication as needed.
//if(bResult) {
//  alert("This IS a valid Credit Card Number!");
//}
//if(!bResult){
  //alert("This is NOT a valid Credit Card Number!");
//}
  return bResult; // Return the results
}

function DisplayField(e,trDisplay,bool)
	{	   	   
	   if(e.childNodes[0].checked == bool)
	   {	        
	        trDisplay.style.display = "";	        
	   }
	   else
	   {	        
	        trDisplay.style.display = "none";	        
	   }	
	}
	
	function DisplayFieldText(e,label,trueText,falseText)
	{	   	   
	   if(e.childNodes[0].checked == true)
	   {	        
	        label.innerHTML = trueText;	        
	   }
	   else
	   {	        
	        label.innerHTML = falseText;	        
	   }	
	}
	
	function removeitem(lbx,hidden)
        {
          //var lbx = document.getElementById(listboxid);
            
           // lbx.options[lbx.selectedIndex].remove();  //?
                      
             var selIndex = lbx.selectedIndex;
              if (selIndex != -1) 
              {
                //for(i=lbx.length-1; i>=0; i--)
                //{
                  //if(lbx.options[i].selected)
                  //{
                  //  lbx.options[i].remove();
                  //}
                //}
                
                lbx.remove(selIndex);
                
                if(hidden.value != "")
                {
                    hidden.value = selIndex;
                }
                else
                {
                    hidden.value = "," + selIndex;
                }

              }
            
        }
        
        function autotab(control1, control2,length)
        {
            if(control1.value.length == parseInt(length))
            {
                control2.focus();
            }        
        }
        
        function SelectAllRows()
    	{
    	   var chkAll = window.event.srcElement; 
    	   var tbl = chkAll.parentNode.parentNode.parentNode.parentNode;
    	   
    	   if (chkAll)
    	   {
    	        for(var i=1;i<tbl.rows.length;i++)
    	        {
    	            var chk = tbl.rows[i].cells[0].firstChild;
    	            chk.checked=chkAll.checked;
    	            //checkRowOfObject(chk);
    	        }
    	   }
    	}


 function uncheckOthers(id)
    {        
        var elm = document.getElementsByTagName('input');        
        for(var i = 0; i < elm.length; i++)
        {                            
            if(elm.item(i).id.substring(id.id.lastIndexOf('_')) == id.id.substring(id.id.lastIndexOf('_')))
            {
                if( elm.item(i).type == "checkbox" && elm.item(i)!=id)
                    elm.item(i).checked = false;
            }
        }
    }  
    
    function DisplayFieldByDropDownText(e,control,text)
	{	    
	    var ddlvalue = e.options[e.selectedIndex].text;
	    
	    if(ddlvalue == text)
	    {
	        control.style.display = "none";	        
	    }
	    else
	    {
	        control.style.display = "";
	    } 
	    //alert("Function call worked");
	}
	
	function getPrint(content,title)
	{ //Creating new pagevar	     	     
	     pp = window.open();
	     //Adding HTML opening tag with <HEAD> &#8230; </HEAD> portion 
	     pp.document.writeln('<HTML><HEAD><title>' + title + '</title>');
	     //pp.document.writeln('<LINK href=includes/liveabove.css type="text/css" rel="stylesheet">');
	     pp.document.writeln('<LINK href=includes/PrintStyle.css type="text/css" rel="stylesheet" media="print">');
	     pp.document.writeln('<base target="_self"></HEAD>');
	     //Adding Body Tag
	     pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="5px"');
	     pp.document.writeln(' leftMargin="5px" topMargin="5px" rightMargin="5px">');
	     //Adding form Tag
	     pp.document.writeln('<form method="post">');
	     //Creating two buttons Print and Close within a HTML table
	     pp.document.writeln('<TABLE width=100%><TR><TD></TD></TR><TR><TD align=right>');
	     pp.document.writeln('<INPUT ID="PRINT" type="button" value="Print" ');
	     pp.document.writeln('onclick="javascript:location.reload(true);window.print();">');
	     pp.document.writeln('<INPUT ID="CLOSE" type="button" value="Close" onclick="window.close();">');
	     pp.document.writeln('</TD></TR><TR><TD></TD></TR></TABLE>');
	     //Writing print area of the calling page
	     pp.document.writeln(document.getElementById(content).innerHTML);	     
	     //Ending Tag of </form>, </body> and </HTML>
	     pp.document.writeln('</form></body></HTML>');	     
	} 

function FieldListSameAsOther(e,fieldnamelistto, fieldnamelistfrom)
	{
	    //var elFieldNameList = document.getElementById(fieldnamelist);
	    //var elFieldValueList = document.getElementById(fieldvaluelist);
	    var arrFieldNameListTo = fieldnamelistto.split(";");
	    var arrFieldNameListFrom = fieldnamelistfrom.split(";");
	    
	   if (e.checked == true)
	   {
	        for (var inx=0 ; inx<arrFieldNameListTo.length ; inx++)
            {
                var eFrom = document.getElementById(arrFieldNameListFrom[inx]);
                var eTo = document.getElementById(arrFieldNameListTo[inx]);
                if(eFrom.type == "select-one")
                {
                    //assume field is either state or country dropdownlist
                    eTo.selectedIndex = eFrom.selectedIndex;                    
                }
                else
                {
                    eTo.value = eFrom.value;                    
                }
            }
       }
       else
       {
            for (var inx=0 ; inx<arrFieldNameListTo.length ; inx++)
            {
                var eFrom = document.getElementById(arrFieldNameListFrom[inx]);
                var eTo = document.getElementById(arrFieldNameListTo[inx]);
                if(eFrom.type == "select-one")
                {
                    //assume field is either state or country dropdownlist
                    //var elddl = document.getElementById(arrFieldNameList[inx]);
                    eTo.options[0].selected=true
                    
                    for (var jnx=0 ; jnx < eFrom.length ; jnx++)
                    {
                        if (eTo.options[jnx].value == "228")
                        {
                            eTo.options[jnx].selected=true;
                            break;
                        }
                    }                        
                }
                else
                {
                    eTo.value = "";
                }
            }
       }
	}
	
	function pausecomp(millis) 
    {
        var date = new Date();
        var curDate = null;

        do { curDate = new Date(); } 
        while(curDate-date < millis);
    } 
    
    function cursor_wait()
    {
        document.body.style.cursor = 'wait';
    }

// Returns the cursor to the default pointer
    function cursor_clear()
    {
        document.body.style.cursor = 'default';
    }
    
    function GoToUrl(url) {
        //alert("click works");
        document.location.href = url;
    }
