﻿function StartNewsTicker(i) {
    pos = 0;
    maxlength = message[i].length + 1;
    writemsg(i);

    if (i < message.length - 1) {
        j = eval(i) + 1;
        timer = setTimeout("StartNewsTicker(" + j + ")", (message[i].length * 50) + 30 + 3000);
    } else {
        timer = setTimeout("StartNewsTicker(" + 0 + ")", (message[i].length * 50) + 30 + 3000);
    }
}


function LimitLength(control) {
    if (control.value.length > 900)
        return false;
    else return true;
}

function writemsg(i) {
    if (pos < maxlength) {
        txt = message[i].substring(pos, 0);
        document.getElementById('ticker').href = tickerlinks[i];
        document.getElementById('ticker').innerHTML = txt;
        pos++;
        var str = "writemsg(" + i + ")";
        timer = setTimeout(str, 50);
    }
}

function stoptimer() {
    clearTimeout(timer);
}

//client side Credit card validation
function HideSearchText(searchtextBox) {
    var txtSearch = document.getElementById(searchtextBox);
    var searchString = txtSearch.value;
    if (searchString == "Quick Search")
        txtSearch.value = "";
}
function ValidateCreditCard(paymentType, creditcardtype, cardnumber, month, year) {
    try {
        var PTypeDrop = document.getElementById(paymentType);
        var PType = PTypeDrop.options[PTypeDrop.selectedIndex].text.toLowerCase();
        if (PType == 'creditcard') {

            var cardTypeDrop = document.getElementById(creditcardtype);
            var cardType = cardTypeDrop.options[cardTypeDrop.selectedIndex].text;

            var Number = document.getElementById(cardnumber).value;
            var expMonthDrop = document.getElementById(month);
            var expMonth = expMonthDrop.options[expMonthDrop.selectedIndex].text;

            var expYearDrop = document.getElementById(year);
            var expYear = expYearDrop.options[expYearDrop.selectedIndex].text;

            if (!IsExpiredCard(expYear, expMonth)) {
                alert("Expired Credit Card!");
                return false;
            }
            if (IsValidCreditCard(cardType, Number)) {
                return true;
            }
            else {
                alert("Invalid Credit Card Number");
                return false;
            }
        }
        else {
            return true;
        }
        return false;
    }
    catch (err) {
        alert("Payment Information - Missing Mandatory Fields!");
        return false;
    }
}

function IsValidCreditCard(type, ccnum) 
{
    if (type == "Visa") {
        // Visa: length 16, prefix 4, dashes optional.
        var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
    } else if (type == "MasterCard") {
        // Mastercard: length 16, prefix 51-55, dashes optional.
        var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
    } else if (type == "Discover") {
        // Discover: length 16, prefix 6011, dashes optional.
        var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
    } else if (type == "American Express") {
        // American Express: length 15, prefix 34 or 37.
        var re = /^3[4,7]\d{13}$/;
    } else if (type == "Diners") {
        // Diners: length 14, prefix 30, 36, or 38.
        var re = /^3[0,6,8]\d{12}$/;
    }
    if (!re.test(ccnum)) return false;
    // Remove all dashes for the checksum checks to eliminate negative numbers
    ccnum = ccnum.split("-").join("");
    // Checksum ("Mod 10")
    // Add even digits in even length strings or odd digits in odd length strings.
    var checksum = 0;
    for (var i = (2 - (ccnum.length % 2)); i <= ccnum.length; i += 2) {
        checksum += parseInt(ccnum.charAt(i - 1));
    }
    // Analyze odd digits in even length strings or even digits in odd length strings.
    for (var i = (ccnum.length % 2) + 1; i < ccnum.length; i += 2) {
        var digit = parseInt(ccnum.charAt(i - 1)) * 2;
        if (digit < 10) { checksum += digit; } else { checksum += (digit - 9); }
    }
    if ((checksum % 10) == 0) return true; else return false;
}

function IsExpiredCard(year, month) 
{
    today = new Date();
    expiry = new Date(year, month);
    if (today.getTime() > expiry.getTime())
        return false;
    else
        return true;
}


var ssArrSrc = String('<%=moreImages%>').split(",");
var ssCurrImgInd = 1;

function ssPreload() {
    var ssArrPL = new Array();
    for (var iPL = 0; iPL < ssArrSrc.length; iPL++) {
        ssArrSrc[iPL] = String(ssArrSrc[iPL]).split('|');
        ssArrPL[iPL] = new Image();
        ssArrPL[iPL].src = ssArrSrc[iPL][0];
    }
}

