 var strSearchString = "";	//This is the string that is tring to be found in the passed control
 var intTimingInterval = 2000;	//This is the amount of time that the script waits before resetting 
				//the strSearchString, this interval is in milliseconds.
 var intTiming = "";		//This is used to keep track of the timer ID.

//Use this function for the unblur even to reset the keypress timer and clear the search string.
 function ctlOnBlur(ctl)
  {
  strSearchString = "";
  clearInterval(intTiming);
  }

 //This function is called to find the current keypress in the passed control  
 function ctlOnKeypress(ctl)
  {
  clearInterval(intTiming);
  
  intTiming = setInterval("javascript:SearchString='';",intTimingInterval);  //timer 

  var e = window.event || arguments.callee.caller.arguments[0];
  if(e.keyCode == 13 )
   {
   //If enter, 
   strSearchString = "";
   }
  else
   {
   //Add the curent key press to the string and make sure it is all lower case.
   strSearchString = strSearchString + String.fromCharCode(e.keyCode).toLowerCase();

   for(var i = 0; i <= ctl.length - 1; i++)
    {
    //If there is a match, save it and break from the loop. Match the same 
    if(strSearchString == ctl.options.item(i).text.substring(0,strSearchString.length).toLowerCase())
     {
     if(i <= ctl.length)
      {
      ctl.selectedIndex = i;
      }
     break;
     }
    }

   //Return false so windows stops prossessing the keypress.
   e.returnValue = false;
   }
  }
  
   //This function is called to find the current keydown in the passed control  
    function ctlOnKeydown(ctl)
    {
    clearInterval(intTiming);
    
    intTiming = setInterval("javascript:SearchString='';",intTimingInterval);  //timer 
	
	var e = window.event || arguments.callee.caller.arguments[0];
    if( e.keyCode == 9)
   {
   //If tab, 
   strSearchString = "";
   }
   
   if( e.keyCode == 8 ){ //if backspace, remove previous letter
  
    	strSearchString = strSearchString.substring(0, strSearchString.length-1);
    	
     for(var i = 0; i <= ctl.length - 1; i++)
      {
      //If there is a match, save it and break from the loop. Match the same 
      if(strSearchString == ctl.options.item(i).text.substring(0,strSearchString.length).toLowerCase())
       {
       if(i <= ctl.length)
        {
        ctl.selectedIndex = i;
        }
       break;
       }
      }
  
     //Return false so windows stops prossessing the keypress.
     e.returnValue = false;  
    }
  }
  

// Auto format phone number
function formatTel(e,inp) {
var k = e.keyCode
if ((k>=37)&&(k<=40)) return
if ((k==8)||(k==46)) return
var t = inp.value
t = t.replace(/[^0-9]/g,"")
var n = t
if (t.length>=3) n = "("+t.substr(0,3)+")"+t.substr(3,3)
if (t.length>=6) n+= "-"+t.substr(6,4)
inp.value = n
}

// return the today's date as a string
function today(day)
{
	var date = new Date();
	if( day !=null )
		date.setDate( parseInt(day) + date.getDate() );	
	var mm = date.getMonth() + 1;
	var dd = date.getDate();
	var yy = date.getFullYear();
	if( mm < 10)
		mm = "0" + mm;
	if( dd < 10 )
		dd = "0" + dd;
	return  mm + "/" + dd + "/" + yy;
}

function formatDate(e,inp) {
var k = e.keyCode
if ((k>=37)&&(k<=40)) return
if ((k==8)||(k==46)) return
var d = inp.value
d = d.replace(/[^0-9/]/g,"")
if (d.length==1) 
	if(d.substr(0,1) == '/')
		d=""
if (d.length==4) 
	if(d.substr(3,1) == '/')
		d=d.substr(0,3)
if (d.length==7) 
	if(d.substr(6,1) == '/')
		d=d.substr(0,6)		
var n = d		
if (d.length>=2) 
{
	if(d.substr(1,1) == '/')
		n= "0"+ d.substr(0,1) + "/" + d.substr(2,2)
    else if(d.substr(2,1) != '/')
		n = d.substr(0,2)+"/"+ d.substr(2,2)
	else
		n=d.substr(0,5)
}
if(d.length>=5)
{
	if(d.substr(4,1) == '/')
		n=d.substr(0,3) + "0" + d.substr(3,1) + "/" + d.substr(5,4)		
	else if (d.length>=5)
	{
		if(d.substr(5,1) != '/')
			n+="/"+ d.substr(5,4)
		else
			n+=d.substr(5,5)
	}
}
if(d.length>=8)
{
	if(d.substr(8,1)=="")
	{
		var td = today();
		var yy = d.substr(6,2);
		if( yy != "19" && yy !="20")
		{
			if( td.substr(8,2) < yy )
				n=d.substr(0,6) + "19" + yy
			else 
				n=d.substr(0,6) + "20" + yy
		}
	}
}
	if( inp.value!=n )
		inp.value = n
}

