// Is it least version IE 5.0 or Net 6.0  
  var isIE = navigator.appVersion.indexOf("MSIE")>0
  if(isIE)
	 var v = navigator.userAgent.charAt(navigator.userAgent.indexOf("MSIE") + 5);
  var isNav = navigator.appName.indexOf("Netscape")>=0
  if (isNav)
	var v = navigator.userAgent.charAt(navigator.userAgent.indexOf("Mozilla") + 8);
  var isIE5 = isIE && v>=5
  var isNav6 = isNav && v>=5


//this is checking for valid amt
function checkAmt(number){
	numericStr = number.value
	var numbersOnly = "";
	var count=0;
	
	//LOOP THRU CHARS AND ONLY ALLOW COMMAS AND DECIMALS
	for (var charPtr=numericStr.length-1; charPtr>-1;charPtr--){
		if (isNaN(numericStr.charAt(charPtr)) && numericStr.charAt(charPtr) != "," && numericStr.charAt(charPtr) != "."){
			alert("MUST BE A VALID AMOUNT.")
			if (isNav6)
				number.value = ""
			else
				number.select()
			return false;
			break;
		}
		else if (numericStr.charAt(charPtr) != ",") numbersOnly = numericStr.charAt(charPtr) + numbersOnly; //&& numericStr.charAt(charPtr) != "."	
		if(numericStr.charAt(charPtr) == ".") count=count+1
							
	}  	 
	 
	if (count>1){
	    alert("MUST BE A VALID AMOUNT.")
	    if (isNav6)
				number.value = ""
			else
				number.select()
			return false;
    			}
	

	//number.value = numericStr		
	number.value=numbersOnly
}

//this is checking for valid positive number
function checkNum(number,atleast,notgreater){
	if (number.value != ""){
		if (isNaN(number.value) == true ||(number.value<0)){
			alert("MUST BE A VALID POSITIVE NUMBER")
			if (isNav6)
				number.value = ""
			else
				number.select()	
			return;
		}

		if (atleast != null){
			if(number.value.length < atleast){
				alert("MUST AT LEAST " + atleast + " DIGITS.")
				if (isNav6)
					number.value = ""
				else
					number.select()	
			}			
		}
		if (notgreater != null){
			if(number.value > notgreater){
				alert("MUST NOT BE GREATER THAN " + notgreater + ".")
				if (isNav6)
					number.value = ""
				else
					number.select()	
			}			
		}
	}
}
//this is checking for required fields
function MarkValid(ValArray,num){
	strAlert = "Please Check the Following Fields:\n\n"
	flag = 0
	for (i=1;i<=num;i++){
		if (ValArray[i].value == "" || ValArray[i].value == "XX" ){
			flag = 1
			strAlert = strAlert + ValArray[i].title + "\n"
			ValArray[i].style.backgroundColor = "#c4d4ca"
		}else
			ValArray[i].style.backgroundColor = "white"
	}
	if (flag == 1){
		alert(strAlert)
		return false;
	}else
		return true; 
}




function checkSSN(number){
	numericStr = number.value
	var numbersOnly = "";
	var count=0;
	
	//LOOP THRU CHARS AND ONLY ALLOW COMMAS AND DECIMALS
	for (var charPtr=numericStr.length-1; charPtr>-1;charPtr--){
		if (isNaN(numericStr.charAt(charPtr)) && numericStr.charAt(charPtr) != "-"){
			alert("MUST BE NUMBER OR -.")
			if (isNav6)
				number.value = ""
			else
				number.select()
			return false;
			break;
		}
		//else if (numericStr.charAt(charPtr) != "-") numbersOnly = numericStr.charAt(charPtr) + numbersOnly; //&& numericStr.charAt(charPtr) != "."	
		//if(numericStr.charAt(charPtr) == ".") count=count+1
							
	}  	 
	 
	//if (count>1){
	 //   alert("MUST BE A NUMBER.")
	  //  if (isNav6)
	//			number.value = ""
//			else
				//number.select()
			//return false;
    		//	}
	

	//number.value = numericStr		
	//number.value=numbersOnly
}

















