
function VerifyEmail(email) 
{ 
	var at_pos =-1; 
	var atMore_pos= -1; 
	var pd_pos = -1; 
	var pdMore_pos = -1;	
	var sp_pos= -1; 
	var em_len= -1; 
	var allowed= -1;
	em_len = email.length; 
	if (em_len < 1) 
	{ 
		return -1; 
	} 

	allowed=allowedCharater(email,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_@.",64,true);

 	if(allowed==2)
  {
		return -9; 
	}
 	
 	if(allowed==3)
  {
		return -10; 
	}

	sp_pos = email.indexOf(" ", 0); 
	if (sp_pos != -1) 
	{ 
		return -2; 
	} 

	at_pos = email.indexOf("@", 0); 
	if (at_pos == -1) 
	{ 
		return -3; 
	} 

	pd_pos = email.indexOf(".", at_pos); 
	if (pd_pos == -1) 
	{ 
		return -4; 
	} 

	if (pd_pos == (em_len - 1)) 
	{ 
		return -5; 
	} 

	if (pd_pos == (at_pos + 1)) 
	{ 
		return -6; 
	} 

	if (at_pos == 0) 
	{ 
	return -7; 
	} 

	atMore_pos = email.indexOf("@", at_pos+1); 
	if (atMore_pos != -1) 
	{ 
		return -8; 
	} 

	while(email.indexOf(".", pd_pos+1)!=-1)
	{
		pdMore_pos = email.indexOf(".", pd_pos+1); 
		if(pdMore_pos == pd_pos+1)
		{
			return -11;
		}
		pd_pos = pdMore_pos;
	}

	return 0; 
} 



function allowedCharater(fieldToBeChecked, allowedChar,maxLength,isMust)
{
  var illegal;
  if(isMust && fieldToBeChecked=="")
  {
    return 1; 
	
  }

  if(fieldToBeChecked.length>maxLength)
  {
    return 2; 
  }



  if(allowedChar=="XXX")
  {
		return 0;
  }

  var checkOK = allowedChar;
  var checkStr = fieldToBeChecked;

  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      return 3;
    }
  }
  return 0;

}

function startsOk(fieldToBeChecked,notAllowedChar)
{
  var checkField = fieldToBeChecked;
  var notAllowed = notAllowedChar;
  var chStart = checkField.charAt(0);
  var chEnd = checkField.charAt(checkField.length-1);

  for (j = 0;  j < notAllowed.length;  j++)
  {
	var chNotAllowed = notAllowed.charAt(j);
      	if((chStart == chNotAllowed) || (chEnd == chNotAllowed))
	{
		return 1; // Not OK
	}
  }
  return 0; // OK

}

function stripSpaces(fieldToBeChecked) 
{
    x = fieldToBeChecked;
    while (x.substring(0,1) == ' ') x = x.substring(1);
    while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
    return x;

}


