//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	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
//	7 = Group Check Box Validation
//	8 = Confirm Email Address Validation
//	9 = Phone Number Strict Validation (format: xxx-xxx-xxxx)
//	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 7:
				errorMsg += validateIsChecked(itemsArray[x][0]);
				break;
			case 8:
				errorMsg += validateConfirmEmail(itemsArray[x][0]);
				break;
			case 9:
				errorMsg += validatePhoneStrict(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>";
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Performs basic IS Number valiation on a Text Box
//	7 = Group Check Box Validation
//	Requires at least one Check Box in the Group to be Checked,
//	it will not validate and display an error. 
//	The error it displays is the ALT text of the Element Node itself
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function validateIsChecked(objID)
{
	var currentElement = document.getElementById(objID);
	var currentElementDiv = document.getElementById(objID + "DivQuestion");
	var currentClassName = currentElementDiv.className;
	var isCheck = false;
	for (xyz=0;xyz<currentElement.childNodes.length-1;xyz++)
	{
		if (currentElement.childNodes[xyz].type == "checkbox")
		{
			tempErrorMsg = currentElement.childNodes[xyz].alt
			if (currentElement.childNodes[xyz].checked == true)
			{
				isCheck = true;
			}
		}
	}

	if (isCheck == false)
	{
		if (!currentClassName.match(" validationError"))
		{
			currentElementDiv.style.color = "red";
		}
		return "<li>" + tempErrorMsg + "</li>";
	}
	else
	{
		currentElementDiv.style.color = "black";
		return "";
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Performs basic IS Number valiation on a Text Box
//	8 = Confirms Email AddressValidation
//	Requires Two Text Fields Delimited by "-_-" ex ("email-_-confirmEmail")
//	The second item must be the confirm email text field and must not be blank or
//	it will not validate and display an error. 
//	The error it displays is the ALT text of the Confirm Email Element Node itself
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function validateConfirmEmail(objID)
{
	var getEmails = objID.split("-_-") ;
	var emailOne = document.getElementById(getEmails[0]);
	var emailTwo = document.getElementById(getEmails[1]);
	var currentClassNameOne = emailOne.className;
	var currentClassNameTwo = emailTwo.className;
	if (emailOne.value != emailTwo.value || emailTwo.value == "")
	{
		if (!currentClassNameTwo.match(" validationError"))
			emailTwo.className += " validationError";
			return "<li>" + emailTwo.alt + "</li>";
	}
	else
	{
		emailTwo.className = currentClassNameTwo.replace(" validationError","");
		return "";
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Performs Phone Number valiation on a Text Box
//	9 = Phone Number Strict 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 validatePhoneStrict(objID)
{
	var currentElement = document.getElementById(objID);
	var reg = /^[2-9]\d{2}-\d{3}-\d{4}$/ //regular expression defining a xxx-xxx-xxxx format phone number
	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>";
	}
}