///////////////may not need
///this is checking for valid +/- number
//function checkAmt(number){
//	numericStr = number.value
//	var numbersOnly = "";
//	neg = false
//	validNum = true
//	
//	if (numericStr.charAt(0) == "-"){
//		neg = true
//		numericStr = numericStr.replace("-","")
//	}
//	//LOOP THRU CHARS AND ONLY ALLOW COMMAS AND DECIMALS
//	for (var charPtr=numericStr.length-1; charPtr>-1;charPtr--){
//		if (isNaN(numericStr.charAt(charPtr)) && numericStr.charAt(charPtr) != "," && numericStr.charAt(charPtr) != "."){
//			alert("MUST BE A VALID AMOUNT.")
//			validNum = false
//			number.select()
//			break;
//		}
//		else if (numericStr.charAt(charPtr) != ",") numbersOnly = numericStr.charAt(charPtr) + numbersOnly; //&& numericStr.charAt(charPtr) != "."						
//	}  	 
//	if (neg)
//		numericStr = "-" + numericStr
//	number.value = numericStr		
//}
///adds a decimal to a numbers
function addDec(amt){
	if(amt.value != ""){
		if(isNaN(Number(amt.value)) == false){
			decimal = amt.value.toString()
			decimal = decimal.lastIndexOf(".")
			prec = amt.value.length - decimal
			if (decimal == -1)
				decimal = amt.value + ".00"
			else if (prec == 2)
				decimal = amt.value + "0"
			else
				decimal = amt.value

			if (decimal.lastIndexOf(",") < 0){
				point = decimal.lastIndexOf(".")
				i = point - 3
				while (i>0){
					decimal = decimal.substr(0,i) + "," + decimal.substr(i,decimal.length)
					i = i - 3
				}
			}
			amt.value = decimal
		}else{
			alert("Must be numeric.")
			amt.value = ""
		}		
	}
}
///validDate
function validDate(eSrc) 
{	
	var len = eSrc.value.length;
	
	var dtA, dtB
	var dtDate = new Date();
	var month = dtDate.getMonth()+1;
	var day = dtDate.getDate(); 
	var year = dtDate.getFullYear();

	if (eSrc.value == "") {

		/*
		if (month <= 9)
			month = "0" + Number(month);

		if (day <= 9)
			day = "0" + Number(day);
			
		DtA = new Array(month, day, year);
		DtB = DtA.join("/");
		eSrc.value = DtB
		*/
		return true;
	}
	else 
	{

		var valid = "0123456789/-."
		var temp;
		for (var i=0; i<eSrc.value.length; i++) 
		{
			temp = "" + eSrc.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1")
			{
				alert("Invalid date!");
				return false;
			}
		}

		/*Check for /, if more then two : Invalid format*/
		var dtSep = 0;
		var sepLoc = -1;
		var sepLoc1;
		var sepChar = "\/";
		if (eSrc.value.indexOf(sepChar, sepLoc+1) > -1) {
			do {
			  sepLoc = eSrc.value.indexOf(sepChar, sepLoc+1)
			  dtSep += 1;
			}
			while (eSrc.value.indexOf(sepChar, sepLoc+1) > -1)
		}
		/*If Sep = 0 then check for .*/
		if (dtSep == 0) {
			sepLoc = -1;
			sepChar = "\.";
			if (eSrc.value.indexOf(sepChar, sepLoc+1) > -1) {
				do {
				  sepLoc = eSrc.value.indexOf(sepChar, sepLoc+1)
				  dtSep += 1;
				}
				while (eSrc.value.indexOf(sepChar, sepLoc+1) > -1)
			}
		}

		/*If Sep = 0 then check for -*/
		if (dtSep == 0) {
			sepLoc = -1;
			sepChar = "\-";
			if (eSrc.value.indexOf(sepChar, sepLoc+1) > -1) {	
				do {
				  sepLoc = eSrc.value.indexOf(sepChar, sepLoc+1)
				  dtSep += 1;
				}
				while (eSrc.value.indexOf(sepChar, sepLoc+1) > -1)
			}
		}

		if (dtSep == 0) {
			if (len <= 2) {
				month = eSrc.value;
				if (len == 1)
					month = "0" + month;
			}
			else if (len <= 4) {
				month = eSrc.value.substr(0, 2);
				day = eSrc.value.substr(2, 2);
				if (day.length == 1)
					day = "0" + day;
			}
			else {
				month = eSrc.value.substr(0, 2);
				day = eSrc.value.substr(2, 2);
				year = eSrc.value.substr(4, 4);
			}
		}
		else if (dtSep ==	1) {
			if (len == 1) {
				alert("Invalid Date.")
				return false;
			}
			sepLoc = 0;
			sepLoc = eSrc.value.indexOf(sepChar, sepLoc);
			if (sepLoc != 0) {
				month = eSrc.value.substr(0, sepLoc);
			}
			sepLoc = eSrc.value.lastIndexOf(sepChar);
			if (sepLoc+1 != len) {
				day = eSrc.value.substr(sepLoc+1, 2);
			}
		}
		else if (dtSep == 2) {
			if (len == 2) {
				alert("Invalid Date.")
				return false;
			}
			sepLoc = 0;
			sepLoc = eSrc.value.indexOf(sepChar, sepLoc);
			if (sepLoc != 0) {
				month = eSrc.value.substr(0, sepLoc);
			}

			sepLoc1 = sepLoc;
			sepLoc = eSrc.value.indexOf(sepChar, sepLoc+1);
			if (sepLoc != 1 && (sepLoc != sepLoc1+1)) {
				day = eSrc.value.substr((sepLoc1+1), (sepLoc - (sepLoc1+1)));
			}
	
			sepLoc = eSrc.value.lastIndexOf(sepChar);
			if (sepLoc != len)	{
				year = eSrc.value.substr(sepLoc+1, 4);
			}
		}
		else {
			alert("Invalid Date.")
			return false;
		}

		if (month <= 9)
			month = "0" + Number(month);

		if (day <= 9)
			day = "0" + Number(day);

		var val = Number(year);
		if (val >= 0 && val <= 9) {
			val = "200" + val.toString();
		}		
		if (val >= 10 && val <= 89) {
			val = "20" + val.toString();
		}
		if (val >= 90 && val <= 99) {
			val = "19" + val.toString();
		}
		if (val >= 100 && val <= 999) {
			val = "2" + val.toString();
		}
		val = Number(val);
		if (val < 1900)	{
			alert("Year should be greater than 1900.");
			return false;
		}
		year = val.toString();
		DtA = new Array(month, day, year);
		DtB = DtA.join("/");
		var myDate = new Date(DtB);
		if (myDate.getMonth()+1 == Number(month)) {
			eSrc.value = DtB
			return true;
		}
		else {
			alert("Invalid Date.")
			return false;
		}
	}
}

