function Checkbox_onChange(field)
{		
	//field_onChange(field);
}

function Launch(url,n,w,h) {
   lWin=window.open(url,n,"menubar=no,scrollbars=no,status=no,width="+w+",height="+h+",top=150,left=150")
}

function OpenNewWindow(url,n,w,h) {
   lWin=window.open(url,n,"menubar=no,scrollbars=yes,status=no,width="+w+",height="+h+",top=100,left=100")
}

function Text_onChange(field)
{	
	//field_onChange(field);
}
function Text_onKeyPress(field)
{
//	Prevent the input of double-quotes or pipes
	//var inputChar = window.event.keyCode;
	var inputChar = (navigator.appName =="Netscape") ? event.which : event.keyCode;
	if(inputChar == 34 || inputChar == 124)
		return window.event.returnValue = false;		
	
}

function Combo_onChange(field)
{				
	//field_onChange(field);
}

function Combo_GetText(field)
{
	if (field.selectedIndex > -1)
		return field.options[field.selectedIndex].text;
	else
		return ' ';
}

function Combo_Clear(field)
{
	while (field.length)
		field.remove(0);
}

function Zip_onChange(field)
{
	if(!Zip_Validate(field))
		return;
	field.value = Zip_Format(field.value);
	//field_onChange(field);
}

function Zip_Validate(field)
{
	var strInput = field.value;
	if(strInput.length == 0)
		return true;

	if	((strInput.indexOf("-") == 5 && strInput.length != 10) ||
		 (strInput.length > 5 && strInput.indexOf("-") != 5) || 
		 (strInput.length < 5))
	{
		HandleClientError("Invalid Zip Code!", field);		
		return false;
	}
	return true;		
}

function Zip_Format(strInput)
{	
	if(strInput.length == 0)
		return strInput;
	
	if(strInput.indexOf("-", 0) > -1) // aleady formated
		return strInput;

	if (strInput.length > 5)
		return strInput.substring(0, 5) + "-" + strInput.substring(5);
	else
		return strInput;
}

function Zip_onKeyPress(field)
{
	//var inputChar = window.event.keyCode;
	var inputChar = (navigator.appName =="Netscape") ? event.which : event.keyCode;
	if(inputChar < 48 || inputChar > 57)
		return window.event.returnValue = false;
	
	var strInput = field.value;	
	if (strInput.length == 5)
		return window.event.returnValue = false;
}

function Phone_onChange(field)
{
	var strInput = field.value;
	if (strInput.substring(0, 1) != "(") {
		strInput = Phone_Format(strInput);
		field.value = strInput;
	}
	//field.value = Phone_Format(field.value);	
	if(!Phone_Validate(field))
		return;
	//field_onChange(field);
}

function Phone_Validate(field)
{
	var strInput = field.value;
	if(strInput.length == 0)
		return true;
	
	if( strInput.indexOf("(") != 0 || 
		strInput.indexOf(")") != 4 || 
		strInput.indexOf(" ") != 5 ||
		strInput.indexOf("-") != 9 ||
		strInput.length < 14 || strInput.length > 14)
	{
		alert("Please enter a valid Phone Number!");	
		field.focus();
		return false;
	}
	return true;
}



function Phone_Format(strInput)
{
	if(strInput.length == 0)
		return strInput;
	
	if(strInput.indexOf("-", 0) > -1 && 
		strInput.indexOf("(") > -1 && 
		strInput.indexOf(")") > -1 &&
		strInput.indexOf(" ") > -1) // aleady formated
		return strInput;
	
	if(strInput.length < 11)
		return "(" + strInput.substring(0, 3) + ")" +" "+ strInput.substring(3, 6) + "-" + strInput.substring(6); //  14 char for phone
	else
		return "(" + strInput.substring(0, 3) + ")" +" "+ strInput.substring(3, 6) + "-" + strInput.substring(6, 10); // + "x" + strInput.substring(10);
	
}


function Phone_UnFormat(strInput)
{
	aCurrStripChars = new Array("(", ")", " ", "-", "x") // Add Space into Array to Check 14 char long
	
	if(strInput.length == 0)
		return strInput;

	return strStrip(strInput, aCurrStripChars)	
}


