function validEmail(obj)
{
	var str=trimAll(obj.value);
	var a;
	var len=str.length;
	var reg=new RegExp("@");
	var ind=str.search(reg);
	var c,d=0;
	var b=0;
	for(a=0;a<=str.length;a++)
	{

		if(str.charAt(a)=="@")
		{
			b++;
		}
		
	}

	if(b>1)
		{
			obj.focus();
			return false;
		}
	else
		{
			for(a=0;a<=str.length;a++)
			{
				if(str.charAt(a)==".")
				{
					d=a;
				}
			
			}
			if((ind>d)||(ind==-1)||(d==0)||(ind+1==d)||(d+1==len))
			{
				obj.focus();
				return false;
			}
			else
			{
				return true;
			}
		}
}

function isNumeric(value)	
{
		return (/^[0-9]+$/.test(value));
}
function isAlphabets(value)	
{
		return (/^[A-Za-z ]+$/.test(value));
}

function HideRequired()
{
	document.getElementById('Fname').style.visibility="hidden";
	document.getElementById('Lname').style.visibility="hidden";
	document.getElementById('Country').style.visibility="hidden";
	document.getElementById('Email').style.visibility="hidden";
	document.getElementById('CardType').style.visibility="hidden";
	document.getElementById('CardNumber').style.visibility="hidden";
	document.getElementById('CardVNumber').style.visibility="hidden";
	document.getElementById('Month').style.visibility="hidden";
	document.getElementById('Year').style.visibility="hidden";
	document.getElementById('HolderName').style.visibility="hidden";
}
function showError(reqID)
{
	document.getElementById(reqID).style.visibility="visible";
}

function showRequired(txtsrc,reqID)
{
	var sourceVal=document.getElementById(txtsrc.id).value;
	if(trimAll(sourceVal)=="" || trimAll(sourceVal)=="-1")
	{
		document.getElementById(reqID).style.visibility="visible";
	}
	else
	{
		document.getElementById(reqID).style.visibility="hidden";
	}
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function ValidateForm(frmID)
{
	var msg="";
	var msg1="";
	var msg2="";
	var msg3="";
	var msg4="";
	var msg5="";
	var msg6="";
	var msg7="";
	var msg8="";
	var msg9="";
	var msg10="";

	var objFrm=document.getElementById(frmID);
	if(trimAll(objFrm.ewayCustomerFirstName.value) == "") 
	{
		objFrm.ewayCustomerFirstName.focus();
		showError('Fname');
		msg1="<li>Please enter your first name.</li>";
	}
	else if(!isAlphabets(trimAll(objFrm.ewayCustomerFirstName.value)))
	{
		objFrm.ewayCustomerFirstName.focus();
		showError('Fname');
		msg1="<li>First name is not valid. Only alphabets are allowed.</li>";
	}

	if(trimAll(objFrm.ewayCustomerLastName.value) == "")
	 {
		objFrm.ewayCustomerLastName.focus();
		showError('Lname');
		msg2="<li>Please enter your last name.</li>";
	}
	else if(!isAlphabets(trimAll(objFrm.ewayCustomerLastName.value)))
	{
		objFrm.ewayCustomerLastName.focus();
		showError('Lname');
		msg2="<li>Last name is not valid. Only alphabets are allowed.</li>";
	}

	if(trimAll(objFrm.ewayCustomerAddress.value) == "" || trimAll(objFrm.ewayCustomerAddress.value) == "-1") 
	{
		objFrm.ewayCustomerAddress.focus();
		showError('Country');
		msg3="<li>Please select your country.</li>";
	}

	if(trimAll(objFrm.ewayCustomerEmail.value) == "" )
	{
		objFrm.ewayCustomerEmail.focus();
		showError('Email');
		msg4="<li>Please enter your email.</li>";
	}
	else if(validEmail(objFrm.ewayCustomerEmail) == false)
	{
		showError('Email');
		msg4="<li>Please enter a valid email.</li>";
	}

	if(trimAll(objFrm.ewayOption2.value) == "") 
	{
		objFrm.ewayOption2.focus();
		showError('CardType');
		msg5="<li>Please select your credit card type.</li>";
	}

	if(trimAll(objFrm.ewayCardNumber.value) == "")
	 {
		objFrm.ewayCardNumber.focus();
		showError('CardNumber');
		msg6="<li>Please enter your credit card number.</li>";
	}
	else if(!isNumeric(trimAll(objFrm.ewayCardNumber.value)))
	{
		objFrm.ewayCardNumber.focus();
		showError('CardNumber');
		msg6="<li>Credit card number is not valid. Only numbers are allowed.</li>";
	}

	if(trimAll(objFrm.ewayCVN.value) == "")
	 {
		objFrm.ewayCVN.focus();
		showError('CardVNumber');
		msg7="<li>Please enter your card verification number.</li>";
	}
	else if(!isNumeric(trimAll(objFrm.ewayCVN.value)))
	{
		objFrm.ewayCVN.focus();
		showError('CardVNumber');
		msg7="<li>Card verification number is not valid. Only numbers are allowed.</li>";
	}
	if(trimAll(objFrm.ewayCardExpiryMonth.value) == "") 
	{
		objFrm.ewayCardExpiryMonth.focus();
		showError('Month');
		msg8="<li>Please select your card expiry month.</li>";
	}

	if(trimAll(objFrm.ewayCardExpiryYear.value) == "") 
	{
		objFrm.ewayCardExpiryYear.focus();
		showError('Year');
		msg9="<li>Please select your card expiry year.</li>";
	}

	if(trimAll(objFrm.ewayCardHoldersName.value) == "")
	 {
		objFrm.ewayCardHoldersName.focus();
		showError('HolderName');
		msg10="<li>Please enter your card holder name.</li>";
	}
	else if(!isAlphabets(trimAll(objFrm.ewayCardHoldersName.value)))
	{
		objFrm.ewayCardHoldersName.focus();
		showError('HolderName');
		msg10="<li>Card holder name is not valid. Only alphabets are allowed.</li>";
	}
	msg=msg1+msg2+msg3+msg4+msg5+msg6+msg7+msg8+msg9+msg10;
	if(trimAll(msg)=="")
	{
		document.getElementById('Errors').innerHTML="&nbsp;";
		document.getElementById('Errors').style.display="none";
	}
	else
	{
		document.getElementById('Errors').innerHTML="<b>Please correct the following errors:</b><ul>"+msg+"</ul>";
		document.getElementById('Errors').style.display="block";
		return false;
	}

	HideRequired();
	return true;
}
