function MM_showHideLayers() { //v3.0
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for (i = 0; i < (args.length - 2); i += 3) if ((obj = MM_findObj(args[i])) != null) {
        v = args[i + 2];
        if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v = 'hide') ? 'hidden' : v; }
        obj.visibility = v;
    }
}
function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}
function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}
function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}
//function oWin(page, name)
//{
//    window.open(page,name,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,screenX=5,screenY=5,width=424,height=423');
//}
function oWin(page, name, iX, iY) {
    window.open(page, name, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no, screenX=5, screenY=5, width=' + iX + ', height=' + iY);
}

function oWinScroll(page, name, iX, iY) {
    window.open(page, name, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no, screenX=5, screenY=5, width=' + iX + ', height=' + iY);
}

function alert1()
{ alert("Error:Please select 1 node to delete") }

function alert2()
{ alert("Error:Please select only 1 node to move") }

function alert3()
{ alert("Error:Please delete all child pages first") }

function isMaxLength100(txtBox) {
    if (txtBox) {
        return (txtBox.value.length <= 100);
    }
}
function isMaxLength500(txtBox) {
    if (txtBox) {
        return (txtBox.value.length <= 500);
    }
}


/* These might not be used any more? 

function calcIncrease() {
var txtCost = document.getElementById('ctl00_cphMain_Product1_fvProductItem_txtPriceCost').value;
var txtMultiplier = document.getElementById('ctl00_cphMain_Product1_fvProductItem_txtIncrease').value;
var result = txtCost * txtMultiplier;
document.getElementById('ctl00_cphMain_Product1_fvProductItem_txtPriceSelling').value = parseFloat(result).toFixed(2);
}

function calcPremiumIncrease() {
var txtCost = document.getElementById('ctl00_cphMain_Product1_fvProductItem_txtPriceCost').value;
var txtMultiplier = document.getElementById('ctl00_cphMain_Product1_fvProductItem_txtPremiumMultiplier').value;
var result = txtCost * txtMultiplier;
document.getElementById('ctl00_cphMain_Product1_fvProductItem_txtPriceSellingPremium').value = parseFloat(result).toFixed(2);
}

*/

function toggleWindow(imgID, tblContentNameID) {
    var img = document.getElementById(imgID);
    var tbl = document.getElementById(tblContentNameID);

    if (tbl.style.display == '' || tbl.style.display == 'block') {
        tbl.style.display = 'none'
        img.src = 'img/max.gif'
        //divHeaderNameID.display='none'
    }
    else {
        tbl.style.display = 'block'
        img.src = 'img/min.gif'
    }
}

function toggleSideWindow(imgID, tblContentNameID) {
    var img = document.getElementById(imgID);
    var tbl = document.getElementById(tblContentNameID);

    if (tbl.style.display == '' || tbl.style.display == 'block') {
        tbl.style.display = 'none'
        img.src = 'img/sidemax.gif'
        //divHeaderNameID.display='none'
    }
    else {
        tbl.style.display = 'block'
        img.src = 'img/sidemin.gif'
    }
}

function IsANumber(str) {
    if (str != null) {
        if (str.length > 0) {
            if (!isNaN(str))
                return true;
        }
    }
    return false;
}

function formatCurrency(i, n) {
    // i is the input element
    // n is the number of decimal places
    if (isNaN(i.value)) i.value = '';
    if (String(i.value).length > 0) {
        i.value = new Number(i.value).toFixed(n);
        if (i.value < 0)
            i.value = i.value * 1;
    }
}

function formatCurrencyDirect(i, n) {
    if (i.toString().length > 0)
        i = new Number(i).toFixed(n);
    return i;
}

function applyAllSelections() {
    getPriceBlack();
    getPriceRed();
    getPrice(1);
    getPrice(2);
    var errorfield = $('#fieldInfo')[0];
    if (errorfield != null)
        errorfield.innerHTML = "<img src=\"img/tick.gif\"/>";
    getPriceRed();
    getPriceBlack();
    getPrice(1);
    getPrice(2);
    CalculateLeasingPrices(); //calculates lease price       
    //need to emulate button press to work in ffox - validates whole page.
    // document.getElementById('btnValidate').click();
}

function hasUserFocus(ctrl) {
    if (ctrl.readOnly)
        ctrl.className = "inputNotActive";
    else
        ctrl.className = "inputActive";
}

function lostUserFocus(ctrl) {
    ctrl.className = "";
}

function hasUserFocusCurrency(ctrl) {
    if (ctrl.readOnly)
        ctrl.className = "inputNotActiveCurrency";
    else
        ctrl.className = "inputActiveCurrency";
}

function lostUserFocusCurrency(ctrl) {
    ctrl.className = "currency";
}

function addAppendFunctionddl(ddl) {
    var divContainer = document.getElementById(ddl.id.substring(0, ddl.id.lastIndexOf('_') + 1) + 'divShowAppendDataBound');
    if (ddl.selectedIndex > 0)
        divContainer.className = '';
    else
        divContainer.className = 'hiddenfield'
}

function setValue(ddl) {
    var txtValue = document.getElementById(ddl.id.substring(0, ddl.id.lastIndexOf('_') + 1) + 'txtValue');
    txtValue.value = ddl.options[ddl.selectedIndex].text;
}

function showInsertImage(btn) {

    document.getElementById(btn.id.substring(0, btn.id.lastIndexOf('_') + 1) + 'tblInsert').style.display = 'block';
    var tblUpdate = document.getElementById(btn.id.substring(0, btn.id.lastIndexOf('_') + 1) + 'tblUpdate');
    if (tblUpdate != null)
        tblUpdate.style.display = 'none';
    return false;
}

function hideInsertImage(btn) {
    var tbl = document.getElementById(btn.id.substring(0, btn.id.lastIndexOf('_') + 1) + 'tblInsert').display = 'none';
    return false;
}

function ValidateSearch() {
    if ($('.searchbox input:text').val().length < 1) {
        alert("Please enter a search term");
        return false;
    }
    return true;
}

$(document).ready(function() {
    main();
});
function main() {
    //selects input type text, input type password and select boxes,
    //adds the class 'input', then adds on focus and on blue events to them all to change their classes
    //on the respective event triggers.
    $('input:text, input:password, select')
                .addClass('input')
                .focus(function() { $(this).removeClass('input').addClass('input_onfocus'); })
                .blur(function() { $(this).removeClass('input_onfocus').addClass('input'); });
    //fires onfocus event so focussed input element executes the event defined above
    $('input:text, input:password, select').triggerHandler("focus");
    //code for tooltips: selects all possible elements know to be used as tooltips and defines their hide() and show() events.
    $(".tooltip a").mouseenter(function() { $(".tooltiptext", this).show() }).mouseleave(function() { $(".tooltiptext", this).hide(); });
    //loops all selected elements and removes their z-index (IE hack) (
    var zIndexNumber = 9990;
    $('a').each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 5;
    });
}

function dbg(obj) {
    console.log(obj);
}
