﻿var errorText_firstname = "Please enter your first name.";
var errorText_lastname = "Please enter your last name.";
var errorText_email = "Please enter a valid email address.";
var errorText_telno = "Please enter your telephone number.";


function ContactUs() {
    var firstnameValid = ValidateFirstName();
    var lastnameValid = ValidateLastName();
    var emailValid = ValidateEmail();
    var telnoValid = ValidateTelephone();

    if (firstnameValid == false || lastnameValid == false || emailValid == false || telnoValid == false) {
        return false;
    }

    var telinfo;
    if (document.getElementById('chk-telephone').status == true || document.getElementById('chk-telephone').checked == true) {
        telinfo = "1"; //"Yes";
    }
    else {
        telinfo = "0"; //"No";
    }

    var salesvisit;
    if (document.getElementById('chk-visit').status == true || document.getElementById('chk-visit').checked == true) {
        salesvisit = "1"; //"Yes";
    }
    else {
        salesvisit = "0"; //"No";
    }

    var sendbrochure;
    if (document.getElementById('chk-brochure').status == true || document.getElementById('chk-brochure').checked == true) {
        sendbrochure = "1"; //"Yes";
    }
    else {
        sendbrochure = "0"; //"No";
    }
    

    xmlhttpPost("/PageTemplates/FormProcessing.aspx?" +
                "FormType=ContactUs" +
                "&FirstName=" + escape(document.getElementById('firstname').value) +
                "&LastName=" + escape(document.getElementById('lastname').value) +
                "&CompanyName=" + escape(document.getElementById('company').value) +
                "&TelephoneNumber=" + escape(document.getElementById('telephone').value) +
                "&Email=" + escape(document.getElementById('email').value) +
                "&Enquiry=" + escape(document.getElementById('enquiry').value) +
                "&TelephoneCallRequested=" + escape(telinfo) +
                "&SalesVisitRequested=" + escape(salesvisit) + 
                "&SendBrochure=" + escape(sendbrochure));

    return false;
}

function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(null);
}


function updatepage(str) {
    window.location = str;
}

function ValidateFirstName() {
    if (document.getElementById('firstname').value != "") {
        document.getElementById('ErrFirstName').style.display = "none";
        document.getElementById('ErrFirstName').innerHTML = "";
        return true;
    }
    else {
        document.getElementById('ErrFirstName').style.display = "";
        document.getElementById('ErrFirstName').innerHTML = errorText_firstname;
        return false;
    }
}

function ValidateLastName() {
    if (document.getElementById('lastname').value != "") {
        document.getElementById('ErrLastName').style.display = "none";
        document.getElementById('ErrLastName').innerHTML = "";
        return true;
    }
    else {
        document.getElementById('ErrLastName').style.display = "";
        document.getElementById('ErrLastName').innerHTML = errorText_lastname;
        return false;
    }
}

function ValidateEmail() {
    var email = document.getElementById('email').value;
    if ((email.indexOf("@") > 1) && //  must contain @, and it must not be the first character
          (email.lastIndexOf(".") > email.indexOf("@")) &&  // last dot must be after the @
          (email.indexOf("@") != email.length) &&  // @ must not be the last character
          (email.indexOf("..") < 0) && // two periods in a row is not valid
          (email.indexOf(".") != email.length) &&  // . must not be the last character
          (AllValidEmailChars(email))) // all characters must be valid
    {
        document.getElementById('ErrEmail').style.display = "none";
        document.getElementById('ErrEmail').innerHTML = "";
        return true;
    }
    else {
        document.getElementById('ErrEmail').style.display = "";
        document.getElementById('ErrEmail').innerHTML = errorText_email;
        return false;
    }
}

function ValidateTelephone() {
    if (document.getElementById('telephone').value != "") {
        document.getElementById('ErrTel').style.display = "none";
        document.getElementById('ErrTel').innerHTML = "";
        return true;
    }
    else {
        document.getElementById('ErrTel').style.display = "";
        document.getElementById('ErrTel').innerHTML = errorText_telno;
        return false;
    }
}

function AllValidEmailChars(email) {
    var isValid = true;
    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
    for (var i = 0; i < email.length; i++) {
        var letter = email.charAt(i).toLowerCase();
        if (validchars.indexOf(letter) != -1) {
            continue;
        }
        else {
            isValid = false;
            break;
        }
    }
    return isValid;
}
