// Used to detect and utilise the
// Correct method depending on the browser 
function fnc_getElementWithId(id){
    
    var obj;
    if(document.getElementById){
        obj = document.getElementById(id);
    }else if(document.all){
        obj = document.all(id);
    }else if(document.layers){
        obj = document.layers(id);
    }
    
    return obj||null;
}
    
// Toggle the leftnav hover setting
function fnc_ToggleNav(intId,strMode) {

    // Either put the image back
    // Or take it off
    if (strMode == "Off") {
        fnc_getElementWithId("tdLeftnav_" + intId).style.backgroundImage = "url('img/leftnav_on.gif')";
    }
    else {
        fnc_getElementWithId("tdLeftnav_" + intId).style.backgroundImage = "url('img/leftnav_off.gif')";
    }
}

// Autosubmit a form
function fnc_AutoSubmit(strTarget) {

    // Submit the form passed in
    fnc_getElementWithId(strTarget).submit();
    
}

// Swap the image on the category page
function fnc_SwapProductImage(strId) {

    var strTmp = "";
    var arrURL;
    
    // Only fire if there's a value
    if (strId != "") { 
        // Get the Id/URL value
        strTmp = fnc_getElementWithId("sel_Size_" + strId).value;
        arrURL = strTmp.split("|")
        
        // Set the image appropriatley
        fnc_getElementWithId("Img_" + strId).src = "img/products/" + arrURL[1];
    }    
}

// Validate that numbers only have been entered
function fnc_ValidateQty(intCount) {
    
    var strValue = "";
    var strError =  "";
    
    // Loop through all the quantity fiels
    // And check that values are numeric
    for (i=0; i<intCount; i++) {
        strValue = fnc_getElementWithId("txt_Qty_" + i).value;
        if (isNaN(strValue) == true) {
            strError = "<br/><br/><li>Please only enter a numerical value in the qauntity field.</li>"
            break;
        }
        if (strValue == "") {
            strError = "<br/><br/><li>Please only enter a numerical value in the qauntity field.</li>"
            break;
        }
    }

    // Either report the error or
    // Submit the form
    if (strError == "") {
        fnc_AutoSubmit("frmBasket")
    }
    else {
        fnc_getElementWithId("divErr").innerHTML = strError
    }
}

// Copy previous engraving text entry
function fnc_CopyText(strId, intPCntr, intSCntr) {

    // Setup counter
    var intPrvCntr = 0;
    intPrvCntr = intSCntr - 1
    
    // Get the previous entry
    var strText = fnc_getElementWithId(strId + intPCntr + "_" + (intPrvCntr)).value;
    
    // Enter it into the current entry
    fnc_getElementWithId(strId + intPCntr + "_" + intSCntr).value = strText;

}