function ssNext(oImg) {
    ssCurrImgInd++;
    if (ssCurrImgInd >= ssArrSrc.length) {
        ssCurrImgInd = 0;
    }
    oImg.src = ssArrSrc[ssCurrImgInd][0];
    oImg.width = ssArrSrc[ssCurrImgInd][1];
}

function ssPrev(oImg) {
    ssCurrImgInd--;
    if (ssCurrImgInd < 0) {
        ssCurrImgInd = ssArrSrc.length - 1;
    }
    oImg.src = ssArrSrc[ssCurrImgInd][0];
    oImg.width = ssArrSrc[ssCurrImgInd][1];
}

function ProductPopUp(id) {
    window.open("viewallimages.aspx?id=" + id + "", "", "width=710, height=560, top=90, left=90, resizable=0")
}

function ProductPopOut() {
    window.close();
}

function LargeImageURL(productImage) {
    window.open("", "url", "align=center, width=300, height=300, bottom=90, top=90, left=70, right=70, resizable=0")
}

//function to set the current page tab highlighted
function setActiveMenu(arr, currentPage) {
    if (currentPage == "") {
        currentPage = "default";
    }
    for (var i = 0; i < arr.length; i++) {
        if (extractPageName(arr[i].href) == currentPage) {
            arr[i].parentNode.id = "current";
            break;
        }
    }
}

//function extraxt the page name to highlight the current page anchor on the top navigation
function extractPageName(hrefString) {
    var arr = hrefString.split('.');
    arr = arr[arr.length - 2].split('/');
    return arr[arr.length - 1].toLowerCase();
}

function setPage() {
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

    sPage = sPage.replace('.aspx', "")

    if (document.getElementById("tabs") != null) {
        setActiveMenu(document.getElementById("tabs").getElementsByTagName("a"), sPage.toLowerCase());
    }
}
function EnableCheckOutButton(btnSagePay, btnPlaceOrder, chkTermsAndConditions) {
    var chkBox = document.getElementById(chkTermsAndConditions);
    var btnSagepay = document.getElementById(btnSagePay);
    var btnOrder = document.getElementById(btnPlaceOrder);
    if (chkBox.checked) {

        if (btnSagepay)
            btnSagepay.disabled = false;
        if (btnOrder)
            btnOrder.disabled = false;
    }
    else {
        if (btnSagepay)
            btnSagepay.disabled = true;
        if (btnOrder)
            btnOrder.disabled = true;
    }
}

function OpenPrintInvoice() {
    window.open("PrintInvoice.aspx", "PrintInvoice", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,height=690,width=670");
}

function ShowProductInfo(url) {
    window.open(url, "Brochure", 'width=600, height=600, menubar=no, resizable=yes');
    return false;
}

function TrackEvent(categories, actions, labels, values) {
    pageTracker._trackEvent(categories, actions, labels, values);
}

function AddItemToCompare(product, e, compareProduct) {
    compareProduct = document.getElementById(compareProduct);
    if (compareProduct.value == '' || compareProduct.value == null)
        compareProduct.value = product;
    else {
        if (e.checked) {
            compareProduct.value += ',' + product;
        }
        else {
            var compareProducts = compareProduct.value.split(",");
            var products = '';
            for (index = 0; index < compareProducts.length; index++) {
                if (compareProducts[index] == product) {
                    compareProducts[index] = '';
                }
                else {
                    if (products == '' || products == null)
                        products = compareProducts[index];
                    else
                        products += ',' + compareProducts[index];
                }
            }
            compareProduct.value = products;
        }
    }
}

function CompareProducts(compareProducts) {
    hdnCompare = document.getElementById(compareProducts);
    if (hdnCompare.value != '' && hdnCompare.value != null)
        window.open("CompareProducts.aspx?Ids=" + hdnCompare.value, "CompareProducts", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,height=600,width=750");
    else
        alert('Please select an item to compare..');

    return false;
}

function closeWindow(url) {

    window.opener.location = url;
    window.close();
}
function RemoveItems(item) {
    hdnCompareIds = document.getElementById('hdnCompare');
    var compareProducts = window.opener.document.getElementById('ctl00_placeholderMain_hdnCompareIds').value.split(",");
    var products = '';
    for (index = 0; index < compareProducts.length; index++) {
        if (compareProducts[index] == item) {
            compareProducts[index] = '';
        }
        else {
            if (products == '' || products == null)
                products = compareProducts[index];
            else
                products += ',' + compareProducts[index];
        }
    }
    window.opener.document.getElementById('ctl00_placeholderMain_hdnCompareIds').value = products;
    hdnCompareIds.value = products;
    if (hdnCompareIds.value == '' || hdnCompareIds.value == null) {
        url = window.opener.location;
        window.opener.location = url;
        window.close();
    }
}

