// JavaScript Document
function emailvalidation(entered, alertbox)
{
// E-mail Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
with (entered)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
lastpos=value.length-1;
if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
{if (alertbox) {alert(alertbox);}  return false; }
else {return true;}
}
} 

// 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 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){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function emptyvalidation(entered, alertbox)
{
// Emptyfield Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
with (entered)
{
if (value==null || value=="")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
} 

function formvalidation(thisform)
{
with (thisform)
{
//if (emptyvalidation(title,"The title cannot be empty")==false) {title.focus(); return false;};
if (emptyvalidation(firstName,"First name cannot be empty")==false) {firstName.focus(); return false;};
if (emptyvalidation(lastName,"Last name cannot be empty!!")==false) {lastName.focus(); return false;};
if ((phoneNumber.value==null)||(phoneNumber.value=="")){ alert("Please Enter your Phone Number"); phoneNumber.focus(); return false};
if (checkInternationalPhone(phoneNumber.value)==false){ alert("Please Enter a Valid Phone Number"); phoneNumber.value=""; phoneNumber.focus(); return false};
if (emptyvalidation(Email,"Please provide an email address!!")==false) {Email.focus(); return false;};
if (emailvalidation(Email,"Email address is not valid!!")==false) {Email.value=""; Email.focus(); return false;};
if (emptyvalidation(companyName,"The company name cannot be empty")==false) {companyName.focus(); return false;};
if (emptyvalidation(companyAddress,"The Company Address cannot be empty")==false) {companyAddress.focus(); return false;};
if (emptyvalidation(zipCode,"The Postal/Zip code cannot be empty")==false) {zipCode.focus(); return false;};
if (emptyvalidation(country,"The Country cannot be empty")==false) {country.focus(); return false;};
}
}
