﻿var rtnCall;

function PWHide(){
    document.getElementById('divPleaseWait').style.display = 'none';
}

function ProcessData(obj, fctCall, rtnFct) {
    if (fctCall == 'RequestInfo') {
        document.getElementById('divPleaseWait').style.display = 'block';
        if (obj.CompanyName.value == "" || obj.CompanyName.value == "Company Name") {
            PWHide();
            alert("Please enter your Company Name.");
            obj.CompanyName.focus();
            return;
        }
        if (obj.ContactName.value == "" || obj.ContactName.value == "Contact Name") {
            PWHide();
            alert("Please enter your Contact Name.");
            obj.ContactName.focus();
            return;
        }
        if (obj.Title.value == "" || obj.Title.value == "Title") {
            PWHide();
            alert("Please enter your Title.");
            obj.Title.focus();
            return;
        }
        if (obj.WorkPhone.value == "" || obj.WorkPhone.value == "Contact Phone") {
            PWHide();
            alert("Please enter your Contact Phone.");
            obj.WorkPhone.focus();
            return;
        }
        if (obj.Email.value == "" || obj.Email.value == "Email") {
            PWHide();
            alert("Please enter your Email.");
            obj.Email.focus();
            return;
        }
        document.getElementById('divPleaseWait').style.display = 'block';
    } 
    else if (fctCall == 'RequestTour') {
        document.getElementById('divPleaseWait').style.display = 'block';
    }
    
    rtnCall = rtnFct;
    
    var xmlHttp;
    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;
            }
        }
    }
    var formData = serialize(obj);
    req_params = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ProcessData xmlns='http://tempuri.org/'>";
    req_params = req_params + "<dataValue>" + formData + "</dataValue>";
    req_params = req_params + "<fctCall>" + fctCall + "</fctCall>";
    req_params = req_params + "</ProcessData></soap:Body></soap:Envelope>";
    
    var url = "WebService.asmx"
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("SOAPAction", "http://tempuri.org/ProcessData");
    xmlHttp.setRequestHeader("Content-Type", "text/xml"); 

    xmlHttp.send(req_params);

    xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4) {
        if(rtnCall == 'ProcessRequestInfo')
            ProcessRequestInfo(xmlHttp.responseXML);
        }
        else if (rtnCall == 'ProcessRequestTour')
        {
            ProcessTour(xmlHttp.responseXML);
        }
    }
}

function ProcessRequestInfo(co) {
    var strToken = co.text
    document.getElementById('divRequestInfo').innerHTML = "<br><h2>Request has been successfully submitted.</h2><p>A Northwest Cosmetic Labs associate will respond with your request shortly!</p>";
    document.getElementById('divRequestBtns').style.display = 'none';
    PWHide();
}

function ProcessTour(co) {
    var strToken = co.text
    document.getElementById('divTourContent').innerHTML = "<h3>Request for a facility tour has been successfully submitted.</h3><p>A Northwest Cosmetic Labs associate will respond with your request shortly!</p>";
    PWHide();
}

function serialize(form) {
        var query = "";
        var inputs = form.getElementsByTagName("input"), x;
        for (x = 0; x < inputs.length; x++) {
            if (inputs[x].type != "submit" && inputs[x] != "button" && inputs[x].type != "file") {
                query += inputs[x].name + "=" + ((inputs[x].value != "") ? inputs[x].value : "") + "|";
                //query += "<" + inputs[x].name + ">" + ((inputs[x].value != "") ? inputs[x].value : "") + "</" + inputs[x].name + ">";
                //s += "<" + p + ">" + SOAPClientParameters._serialize(o[p]) + "</" + p + ">";
            }
        }
        var tas = form.getElementsByTagName("textarea");
        for (x = 0; x < tas.length; x++) {
            query += tas[x].name + "=" + tas[x].value + "|";
            //query += "<" + tas[x].name + ">" + tas[x].value + "</" + tas[x].name + ">";
        }
        return query.replace(/\&*$/, "");
    }

function getUrlValue(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
