//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Function:	Validates Array of Items
//	Arguments:	Single Multi Dim. Array
//	Returns:	A HTML read out of the Error Msg and highlights offending
//				Element Nodes in Red.
//	Example:	myArray = new Array(["first_name",1],["last_name",1],["email",2],["state",4],["zipCode",3])
//	1 = Basic Validation
//	2 = Email Validation
//	3 = Zip Code Validation
//	4 = Combo Box Validation
//	5 = Phone Number Validation
//	6 = IS Number Validation
//	sp1 = Basic Validation when changes Item 1 to red if invalid
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function validateFormItems(itemsArray,displayMsgID)
{
	errorMsg = "";
	
	//Loops through Array and validates according to the Second Dim. of the Array
	for (x=0; x<itemsArray.length;x++)
	{
		//alert("Loop: " + x + "-" + itemsArray[x][0] + "-" + itemsArray[x][1]);
		//Selects proper Validation Function based on the valType from the Array
		switch (itemsArray[x][1])
		{
			case 1:
				errorMsg += basicValidation(itemsArray[x][0]);
				break;
			case 2:
				errorMsg += emailValidation(itemsArray[x][0]);
				break;
			case 3:
				errorMsg += zipCodeValidation(itemsArray[x][0]);
				break;
			case 4:
				errorMsg += comboBoxValidation(itemsArray[x][0]);
				break;
			case 5:
				errorMsg += validatePhone(itemsArray[x][0]);
				break;
			case 6:
				errorMsg += validateIsNum(itemsArray[x][0]);
				break;
			case "sp1":
				tempMsg = basicValidation(itemsArray[x][0]);
				if (tempMsg != '') { document.getElementById('item1').style.color = 'red'; errorMsg += tempMsg; }
				else { document.getElementById('item1').style.color = ''; }
				break;
			default:
				break;
		}
	}
	var errMsgDiv = document.getElementById(displayMsgID);
	if (errorMsg != "")
	{
		//Populate Error Div with Error Msg and Display Div
		errMsgDiv.innerHTML = errorMsg;
		errMsgDiv.parentNode.className = "showErrors";
		return false;
	}
	else
	{
		//Clear Error Div with Error Msg and Hide Div
		errMsgDiv.innerHTML = "";
		errMsgDiv.parentNode.className = "noErrors";
		return true;
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Performs basic valiation on a Text Box
//	1 = Basic Validation
//	Requires the value to be greater than null/empty
//	are it will not validate and display an error. 
//	The error it displays is the ALT text of the Element Node itself
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function basicValidation(objID)
{
	var currentElement = document.getElementById(objID);
	var currentClassName = currentElement.className;
	if (currentElement.value == "")
	{
		if (!currentClassName.match(" validationError"))
		{
			currentElement.className += " validationError";
		}
		return "<li>" + currentElement.alt + "</li>";
	}
	else if (currentElement.value != "")
	{
		currentElement.className = currentClassName.replace(" validationError","");
		return "";
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Performs basic valiation on a Text Box
//	2 = Email Validation
//	Requires the value to have an "@" sign and "." with Numbers and Letters
//	are it will not validate and display an error. 
//	The error it displays is the ALT text of the Element Node itself
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function emailValidation(objID)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var currentElement = document.getElementById(objID);
	var currentClassName = currentElement.className;
	
	if ((currentElement.value == '') || (reg.test(currentElement.value) == false))
	{
		//return "* Email: Please enter.<br />";
		if (!currentClassName.match(" validationError"))
		{
			currentElement.className += " validationError";
		}
		return "<li>" + currentElement.alt + "</li>";
	}
	else
	{
		currentElement.className = currentClassName.replace(" validationError","");
		return "";
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Performs basic valiation on a Text Box
//	3 = Zip Code Validation
//	Requires the length to be 5 digits and only Numbers
//	are it will not validate and display an error. 
//	The error it displays is the ALT text of the Element Node itself
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function zipCodeValidation(objID)
{
	var currentElement = document.getElementById(objID);
	var RE = /^-{0,1}\d*\.{0,1}\d+$/;
	var currentClassName = currentElement.className;
	
	if(currentElement.value.length != 5)
	{
		if (!currentClassName.match(" validationError"))
		{
			currentElement.className += " validationError";
		}
		return "<li>" + currentElement.alt + "</li>";
	}
	else if (!RE.test(currentElement.value))
	{
		if (!currentClassName.match(" validationError"))
		{
			currentElement.className += " validationError";
		}
		return "<li>" + currentElement.alt + "</li>";
	}
	else
	{
		currentElement.className = currentClassName.replace(" validationError","");
		return "";
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Performs basic valiation on a Combo Box
//	4 = Combo Box Validation
//	Requires another Option besides theFirst Option is
//	selected are it will not validate and display an error. 
//	The error it displays is the TITLE text of the Element Node itself
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function comboBoxValidation(objID)
{
	var currentElement = document.getElementById(objID);
	var currentClassName = currentElement.className;
	if((currentElement.options[0].selected) || (currentElement.selectedIndex == 0))
	{
		if (!currentClassName.match(" validationError"))
		{
			currentElement.className += " validationError";
		}
		return "<li>" + currentElement.title + "</li>";
	}
	else
	{
		currentElement.className = currentClassName.replace(" validationError","");
		return "";
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Performs Phone Number valiation on a Text Box
//	5 = Phone Number Validation
//	Requires '(xxx) xxx-xxxx' Format for Phone Number
//	otherwise it will not validate and display an error. 
//	The error it displays is the ALT text of the Element Node itself
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function validatePhone(objID)
{
	var currentElement = document.getElementById(objID);
	var currentClassName = currentElement.className;
	
	if (currentElement.value != '')
	{
	    if (currentElement.value == ' (xxx) xxx-xxxx') 
	    {
			if (!currentClassName.match(" validationError"))
			{
				currentElement.className += " validationError";
			}
		    return "<li>" + currentElement.alt + "</li>";
	    }
		else
		{
			currentElement.className = currentClassName.replace(" validationError","");
			return "";
		}
	}
	else
	{
		if (!currentClassName.match(" validationError"))
		{
			currentElement.className += " validationError";
		}
		return "<li>" + currentElement.alt + "</li>";
	}
/* Commented out due to Stakeholder changing thier minds on the validation of this field several times. */
/*
	var reg = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
	var currentElement = document.getElementById(objID);
	var currentClassName = currentElement.className;
	
	if (currentElement.value != '')
	{
	    if (reg.test(currentElement.value) == false) 
	    {
			if (!currentClassName.match(" validationError"))
			{
				currentElement.className += " validationError";
			}
		    return "<li>" + currentElement.alt + "</li>";
	    }
		else
		{
			currentElement.className = currentClassName.replace(" validationError","");
			return "";
		}
	}
	else
	{
		if (!currentClassName.match(" validationError"))
		{
			currentElement.className += " validationError";
		}
		return "<li>" + currentElement.alt + "</li>";
	}
*/
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Performs basic IS Number valiation on a Text Box
//	6 = IS Number Validation
//	Requires value to be a numeric value otherwise
//	it will not validate and display an error. 
//	The error it displays is the ALT text of the Element Node itself
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function validateIsNum(objID)
{
	var currentElement = document.getElementById(objID);
	var RE = /^-{0,1}\d*\.{0,1}\d+$/;
	
	if (!RE.test(currentElement.value))
	{
		if (!currentClassName.match(" validationError"))
		{
			currentElement.className += " validationError";
		}
		return "<li>" + currentElement.alt + "</li>";
	}
}