function isValidMail(e) {
 var re =
/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
 return re.test(e)
}
function checkMail(i) {
if ( trim(i.value)==""  || isValidMail(trim(i.value))) return
alert('The input email address is not valid')
i.select()
}

// check the int number field.
function checkIntNumber(e,inp, max) {
	var k = e.keyCode
	if ((k>=37)&&(k<=40)) return
	if ((k==8)||(k==46)) return
	var d = inp.value
	d = d.replace(/[^0-9]/g,"")
	if( d>max ) d = max
	if( inp.value!=d )
		inp.value = d
}

// Clear the Pre data from the selected field
function clearField(thefield)
{
	if (thefield.defaultValue == thefield.value)
		thefield.value = "";
}

// Removes leading and trailing spaces from the passed string. Also removes
// consecutive spaces and replaces it with one space. If something besides
// a string is passed in (null, custom object, etc.) then return the input.
function trim(inputString) 
{	
   if (typeof inputString != "string"){ return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

var use_gebi = false;
var use_css = false;
var use_layers = false;
if (document.getElementById) { use_gebi = true; }
else if (document.all) { use_css = true; }
else if (document.layers) { use_layers = true; }

// Show Insert New Timesheet interface box
function showInsert(layer){
	draglayer = layer;
	if (use_gebi) {
		document.getElementById(layer).style.visibility = "visible";
		}
	else if (use_css) {
		document.all[layer].style.visibility = "visible";
		}
	else if (use_layers) {
		document.layers[layer].visibility = "visible";
		}
	//document.insertTimesheet.enddate.selectedIndex = -1;
}

// Hide Insert New Timesheet interface box
function hideInsert(layer){
	if (use_gebi) {
		document.getElementById(layer).style.visibility = "hidden";
		}
	else if (use_css) {
		document.all[layer].style.visibility = "hidden";
		}
	else if (use_layers) {
		document.layers[layer].visibility = "hidden";
		}
}

// Swaps the State Field depending on the coutry
function ChangeState(valu)
{
	if (valu == "Canada")
	{
		document.getElementById('stateslct').innerHTML = "<SELECT NAME=\"state\" STYLE=\"width : 200px\">" +
			"<option Value=\"\" >- Select Province -</OPTION>" +
			"<option Value=\"AB\">Alberta</OPTION>" +
			"<option Value=\"BC\">British Columbia</OPTION>" +
			"<option Value=\"MB\">Manitoba</OPTION>"+
			"<option Value=\"NB\">New Brunswick</OPTION>"+
			"<option Value=\"NF\">Newfoundland</OPTION>"+
			"<option Value=\"NT\">Northwest Territories</OPTION>"+
			"<option Value=\"NS\">Nova Scotia</OPTION>"+
			"<option Value=\"ON\">Ontario</OPTION>"+
			"<option Value=\"PE\">Prince Edward Island</OPTION>"+
			"<option Value=\"QC\">Quebec</OPTION>"+
			"<option Value=\"AK\">Saskatchewan</OPTION>"+
			"<option Value=\"YT\">Yukon</OPTION>" +     
		"</SELECT>"; 

		document.getElementById('statex').innerHTML = "Province&nbsp;:";		
	}
	else if (valu=="United States" || valu=="" )
	{
		document.getElementById('stateslct').innerHTML = "<SELECT NAME=\"state\" STYLE=\"width : 200px\"> " +
			"<OPTION VALUE=\"\" >- Select State -</OPTION> " +
			"<OPTION VALUE=\"AL\">AL-Alabama</OPTION> " +
			"<OPTION VALUE=\"AK\">AK-Alaska</OPTION> " +
			"<OPTION VALUE=\"AZ\">AZ-Arizona</OPTION> " +
			"<OPTION VALUE=\"AR\">AR-Arkansas</OPTION> " +
			"<OPTION VALUE=\"CA\">CA-California</OPTION> " +
			"<OPTION VALUE=\"CO\">CO-Colorado</OPTION> " +
			"<OPTION VALUE=\"CT\">CT-Connecticut</OPTION> " +
			"<OPTION VALUE=\"DE\">DE-Delaware</OPTION> " +
			"<OPTION VALUE=\"DC\">DC-District of Columbia</OPTION> " +
			"<OPTION VALUE=\"FL\">FL-Florida</OPTION>" +
			"<OPTION VALUE=\"GA\">GA-Georgia</OPTION>" +
			"<OPTION VALUE=\"HI\">HI-Hawaii</OPTION>" +
			"<OPTION VALUE=\"ID\">ID-Idaho</OPTION>" +
			"<OPTION VALUE=\"IL\">IL-Illinois</OPTION>" +
			"<OPTION VALUE=\"IN\">IN-Indiana</OPTION>" +
			"<OPTION VALUE=\"IA\">IA-Iowa</OPTION>" +
			"<OPTION VALUE=\"KS\">KS-Kansas</OPTION>" +
			"<OPTION VALUE=\"KY\">KY-Kentucky</OPTION>" +
			"<OPTION VALUE=\"LA\">LA-Louisiana</OPTION>" +
			"<OPTION VALUE=\"ME\">ME-Maine</OPTION>" +
			"<OPTION VALUE=\"MD\">MD-Maryland</OPTION>" +
			"<OPTION VALUE=\"MA\">MA-Massachusetts</OPTION>" +
			"<OPTION VALUE=\"MI\" selected>MI-Michigan</OPTION>" +
			"<OPTION VALUE=\"MN\">MN-Minnesota</OPTION>" +
			"<OPTION VALUE=\"MS\">MS-Mississippi</OPTION>" +
			"<OPTION VALUE=\"MO\">MO-Missouri</OPTION>" +
			"<OPTION VALUE=\"MT\">MT-Montana</OPTION>" +
			"<OPTION VALUE=\"NE\">NE-Nebraska</OPTION>" +
			"<OPTION VALUE=\"NV\">NV-Nevada</OPTION>" +
			"<OPTION VALUE=\"NH\">NH-New Hampshire</OPTION>" +
			"<OPTION VALUE=\"NJ\">NJ-New Jersey</OPTION>" +
			"<OPTION VALUE=\"NM\">NM-New Mexico</OPTION>"+
			"<OPTION VALUE=\"NY\">NY-New York</OPTION>"+
			"<OPTION VALUE=\"NC\">NC-North Carolina</OPTION>"+
			"<OPTION VALUE=\"ND\">ND-North Dakota</OPTION>"+
			"<OPTION VALUE=\"OH\">OH-Ohio</OPTION>"+
			"<OPTION VALUE=\"OK\">OK-Oklahoma</OPTION>"+
			"<OPTION VALUE=\"OR\">OR-Oregon</OPTION>"+
			"<OPTION VALUE=\"PA\">PA-Pennsylvania</OPTION>"+
			"<OPTION VALUE=\"RI\">RI-Rhode Island</OPTION>"+
			"<OPTION VALUE=\"SC\">SC-South Carolina</OPTION>"+
			"<OPTION VALUE=\"SD\">SD-South Dakota</OPTION>"+
			"<OPTION VALUE=\"TN\">TN-Tennessee</OPTION>"+
			"<OPTION VALUE=\"TX\">TX-Texas</OPTION>"+
			"<OPTION VALUE=\"UT\">UT-Utah</OPTION>"+
			"<OPTION VALUE=\"VT\">VT-Vermont</OPTION>"+
			"<OPTION VALUE=\"VA\">VA-Virginia</OPTION>"+
			"<OPTION VALUE=\"WA\">WA-Washington</OPTION>"+
			"<OPTION VALUE=\"WV\">WV-West Virginia</OPTION>"+
			"<OPTION VALUE=\"WI\">WI-Wisconsin</OPTION>"+
			"<OPTION VALUE=\"WY\">WY-Wyoming</OPTION>" +
		"</SELECT>";

		document.getElementById('statex').innerHTML = "State&nbsp;:";
	}
	else //if (valu == "Other")
	{
		document.getElementById('stateslct').innerHTML = "<INPUT class='text' TYPE=\"text\" NAME=\"state\" STYLE=\"width : 200px;\" value=\"- Enter Location -\" onFocus=\"clearField(this);\">";
		document.getElementById('statex').innerHTML = "Other&nbsp;:";
	}	
}