// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

// email
/*
check to see if the email string looks like an email address. 
We want it to follow this format: some characters, then an at symbol (@), 
then some more characters, then a dot (.), then two or three more characters, and that’s it.
*/
function checkEmail (strng, field) {
	var error="";
	if (strng == "")
	{
		error = "The " + field + " field cannot be empty.";	
	}
	
	if (error == "")
	{
		// Test the Length
		if (strng.length > 45) 
		{
			error = "The " + field + " cannot be longer than 45 characters";
		}
		else
		{	
			//test email for illegal characters
			var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
			if (strng.match(illegalChars))
			{
				error = "Illegal characters.";
			}
		}
		
		// Test the Format and Syntax
		var emailFilter=/^([a-zA-Z0-9]+([\.+_-][a-zA-Z0-9]+)*)@(([a-zA-Z0-9]+((\.|[-]{1,2})[a-zA-Z0-9]+)*)\.[a-zA-Z]{2,6})$/;
		if (!(emailFilter.test(strng))) {
			error += "Invalid Email Format.";
		}
	}
	
	return error;    
}
		
		
function checkZipCode (strng, field) {
	var error="";
	if (strng == "")
	{
		error = "The " + field + " field cannot be empty.";	
	}
	
	if (error == "")
	{
		// Test the Format and Syntax
		var zipCodeFilter=/(^\d{5}$)|(^\d{5}-\d{4}$)/;
		if (!(zipCodeFilter.test(strng))) {
			error += "Invalid Zip Code Format.";
		}
	}
	
	return error;    
}	

function checkHeight (strng, field, isRequired) {
	var error="";
	if ( isRequired && (strng == "") ) 
	{
		error += "The " + field + " cannot be empty.";
	}	
	
	if (error == "")
	{
		if(!isNumber(strng))
		{
			// Test the Format and Syntax
			var heightFilter=/(^\d?\d'(\d|1[01])?.?(\d|1[01])"$)/;
			if (!(heightFilter.test(strng))) {
				error += "Invalid Height Format.";
			}
		}
	}
	
	return error; 
}	


// phone number - strip out delimiters and check for 10 digits
/*
To validate a phone number, first we want to clear out any spacer characters, such as parentheses, dashes, spaces, and dots. 
Having done that, we look at what we have left with the isNaN() function (which checks to see if a given input is Not A Number), to test if it's an integer or not. 
If it contains anything other than digits, we reject it.
Then we count the length of the number. 
It should have exactly ten digits — any more or less, and we reject it.
*/

function checkPhone (phoneNumber)
{
	var error = "";
	
	var stripped = phoneNumber.replace(/[\(\)\.\-\ \+]/g, ''); // strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped)))
	{
		error = "The mobile number entered is not valid.";		
	}
	
	if (!(stripped.length > 5)) 
	{
		error = "The mobile number entered is not valid.";
	}
	
	return error;
}
		
/*

For the password field, we want to constrain the length again (this time, we’ll keep it between 6 and 8 characters), and we want to allow only letters and numbers — no underscores this time. So we have to use a new regular expression to define which characters we’re banning. This one, like the last one, includes \W — everything but letters, numbers, and underscores — but we also need to explicitly mention underscores, so as to permit only letters and numbers. Hence: /[\W_]/.

When it comes to passwords, we want to be strict with our users. It’s for their own good; we don’t want them choosing a password that’s easy for intruders to guess, like a dictionary word or their kid’s birthday. So we want to insist that every password contain a mix of uppercase and lowercase letters and at least one numeral. We specify that with three regular expressions, a-z, A-Z, and 0-9, each followed by the + quantifier, which means “one or more,” and we use the search() method to make sure they’re all there:
*/
// password - between 6-8 chars, uppercase, lowercase, and numeral
function checkPassword (strng, field) {
	var error = "";
	
	if (strng == "") 
	{
		error = "The " + field + " field cannot be empty.";	
	}
	
	if (error == "")
	{
		// Test the password length
		if ((strng.length < 6) || (strng.length > 15)) 
		{
		   error = "The " + field + " length must be between 6 and 15 characters";
		}
		else
		{
			if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) 
			{
		   		error = "The " + field +" must contain at least one uppercase letter, one lowercase letter, and one numeric digit.";
			}
		}
	}
	
	return error;
}