function Phone_onKeyPress(field)
{  

	var inputChar;
	if (navigator.appName != "Netscape") 
		inputChar = event.keyCode;
	if(inputChar < 48 || inputChar > 57)
		return window.event.returnValue = false;
		
	var strInput = field.value;	
	if (strInput.length ==1 && strInput !="(")
		field.value = "(" + strInput;
	if (strInput.length == 4)
		field.value += ") ";
	if (strInput.length == 5)
		field.value += " ";
	if (strInput.length == 9)
		field.value += "-";	
	//if (strInput.length == 13)
	//	field.value += "x";

	if (strInput.length > 13)
		return window.event.returnValue = false;
}

function field_onChange(field)
{
  // the page data has changed.
	m_bChanged = true;
			

  // for text fields, show data has changed
	if(field.type == "text")
	{
		field.style.backgroundColor="darkblue";
		field.style.color="white";
	}
}

function HandleClientError(sError, field)
{
	alert(sError);
	if(field != null && !field.disabled)
	{
	  setTimeout("sun2." + field.name + ".focus()",1,"JavaScript");
	}
}

function TitleCaseNow(fldTheField)
{
/* JavaScript procedure to capitalize first letter of each word in a text box */
var sTemp = "";
var iLoop;
var sLetter;
var sInString = fldTheField.value;

    for (iLoop = 0; iLoop <= sInString.length - 1; iLoop++)
    {
        if (iLoop == 0)
            sLetter = sInString.substring(iLoop, iLoop + 1).toUpperCase();
        else if ((sInString.substring(iLoop - 1, iLoop) == " ") || (sInString.substring(iLoop - 1, iLoop) == "-") || 
                  (sInString.substring(iLoop - 1, iLoop) == "'") || (sInString.substring(iLoop - 1, iLoop) == ".")|| 
                  (sInString.charCodeAt(iLoop - 1) == 10))
            sLetter = sInString.substring(iLoop, iLoop + 1).toUpperCase();
        else
            sLetter = sInString.substring(iLoop, iLoop + 1).toLowerCase();
        sTemp = sTemp + sLetter
    }
    
    fldTheField.value = sTemp;
}

function Number_onChange(field)
{		
	field.value = Number_Format(field.value);
	//field_onChange(field);	
}

function Number_Format(strInput)
{
	return strInput;
}

function Number_onKeyPress(field, bPositive, bInt)
{
	//var inputChar = window.event.keyCode;	
	var inputChar = (navigator.appName =="Netscape") ? event.which : event.keyCode;
	if(	(inputChar < 48 || inputChar > 57) &&	// not a number 
		((!bInt && inputChar!= 46) || bInt) &&	// not a "." when not a integer field		
		((!bPositive && inputChar!= 45) || bPositive))// If not "-" when not a positive field
	{
		return window.event.returnValue = false;
	}	

	var strInput = field.value;	
	if ( (inputChar == 46 && strInput.indexOf(".", 0) > -1) || // found "." already
		 (inputChar == 45 && strInput.length > 0) )    // "-" only allow at the beginning
	{
		return window.event.returnValue = false;
	}	
}

function Date_onChange(field)
{	
	if(!Date_Validate(field))
		return;
	
	field.value = Date_Format(field.value);
	//field_onChange(field);
}

function Date_Validate(field)
{
	var strInput = field.value;	
	if(strInput.length == 0)
		return true;
	
	var month ;
	var day;
	var year;
	
	if(strInput.indexOf("/") > 0)
	{
		month = strInput.substring(0, strInput.indexOf("/"));	
		var pos = strInput.lastIndexOf("/")
		day = strInput.substring(strInput.indexOf("/") + 1, pos);			
		if(strInput.indexOf("/") != strInput.lastIndexOf("/"))
			year = strInput.substring(strInput.lastIndexOf("/") + 1);
		else
			year = "";
	}
	else
	{
		if(strInput.length == 8)
		{
			month = strInput.substring(0, 2);
			day = strInput.substring(2, 4);
			year = strInput.substring(4, 8);
		}
		else
		{		
			HandleClientError("Invalid date.", field);
			return false;
		}		
	}
	if (year.length != 4)
	{
		HandleClientError("4 digits must be entered for the year.", field);
		return false;
	}
	month = month - 0;
	day = day - 0;
	year = year - 0;
	if (month ==0 || month > 12 || day == 0 || day > getDaysInMonth(month, year))
	{		
		HandleClientError("Invalid date", field);
		return false;	
	}
	if (year > 3000)
	{
		HandleClientError("Date is too far in the future.", field);
		return false;	
	}
	if (year < 1753)
	{
		HandleClientError("Date is too far in the past.", field);
		return false;	
	}
	return true;
}

