<!-- Include in your html <script LANGUAGE="JavaScript" src="js\validate.js"></script> -->

function cleanSalary( asNumbers ) {
	var checkOK = "0123456789";
	var checkStr = asNumbers;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				allNum += ch;
				break;
		if (j == checkOK.length) {
			break;
		}
		
	}

	return allNum;
}



function isBlank(s) {
	var c, i;
	for(i=0; i < s.length; i++) {
		c = s.charAt(i);
		if (( c!= " ") && (c != "\n") && (c != "\t")) return false;
	}
	return true;
}

function isDollar( asNumbers ) {
	var checkOK = "0123456789.";
	var checkStr = asNumbers;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	var decimal = asNumbers.substring(asNumbers.indexOf(".")+1);
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
		allNum += ch;
	}
	if (asNumbers.indexOf(".") != -1) {
		if (decimal.length > 2) {
			allValid = false;
		}
	}
	return allValid
}


function isNumeric( asNumbers ) {
	var checkOK = "0123456789";
	var checkStr = asNumbers;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";        
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
		allNum += ch;
	}
	return allValid
}

function isReal( asNumbers ) {
   var checkStr;

   if (asNumbers.substr(0,1) == ".") {
        asNumbers = "0" + asNumbers;
   }
   
   checkStr = asNumbers;
   
   if (checkStr.length == 0) {
      return true;
   } else {
      return /^-{0,1}\d+(.\d{1,3}){0,1}$/.exec(asNumbers);
   }
}

function isPhone(sAreaCode, sPhone) {
    // Function returns the following values
    // 0 - Invalid area code and/or phone number
    // 1 - Valid area code and phone number ( 5 Digits )
    var RE;
        
    RE = /[^0-9]/g;
    sAreaCode = sAreaCode.replace(RE,'');
    sPhone = sPhone.replace(RE,'');
            
    if ((sAreaCode.length == 3) && (sPhone.length == 7)) {
       return 1;
    }
                   
    return 0;
        
}	 

function isPhoneAndArea(sPhone) {
    // Function returns the following values
    // 0 - Invalid area code and/or phone number
    // 1 - Valid area code and phone number ( 10 Digits )
    var RE;
        
    RE = /[^0-9]/g;
    
    sPhone = sPhone.replace(RE,'');            
    if (sPhone.length == 10) {
       return 1;
    }
                   
    return 0;
        
}	 

function cleanZIP(sZip) {
        // Function removes anthing that is not a digit or character        
	sZip = sZip.toUpperCase();
	sZip = sZip.replace(/[^0-9|^A-Z]/g,'');
        return sZip;
}

function isZIP(sZip) {
    // Function returns the following values
    // 0 - Invalid code
    // 1 - USA Zip ( 5 Digits )
    // 2 - Canadian ( Number, Alpha pair )    
    match = /\d{5,5}/.exec(sZip);
    if (match && (sZip.length == 5)) {
        return 1;
    }
    
    match = /(([A-Z]\d){3})/.exec(sZip);
    if (match && (sZip.length == 6)) {
        return 2;
    }    
    return 0;      
}

function isSSN(sSSN) {
	var sTemp="",bSuccess=true;
	sTemp = sSSN.substring(0,sSSN.indexOf("-"));
	if ((isNumeric(sTemp)) && (sTemp.length == 3)) {
		//OK
	} else {
		bSuccess = false;
	}
	sTemp = sSSN.substring(sSSN.indexOf("-")+1,sSSN.lastIndexOf("-"));
	if ((isNumeric(sTemp)) && (sTemp.length == 2)) {
		//OK
	} else {
		bSuccess = false;
	}
	sTemp = sSSN.substring(sSSN.lastIndexOf("-")+1);
	if ((isNumeric(sTemp)) && (sTemp.length == 4)) {
		//OK
	} else {
		bSuccess = false;
	}
	return bSuccess;
}

function isMemberCode(sCode) {
    // Function returns true if the code is valid
    // Should match AA9999        
    sCode = sCode.toUpperCase();
    match = /^([A-Z]{2}\d{4})/.exec(sCode);
    if (match) {
        return true;
    }
    return 0;      
}

function isYear(sYear) {
        var sMsg = '';
        if (isBlank(sYear)) {
                sMsg = "\tYear is blank";
        } else if ( ! isNumeric(sYear) ) {                
                sMsg = "\tYear must be numeric";
        } else if ( parseFloat(sYear) > 2010 ) {
                sMsg = "\tYear cannot be more than 2010";
        } else if ( parseFloat(sYear) < 1950 ) {
                sMsg = "\tYear has to be more than 1950";
        }
        return sMsg;
}

function isDate(sMonth,sDay,sYear) {
	var now;
	var ddate;
	var msg;
	
	now = new Date();
	ddate = new Date(sMonth + "/" + sDay + "/" + sYear);				
	msg = "";

	if ( isBlank(sMonth) ) return "\tThe MONTH was left blank";
	if ( isBlank(sDay) ) return "\tThe DAY was left blank";
	if ( isBlank(sYear) ||  (sYear.length != 4) ) return "\tThe YEAR was left blank or is not 4 digits long.";

	if (isNumeric(sDay)) {
		if (isNumeric(sMonth)) {
			if (isNumeric(sYear)) {
				if (sDay > 31 || sDay < 1) {
					msg = "\tThe DAY is not between 1 and 31";
				} else {
					if ((sMonth > 12) || (sMonth < 1)) {
						msg = "\tThe MONTH is not between 1 and 12.";
					} else {
						if (sDay != ddate.getDate()) {
							msg = "\tThe DATE is not a valid date.";
						}
					}
				}

			} else {
				msg = "\tThe YEAR is not a number.";
			}
		} else {
			msg = "\tThe MONTH is not a number.";
		}

	} else {
		msg = "\tThe DAY is not a number.";
	}
	return msg;
}