// username - 4-10 chars, uc, lc, and underscore only.
function checkField(strng, field, isRequired)
{
	var error = "";
	if ( isRequired && (strng == "") ) 
	{
		error += "The " + field + " cannot be empty.";
	}	

	// Commented reason: Used in too many fields at our site we should only check for require field here...
	/*if(error == "")
	{
		if (strng.length > 45) 
		{
			error += "The " + field + " cannot be longer than 45 characters";
		}

		var illegalChars = /\W/; // allow letters, numbers, and underscores
		strng = strng.replace(/\s/, '_');
		if (illegalChars.test(strng)) 
		{
			error += "The " + field + " contains illegal characters (only letters, numbers and underscores are allowed).";
		}
	}*/
	
	return error;
}
		
// was textbox altered
function isDifferent(strng) {
	var error = ""; 
	if (strng != "Can\'t touch this!") {
		error = "You altered the inviolate text area.\n";
	}
	return error;
}

/*

To make sure that a radio button has been chosen from a selection, we run through the array of radio buttons and count the number that have been checked. Rather than sending the whole radio object to a subfunction, which can be problematic (because the radio object has no property indicating which value has been chosen), we pre-process the radio form element in a for loop and send that result to a subfunction for evaluation.

for (i=0, n=theForm.radios.length; i<n; i++) {
   if (theForm.radios[i].checked) {
      var checkvalue = theForm.radios[i].value;
      break;
   }
}
why += checkRadio(checkvalue);
*/
// exactly one radio button is chosen
function checkRadio(checkvalue) {
	var error = "";
	if (!(checkvalue)) {
		error = "Please check a radio button.\n";
	}
	return error;
}
		
// valid selector from dropdown list
function checkDropdown(choice) {
	var error = "";
	if (choice == 0) {
		error = "You didn't choose an option from the drop-down list.\n";
	}    
	return error;
}    

// End of Apple sample code 

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function isNumber(sText)
{
	var ValidChars = "0123456789.+-";
	var IsNumber   = true;

	var Char;
	for (i = 0; i < sText.length; i++)
	{   
        // Check that current character is number.
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			IsNumber = false;
		}
    }

    // All characters are numbers "+", "-" or "."
	return IsNumber;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr, format){
	var error = "";
	var daysInMonth = DaysArray(12);
	var pos1        = dtStr.indexOf(dtCh);
	var pos2        = dtStr.indexOf(dtCh, pos1 + 1);
	
	if (format == 'mm/dd/yyyy'){
		// mm/dd/yyyy
		var strMonth = dtStr.substring(0, pos1);
		var strDay   = dtStr.substring(pos1 + 1, pos2);
	}else{
		var strDay   = dtStr.substring(0, pos1);
		var strMonth = dtStr.substring(pos1 + 1, pos2);
	}
	
	var strYear = dtStr.substring(pos2 + 1);
	strYr       = strYear;
	
	if (strDay.charAt(0)=="0" && strDay.length > 1)
		strDay = strDay.substring(1);
	if (strMonth.charAt(0) == "0" && strMonth.length > 1)
		strMonth=strMonth.substring(1);

	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1)
			strYr=strYr.substring(1);
	}
	
	month = parseInt(strMonth);
	day   = parseInt(strDay);
	year  = parseInt(strYr);
	
	if (pos1 == -1 || pos2 == -1){
		return "The date format should be : " +  format;
	}
	
	if (strMonth.length < 1 || month < 1 || month > 12){
		return "Please enter a valid month";
	}

	if (strDay.length < 1 || day < 1 || day > 31 || (month==2 && day > daysInFebruary(year)) || day > daysInMonth[month]){
		return error = "Please enter a valid day";
	}
	
	if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear){
		return "Please enter a valid 4 digit year between " + minYear + " and " + maxYear;
	}
	
	if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false){
		return "Please enter a valid date";
	}

	return "";
}

function checkAlertCode(sText)
{
	var error = "";
	if(sText.length != 4 )
	{
		error = "The code must be four digits length.";
	}
	else
	{
		if(isInteger(sText) == false )
		{
			error = "The code must contain only numbers.";
		}
	}
	return error;
}