﻿var strChatURL = "https://chat.partners.extranet.microsoft.com/narouter/nadispatch.aspx"
var proxyUrl = "https://chat.partners.extranet.microsoft.com/narouter/enterprisequeuecheck.aspx";
var xmlhttprequest;

function checkForm() {

    if (document.getElementById('LoginName').value == "") {
        alert("Please enter your Name.");
        document.getElementById('LoginName').focus();
    }

    else if (!validateEmailID(document.getElementById('Email'))) {
        alert("Please enter a valid Email.");
        document.getElementById('Email').focus();
    }

    else {
        startChatPOST();
    }
}

function validateEmailID(objID) {
    var iPos, bValid = true;
    var email = objID.value;

    if (email.length == 0) {
        return true;
    }

    if (email.indexOf(" ") > -1 || email.indexOf(",") > -1 || email.indexOf(";") > -1) {
        return false;
    }

    iPos = email.indexOf("@")
    if (iPos < 1) return false;

    if (email.indexOf("@", iPos + 1) > -1) return false;

    if (email.indexOf(".", iPos + 1) == iPos + 1) return false;
    if ((email.charAt(0) == '.') || (email.charAt(email.length - 1) == '.')) return false;

    iPos = email.indexOf(".", iPos + 2)
    if (iPos < 1) return false;

    var pos = iPos;
    while (pos > 0 && (pos < email.length)) {
        var curpos = email.indexOf(".", pos + 1);
        if (curpos == pos + 1) {
            return false;
            break;
        }
        else pos = email.indexOf(".", pos + 1);
    }

    if ((iPos + 1) >= email.length) return false;

    return true;
}

function startChatPOST() {
    var form = document.getElementById('frm');

    window.open('', 'Chat', 'width=700,height=270,scrollbars=yes,resizable=yes,toolbar=no,menubar=no,status=no,location=no,directories=no');
    form.target = "Chat";
    form.action = strChatURL;
    form.method = "POST";
    form.submit();
}

function closeWin() {
    window.close();
}

function makeRequest() {
    xmlhttprequest = createRequestObject();
    var url = "ServerPage.aspx?proxyUrl=" + proxyUrl + "&r=" + Math.random();
    xmlhttprequest.open("GET", url, true);
    xmlhttprequest.onreadystatechange = getData;
    xmlhttprequest.send(null);
}

function createRequestObject() {
    if (window.XMLHttpRequest) {
        return xmlhttprequest = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        return xmlhttprequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function getData() {
    if ((xmlhttprequest.readyState == 4) && (xmlhttprequest.status == 200)) {
        var xmldoc = xmlhttprequest.responseXML;
        var root = xmldoc.documentElement;
        for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
            var node = root.childNodes[iNode];
            for (i = 0; i < node.childNodes.length; i++) {
                if (node.childNodes[1].firstChild.nodeValue == "VSTA.EU.NL.T4H.MAA.CSO.1FL.VI") {
                    if (node.childNodes[2].firstChild.nodeValue == "true") {
                        $("#indienen").attr("href", "javascript: startChatPOST();");
                        $("#indienen").css("visibility", "visible");
                        $("#indienenText").css("visibility", "hidden");
                        $("#indienen img").css("width", "auto");
                    }
                    else {
                        $("#indienenText").attr("innerHTML", "Wij kunnen je aanvraag momenteel niet uitvoeren. Keer terug naar de voorgaande pagina en probeer het later nog eens.");
                        $("#indienenText").css("padding-left", "10px");
                    }
                }
            }
        }
    }
}

$(document).ready(function() {
    $("#indienen").css("visibility", "hidden");
    $("#indienen img").css("width", "0");
});
