
function Validate_Index_Form(theForm)
{
	
   if (theForm.First_Name.value == "")
  {
    alert("Please enter a value for the \"First Name\" field.");
    theForm.First_Name.focus();
    return (false);
  }
 
  
  if (theForm.Last_Name.value == "")
  {
    alert("Please enter a value for the \"Last Name\" field.");
    theForm.Last_Name.focus();
    return (false);
  }  
  
/*    if (theForm.Middle_Name.value == "")
  {
    alert("Please enter a value for the \"Middle Initial\" field.");
    theForm.Middle_Name.focus();
    return (false);
  }  */  

  if (theForm.Primary_Email_Address.value == "")
  {
    alert("Please enter a value for the \"Email\" field.");
    theForm.Primary_Email_Address.focus();
    return (false);
  }
     
 if (echeck(theForm.Primary_Email_Address.value) == false) {
	alert("Please enter a valid Email id");
    theForm.Primary_Email_Address.focus();
    return (false);
  }
  
  
  if (theForm.Primary_Phone.value == "")
  {
    alert("Please enter a value for the \"Phone\" field.");
    theForm.Primary_Phone.focus();
    return (false);
  }

  if (validate_phonenum(theForm) == false) 
  {
	alert("Please enter a valid phone number");
        theForm.Primary_Phone.focus();
        return (false);
  }
  
    if (theForm.Ship_Address1.value == "")
  {
    alert("Please enter a value for the \"Address\" field.");
    theForm. Ship_Address1.focus();
    return (false);
  }


/*    if (theForm.Ship_Address2.value == "")
  {
    alert("Please enter a value for the \"Address2\" field.");
    theForm. Ship_Address2.focus();
    return (false);
  }
*/
  
    if (theForm.Ship_State.value == 0)
  {
    alert("Please select the \"State\" field.");
    theForm.Ship_State.focus();
    return (false);
  }



  if (theForm.Ship_City.value == "")
  {
    alert("Please enter a value for the \"City\" field.");
    theForm.Ship_City.focus();
    return (false);
  } 
  
  if (theForm.Ship_Postal_Code.value =="")
  {
    alert("Please enter the \"Ship_Postal_Code\" code.");
    theForm.Ship_Postal_Code.focus();
    return (false);
  }
  
  if(theForm.email_check.value==0)
  {
	  theForm.Primary_Email_Address.value=="";
	  alert("Please enter your emailid");
	  theForm.Primary_Email_Address.focus();
	  return false;
  }    

return true;

}
  
  
  var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function formatPhone(num)
{ 
  var _return=false;
  /*
   * 7181238748 to 1(718)123-8748
   */ 

  if(num.length != 10)
  { 
    /* 
     * if user did not enter 10 digit phone number then simply print whatever user entered 
     */ 
	_return=_OUTPUT?num:false;
  } 
  else
  { 
    /* formating phone number here */ 
	_return="(";
	var ini = num.substring(0,3);
	_return+=ini+")";
	var st = num.substring(3,6);
	_return+=st+"-";
	var end = num.substring(6,10);
	_return+=end;
 }
  return _return; 
} 


function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function validate_phonenum(theForm){

    var s;

	s=stripCharsInBag(theForm.Primary_Phone.value,validWorldPhoneChars);

	ret = isInteger(s) && (s.length > 4 && s.length < 16);

	if (ret == false) {
		return false;
	}

	if (theForm.Primary_Phone.value.charAt(0) != "+") {
	    if (s.length == 10) {
	        theForm.Primary_Phone.value = formatPhone(s); 
        }
    }

	return true;
}

/* Validate Email address */

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
