// filename:		validate.js
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		this is a javascript file that is contains functions to check a
//					form for correct inputs


// function name: 	checkWholeForm()
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		This function calls a series of subfunctions, each of which
//					checks a single form element for compliance with a specific
//					string format and returns a message describing the error.
//					If the function returns an empty string, we know the element complies.
// parameters:		p_objForm - the form to be checked
// return value:	true or false - prior to returning false an alert box is displayed
// global refs:		none
function checkWholeForm(p_objForm)
{
	var strErrorMsg = "";
	strErrorMsg += checkEmail(p_objForm.email.value);
	strErrorMsg += checkPhone(p_objForm.phone.value);
	strErrorMsg += checkPassword(p_objForm.password.value);
	strErrorMsg += checkUsername(p_objForm.username.value);
	strErrorMsg += isEmpty(p_objForm.notempty.value);
	strErrorMsg += isDifferent(p_objForm.different.value);
	for (i=0, n=p_objForm.radios.length; i<n; i++)
	{
		if (p_objFormrm.radios[i].checked)
		{
			var blnCheckValue = p_objForm.radios[i].value;
			break;
		}
	}
	strErrorMsg += checkRadio(blnCheckValue);
	strErrorMsg += checkDropdown(p_objForm.choose.selectedIndex);
	if (strErrorMsg != "")
	{
		alert(strErrorMsg);
		return false;
	}
	return true;
}

// function name: 	checkEmail()
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		check email
// parameters:		p_strText - text entered to check
// return value:	strError - empty is everything is alright otherwise the error.
// global refs:		none
function checkEmail(p_strText, p_strFieldName)
{
	var strError="";
	if (p_strFieldName == null) p_strFieldName = "";
	if (p_strText == "")
	{
		strError = "You didn't enter an " + p_strFieldName + " email address.\n";
	}
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(p_strText)))
	{
		strError = "Please enter a valid " + p_strFieldName + " email address.\n";
	}
	else
	{
		// test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (p_strText.match(illegalChars))
		{
			strError = "The " + p_strFieldName + " email address contains illegal characters.\n";
		}
	}
	return strError;
}

// function name: 	checkPhone()
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		phone number - strip out delimiters and check for 11 digits
// parameters:		p_strText - text entered to check
// return value:	strError - empty is everything is alright otherwise the error.
// global refs:		none
function checkPhone(p_strText)
{
	var strError = "";
	if (p_strText == "")
	{
		strError = "You didn't enter a phone number.\n";
	}
	var varStripped = p_strText.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
	if (isNaN(parseInt(varStripped)))
	{
		strError = "The phone number contains illegal characters.\n";
	}
	if (!(varStripped.length == 11))
	{
		strError = "The phone number is the wrong length. Make sure you included an area code.\n";
	}
	return strError;
}

// function name: 	checkPassword()
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		password - between 6-8 chars, uppercase, lowercase, and numeral
// parameters:		p_strText - text entered to check
// return value:	strError - empty is everything is alright otherwise the error.
// global refs:		none
function checkPassword (p_strText)
{
	var strError = "";
	if (p_strText == "")
	{
		strError = "You didn't enter a password.\n";
	}
	var illegalChars = /[\W_]/; // allow only letters and numbers
	if ((p_strText.length < 6) || (p_strText.length > 8))
	{
		strError = "The password is the wrong length.\n";
    }
	else if (illegalChars.test(p_strText))
	{
		strError = "The password contains illegal characters.\n";
    }
    else if (!((p_strText.search(/(a-z)+/)) && (p_strText.search(/(A-Z)+/)) && (p_strText.search(/(0-9)+/))))
	{
		strError = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }
	return strError;
}

// function name: 	()
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		username - 4-10 chars, uc, lc, and underscore only.
// parameters:		p_strText - text entered to check
// return value:	strError - empty is everything is alright otherwise the error.
// global refs:		none
function checkUsername (p_strText)
{
	var strError = "";
	if (p_strText == "")
	{
		strError = "You didn't enter a username.\n";
	}
	var illegalChars = /\W/; // allow letters, numbers, and underscores
	if ((p_strText.length < 4) || (p_strText.length > 10))
	{
		strError = "The username is the wrong length.\n";
	}
	else if (illegalChars.test(p_strText))
	{
		strError = "The username contains illegal characters.\n";
	}
	return strError;
}

