//Function to validate form

var ValidDigits = "0123456789";
// non-digit characters which are allowed in phone numbers
var PhoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
var ValidWorldPhoneChars = PhoneNumberDelimiters + "+";
// Minimum no of ValidDigits in an international phone no.
var MinDigitsInIPhoneNumber = 10;

function validate_form(form) {
    if (validate_required(form.firstname, "First Name must be filled out!") == false)
    { form.firstname.focus(); return false; }

    if (validate_required(form.lastname, "Last Name must be filled out!") == false)
    { form.lastname.focus(); return false; }

    if (validate_required(form.company, "Company must be filled out!") == false)
    { form.company.focus(); return false; }

    if (validate_required(form.title, "Title must be filled out!") == false)
    { form.title.focus(); return false; }

    if (validate_required(form.email, "Email must be filled out!") == false)
    { form.email.focus(); return false; }

    if (validate_email(form.email, "Not a valid e-mail address!") == false)
    { form.email.focus(); return false; }

    if (validate_required(form.phone, "Phone must be filled out!") == false)
    { form.phone.focus(); return false; }

    if (checkInternationalPhone(form.phone.value) == false) {
        alert("Please Enter a Valid Phone Number")
        form.phone.value = ""
        form.phone.focus();
        return false;
    }

    if (validate_required(form.state, "State must be filled out!") == false)
    { form.state.focus(); return false; }

    return true;
}

function validate_required(field, alerttxt) {
    with (field) {
        if (value == null || value == "") {
            alert(alerttxt); return false;
        }
        else {
            return true;
        }
    }
}
function validate_email(field, alerttxt) {
    with (field) {
        apos = value.indexOf("@");
        dotpos = value.lastIndexOf(".");
        if (apos < 1 || dotpos - apos < 2)
        { alert(alerttxt); return false; }
        else { return true; }
    }
}
//To Check Integer value
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;
}
//trim string
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);
}


function SendMail() {
    //Create the MIME headers for the PHP "mail()" function
    //Pass to the PHP page all the data necessary to send the email

    var mailaction = "sendemail";
    mailaction += ".asp";
    mailaction += "?address=" + escape(this.To) + "&subject=" + escape(this.Subject) + "&message=" + escape(this.Message) + "&cc=" + escape(this.CC) + "&bcc=" + escape(this.Bcc) + "&replyto=" + escape(this.ReplyTo);
    //Check if browser supports script calling, if so then call the server side page as a JavaScript file
    var emailwin = window.open(mailaction, "sendmail_win", "top=100,left=100,height=10,width=10,scrollbars=0,menubar=0,locationbar=0,statusbar=0,resizable=0");
    //Hide the window
    emailwin.blur();
    //Set focus to the web page
    window.focus();
}

function CheckEmail() {
    //Show the message in the status bar
    // window.status="Sending email...";
    //Check if the ASP/PHP email page was al // ready loaded
    if (EmailSenderAccessed) {
        //Now check if the email was sended or n // ot and show the message
        if (EmailSended) window.alert("The email was sended succesully!");
        else window.alert("The email could not be sended");
    }
    //If ASP/PHP email page is not loaded ye // t, then check it again in 0.5 seconds (500 milliseconds)
    else {
        setTimeout("CheckEmail();", 500);
    }

}

function SendEmail(form) {

    var To = "";
    var Cc = "";
    var Bcc = "";
    var ServerType = "PHP_SERVER";
    var ReplyTo = "";
    var Subject = "";
    var Message = "";
    var Send = "SendMail";
    var strbody = "";
    EmailSenderAccessed = false;
    EmailSended = false;

    ServerType = "ASP_SERVER";
    //Now set the address to which the email will be sended
    //easymail.To="bhupendra.mahalwal@cipherdyn.com";


    To = "bforrest@akaes.com";

    //Now set the address of the one that sends the email

    ReplyTo = "bforrest@akaes@interdyn.com";
    //Now set the subject of the email
    Subject = window.location.search.substring(1);
    var MEETING_Response;
    if (form.radio.checked)
        MEETING_Response = 'Yes';
    else
        MEETING_Response = 'No';

    strbody += "Name:					" + form.firstname.value + ",  ";
    strbody += "LastName:               " + form.lastname.value + ",  ";
    strbody += "Company:				" + form.company.value + ", ";
    strbody += "State:				    " + form.state.value + ", ";
    strbody += "Title:					" + form.title.value + ", ";
    strbody += "Email:					" + form.email.value + ", ";
    strbody += "Phone:					" + form.phone.value + ", ";
    strbody += "SCHEDULE A MEETING?:	" + MEETING_Response + ", ";
    strbody += "Comments:               " + form.textarea.value + ",";

    //Set the message of the email
    Message = strbody;

    var mailaction;
    mailaction = "sendemail";
    mailaction += ".asp";
    mailaction += "?address=bforrest@akaes.com&subject=" + Subject + "&message=" + Message + "&cc=&bcc=&replyto=bforrest@akaes.com";
    //Open the popup window
    var emailwin = window.open(mailaction, "sendmail_win", "top=100,left=100,height=600,width=400,scrollbars=0,menubar=0,locationbar=0,statusbar=0,resizable=1");
    //Hide the window
    emailwin.blur();
    //Set focus to the web page
    window.focus();

}




