/*** FORM VALIDATION ***/

// Is the field blank
function isBlank(strField)
{
	if (!strField || strField=="" || strField==null) 
	{
		return true;
	}
	return false;
}

// is the field text only
function isValidString(strField)
{
	var illegalChars = /[\W(0-9)\_]/;

	if (illegalChars.test(strField)) 
	{
		return false;
	}
	return true;
}

// is the field text + hyphen
function isValidTextString(strField) 
{
	var illegalTextChars = /[_(0-9)\!\@\#\$\%\^\&\*\(\)\=\+\\\/\[\]\{\}\"\'\:\;\>\.\<\,\~\`\?]/;
	if (illegalTextChars.test(strField)) 
	{
		return false;
	}
	return true;
}

//is the field a number
function isValidNumber(i)
{
	if (i/i != 1)
	{
		return false;
	}
	return true;
}

//is the field a phone number
function isValidPhoneNumber(strField)
{
	var illegalPhoneChars = /[(a-zA-Z)\_\!\@\#\$\%\^\&\*\(\)\=\+\\\/\[\]\{\}\"\'\:\;\>\<\,\~\`\?]/;
	if (illegalPhoneChars.test(strField))
	{
		return false;
	}
	return true
}

// is it a valid email
function isValidEmail(strField)
{
	var validEmailString = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalEmailChars = /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	if (!validEmailString.test(strField)) {
		return false;
	}

	if (illegalEmailChars.test(strField)) {
		return false;
	}
	return true;
}

// is valid US SSN
function isValidSSN(ssn) {
    var ssnRE = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/;
    if (!ssnRE.test(ssn)) { return false; }
    var tempSSN = ssn;
    if (ssn.indexOf("-") != -1) { tempSSN = (ssn.split("-")).join(""); }
    if (ssn.indexOf(" ") != -1) { tempSSN = (ssn.split(" ")).join(""); }
    if (tempSSN.substring(0, 3) == "000") { return false; }
    if (tempSSN.substring(3, 5) == "00") { return false; }
    if (tempSSN.substring(5, 9) == "0000") { return false; }
    if (tempSSN.substring(0, 9) == "111111111") { return false; }
    if (tempSSN.substring(0, 9) == "222222222") { return false; }
    if (tempSSN.substring(0, 9) == "333333333") { return false; }
    if (tempSSN.substring(0, 9) == "444444444") { return false; }
    if (tempSSN.substring(0, 9) == "555555555") { return false; }
    if (tempSSN.substring(0, 9) == "666666666") { return false; }
    if (tempSSN.substring(0, 9) == "123456789") { return false; }
    return true;
}

function isValidPassword(strPass1, strPass2, intMin, intMax) {
	if(intMin=null) { intMin=4; }
	if(intMax=null) { intMin=16; }

	if(isBlank(strPass1) || isBlank(strPass2)) { return false; }
	if(strPass1 < intMin || strPass1 > intMax) { return false; } 
	if(strPass1 != strPass2) { return false; }
	return true;
}

function isTOS(strCheck) {
	return strCheck.checked;
}

/** totally doesnt work, bloody pathetic language that doesnt have this anyway
/*function is_array(arrInput)
{
	return typeof(arrInput)=='object'&&(arrInput instanceof Array);
}*/
/*
function is_array(arrInput) {
	return (arrInput.constructor == Array);
}*/

function isset(anyVar) {
	return (anyVar==undefined) ? false: true;
}


// loop the form
function isValidForm(strForm)
{
	var alertMsg;
	var emptyFields = "";
	var errors = "";

	// any form element that doesnt have optional property
	for (var i = 0; i < strForm.length; i++)
	{
		var e = strForm.elements[i];
		var ebackground = "#F20000";
		var sbackground = "#00F200";
		var cbackground = "#FFFFFF";

		if (((e.type == "text") || (e.type == "textarea") || (e.type == "file")) && !e.optional)
		{
			// if the element is blank, store it
			if ((e.value == null) || (e.value == "") || isBlank(e.value))
			{
				emptyFields += "\n" + e.name;
				e.style.background = ebackground;
				continue;
			}

			// handle text string
			if (e.text)
			{
				if (!isValidString(e.value))
				{
					errors += e.name + " may contain numbers or punctuation.\n";
					e.style.background = ebackground;
					continue;
				}
				e.style.background = sbackground; /*** This needs to be reworked ***/
				continue;
			}

			// handle text string + hyphen
			if (e.textHyphen)
			{
				if (!isValidTextString(e.value))
				{
					errors += e.name + " may contain numbers or punctuation.\n";
					e.style.background = ebackground;
					continue;
				}
				e.style.background = sbackground; /*** This needs to be reworked ***/
				continue;
			}

			// handle regular numbers
			if (e.numeric)
			{
				if (!isValidNumber(e.value))
				{
					errors += e.name + " is not a valid number.\n";
					e.style.background = ebackground;
					continue;
				}
				e.style.background = sbackground; /*** This needs to be reworked ***/
				continue;
			}

			// handle phone numbers, i.e. regular 
			// phone numbers with hypens or periods 
			// as seperators
			if (e.phone)
			{
				if (!isValidPhoneNumber(e.value))
				{
					errors += e.name + " is not a valid phone number.\n";
					e.style.background = ebackground;
					continue;
				}
				e.style.background = sbackground; /*** This needs to be reworked ***/
				continue;
			}

			// handle email
			if (e.email)
			{
				if (!isValidEmail(e.value))
				{
					errors += e.name + " is not a valid email.\n";
					e.style.background = ebackground;
					continue;
				}
				e.style.background = sbackground; /*** This needs to be reworked ***/
				continue;
			}
		}

		if(e.tos && !e.optional) {
			if(!isTOS(e)) {
				errors += "You must agree to the Terms and Conditions.\n\n";
				e.style.background = ebackground;
				//f.style.background = ebackground;
				continue;
			}
		}

		if(e.type == "password" && !e.optional) 
		{
			j=i+1;
			var f = strForm.elements[j];

			if(!isValidPassword(e.value, f.value)) 
			{
				errors += "\nEither your passwords do not match, are blank, or your password does not fall between 4 and 16 characters.\n\n";
				e.style.background = ebackground;
				f.style.background = ebackground;
				continue;
			}
				e.style.background = sbackground; /*** This needs to be reworked ***/
				f.style.background = sbackground; /*** This needs to be reworked ***/
				continue;
		}
	}

	// no errors? you passed validation! :)
	if (!emptyFields && !errors) { return true; }

	// start building error message
	msg = "_________________________________________________________________\n";
	msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "_________________________________________________________________\n\n";

	// add empty boxes, if any
	if (emptyFields) {
		msg += "- The following required field(s) are empty:" + emptyFields + "\n";
		if (errors) msg += "\n";
	}

	// add errors to msg
	msg += errors;
	// show message
	alert(msg);
	// you didnt pass :(
	return false;
}