// function name: 	isEmpty()
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		non-empty textbox
// parameters:		p_strText - text entered to check, p_strFieldName if passed through
//					will be used for a more detailed error message
// return value:	strError - empty is everything is alright otherwise the error.
// global refs:		none
function isEmpty(p_strText, p_strFieldName)
{
	var strError = "";
	if (p_strFieldName == null) p_strFieldName = "";
	if (p_strText.length == 0)
	{
		if (p_strFieldName.length == 0) strError = "The mandatory text area has not been filled in.\n"
		else strError = "Please enter a " + p_strFieldName + "\n"
	}
	return strError;	  
}

// function name: 	isDifferent()
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		description
// parameters:		p_strText - text entered to check
// return value:	strError - empty is everything is alright otherwise the error.
// global refs:		none
// was textbox altered
function isDifferent(p_strText1, p_strText2, p_strErrorText)
{
	var strError = "";
	if (p_strErrorText == null) p_strErrorText = "";
	if (p_strText1 != p_strText2)
	{
  	if (p_strErrorText == "")
    	strError = "The values entered do not match.\n";
    else
      strError = p_strErrorText;
	}
	return strError;
}

// function name: 	checkRadio()
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		exactly one radio button is chosen
// parameters:		p_blnCheckValue - the checked or not value, 
// return value:	strError - empty is everything is alright otherwise the error.
// global refs:		none
function checkRadio(p_blnCheckValue)
{
	var strError = "";
	if (!(p_blnCheckValue))
	{
		strError = "Please check a radio button.\n";
	}
	return strError;
}

// function name: 	checkDropdown()
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		valid selector from dropdown list
// parameters:		p_intChoice - the selected index to check
// return value:	strError - empty is everything is alright otherwise the error.
// global refs:		none
function checkDropdown(choice, p_strFieldName)
{
	var strError = "";
	if (p_strFieldName == null) p_strFieldName = "";
	if (choice == "" || choice == "null")
	{
		if (p_strFieldName.length == 0) strError = "You didn't choose an option from the drop-down list.\n";
		else strError = "You didn't choose an option from the drop-down list, " + p_strFieldName + ".\n";
	}
	return strError;
}

// function name: 	checkNumber()
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		number - strip out delimiters and check is a number (integer)
// parameters:		p_strText - text entered to check
// return value:	strError - empty is everything is alright otherwise the error.
// global refs:		none
function checkNumber(p_strText, p_strFieldName)
{
	var strError = "";
	if (p_strFieldName == null) p_strFieldName = "";
	if (p_strText.length == 0)
	{
		if (p_strFieldName.length == 0) strError = "You didn't enter a number.\n";
		else strError = "You didn't enter a number for, " + p_strFieldName + ".\n";
		return strError;
	}
	
	var varStripped = p_strText.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
	if (isNaN(parseInt(varStripped)))
	{
		strError = p_strFieldName + " contains illegal characters.";
	}
	return strError;
}

// function name: 	checkCheckbox()
// author:			Dean Pollard / Rob Jones
// copyright:		Sunday Launch Limited www.sundaylaunch.co.uk
// description:		exactly one checkbox button is chosen in a check box array
// parameters:		p_objCheckValue - the object or undefined, 
// return value:	strError - empty is everything is alright otherwise the error.
// global refs:		none
function checkCheckbox(p_objCheckValue, p_strFieldName)
{
	var strError = "";
	if (p_strFieldName == null) p_strFieldName = "";
	if (p_objCheckValue == null || p_objCheckValue == "")
	{
		if (p_strFieldName.length == 0) strError = "Please check a checkbox.\n";
		else strError = "Please check a checkbox for " + p_strFieldName + ".\n";
	}
	return strError;
}