function isDateV(sMonth,sDay,sYear) {
	var now;
	var ddate;
	var msg;
	
	now = new Date();
	ddate = new Date(sMonth + "/" + sDay + "/" + sYear);				
	msg = "";

	if ( isBlank(sMonth) ) msg += "\tThe MONTH was left blank\n";
	if ( isBlank(sDay) ) msg += "\tThe DAY was left blank\n";
	if ( isBlank(sYear) ) msg += "\tThe YEAR was left blank\n";
        
	if ( ! isNumeric(sDay) ) msg += "\tThe DAY is not a number.\n";
	if ( ! isNumeric(sMonth) ) msg += "\tThe MONTH is not a number.\n";
	if ( ! isNumeric(sYear) ) msg += "\tThe YEAR is not a number.\n";
        if ( sYear.length != 4 ) msg += "\tThe YEAR must be 4 digits long\n";
	if ( sDay > 31 || sDay < 1 ) msg += "\tThe DAY is not between 1 and 31\n";
	if ( sMonth > 12 || sMonth < 1 ) msg += "\tThe MONTH is not between 1 and 12.\n";
	if ( sDay != ddate.getDate() )  msg += "\tThe DATE is not a valid date.\n";

	return msg;
}

function jf_check_ssn(oSSN) {
	if (isBlank(oSSN.value)) { return; }
	if (! isSSN(oSSN.value)) { 
		window.alert("SSN is not in the correct format.");
	}
}

function jf_days_between(sFromMM, sFromDD, sFromYYYY, sToMM, sToDD, sToYYYY) {
        // Give the days between FROM and TO.  If the To Fields are empty todays date will be used.
	var oFrom, oTo, oToday, lDifference;	        
 
	oFrom = new Date(sFromMM + "/" + sFromDD +"/"+ sFromYYYY );
        if ( sToMM != null ) {
                oTo = new Date(sToMM + "/" + sToDD +"/"+ sToYYYY );
        } else {
	        oToday = new Date();
                oTo = new Date( oToday.getFullYear(), oToday.getMonth(), oToday.getDate() ); 
        }
	lDifference = oFrom.getTime() - oTo.getTime();	        
        lDifference = lDifference / (1000 * 60 * 60 * 24);	     
        return lDifference;
}

function setToday(sMonth, sDay, sYear) {
	var dtoday;

	dtoday = new Date();
	if (sMonth.selectedIndex == 0) {
		sMonth.options[dtoday.getMonth() + 1].selected="SELECTED";
	}
	if (isBlank(sDay.value)) {
		sDay.value   = dtoday.getDate();
	}
	if (isBlank(sYear.value)) {
		sYear.value  = dtoday.getFullYear();
	}
}

function checkPrice( sPrice ) {
        var sWarningMsg;
        sWarningMsg = ''
        if (parseFloat(sPrice) < 25 || parseFloat(sPrice) > 125) {
		sWarningMsg = 'The order price, $'+sPrice+', is out of the normal range of $25-$125.\n\nIf this is correct press Ok.\nIf you would like to modify the amount press Cancel';                				
	}
        return sWarningMsg;
}

function ReplaceString(sString, sOld, sNew) {
	var aParts;
        var sNewString;
	aParts = sString.split(sOld);
        sNewString = aParts.join(sNew);
        return sNewString;
}

function isEmail (s){
    var digits = "0123456789";
    var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
    var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //electronic addresses
    var validDomainNameChars = digits + uppercaseLetters + lowercaseLetters + "-_.";

    if (isBlank(s)) {
        return 'email address is blank';
    }
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++ }
    if ((i >= sLength) || (s.charAt(i) != "@")) return ("no @ sign");
    else atloc = i;
    j = i+1;
    i += 1;
    while ((j < sLength) && (validDomainNameChars.indexOf(s.charAt(j)) != -1))
    { j++ }
    if (j < sLength) return("invalid character in domain name: "+s.charAt(j));
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++ }
    if (i == sLength) return("no . in domain name");
    if (i == (atloc +1)) return("not enough space between @ and .");
    k = atloc+1;
    while (k < sLength){
      if ((s.charAt(k) == ".") && (s.charAt(k+1) == ".")) return("too many .'s");
      k++
    }
    l = sLength;
    while ((i < sLength -2) && (l != i) && (s.charAt(l) != "."))
    { l = l-1 }
    if ((i >= sLength - 2) || (s.charAt(i) != ".") || (l >= sLength - 2)) return("not enough chars after .");
    else return '';
}

function isTooLong(oTextBox, nMaxLen, sName, sName2) {
        var sMsg;
	sMsg = '';
	if ( oTextBox.value.length > parseInt(nMaxLen) ) {
	        sMsg = sName + " can not be more than " + nMaxLen + " characters.  Your " + sName2 +" is currently " + oTextBox.value.length + " characters long.";
	}
        return sMsg;        
}

