﻿var newsXmlHttp;
var cfindXmlHttp;
var basketXmlHttp;
var delOptXmlHttp;
var couponXmlHttp;
var predictXmlHttp;
var xmlHttp;
var prodXmlHttp;
var menuXmlHttp;
var cableXmlHttp;
var gMenuID;

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}


function CreateXmlHttpObject() {
    var objXMLHttp = null
    if (window.XMLHttpRequest) {
        objXMLHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp
}

function hoverMenu(menuID, moveType) {

    if (moveType == 'over') {
        document.getElementById("menuItem_" + menuID).style.backgroundImage = 'url(/img/buttonRollover.jpg)';
        document.getElementById("hoverMenu_" + menuID).style.display = 'block';
    }
    else {
        document.getElementById("menuItem_" + menuID).style.backgroundImage = '';
        document.getElementById("hoverMenu_" + menuID).style.display = 'none';
    }

}

function hoverSubMenu(menuID, moveType) {

    if (document.getElementById("hoverSubMenu_" + menuID) != null) {

        if (moveType == 'over') {
            //document.getElementById("subMenuItem_" + menuID).style.backgroundImage = 'url(img/buttonRollover.jpg)';
            document.getElementById("hoverSubMenu_" + menuID).style.display = 'block';
        }
        else {
            //document.getElementById("menuItem_" + menuID).style.backgroundImage = '';
            document.getElementById("hoverSubMenu_" + menuID).style.display = 'none';
        }
    }
}

function checkEmail() {

    var email = document.getElementById("frmEmail").value;
    var name = document.getElementById("frmName").value;
    var message = document.getElementById("frmMessage").value;

    var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;

    if (name.length == 0) {
        alert('please enter your name.');
        document.getElementById("frmName").focus();
    }
    else if (!pattern.test(email)) {
        alert('please enter a valid email address.');
        document.getElementById("frmEmail").focus();
    }
    else if (message.length == 0) {
        alert('please enter a message.');
        document.getElementById("frmMessage").focus();
    }
    else {
        alert('thank you.\r\n\r\nwe will respond to ' + email + ' as soon as possible.\r\n\r\nthatcable.');
        document.frmContact.submit();
    }

}

function addBasket(productID, productDesc, productPrice, productWeight) {

    basketXmlHttp = CreateXmlHttpObject()
    if (basketXmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    basketXmlHttp.onreadystatechange = addBasket_stateChanged;
    basketXmlHttp.open("GET", "/includes/basket.asp?action=add&productID=" + productID + "&productDesc=" + productDesc + "&productPrice=" + productPrice + "&productWeight=" + productWeight, true);
    basketXmlHttp.send(null);
}

function addBasket_stateChanged() {
    if (basketXmlHttp.readyState == 4 || basketXmlHttp.readyState == "complete") {
        window.location = 'https://' + document.getElementById('siteBaseUrl').value + '/your-basket';
    }
}


function removeBasket(productID, productPrice, productWeight) {

    basketXmlHttp = CreateXmlHttpObject()
    if (basketXmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    basketXmlHttp.onreadystatechange = removeBasket_stateChanged;
    basketXmlHttp.open("GET", "/includes/basket.asp?action=remove&productID=" + productID + "&productPrice=" + productPrice + "&productWeight=" + productWeight, true);
    basketXmlHttp.send(null);
}

function removeBasket_stateChanged() {
    if (basketXmlHttp.readyState == 4 || basketXmlHttp.readyState == "complete") {
        //alert('product added to basket');
        window.location = 'your-basket';
    }
}

function getBlogger(update) {

    newsXmlHttp = CreateXmlHttpObject()
    if (newsXmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    newsXmlHttp.onreadystatechange = getBlogger_stateChanged;
    newsXmlHttp.open("GET", "/includes/ajaxresponse.asp?f=getBlogger&update=" + update, true);
    newsXmlHttp.send(null);
}

function getBlogger_stateChanged() {

    if (newsXmlHttp.readyState == 4 || newsXmlHttp.readyState == "complete") {
        document.getElementById('blogger').innerHTML = newsXmlHttp.responseText;
    }
}

function rollImage() {

    var currImage = document.getElementById("currentImage").value

    var nextImage = parseInt(currImage) + 1;

    if (nextImage > document.getElementById("imageCount").value) {
        nextImage = 1
    }

    document.getElementById("currentImage").value = nextImage;

    var imageFile = document.getElementById("homeImage" + nextImage).value;
    var imageID = document.getElementById("homeImage" + nextImage + "_id").value;
    var imageName = document.getElementById("homeImage" + nextImage + "_name").value;
    var imageURL = document.getElementById("homeImage" + nextImage + "_URL").value;

    crossfade(document.getElementById('homeImage'), '/img/products/' + imageFile, '2', '');

    // set hyperlink to product id (encoded in name)
    document.getElementById('homeImage').setAttribute('onclick', 'location.href="/product/' + imageURL + '"');
    document.getElementById('homeImage').setAttribute('title', imageName);
}

function findCables() {

    window.location = 'http://' + document.getElementById('siteBaseUrl').value + '/cable-finder/' + document.getElementById("endA").value + '/' + document.getElementById("endB").value;

}

function setCableEnd(endID, imageLink, imageName) {
    document.getElementById(endID).value = imageLink;
    if (imageLink == '') {
        imageLink = 'all-cable-ends.png';
    }
    document.getElementById(endID + "Image").src = '/img/products/' + imageLink;
    document.getElementById(endID + "Name").innerHTML = imageName;
    document.getElementById(endID + "Panel").style.display = 'none';
    document.getElementById(endID + "Image").style.display = 'inline';
    document.getElementById(endID + "Choose").style.backgroundImage = '';
}

function setCableEndInit() {
    setCableEnd('endA', document.getElementById("endAImage").src.replace('/img/products', '').replace('http://www.thatcable.local', '').replace('http://www.thatcable.com', '').replace('http://new.thatcable.com', ''), document.getElementById("endAName").innerHTML);
    getCableEnds();
}

function getCableEnds() {

    document.getElementById('cableEndBfiltered').innerHTML = '<div style="padding:20px">Loading...&nbsp; <img src="/img/ajax_loader.gif" style="width:15px; height:15px; vertical-align:text-bottom;" width="20px" height="20px" /></div>';

    var cableEndA = document.getElementById('endA').value;
    var cableEndB = document.getElementById('endB').value;

    cfindXmlHttp = CreateXmlHttpObject()
    if (cfindXmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    cfindXmlHttp.onreadystatechange = getCableEnds_stateChanged;
    cfindXmlHttp.open("GET", "/includes/ajaxresponse.asp?f=getcableends&enda=" + cableEndA + "&endb=" + cableEndB, true);
    cfindXmlHttp.send(null);
}

function getCableEnds_stateChanged() {

    if (cfindXmlHttp.readyState == 4 || cfindXmlHttp.readyState == "complete") {
        //alert(cfindXmlHttp.responseText);
        document.getElementById('cableEndBfiltered').innerHTML = cfindXmlHttp.responseText;

        if (document.getElementById('endB').value != '') {
            setCableEnd('endB', document.getElementById('endBFirstImage').value, document.getElementById('endBFirstName').value);
        }
    }
}

function searchBox(e) {

    e = e || window.event;
    var code = e.keyCode || e.which;

    // check predictive
    predictXmlHttp = CreateXmlHttpObject()
    if (predictXmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    predictXmlHttp.onreadystatechange = searchBox_stateChanged;
    predictXmlHttp.open("GET", "/includes/ajaxresponse.asp?f=getPredictive&ss=" + document.getElementById('searchString').value, true);
    predictXmlHttp.send(null);

    if (code == 13) {
        window.location = '/search/' + document.getElementById('searchString').value.trim();
    }
}

function searchBox_stateChanged() {

    if (predictXmlHttp.readyState == 4 || predictXmlHttp.readyState == "complete") {

        document.getElementById('predictiveSearch').innerHTML = predictXmlHttp.responseText;

        if (predictXmlHttp.responseText != "") {
            document.getElementById('predictiveSearch').style.visibility = 'visible';
        }
        else {
            document.getElementById('predictiveSearch').style.visibility = 'hidden';
        }
    }
}

function addDelivery() {

    if (document.getElementById("basketValue")) {

        var basketTotal = document.getElementById("basketValue").value;
        var deliveryCost = document.getElementById("postage").value;

        document.getElementById("deliveryChoice").value = deliveryCost;

        if (deliveryCost == -1) { deliveryCost = 0 };
        
        document.getElementById("basketTotal").innerHTML = (Math.round((parseFloat(basketTotal) + parseFloat(deliveryCost)) * 100) / 100).toFixed(2);
        document.getElementById("PurchaseAmount").value = parseInt((Math.round((parseFloat(basketTotal) + parseFloat(deliveryCost)) * 100) / 100) * 100);

    }
}

function getDeliveryOptions() {
    var countryCode = document.getElementById("ShippingCountry")[document.getElementById("ShippingCountry").selectedIndex].value;
    document.getElementById("deliveryChoice").value = -1

    delOptXmlHttp = CreateXmlHttpObject()
    if (delOptXmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    delOptXmlHttp.onreadystatechange = getDeliveryOptions_stateChanged;
    delOptXmlHttp.open("GET", "includes/ajaxresponse.asp?f=getdelopts&country=" + countryCode, true);
    delOptXmlHttp.send(null);
}

function getDeliveryOptions_stateChanged() {

    if (delOptXmlHttp.readyState == 4 || delOptXmlHttp.readyState == "complete") {
        document.getElementById('deliveryChoices').innerHTML = delOptXmlHttp.responseText;
        addDelivery();
    }
}


function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function showPanel(tabID) {

    document.getElementById('overviewPanel').style.display = 'none';
    document.getElementById('faqPanel').style.display = 'none';

    document.getElementById('overviewTab').style.color = 'gray';
    document.getElementById('faqTab').style.color = 'gray';

    document.getElementById('overviewTab').style.backgroundImage = 'url(/img/tabUnselected.gif)';
    document.getElementById('faqTab').style.backgroundImage = 'url(/img/tabUnselected.gif)';

    document.getElementById(tabID + 'Panel').style.display = 'inline';
    document.getElementById(tabID + 'Tab').style.backgroundImage = 'url(/img/tabSelected.gif)';
    document.getElementById(tabID + 'Tab').style.color = '#1C99CF';
}

function showAdminPanel(tabID) {

    document.getElementById('ordersPanel').style.display = 'none';
    document.getElementById('productsPanel').style.display = 'none';
    document.getElementById('customersPanel').style.display = 'none';

    document.getElementById('ordersTab').style.color = 'gray';
    document.getElementById('productsTab').style.color = 'gray';
    document.getElementById('customersTab').style.color = 'gray';

    document.getElementById('ordersTab').style.backgroundImage = 'url(/img/tabUnselected.gif)';
    document.getElementById('productsTab').style.backgroundImage = 'url(/img/tabUnselected.gif)';
    document.getElementById('customersTab').style.backgroundImage = 'url(/img/tabUnselected.gif)';

    document.getElementById(tabID + 'Panel').style.display = 'inline';
    document.getElementById(tabID + 'Tab').style.backgroundImage = 'url(/img/tabSelected.gif)';
    document.getElementById(tabID + 'Tab').style.color = '#1C99CF';
}

function applyCoupon() {

    var basketValue = document.getElementById('basketValue').value;
    var couponCode = document.getElementById('couponCode').value;

    couponXmlHttp = CreateXmlHttpObject()
    if (couponXmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    couponXmlHttp.onreadystatechange = applyCoupon_stateChanged;
    couponXmlHttp.open("GET", "/includes/ajaxresponse.asp?f=coupon&total=" + basketValue + "&couponCode=" + couponCode, true);
    couponXmlHttp.send(null);
}

function applyCoupon_stateChanged() {
    if (couponXmlHttp.readyState == 4 || couponXmlHttp.readyState == "complete") {
        if (couponXmlHttp.responseText != '0') {

            var couponDesc = couponXmlHttp.responseText.split("|")[0];
            var couponAmt = parseFloat(couponXmlHttp.responseText.split("|")[1]);

            document.getElementById("couponAmount").value = parseFloat(couponAmt)

            document.getElementById('couponEntry').innerHTML = couponDesc;

            var purAmt = parseFloat(document.getElementById("PurchaseAmount").value)

            document.getElementById("basketValue").value = (Math.round((parseFloat(purAmt) / 100 - parseFloat(couponAmt)) * 100) / 100).toFixed(2);
            document.getElementById("basketTotal").innerHTML = (Math.round((parseFloat(purAmt)/100 - parseFloat(couponAmt)) * 100) / 100).toFixed(2);
            document.getElementById("PurchaseAmount").value = parseInt((Math.round((parseFloat(purAmt)/100 - parseFloat(couponAmt)) * 100) / 100) * 100);

            document.getElementById('couponDesc').innerHTML = '- &pound;' + (Math.round(parseFloat(couponAmt) * 100) / 100).toFixed(2);
        }
        
    }
}


function showCoupon() {
    var couponCode = document.getElementById("couponSelect")[document.getElementById("couponSelect").selectedIndex].value;

    couponXmlHttp = CreateXmlHttpObject()
    if (couponXmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    couponXmlHttp.onreadystatechange = showCoupon_stateChanged;
    couponXmlHttp.open("GET", "includes/ajaxresponse.asp?f=showCoupon&code=" + couponCode, true);
    couponXmlHttp.send(null);
}

function showCoupon_stateChanged() {

    if (couponXmlHttp.readyState == 4 || couponXmlHttp.readyState == "complete") {
        document.getElementById('couponDisplay').innerHTML = couponXmlHttp.responseText;
    }
}

function showPanelElement(panelID) {

    if (document.getElementById(panelID).style.display == 'inline') {
        document.getElementById(panelID).style.display = 'none';
    }
    else {
        document.getElementById(panelID).style.display = 'inline';
    }

}

function checkCodeExists(e) {

    e = e || window.event;
    var code = e.keyCode || e.which;

    // check predictive
    xmlHttp = CreateXmlHttpObject()
    if (xmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    xmlHttp.onreadystatechange = checkCodeExists_stateChanged;
    xmlHttp.open("GET", "/includes/ajaxresponse.asp?f=checkCodeExists&prid=" + document.getElementById('prodCode').value, true);
    xmlHttp.send(null);

}

function checkCodeExists_stateChanged() {

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {

        document.getElementById('codeExistsAlert').innerHTML = xmlHttp.responseText;

        if (document.getElementById('prodCode').value == "") {
            document.getElementById('codeExistsAlert').innerHTML = "";
            document.getElementById('btnAddProd').disabled = "disabled";
        }
        else if (xmlHttp.responseText.indexOf("OK") != -1) {
            document.getElementById('btnAddProd').disabled = "";
        }
        else {
            document.getElementById('btnAddProd').disabled = "disabled";
        }
    }
}


function checkURLExists(e) {

    e = e || window.event;
    var code = e.keyCode || e.which;

    // check predictive
    xmlHttp = CreateXmlHttpObject()
    if (xmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    xmlHttp.onreadystatechange = checkURLExists_stateChanged;
    xmlHttp.open("GET", "/includes/ajaxresponse.asp?f=checkURLExists&prid=" + document.getElementById('prodURL').value, true);
    xmlHttp.send(null);

}

function checkURLExists_stateChanged() {

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {

        document.getElementById('urlExistsAlert').innerHTML = xmlHttp.responseText;

        if (document.getElementById('prodURL').value == "") {
            document.getElementById('urlExistsAlert').innerHTML = "";
            document.getElementById('btnAddProd').disabled = "disabled";
        }
        else if (xmlHttp.responseText.indexOf("OK") != -1) {
            document.getElementById('btnAddProd').disabled = "";
        }
        else {
            document.getElementById('btnAddProd').disabled = "disabled";
        }
    }
}


function getSubCatSelect() {

    xmlHttp = CreateXmlHttpObject()
    if (xmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    xmlHttp.onreadystatechange = getSubCatSelect_stateChanged;
    xmlHttp.open("GET", "/includes/ajaxresponse.asp?f=getSubCatSelect&catid=" + document.getElementById('catSelect').value, true);
    xmlHttp.send(null);

}

function getSubCatSelect_stateChanged() {

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
      
        document.getElementById('subCatSelector').innerHTML = xmlHttp.responseText;
        getProdCatSelect();
    }
}


function getProdCatSelect() {

    prodXmlHttp = CreateXmlHttpObject()
    if (prodXmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }
    prodXmlHttp.onreadystatechange = getSubProdSelect_stateChanged;
    prodXmlHttp.open("GET", "/includes/ajaxresponse.asp?f=getProdCatSelect&subcatid=" + document.getElementById('subCatSelect').value, true);
    prodXmlHttp.send(null);

}

function getSubProdSelect_stateChanged() {

    if (prodXmlHttp.readyState == 4 || prodXmlHttp.readyState == "complete") {

        document.getElementById('prodCatSelector').innerHTML = prodXmlHttp.responseText;

    }
}


function changeLengths(direction) {

    if (direction == "rem") {
        document.getElementById('nextLength' + (document.getElementById('nextLength').value - 1)).innerHTML = '<div id=""nextLength' + (document.getElementById('nextLength').value - 1) + '""><input id=""nextLength"" type=""hidden"" value=""' + (document.getElementById('nextLength').value - 1) + '"" /></div>';
        //document.getElementById('nextLength').value = (document.getElementById('nextLength').value - 1)
    }

    else {

        prodXmlHttp = CreateXmlHttpObject()
        if (prodXmlHttp == null) {
            alert("Browser does not support AJAX");
            return;
        }
        prodXmlHttp.onreadystatechange = changeLengths_stateChanged;
        prodXmlHttp.open("GET", "/includes/ajaxresponse.asp?f=changeLengths&next=" + document.getElementById('nextLength').value, true);
        prodXmlHttp.send(null);
    }
}

function changeLengths_stateChanged() {

    if (prodXmlHttp.readyState == 4 || prodXmlHttp.readyState == "complete") {

        document.getElementById('nextLength' + document.getElementById('nextLength').value).innerHTML = prodXmlHttp.responseText;
        document.getElementById('nextLength').value = document.getElementById('nextLength').value + 1
    }
}


function getMenuItems(menuID) {

    if (document.getElementById('menuFetched_' + menuID).value == 'false') {

        gMenuID = menuID;

        menuXmlHttp = CreateXmlHttpObject()
        if (menuXmlHttp == null) {
            alert("Browser does not support AJAX");
            return;
        }
        menuXmlHttp.onreadystatechange = getMenuItems_stateChanged;
        menuXmlHttp.open("GET", "/includes/ajaxresponse.asp?f=getMenuItems&menuID=" + menuID, true);
        menuXmlHttp.send(null);
    }
}

function getMenuItems_stateChanged() {

    if (menuXmlHttp.readyState == 4 || menuXmlHttp.readyState == "complete") {
        //alert(menuXmlHttp.responseText);
        document.getElementById('hoverMenu_' + gMenuID).innerHTML = menuXmlHttp.responseText;
    }
}

function getCableItems() {

    if (document.getElementById('cableItemsFetched').value == 'false') {

        cableXmlHttp = CreateXmlHttpObject()
        if (cableXmlHttp == null) {
            alert("Browser does not support AJAX");
            return;
        }
        cableXmlHttp.onreadystatechange = getCableItems_stateChanged;
        cableXmlHttp.open("GET", "/includes/ajaxresponse.asp?f=getCableItems", true);
        cableXmlHttp.send(null);
    }
}

function getCableItems_stateChanged() {

    if (cableXmlHttp.readyState == 4 || cableXmlHttp.readyState == "complete") {
        //alert(cableXmlHttp.responseText);
        document.getElementById('endAPanel').innerHTML = cableXmlHttp.responseText;
    }
}
