var windows = new Array();

function ClosePopupWindow()
{
	for(w in windows){
		if(!windows[w].closed)
			windows[w].close();
	}
}

function checkBrowser(){
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0
	this.ie=(this.ver.indexOf("MSIE")>-1 && this.dom)?1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie || this.ns4 || this.ns5)
	return this
}

function checkEmail(theField)
{
var reEmail = /^.+\@.+\..+$/

	if(isEmpty(theField.value))
		return true;
	else{
		if(!reEmail.test(theField.value)){
			alert("This field must be a valid email address (like xxx@xxx.com).");
			theField.focus();
			theField.select();
			return false;
		}
		else
			return true;
	}
}

function checkNumeric(theField)
{
	var MsgNumeric="This field should be numeric.";
	var reDigit = /^\d+$/
	
	if(theField.value.length==0)
		return true;
	if(!reDigit.test(theField.value)){
		alert("This field should be numeric.");
		theField.value="";
		theField.focus();
		theField.select();
		return false;
	}
	else
		return true;
}
function checkAreaCode(theField)
{
	var reDigit = /^\d+$/
	
	alert(theField.name);
	return true;
	
	if(theField.value.length==0)
		return ture;
		
	if(!reDigit.test(theField.value)){
		alert("This field should be numeric.");
		theField.focus();
		theField.select();
		return false;
	}
	else{
		anotherField.focus();
		return true;
	}
}

function checkPCPID(theField)
{
var iPCPID = "This field must be a 6 digit. Please reenter it now."
	
	if (isEmpty(theField.value))
		return true;
    else{ 
		if (!isPCPID(theField.value, false)) {
				theField.focus()
				theField.select()
				alert(iPCPID)
				return false         
		}
      else 
      {  // if you don't want to insert a hyphen, comment next line out
         return true;
      }
    }
}

function isPCPID(s)
{
var digitsInPCPID = 6;
	if (isEmpty(s)) 
		return true;
	else{
		return (isInteger(s) && (s.length == digitsInPCPID))
	}
}

function checkZIPCode(theField)
{   
var iZIPCode = "This field must be a 5 digit U.S. ZIP Code (like 94043). Please reenter it now."
	
	if (isEmpty(theField.value))
		return true;
    else{ 
        var normalizedZIP = stripCharsInBag(theField.value, "-")
		if (!isZIPCode(normalizedZIP, false)) {
				theField.focus()
				theField.select()
				alert(iZIPCode)
				return false         
		}
      else 
      {  // if you don't want to insert a hyphen, comment next line out
         theField.value = reformatZIPCode(normalizedZIP)
         return true;
      }
    }
}
function isZIPCode (s)
{  
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
	if (isEmpty(s)) 
		return true;
	else{
		return (isInteger(s) && ((s.length == digitsInZIPCode1) || (s.length == digitsInZIPCode2)))
	}
}

function isInteger (s)
{   
var reInteger = /^\d+$/
    if (isEmpty(s)) 
		return true;
	else
	    return reInteger.test(s)
}

function reformatZIPCode (ZIPString)
{   if (ZIPString.length == 5) return ZIPString;
    else return (reformat (ZIPString, "", 5, "-", 4));
}

function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}
function hasChar(sValue)
{
	var bChr=false;
	var c;
	for(var i=0; i<sValue.length; i++){
		c = sValue.charCodeAt(i);
		if(c != 160){
			bChr=true;
			break;
		}
	}
	return bChr;
}

function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

function checkEffectiveValidDate(theField, sDefaultEffDate)
{
	if(checkValidDate(theField)==true){
		if(checkFirstDayMonth(theField)==true){
	
			//if(dateDiff(theField.value, sDefaultEffDate) < 0 ){
				//alert("Effective Date can not be earlier than " + sDefaultEffDate + ".")
				//theField.value="";
				//theField.focus();
				//theField.select();
				//return false;
			//}
		}
	}
	return true;
}

function checkFirstDayMonth(theField){
	var vDate = new Date(theField.value);
	var vDay;
	vDay=vDate.getDate();
	
	if(vDay !=1){
		alert("Effective should be first day of the month.");
				theField.value="";
				theField.focus();
				theField.select();
				return false;
	}
	else{
		return true;
	}


}