function Date_Format(strInput)
{
	if(strInput.length == 0)
		return strInput;

	var month; 
	var day;
	var year;
	if(strInput.indexOf("/", 0) == -1) 
	{
		month = strInput.substring(0, 2) - 0;
		day = strInput.substring(2, 4) - 0;
		year = strInput.substring(4);
	}		
	else
	{
		var pos1 = strInput.indexOf("/");
		month = strInput.substring(0, pos1) - 0;
		var pos2 = strInput.lastIndexOf("/");
		day = strInput.substring(pos1 + 1, pos2) - 0;
		var pos3 = strInput.lastIndexOf(" ");
		if(pos3 == -1)
			year = strInput.substring(pos2 + 1);
		else
			year = strInput.substring(pos2 + 1, pos3) - 0;
	}

	if (month < 10)
		month = "0" + month;
	
	if (day < 10)
		day = "0" + day;

	if (year.length < 4 && (year - 0) < 100)
		year = (year - 0) + 1900;
	
	return month + "/" + day + "/" + year;
}

function Date_onKeyPress(field)
{	
	//var inputChar = window.event.keyCode;
	var inputChar = (navigator.appName =="Netscape") ? event.which : event.keyCode;
	var strInput = field.value;	
	if(inputChar == 67 || inputChar == 99)
	{
        if (strInput.length < 8)
		  field.value = "";		
	    DateButton_OnClick(field);
	}
	if(inputChar < 47 || inputChar > 57)	
		return window.event.returnValue = false;
	
	var month; 
	var day;
	var year;
	
	if(strInput.indexOf("/") > 0)
	{
		month = parseInt(strInput.substring(0, strInput.indexOf("/")));
		day = strInput.substring(strInput.indexOf("/") + 1);
		if(strInput.indexOf("/") != strInput.lastIndexOf("/"))
		{
			year = strInput.substring(strInput.lastIndexOf("/") + 1);
			if(year.length > 3)
			{
				return window.event.returnValue = false;
			}
		}
	}

	if (inputChar == 47 && 
		(strInput.length == 0 ||								// no "/" at the first position
		 strInput.lastIndexOf("/") == strInput.length -1 ||	// no "/" after another
		 strInput.lastIndexOf("/") >= 3))						// no more than two "/" 
	{
		return window.event.returnValue = false;
	}
	if (strInput.length == 1 && 
		((parseInt(strInput) > 1  && inputChar != 47 ) || 
		(parseInt(strInput) == 1 && inputChar > 50) ))
	{
		field.value += "/";	
	}

	if (strInput.length == 2)
	{
		if(strInput.substring(1, 2) == "/" && inputChar == 47)	
			return window.event.returnValue = false;	
		
		if(strInput.substring(1, 2) != "/" && inputChar != 47)									
			field.value += "/";		  
	}
			
	if (strInput.length == 3)
	{							
		if(	(month == 2 && day > 2 || month !=2 && day > 3 && inputChar != 47) ||
			(month != 2 && day == 3 && inputChar > 49))					
			field.value += "/";				 				
	}	
		
	if (strInput.length == 4)
	{							
		if((month == 2 && day > 2 || month !=2 && day > 3) && inputChar != 47)								
			field.value += "/";
		
		if(month != 2 && day == 3 && inputChar > 49)
			return window.event.returnValue = false;
	}	
	if (strInput.length == 5)
	{
		if(strInput.lastIndexOf("/") < 3 && inputChar != 47)
			field.value += "/"; 	
	}	
	return window.event.returnValue = true;	
}


function getDaysInMonth(month, year)
{
	var daysInMonth= new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if (month == 2) // Test for leap year when Febrary is selected
		return ((year % 4 ==0) && (year % 100 != 0)) || (year % 400 == 0) ? 29 : 28;
	else
		return daysInMonth[month - 1];
}

function CalcAge(sDt)
{
   var d, dt;
   var age;
   var yrDiff;
   var monDiff;

   d = new Date(); //system date
   dt = new Date(sDt); //convert date passed to Date object

   age = "";

   if (dt > d)
   { //check if the date is in future!!
     alert("not a valid birth date");
     return age;
   }
   yrDiff = d.getFullYear()-dt.getFullYear();
   if  (d.getMonth() < dt.getMonth())
   {
     age = (yrDiff-1) + "y " + (12 + d.getMonth() - dt.getMonth()) + "m";
   }
   else
      {
        if (d.getMonth() > dt.getMonth())
          age = (yrDiff) + "y " + (d.getMonth() - dt.getMonth()) + "m";
        else
          age = (yrDiff) + "yr ";
      }
  return age;
}