function validateaddress(theForm)
{
  var retStatus = 0;
  
	// First Name Validation
  theForm.firstname.value = stripSpaces(theForm.firstname.value);
  retStatus = allowedCharater(theForm.firstname.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .",24,true);

  if(retStatus==1)
  {
    alert("Please enter First Name");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the First Name field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphabet and Whitespace characters in the First Name field.");
  }

  if(retStatus!=0)
  {

    theForm.firstname.focus();
    theForm.firstname.select();
    return (false);
  }
  
	// Last Name Validation
  theForm.lastname.value = stripSpaces(theForm.lastname.value);
  retStatus = allowedCharater(theForm.lastname.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .",24,true);

  if(retStatus==1)
  {
    alert("Please enter Last Name");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the Last Name field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphabet and Whitespace characters in the Last Name field.");
  }

  if(retStatus!=0)
  {

    theForm.lastname.focus();
    theForm.lastname.select();
    return (false);
  }
  
  //Company Name validation
  theForm.company.value = stripSpaces(theForm.company.value);
  retStatus = allowedCharater(theForm.company.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .-#&*()[];,/",36,false);

  if(retStatus==2)
  {
    alert("Please enter at most 36 characters in the Company field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphabet and Whitespace characters in the Company field.");
  }

  if(retStatus>1)
  {

    theForm.company.focus();
    theForm.company.select();
    return (false);
  }

  //Address1 validation
  theForm.addr1.value = stripSpaces(theForm.addr1.value);
  retStatus = allowedCharater(theForm.addr1.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ._-@#&*()[];,/",24,true);

  if(retStatus==1)
  {
    alert("Please enter Address 1");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the Address 1 field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphanumeric characters and \"-\",\"_\", \"@\",\".\",\"#\",\"&\",\"*\",\"(\",\")\",\"[\",\"]\",\";\",\",\",\"/\" in the Address 1 field.");
  }

  if(retStatus!=0)
  {

    theForm.addr1.focus();
    theForm.addr1.select();
    return (false);
  }

  //Address2 validation
  theForm.addr2.value = stripSpaces(theForm.addr2.value);
  retStatus = allowedCharater(theForm.addr2.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ._-@#&*()[];,/",24,false);

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the Address 2 field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphanumeric characters and \"-\",\"_\", \"@\",\".\",\"#\",\"&\",\"*\",\"(\",\")\",\"[\",\"]\",\";\",\",\",\"/\" in the Address 2 field.");
  }

  if(retStatus>1)
  {

    theForm.addr2.focus();
    theForm.addr2.select();
    return (false);
  }

  //Address3 validation
  theForm.addr3.value = stripSpaces(theForm.addr3.value);
  retStatus = allowedCharater(theForm.addr1.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ._-@#&*()[];,/",24,false);

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the Address 3 field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphanumeric characters and \"-\",\"_\", \"@\",\".\",\"#\",\"&\",\"*\",\"(\",\")\",\"[\",\"]\",\";\",\",\",\"/\" in the Address 3 field.");
  }

  if(retStatus>1)
  {

    theForm.addr3.focus();
    theForm.addr3.select();
    return (false);
  }

	// City Validation
  theForm.city.value = stripSpaces(theForm.city.value);
  retStatus = allowedCharater(theForm.city.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .",24,true);

  if(retStatus==1)
  {
    alert("Please enter city");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the city field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphabet and Whitespace characters in the city field.");
  }

  if(retStatus!=0)
  {

    theForm.city.focus();
    theForm.city.select();
    return (false);
  }

	// State Validation
  if(theForm.state.value == "")
  {
    alert("Please select state");
    theForm.state.focus();
    //theForm.state.select();
    return (false);
  }


	// Zip Validation
  theForm.zip.value = stripSpaces(theForm.zip.value);
  retStatus = allowedCharater(theForm.zip.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_- .",16,true);

  if(retStatus==1)
  {
    alert("Please enter Zip code");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 16 characters in the Zip field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphanumeric and Whitespace characters in the Zip field.");
  }

  if(retStatus!=0)
  {

    theForm.zip.focus();
    theForm.zip.select();
    return (false);
  }

	// country Validation
  theForm.country.value = stripSpaces(theForm.country.value);
  retStatus = allowedCharater(theForm.country.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_- .",24,true);

  if(retStatus==1)
  {
    alert("Please enter Country");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the Country field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphanumeric and Whitespace characters in the Country field.");
  }

  if(retStatus!=0)
  {

    theForm.country.focus();
    theForm.country.select();
    return (false);
  }

	// phone Validation
  theForm.phone.value = stripSpaces(theForm.phone.value);
  retStatus = allowedCharater(theForm.phone.value, "1234567890 -",16,true);

  if(retStatus==1)
  {
    alert("Please enter Phone code");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 16 characters in the Phone field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Numeric and Whitespace characters in the Phone field.");
  }

  if(retStatus!=0)
  {

    theForm.phone.focus();
    theForm.phone.select();
    return (false);
  }

	return (true)
}


function validateshipmethod(theForm)
{
	// Shipping Method Validation
  if(theForm.shipmethod.value == "")
  {
    alert("Please select shipping method");
    theForm.shipmethod.focus();
    return (false);
  }
  return (true)
}


function validateemail(theForm)
{
	var isValidMail=0; 

	theForm.email.value = stripSpaces(theForm.email.value);
	isValidMail = startsOk(theForm.email.value,"-_.@");

	if(isValidMail!=0)
	{
		alert("E-Mail Address field can not start or end with \"-\",\"_\",\"@\",\".\".");
		theForm.email.focus();
    theForm.email.select();
	   return (false);
	}
	else
	{
		isValidMail = VerifyEmail(theForm.email.value);

		if(isValidMail == -9)
	  {
			alert("Please enter atmost 64 characters in E-Mail Address.");
			theForm.email.focus();
	    theForm.email.select();
			return(false);
		}
		else
  		{
				if(isValidMail!=0)
				{
					alert("Please enter a Valid E-Mail Address . Only Alphanumeric characters and \"-\",\"_\", \"@\",\".\" characters are allowed in E-Mail Address field.");
					theForm.email.focus();
					theForm.email.select();
					 return (false);
				}
  		}

	}
	return (true)
}


function validatecard(theForm)
{

  var retStatus = 0;
	
	if (!theForm.radcardtype[0].checked && !theForm.radcardtype[1].checked && !theForm.radcardtype[3].checked && !theForm.radcardtype[4].checked)
	{
		alert("Please select a payment method.");
		theForm.radcardtype[0].focus();
		return (false);
	}

	// Card No Validation
  theForm.txtcardno.value = stripSpaces(theForm.txtcardno.value);
  retStatus = allowedCharater(theForm.txtcardno.value, "1234567890",16,true);

  if(retStatus==1)
  {
    alert("Please enter Credit Card Number.");
  }
  if(retStatus==2)
  {
    alert("Please enter at most 16 characters in the Credit Card Number Field.");
  }
  if(retStatus==3)
  {
    alert("Please enter only Numeric characters in the Credit Card Number Field");
  }
  if(retStatus!=0)
  {
    theForm.txtcardno.focus();
    theForm.txtcardno.select();
    return (false);
  }
  
	// Card No Validation
  theForm.txtcardno.value = stripSpaces(theForm.txtcardno.value);
  retStatus = allowedCharater(theForm.txtcardno.value, "1234567890",16,true);

  if(retStatus==1)
  {
    alert("Please enter Credit Card Number.");
  }
  if(retStatus==2)
  {
    alert("Please enter at most 16 characters in the Credit Card Number Field.");
  }
  if(retStatus==3)
  {
    alert("Please enter only Numeric characters in the Credit Card Number Field");
  }
  if(retStatus!=0)
  {
    theForm.txtcardno.focus();
    theForm.txtcardno.select();
    return (false);
  }
  
  //Expiry Date Validation
  if(theForm.selmonth.value != "" && theForm.selyear.value != "" )
  {
  	if (!isExpiryDate(theForm.selyear.value,theForm.selmonth.value))
  	{
			alert("This card has already expired. Please enter a valid credit card information.");
			theForm.selmonth.focus();
			return (false);
  	}
  
  }
  else
  {
    alert("Please select expiration month and year.");
    theForm.selmonth.focus();
    return (false);
  }
  
  
	return (true);
}

function isExpiryDate(nyear,nmonth) 
{
	var today = new Date();
	var expiry = new Date(nyear, nmonth);
	
	if (today.getTime() > expiry.getTime())
		return false;
	else
		return true;
}


function validateenquiry(theForm)
{
  var retStatus = 0;
  
	// contactname Validation
  theForm.contactname.value = stripSpaces(theForm.contactname.value);
  retStatus = allowedCharater(theForm.contactname.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .",24,true);

  if(retStatus==1)
  {
    alert("Please enter Contact Name");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the Contact Name field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphabet and Whitespace characters in the Contact Name field.");
  }

  if(retStatus!=0)
  {

    theForm.contactname.focus();
    theForm.contactname.select();
    return (false);
  }
    
  //Company Name validation
  theForm.company.value = stripSpaces(theForm.company.value);
  retStatus = allowedCharater(theForm.company.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .-#&*()[];,/",36,false);

  if(retStatus==2)
  {
    alert("Please enter at most 36 characters in the Company field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphabet and Whitespace characters in the Company field.");
  }

  if(retStatus>1)
  {

    theForm.company.focus();
    theForm.company.select();
    return (false);
  }

  //Address validation
  theForm.address.value = stripSpaces(theForm.address.value);
  retStatus = allowedCharater(theForm.address.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ._-@#&*()[];,/",64,false);

  if(retStatus==1)
  {
    alert("Please enter Address.");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the Address field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphanumeric characters and \"-\",\"_\", \"@\",\".\",\"#\",\"&\",\"*\",\"(\",\")\",\"[\",\"]\",\";\",\",\",\"/\" in the Address field.");
  }

  if(retStatus!=0)
  {

    theForm.address.focus();
    theForm.address.select();
    return (false);
  }

	// City Validation
  theForm.city.value = stripSpaces(theForm.city.value);
  retStatus = allowedCharater(theForm.city.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .",24,false);

  if(retStatus==1)
  {
    alert("Please enter city");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the city field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphabet and Whitespace characters in the city field.");
  }

  if(retStatus!=0)
  {

    theForm.city.focus();
    theForm.city.select();
    return (false);
  }

	// State Validation
  theForm.state.value = stripSpaces(theForm.state.value);
  retStatus = allowedCharater(theForm.state.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .",24,false);

  if(retStatus==1)
  {
    alert("Please enter State");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the State field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphabet and Whitespace characters in the State field.");
  }

  if(retStatus!=0)
  {

    theForm.state.focus();
    theForm.state.select();
    return (false);
  }

	// Zip Validation
  theForm.zip.value = stripSpaces(theForm.zip.value);
  retStatus = allowedCharater(theForm.zip.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_- .",16,false);

  if(retStatus==1)
  {
    alert("Please enter Zip code");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 16 characters in the Zip field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphanumeric and Whitespace characters in the Zip field.");
  }

  if(retStatus!=0)
  {

    theForm.zip.focus();
    theForm.zip.select();
    return (false);
  }

	// country Validation
  theForm.country.value = stripSpaces(theForm.country.value);
  retStatus = allowedCharater(theForm.country.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_- .",24,false);

  if(retStatus==1)
  {
    alert("Please enter Country");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 24 characters in the Country field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Alphanumeric and Whitespace characters in the Country field.");
  }

  if(retStatus!=0)
  {

    theForm.country.focus();
    theForm.country.select();
    return (false);
  }

	// phone Validation
  theForm.phone.value = stripSpaces(theForm.phone.value);
  retStatus = allowedCharater(theForm.phone.value, "1234567890 -",16,true);

  if(retStatus==1)
  {
    alert("Please enter Phone Number");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 16 characters in the Phone Number field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Numeric and Whitespace characters in the Phone Number field.");
  }

  if(retStatus!=0)
  {

    theForm.phone.focus();
    theForm.phone.select();
    return (false);
  }

  if(theForm.edition.value == "")
  {
    alert("Please select choice of your implementation edition.");
    theForm.edition.focus();
    return (false);
  }

  if(theForm.platform.value == "")
  {
    alert("Please select choice of your platform for your implementation.");
    theForm.platform.focus();
    return (false);
  }

  if(theForm.dbchoice.value == "")
  {
    alert("Please select choice of your Database for your implementation.");
    theForm.dbchoice.focus();
    return (false);
  }

	// License Validation
  theForm.license.value = stripSpaces(theForm.license.value);
  retStatus = allowedCharater(theForm.license.value, "1234567890",4,true);

  if(retStatus==1)
  {
    alert("Please enter Number of License Required");

  }

  if(retStatus==2)
  {
    alert("Please enter at most 4 characters in the Number of License Required field.");
  }

  if(retStatus==3)
  {

    alert("Please enter only Numeric characters in the Number of License Required field.");
  }

  if(retStatus!=0)
  {

    theForm.license.focus();
    theForm.license.select();
    return (false);
  }
  
  return (true)
}