function dateDiff(sFirstDate, sSecondDate) {
	date1 = new Date();
	date2 = new Date();
	diff  = new Date();
	var days=0;
	
	date1temp = new Date(sFirstDate);
	date1.setTime(date1temp.getTime());
	
	date2temp = new Date(sSecondDate);
	date2.setTime(date2temp.getTime());

	//diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
	diff.setTime(date1.getTime() - date2.getTime());
	
	timediff = diff.getTime();
	days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
	
	//dateform.difference.value = " days "  + days;
	return days;

}


function checkValidDate(theField)
{
	if(!isValidDate(theField.value)){
		alert("This field must be a valid date format (like mm/dd/yyyy).");
		theField.focus();
		theField.select();
		return false;
	}
	else
		return true;
}

function isValidDate(dateStr)
{
// dateStr must be of format month day year with 
// or dashes separating the 
// to be made to use day month year or another format. 
// This function returns True if the date is valid.
	var slash1=dateStr.indexOf("/");
	if(slash1==-1){slash1 = dateStr.indexOf("-");}
	// if no slashes or dashes, invalid date    
	if(slash1 ==-1) {return false;}
	var dateMonth = dateStr.substring(0, slash1);
	var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
		
	var slash2 = dateMonthAndYear.indexOf("/");
	if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
		
	// if not a second slash or dash, invalid date    
	if (slash2 == -1) { return false;}

	var dateDay = dateMonthAndYear.substring(0, slash2);
	var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
	if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
	
	// if any non-digits in the month, invalid date    
	for(var x=0; x < dateMonth.length; x++){
		var digit = dateMonth.substring(x, x+1);
		if ((digit < "0") || (digit > "9")){
			return false;
		}
	}	
	//alert(dateStr);

	// convert the text month to a number    
	var numMonth = 0;
	for (var x=0; x < dateMonth.length; x++){
		digit=dateMonth.substring(x, x+1);
		numMonth *= 10;
		numMonth += parseInt(digit);
	}

	if ((numMonth <= 0) || (numMonth > 12)) { return false; }
	
	// if any non-digits in the day, invalid date    
	for (var x=0; x < dateDay.length; x++) {
		digit = dateDay.substring(x, x+1);
		if ((digit < "0") || (digit > "9")) { return false;}
	}
	// convert the text day to a number    
	var numDay = 0;
	for (var x=0; x < dateDay.length; x++) {
		digit = dateDay.substring(x, x+1);
		numDay *= 10;
		numDay += parseInt(digit);
	}
	
	
	if ((numDay <= 0) || (numDay > 31)) { return false; }
	// February can't be greater than 29 (leap year calculation comes later)
	if ((numMonth == 2) && (numDay >29)) { return false; }
	// check for months with only 30 days    
	if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) {
		if (numDay > 30){return false;}
	}


	// if any non-digits in the year, invalid date    
	for (var x=0; x < dateYear.length; x++){
		digit = dateYear.substring(x, x+1);
		if ((digit < "0") || (digit > "9")) {return false;}
	}
	

	// convert the text year to a number    
	var numYear = 0;
	for (var x=0; x < dateYear.length; x++) {
		digit = dateYear.substring(x, x+1);
		numYear *= 10;
		numYear += parseInt(digit);
	}

	
	// Year must be a 2-digit year or a 4-digit year    
	//if ( (dateYear.length != 2) && (dateYear.length!= 4) ) { return false; }
	if (dateYear.length!= 4){ return false; }
	
	// if 2-digit year, use 50 as a pivot date   
	if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000;}
	if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900;}
	if ((numYear <= 0) || (numYear > 9999)) {return false; }
	// check for leap year if the month and day is Feb 29    

	if ((numMonth == 2) && (numDay == 29)){
		var div4=numYear%4;
		var div100=numYear%100;
		var div400=numYear%400;
		
		// if not divisible by 4, then not a leap year so Feb 29 is invalid
		if(div4 != 0){return false;}
		// at this point, year is		
		//divisible by 4. So if year is divisible by   
		// 100 and not 400, then it's not a leap year so Feb 29 is invalid 
		if((div100==0) && (div400 !=0)){return false;}
	}

	return true;

}

function CheckValueAndGotoAnotherField(theField, thFieldLength, anotherField)
{
	var reDigit = /^\d+$/
			
	if(theField.value.length < thFieldLength){
		return true;
	}
	else{
		if(!reDigit.test(theField.value)){
				alert("This field should be numeric.");
				theField.focus();
				theField.select();
				return false;
			}
		else{
			anotherField.focus();
		}	
	}
	return true;
}

