﻿//------------------------------------------------------------------------------------
//Validation for mandatory fields
//------------------------------------------------------------------------------------

function CheckMandatory(oForm, MandatoryString) {
    var Completed = true;

    var temp = oForm.elements.length;

    //MandatoryString="Salutation~FirstName~LastName~Email~Company~Function~Username~Password"
    //alert(MandatoryString)
    MandatoryArray = MandatoryString.split("~");

    for (i = 0; i < temp; i++) {
        for (j = 0; j < MandatoryArray.length; j++) {
            //alert("Mandatory For loop " + oForm.elements[j].name)
            if (oForm.elements[i].name == MandatoryArray[j]) {
                //alert("If Stmt " + oForm.elements[i].name)
                if (oForm.elements[i].value == "") {
                    //alert("Null Value=" + oForm.elements[i].name)
                    Completed = false;
                }
            }
        }
    }
    if (Completed == false) {
        alert("Please give information marked as mandatory (*) !");
        return false;
    }
    else
        return true;
}

//------------------------------------------------------------------------------------
//Validation for email addresses
//------------------------------------------------------------------------------------


function email_check(str) {
    var at = "@";
    var dot = ".";
    var lat = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);
    if (str.indexOf(at) == -1) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.indexOf(" ") != -1) {
        alert("Invalid E-mail Address");
        return false;
    }

    return true;
}


function emailcheck(str) {
    var at = "@";
    var dot = ".";
    var lat = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);
    if (str.indexOf(at) == -1) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        alert("Invalid E-mail Address");
        return false;
    }

    if (str.indexOf(" ") != -1) {
        alert("Invalid E-mail Address");
        return false;
    }

    return true;
}

//------------------------------------------------------------------------------------
//validation for leading/endiing spaces
//------------------------------------------------------------------------------------
function trim(inputString) {
    // http://www.breakingpar.com/bkp/home.nsf/0/87256B14007C5C6A87256AFB0013C722
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof inputString != "string") { return inputString; }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length - 1, retValue.length);
    while (ch == " ") { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length - 1);
        ch = retValue.substring(retValue.length - 1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
        retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length); // Again, there are two spaces in each of the strings
    }
    return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function setValue(eID) {
    var eVal = document.getElementById(eID).value;
    if (trim(eVal) == 'Name *') {
        document.getElementById(eID).value = "";
    }
    if (trim(eVal) == 'E-mail *') {
        document.getElementById(eID).value = "";
    }
    if (trim(eVal) == 'Telephone *') {
        document.getElementById(eID).value = "";
    }
    if (trim(eVal) == 'Enquiry *') {
        document.getElementById(eID).value = "";
    }
    if (trim(eVal) == 'Company') {
        document.getElementById(eID).value = "";
    }
    if (trim(eVal) == 'Position') {
        document.getElementById(eID).value = "";
    }
    if (trim(eVal) == 'Address') {
        document.getElementById(eID).value = "";
    }
    if (trim(eVal) == 'Postcode') {
        document.getElementById(eID).value = "";
    }
    if (trim(eVal) == 'Fax') {
        document.getElementById(eID).value = "";
    }
    if (trim(eVal) == 'Website address') {
        document.getElementById(eID).value = "";
    }
}


function checkValue(eID, strVal) {
    var eVal = document.getElementById(eID).value;
    if (trim(eVal) == '') {
        document.getElementById(eID).value = strVal;
    }
}


// -----------
function ValidateCallBackRequestSubscription() {
    var oForm = document.getElementById("CallBackRequest");
    var fEmail = document.getElementById("fEmail");
    var fName = document.getElementById("fName");
    var fTelephone = document.getElementById("fTelephone");
    var fEnquiry = document.getElementById("fEnquiry");
    //var fTitle        = document.getElementById("hFrmTitle");
    var MandatoryString, bValidEmail;
    var url, htmlMsgBoardID;

    bValidEmail = false;
    MandatoryString = "fName~fEmail~fTelephone~fEnquiry";

    if ((trim(fName.value) != 'Name *') && (trim(fEmail.value) != 'E-mail *') && (trim(fName.value) != 'Telephone *') && (trim(fName.value) != 'Enquiry *')) {
        if (CheckMandatory(oForm, MandatoryString) == true) {
            if (emailcheck(trim(fEmail.value)) == true) {
                oForm.hFrmAction.value = "Subscribe";
                bValidEmail = true;
                // alert(oForm.hFrmAction.value);
            }
        }
    }
    else {
        alert('Please enter valid required details.');
    }

    if (bValidEmail) {
        url = "CallBackRequestProcess.asp?fName=" + fName.value + "&fEmail=" + fEmail.value + "&fTelephone=" + fTelephone.value + "&fEnquiry=" + fEnquiry.value + "&hFrmAction=" + oForm.hFrmAction.value;
        htmlMsgBoardID = "msgBoard";
        // alert(url);
        ajaxFunction(url, htmlMsgBoardID);
    }

    return bValidEmail;
}