function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
     // alert (checkstr.indexOf(DateValue.substr(i,1)))
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
		 //alert (DateTemp)
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 19xx */
   //if (DateValue.length == 6) {
   //   DateValue = DateValue.substr(0,4) + '19' + DateValue.substr(4,2); }
    if (DateValue.length != 8 && DateValue.length != 0 ) {
      err = 19;}
   /* year is wrong if year = 0000 */
   else {//beginofdetailcheck
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(0,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(2,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   addyear= parseInt(year) +18
  // alert (addyear)
   var testdate= new Date(addyear, month-1, day)
   var xx= new Date ()
   
   //alert ("xx is " +xx)
   //alert("testdate is " +testdate)
   //alert (xx-testdate)
  if(xx<testdate) {
  err=27;
  }  
  addyear2=parseInt(year)+100
  var testdate2= new Date(addyear2, month-1, day)
  if(xx>testdate2) {
  err=28;
  } 
  }//endofdetal
    // var errormessage = "Date is incorrect! Please strictly conform to mm/dd/yyyy format!(use 01 for 1, 1968 for 68)"
   if (err == 0) {
      DateField.value = month  + seperator + day  + seperator + year;
   }
   /* Error-message if err != 0 */

   else {
     switch (err) {
	  case 19: errormessage = "Please strictly conform to mm/dd/yyyy format! (use 01 for 1, 1968 for 68)"; break
	  case 20: errormessage = "Year cannot be 0000!" ; break
	  case 21: errormessage="Month range is incorrect"; break
	  case 22:  errormessage="Day cannot be 0"; break
	  case 23: errormessage="The day of February in leap year can't be greater than 29"; break
	  case 24: errormessage="The day of February in non-leap year can't be greater than 28"; break
	  case 25: errormessage="Day range cannot exceed 31 days"; break
	  case 26: errormessage="There isnot 31 days in that month!"; break
	  case 27: errormessage="You must be 18 years old!" ; break
	  case 28: errormessage="Applicant must be younger than 100 years. If you are indeed greater than 100 years, please contact Homestar via Contact Us. "
	  }
      alert(errormessage);
      //DateField.select();
	  DateField.value="";
	  DateField.focus();
   }
}

function EmailCheck(thisfield)
{
    if (thisfield.value != "")
	{
	if (thisfield.value.indexOf("@") == "-1" || thisfield.value.indexOf(".") == "-1")
	{
	alert("Please enter a valid e-mail address.");
	thisfield.focus();
	}
	if (thisfield.value.indexOf(" ") != "-1")
	{
	alert("Please remove any spaces from your email address.");
	thisfield.focus();
	}
}	
}
