/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
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 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 trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
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 checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

// JavaScript Document

function field_validation(){
	var firstname=document.frm_buy.firstname.value;
 if(isWhitespace(firstname))
			 {
			 alert("First name is empty");
			 document.frm_buy.firstname.focus();
			 return false;
			 }
 if(!isString(firstname))
			 {
			 alert("First name must be String");
			 document.frm_buy.firstname.focus();
			 return false;
			 }
 var secondname=document.frm_buy.lastname.value;
 if(isWhitespace(secondname))
			 {
			 alert("Last name is empty");
			 document.frm_buy.lastname.focus();
			 return false;
			 }
 if(!isString(secondname))
			 {
			 alert("Last name must be String");
			 document.frm_buy.lastname.focus();
			 return false;
			 }
 var status=document.frm_buy.MaritalStatus.value;
 if(document.frm_buy.MaritalStatus.value==""){
	 		alert("Please select Marital Status");
			 document.frm_buy.MaritalStatus.focus();
			 return false;
 }
 if(document.frm_buy.MaritalStatus.value=="Married"){
 var spousename=document.frm_buy.spousename.value;
 if(isWhitespace(spousename))
			 {
			 alert("Spouse's name is empty");
			 document.frm_buy.spousename.focus();
			 return false;
			 }
 if(!isString(spousename))
			 {
			 alert("Spouse's name must be String");
			 document.frm_buy.spousename.focus();
			 return false;
			 }
 }
 var title=document.frm_buy.title.value;
 if(document.frm_buy.title.value==""){
	 		alert("Please select Title");
			 document.frm_buy.title.focus();
			 return false;
 }
 var areacode = document.frm_buy.AreaCode.value;
 if (isNaN(areacode))
	{
	alert("Please enter your area code as a number");
	document.frm_buy.AreaCode.focus();
	return false;
	} 
  var email = document.frm_buy.ContactEmail.value.toLowerCase();
;
  if (!validateEmail(email ,1,1)) 
		 {
			 document.frm_buy.ContactEmail.focus();
			 return false;
		 }
var ContactPhone = document.frm_buy.ContactPhone.value;
if (checkInternationalPhone(ContactPhone)==false){
		alert("Please Enter a Valid Phone Number")
		document.frm_buy.ContactPhone.focus()
		return false
	}

if(document.frm_buy.Cardstreet.value=="")
			 {
			 alert("Street Address is empty");
			 document.frm_buy.Cardstreet.focus();
			 return false;
			 }
 var Cardcity=document.frm_buy.Cardcity.value;
 if(isWhitespace(Cardcity))
			 {
			 alert("City Name is empty");
			 document.frm_buy.Cardcity.focus();
			 return false;
			 }
 if(!isString(Cardcity))
			 {
			 alert("City name must be String");
			 document.frm_buy.Cardcity.focus();
			 return false;
			 }
 var Cardstate=document.frm_buy.Cardstate.value;
 if(document.frm_buy.Cardstate.value==""){
	 		alert("Please select State");
			 document.frm_buy.Cardstate.focus();
			 return false;
 }
 var Cardzip=document.frm_buy.Cardzip.value;
 if(isWhitespace(Cardzip)){
	 		alert("Please enter Zipcode");
			 document.frm_buy.Cardzip.focus();
			 return false;
 }
 var agree=document.frm_buy.agree;
 if(document.frm_buy.agree.checked==false){
	 alert("Before Submit You must agree the terms");
	 document.frm_buy.agree.focus();
	  return false;
 }
 
  var referral=document.frm_buy.referral.value;
 if(document.frm_buy.referral.value==""){
	 		alert("Please let us know how you found us!");
			 document.frm_buy.referral.focus();
			 return false;
 }

}

var whitespace = " \t\n\r";


function isWhitespace (s) //Checks for White spaces in the string 
	{  
		var i;
	 	if (isEmpty(s)) return true;
	 	   for (i = 0; i < s.length; i++)
	 	   {   
	 	       var c = s.charAt(i);
	 	       if (whitespace.indexOf(c) == -1) return false;
	 	   }
		   return true;
	}
	
function isEmpty(s)  //Checks whether string is Empty
	{  
		return ((s == null) || (s.length == 0))
	}

function isString (s) //Checks whether the string contains only alphabets, . and _
	{  
	    var i;
		var bag="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz _.\/',:;"
	    for (i = 0; i < s.length; i++)
	    {   
	        var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) return false;
	    }
	    return true;
	}
function isNumber (s) //Checks whether the number is numeric
	{  
	    var i;
		var bag="1234567890"
	    for (i = 0; i < s.length; i++)
	    {   
	        var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) return false;
	    }
	    return true;
	}
	
	function validateEmail(addr,man,db) {
	if (addr == '' && man) {
	   if (db) alert('Email address is mandatory');
	   return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) alert('Email address contains invalid characters');
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) alert("Email address contains non ascii characters.");
		  return false;
	   }
	}
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) alert('Email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	   if (db) alert('Email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) alert('Email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) alert('Email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) alert('period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) alert('period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) alert('two periods must not be adjacent in email address');
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   if (db) alert('invalid primary domain in email address');
	   return false;
	}
return true;
}

function showgiftdetail(){
		if (document.frm_buy.MartialStatus[1].selected){
		ap1=document.getElementById("showattrib");
		    if(ap1){
			ap1.style.display="block";
			ap1.style.visibility="visible";
				}				
		}
		else
		{
			ap1=document.getElementById("showattrib");
			if(ap1){
			ap1.style.display="none";
			ap1.style.visibility="hidden";
				}
		}
	}


