// JScript File
function initAjaxProgress() {
    var pageHeight = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
    //SET HEIGHT OF BACKGROUND
    var bg = document.getElementById('ajaxProgressBg');
    bg.style.height = (pageHeight + 1000) + 'px';
    //POSITION THE PROGRESS INDICATOR ON INITIAL LOAD
    reposAjaxProgress();
    //REPOSITION THE PROGRESS INDICATOR ON SCROLL
    window.onscroll = reposAjaxProgress;
}

function reposAjaxProgress() {
    var div = document.getElementById('ajaxProgress');
    var st = document.body.scrollTop;
    if (st == 0) {
        if (window.pageYOffset) st = window.pageYOffset;
        else st = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }
    div.style.top = 150 + st + "px";
}

function LocalizePage(elem) {

    if (jQuery.cookie("country") == "EUR")
        a.preferCulture("fr-FR");
    else
        a.preferCulture("en-US");

    if (elem == null) elem = $("body");

    elem.find('.Currency2').each(function () {
        var numberString = $(this).text().replace('$', '');
        numberString = numberString.replace('€', '');
        // need to remove commas or dots to convert correctly.
        commaPos = numberString.indexOf(',');
        dotPos = numberString.indexOf('.');

        if (commaPos > dotPos) // remove dots 1.333.222,00
        {
            numberString = numberString.replace('.', '');
            numberString = numberString.replace(',', '.');
        }

        if (dotPos > commaPos) // remove commas 1,323.00
            numberString = numberString.replace(',', '');

        numberString = numberString.replace(' ', '');

        var val = parseFloat(numberString);

        $(this).html(a.format(val, "c"));
        $(this).show();
    });

    elem.find(".Currency4").each(function () {
        var numberString = $(this).text().replace('$', '')
        // need to remove commas or dots to convert correctly.
        commaPos = numberString.indexOf(',');
        dotPos = numberString.indexOf('.');

        if (commaPos > dotPos) // remove dots 1.333.222,00
        {
            numberString = numberString.replace('.', '');
            numberString = numberString.replace(',', '.');
        }

        if (dotPos > commaPos) // remove commas 1,323.00
            numberString = numberString.replace(',', '');

        numberString = numberString.replace(' ', '');

        var val = parseFloat(numberString);
        //alert(val);
        $(this).html(a.format(val, "c4"));
        $(this).show();
    });

    elem.find(".ShortDate").each(function () {
        var val = a.parseDate($(this).text(), "M/d/yyyy h:mm:ss tt", "en-US"); //TODO Assume US as culture when parsing. Needto specify this
        $(this).html(a.format(val, "d"));
        $(this).show();
    });

}


function GetShortZoneString(z) {
    // TODO this procedure takes first and last zone. It need to be much smarter and create comma separated list if there are gaps anywhere.
    try {
        if (z == null) z = '';

        var zones = z.split(',');
        var ret = '';
        if (zones.length > 0) ret += zones[0];
        if (zones.length > 1) ret += '-' + zones[zones.length - 1];

        if (ret == '') ret = 'N/A';

        return ret;
    } catch (e) {
        return z;
    }

}

function getRegionCode() {
    var country = jQuery.cookie("country");
    if (country == null)
        return "USA";
    else
        return country;
}
function ImgError(source) {
    source.onerror = "";
    source.src = "./Assets/NoImageAvailable.png";
    //alert(source.src);  
    return true;
}

function RoundHundreds(source) {
    var qty = parseInt(source.val());
    if (isNaN(qty) || (qty == 1))
        return;
    //if (qty % 100 >= 50) {
    //    qty += 100 - (qty % 100);
    //}
    //else if (qty % 100 < 50) {
    //    qty -= (qty % 100);
    //}
    if (qty == 0)
        qty = 1;
    var hiddenVal = parseInt($(source.closest("ul")).data("MinQuantity"));

    if (hiddenVal != null && !isNaN(hiddenVal)) {
        var CSRval = jQuery('.IsCSR').val();
        if (qty >= parseInt(hiddenVal) || qty == 0 || CSRval == 1) {
            // OK
        }
        else {
            // Comparing the quantity with the hidden value and also the CSR value
            alert("The plant quantity value cannot be less than " + hiddenVal);
            qty = hiddenVal;
        }
    }
    source.val("" + qty);
}

function RoundFifties(source) {
    var qty = parseInt(source.val());
    if (isNaN(qty) || (qty == 1))
        return;

    // nick acupido 2/14/2012

//    if (qty % 50 >= 25) {
//        qty += 50 - (qty % 50);
//    }
//    else if (qty % 50 < 25) {
//        qty -= (qty % 50);
//    }
    if (qty == 0)
        qty = 1;

    var hiddenVal = parseInt($(source.closest("ul")).data("MinQuantity"));

    if (hiddenVal != null && !isNaN(hiddenVal)) {
        var CSRval = jQuery('.IsCSR').val();
        if (qty >= parseInt(hiddenVal) || qty == 0 || CSRval == 1) {
            // OK
        }
        else {
            // Comparing the quantity with the hidden value and also the CSR value
            alert("The plant quantity value cannot be less than " + hiddenVal);
            qty = hiddenVal;
        }
    }
    source.val("" + qty);
}


function AllowNumericOnly() {

    var key_code = window.event.keyCode;
    var oElement = window.event.srcElement;
    if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
        if ((key_code > 47 && key_code < 58) ||
        (key_code > 95 && key_code < 106)) {
            if (key_code > 95)
                key_code -= (95 - 47);
            //oElement.value = oElement.value;
        } else if ((key_code == 8) || (key_code == 46)) { // backspace or delete
            oElement.value = oElement.value;
        } else if (key_code != 9) {
            event.returnValue = false;
        }
    }
}