function ValidateGetInTouchSubscription() {
    var oForm = document.getElementById("GetInTouch");
    var fEmail = document.getElementById("fEmail");
    var fName = document.getElementById("fName");
    var fTelephone = document.getElementById("fTelephone");
    var fEnquiry = document.getElementById("fEnquiry");
    //var fTitle        = document.getElementById("hFrmTitle");
    var MandatoryString, bValidEmail;
    var url, htmlMsgBoardID;

    bValidEmail = false;
    MandatoryString = "fName~fEmail~fTelephone~fEnquiry";

    if ((trim(fName.value) != 'Name *') && (trim(fEmail.value) != 'E-mail *') && (trim(fName.value) != 'Telephone *') && (trim(fName.value) != 'Enquiry *')) {
        if (CheckMandatory(oForm, MandatoryString) == true) {
            if (emailcheck(fEmail.value) == true) {
                oForm.hFrmAction.value = "Subscribe";
                bValidEmail = true;
                // alert(oForm.hFrmAction.value);
            }
        }
    }
    else {
        alert('Please enter valid required details.');
    }

    if (bValidEmail) {
        url = "GetInTouchProcess.asp?fName=" + fName.value + "&fEmail=" + fEmail.value + "&fTelephone=" + fTelephone.value + "&fEnquiry=" + fEnquiry.value + "&hFrmAction=" + oForm.hFrmAction.value;
        htmlMsgBoardID = "msgBoardGetInTouch";
        // alert(url);
        ajaxFunction(url, htmlMsgBoardID);
    }

    return bValidEmail;
}


function ValidateRequestAQuoteSubscription() {
    var oForm = document.getElementById("RequestAQuote");
    var fEmail = document.getElementById("f2Email");
    var fName = document.getElementById("f2Name");
    var fTelephone = document.getElementById("f2Telephone");
    var fEnquiry = document.getElementById("f2Enquiry");
    var fCompany = document.getElementById("f2Company");
    var fPosition = document.getElementById("f2Position");
    var fAddress = document.getElementById("f2Address");
    var fPostcode = document.getElementById("f2Postcode");
    var fFax = document.getElementById("f2Fax");
    var fWebsiteAddress = document.getElementById("f2WebsiteAddress");
    var fHearUs = document.getElementById("f2HearUs");
    var fChkBox = '';

    var MandatoryString, bValidEmail;
    var url, htmlMsgBoardID;

    bValidEmail = false;
    MandatoryString = "f2Name~f2Email~f2Telephone~f2Enquiry";

    if ((trim(fName.value) != 'Name *') && (trim(fEmail.value) != 'E-mail *') && (trim(fName.value) != 'Telephone *') && (trim(fName.value) != 'Enquiry *')) {
        if (CheckMandatory(oForm, MandatoryString) == true) {
            if (emailcheck(fEmail.value) == true) {
                oForm.h2FrmAction.value = "Subscribe";
                bValidEmail = true;
                // alert(oForm.hFrmAction.value);
            }
        }
    }
    else {
        alert('Please enter valid required details.');
    }

    if (bValidEmail) {
        // prepare selected requiremnts as a string
        if (oForm.f2ChkBox.length > 1) {
            for (var i = 0; i < oForm.f2ChkBox.length; i++) {
                if (oForm.f2ChkBox[i].checked) {
                    fChkBox += oForm.f2ChkBox[i].value + "/";
                }
            }
        } else {
            fChkBox = oForm.f2ChkBox.value;
        }
        // document.getElementById("f2ChkBox");

        url = "RequestAQuoteProcess.asp?fName=" + fName.value + "&fEmail=" + fEmail.value + "&fTelephone=" + fTelephone.value + "&fEnquiry=" + fEnquiry.value + "&hFrmAction=" + oForm.h2FrmAction.value +
		        "&fCompany=" + fCompany.value + "&fPosition=" + fPosition.value + "&fAddress=" + fAddress.value + "&fPostcode=" + fPostcode.value +
		        "&fFax=" + fFax.value + "&fWebsiteAddress=" + fWebsiteAddress.value + "&fHearUs=" + fHearUs.value + "&fChkBox=" + fChkBox;

        htmlMsgBoardID = "msgBoardRequestAQuote";
        //alert(url);
        ajaxFunction(url, htmlMsgBoardID);
    }

    return bValidEmail;
}

//-----------
function ajaxFunction(url, htmlMsgBoardID) {
    var xmlHttp = null;
    //var eVal = document.getElementById(htmlMsgBoardID);
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
            document.getElementById(htmlMsgBoardID).innerHTML = xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}
