//VP JavaScript Validation Document

//VP Email validation format
vld_Email 					= /^(([a-zA-Z0-9\-])+((\.|_)([a-zA-Z0-9\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*)))?$/;
//vld_Email_Message			= "Format should be -\n\t user.123@abc.com\n\tor user_123@abc.com\n\tor user@abc.co.in"

/*
vld_ZipPostalCodeUS 		= /^(([0-9]{5}(( |\-)[0-9]{4})?){1})?$/;
vld_ZipPostalCodeUS_Message = "Use xxxxx-xxxx or xxxxx xxxx or xxxxx format.";

vld_ZipPostalCodeI 			= /^([0-9]{6})?$/;
vld_ZipPostalCodeI_Message 	= "It can contain a 6 Digit Number only";
*/

vld_ZipCode 				= /^([0-9]{5,10})?$/;
//vld_ZipCode_Message 		= "It can contain a 5 Digit Number only";


function ValidateForm(pobjForm)
{
	var IsValidField = true;
	
	lobjElements = pobjForm.elements;
	//alert(lobjElements.length);
	
	for(lintLopCnt = 0; lintLopCnt < lobjElements.length; lintLopCnt ++ )
	{
		if(lobjElements[lintLopCnt].getAttribute("required") == "yes")
		{
			//alert(lobjElements[lintLopCnt].getAttribute("fieldName"));
			if (lobjElements[lintLopCnt].type=="text")
			{
				if(lobjElements[lintLopCnt].value == "")
				{
					lstrErrSpan = lobjElements[lintLopCnt].getAttribute("fieldName");
					//alert(lobjElements[lintLopCnt].value);
					//document.getElementById(lstrErrSpan).innerHTML = "Required";
					alert("Please Fill "+lstrErrSpan+" Field !");
					return false;
					//IsValidField = false;
				}
				else if(lobjElements[lintLopCnt].value != "")
				{
					lstrErrSpan = "err" + lobjElements[lintLopCnt].getAttribute("fieldName");
											
					if (validate(lobjElements[lintLopCnt]))
					{	
						//document.getElementById(lstrErrSpan).innerHTML = "";
					}
					else
					{
					//	document.getElementById(lstrErrSpan).innerHTML = "Incorrect";
					    return false;
						//IsValidField = false;
					}
				}
			}
			
			//VP textarea
			
			if (lobjElements[lintLopCnt].type=="textarea")
			{
				//
			}
			
			//VP validate combo box
			if (lobjElements[lintLopCnt].type=="select-one")
			{
				if(lobjElements[lintLopCnt].getAttribute("required") == "yes")
				{
					if(lobjElements[lintLopCnt].selectedIndex == 0)
					{
						lstrErrSpan = "err" + lobjElements[lintLopCnt].getAttribute("fieldName");
						document.getElementById(lstrErrSpan).innerHTML = "Required";
						IsValidField = false;
					}
					else
					{
						lstrErrSpan = "err" + lobjElements[lintLopCnt].getAttribute("fieldName");
						document.getElementById(lstrErrSpan).innerHTML = "";
					}
				}
			}
		}
	}
	
	if(IsValidField == true)
	{
		return true;
	}
	else
	{
		//alert('Please fill Required fields.');
		return false;
	}
}

function validate(pObj)
{
	IsValidField = true;
	lstrErrSpan = lobjElements[lintLopCnt].getAttribute("fieldName");
	if(pObj.getAttribute("validation") != null)
	{
		lstrValid = pObj.getAttribute("validation");
		//alert(lstrValid);
		if ( (eval(lstrValid).test(pObj.value)== false ) )
		{
			alert(lstrErrSpan+" Field is Incorrect !") 
			return false;
			//IsValidField = false;
			
		}
	}
	
	if (IsValidField == true)
	{
		return true;
	}
	else
	{
		return false;
	}
}