

function applyCascadingDropdown(sourceId, targetId) {
    var source = document.getElementById(sourceId);
    var target = document.getElementById(targetId);
    if (source && target) {
        source.onchange = function() {
            displayOptionItemsByClass(target, source.value);
            color.goto_slide_by_id(source.value);
            setprice();
        }
        displayOptionItemsByClass(target, source.value);
    }
}

function displayOptionItemsByClass(selectElement, className) {
    if (!selectElement.backup) {
        selectElement.backup = selectElement.cloneNode(true);
    }
    var options = selectElement.getElementsByTagName("option");
    for (var i = 0, length = options.length; i < length; i++) {
        selectElement.removeChild(options[0]);
    }
    var options = selectElement.backup.getElementsByTagName("option");
    for (var i = 0, length = options.length; i < length; i++) {
        if (options[i].className == className)
            selectElement.appendChild(options[i].cloneNode(true));
    }
    size.goto_slide_by_id(selectElement.value);

}

function applyCascadingDropdowns() {
    applyCascadingDropdown("ctl00_CP_ddlColor", "sizes");
    setprice();

}

//window.onload = applyCascadingDropdowns();


function quantityvalidation(MinimumQty, maxqty) {

    if (isNaN(MinimumQty)) {
        MinimumQty = 1;
    }

    var mmid = 0;

    //if (typeof (document.getElementById('sizes')) == "undefined" || document.getElementById('sizes') == null) {
    if (typeof (document.getElementById('ctl00_CP_ddlColor')) != "undefined" && document.getElementById('ctl00_CP_ddlColor') != null)
        mmid = document.getElementById("ctl00_CP_ddlColor").options[document.getElementById("ctl00_CP_ddlColor").selectedIndex].id;
    else
        mmid = location.href.substring(location.href.indexOf('MID') + 3, location.href.indexOf('.aspx'));
    //}

    if (String(maxqty) != "null") {
        if (typeof (document.getElementById('sizes')) != "undefined" && document.getElementById('sizes') != null) {
            if (maxqty > size.get_qty_by_id(document.getElementById('sizes').value)) {
                maxqty = size.get_qty_by_id(document.getElementById('sizes').value);
            }
        }
        else {
            //Following if condition was added by sunil in order to validate the user to specify the quantity between 
            //max and min values specified per order in product card for the selected product ---- 31/03/2009
            if (maxqty > size.get_qty_by_id(mmid))
                maxqty = size.get_qty_by_id(mmid);
        }
    }
    else {
        if (typeof (document.getElementById('sizes')) != "undefined" && document.getElementById('sizes') != null) {
            maxqty = size.get_qty_by_id(document.getElementById('sizes').value);
        }
        else {
            maxqty = size.get_qty_by_id(mmid);
        }
    }  //included by lava kumar to resolve the problem when minqty greater than qty available
    if (MinimumQty > maxqty) {
        MinimumQty = maxqty;
    }

    if (isNaN(document.getElementById('ctl00_CP_txtQuantity').value) || document.getElementById('ctl00_CP_txtQuantity').value == '' || Number(document.getElementById('ctl00_CP_txtQuantity').value) <= 0) {
        alert('Please enter a valid Quantity');
        return false;
    }
    else if (isNaN(maxqty)) {
        alert('Please enter quantity larger than ' + MinimumQty + ' for this item');
        return false;
    }
    else if ((Number(document.getElementById('ctl00_CP_txtQuantity').value) < Number(MinimumQty))
				         || (Number(document.getElementById('ctl00_CP_txtQuantity').value) > Number(maxqty))) {
        if (MinimumQty == maxqty)
            alert('Please enter a quantity of ' + MinimumQty + ' for this item');
        else
            alert('Please enter a quantity between ' + MinimumQty + ' and ' + maxqty + ' for this item');
        //alert('Please enter a Minimum of ' + MinimumQty + ' Products'); 
        return false;
    }
    else if (document.getElementById('sizes') != null && typeof (document.getElementById('sizes')) != "undefined") {
        if ((Number(document.getElementById('ctl00_CP_txtQuantity').value))
					        > (Number(size.get_qty_by_id(document.getElementById('sizes').value)))) {
            //alert('Please enter a quantity less than ' + size.get_qty_by_id(document.getElementById('sizes').value)); 
            alert('Please enter a quantity of ' + size.get_qty_by_id(document.getElementById('sizes').value) + ' for this item');
            //alert('Quantity available is ' + size.get_qty_by_id(document.getElementById('sizes').value)); 
            return false;
        }
        else
            return true;
    }
    else if (document.getElementById('sizes') != null && typeof (document.getElementById('sizes')) != "undefined") {
        if ((Number(document.getElementById('ctl00_CP_txtQuantity').value))
					        > (Number(size.get_qty_by_id(mmid)))) {
            //alert('Please enter a quantity less than ' + size.get_qty_by_id(document.getElementById('sizes').value)); 
            alert('Please enter a quantity of ' + size.get_qty_by_id(mmid) + ' for this item');
            //alert('Quantity available is ' + size.get_qty_by_id(document.getElementById('sizes').value)); 
            return false;
        }
        else
            return true;
    }
    else {
        return true;

    }

}



function gethref() {
    var img = document.getElementsByName("colorIMG")[0];
    newpath = img.src;
    var Url = newpath.substring(0, newpath.indexOf("UploadedFiles/"));
    newpath = newpath.substring(newpath.indexOf("ProductImage/"));
    var str = newpath.split("/");
    str[0] = Url + "ViewImage.aspx?pid=" + str[1] + "&color=" + str[2] + "&fn=" + str[4].toString().split(".")[0];
    str[0] = str[0] + "&fe=." + str[4].toString().split(".")[1];

}

//changed by lava kumar to open the shadow box for each view - 13/June/2009
function li(path, imgnumber) {

    var newpath = path.replace("Thumb", "Product");

    var extension = newpath.substring(0, newpath.lastIndexOf(".")); // Multi image
    newpath = newpath.substring(newpath.lastIndexOf(".") - 1, newpath.lastIndexOf("."));
    if (!isNaN(newpath)) { // Multi image  {{{{{{
        if (newpath == 1)
            document.images.colorIMG.src = extension + document.images.i0.name;
        else if (newpath == 2)
            document.images.colorIMG.src = extension + document.images.i1.name;
        else if (newpath == 3)
            document.images.colorIMG.src = extension + document.images.i2.name;
        else if (newpath == 4)
            document.images.colorIMG.src = extension + document.images.i3.name;
        else if (newpath == 5)
            document.images.colorIMG.src = extension + document.images.i4.name;
        else if (newpath == 6)
            document.images.colorIMG.src = extension + document.images.i5.name;
    }    // Multi image  }}}}}}}}


    if (!isNaN(newpath)) {
        var i = 0;
        for (i = 0; i < $d('largeview').childNodes.length; i++) {
            if ($d('largeview').childNodes[i].href.lastIndexOf("/" + newpath + ".") > 0) {
                $d('largeview').childNodes[i].innerHTML = "Large View";
                $d('lnkX').href = $d('largeview').childNodes[i].href;
                $d('lnkX').rel = $d('largeview').childNodes[i].rel;
                var rel = $d('lnkX').rel;
                rel = rel.replace(rel.substring(rel.indexOf('['), rel.indexOf(']')), rel.substring(rel.indexOf('['), rel.indexOf(']')) + "999");
                $d('lnkX').rel = rel;
            }
            else
                $d('largeview').childNodes[i].innerHTML = "";
        }
    }
    else {
        if ($d("lnkX").childNodes[1] != undefined) {
            $d('lnkX').href = $d("lnkX").childNodes[1].src;
        }
        else {
            if ($d("lnkX").childNodes[0] != undefined)
                $d('lnkX').href = $d("lnkX").childNodes[0].src;
        }
        $d('lnkX').rel = "shadowbox[p999xxx];options={counterType:'skip',continuous:true,animSequence:'sync'}";
    }
    if ($d('divvideo').style.display != "none")
        setlargeviewtonull()
    Shadowbox.setup();
}




function setlargeviewtonull() {
    var i = 0;
    for (i = 0; i < $d('largeview').childNodes.length; i++) {
        $d('largeview').childNodes[i].innerHTML = "";

    }

}





function setimageforonlycolor() {
    if (typeof (document.getElementById('sizes')) == "undefined" || document.getElementById('sizes') == null) {
        //if condition added by lava kumar,if there is no color and no size it is giving java script problem
        if (document.getElementById('ctl00_CP_ddlColor') != null) {
            var mmid = document.getElementById("ctl00_CP_ddlColor").options[document.getElementById("ctl00_CP_ddlColor").selectedIndex].id;
        }
        else {
            var mmid = location.href.substring(location.href.indexOf('MID') + 3, location.href.indexOf('.aspx'));
        }
    }

    if (document.getElementById('ctl00_CP_ddlColor') != null) {
        color.goto_slide_by_id(document.getElementById('ctl00_CP_ddlColor').value);
        hidequantityforsoldout();
        if (typeof (document.getElementById('sizes')) != "undefined" && document.getElementById('sizes') != null) {
            document.getElementById('PriceText').innerHTML = size.get_price_by_id(document.getElementById('sizes').value);
        }
        else {
            document.getElementById('PriceText').innerHTML = size.get_price_by_id(mmid);
        }
    }
    setprice();

}

//included by lavakumar,it hides the thumb image if it has only one view
function hideimages() {
    if (document.getElementById('ctl00_CP_hdnimage').value == 'nocolor') {
        document.getElementById('ctl00_CP_dlImage').style.cssText = "display:none;";
    }
    else if (document.getElementById('ctl00_CP_hdnimage').value != '') {
        if ((document.getElementById('ctl00_CP_hdnimage').value.indexOf(',' + document.getElementById("ctl00_CP_ddlColor").options[document.getElementById("ctl00_CP_ddlColor").selectedIndex].text + ',')) >= 0) {
            document.getElementById('ctl00_CP_dlImage').style.cssText = "display:none;";
        }
        else {
            document.getElementById('ctl00_CP_dlImage').style.cssText = "display:;";
        }
    }
    else {
        document.getElementById('ctl00_CP_dlImage').style.cssText = "display:;";
    }
}

function setprice() {

    Shadowbox.init({ skipSetup: false }); Shadowbox.setup();
    //included by lavakumar,it hides the thumb image if it has only one view
    hideimages();
    if (typeof (document.getElementById('sizes')) == "undefined" || document.getElementById('sizes') == null) {
        if (typeof (document.getElementById('ctl00_CP_ddlColor')) != "undefined" && document.getElementById('ctl00_CP_ddlColor') != null)
            mmid = document.getElementById("ctl00_CP_ddlColor").options[document.getElementById("ctl00_CP_ddlColor").selectedIndex].id;
        else
            mmid = location.href.substring(location.href.indexOf('MID') + 3, location.href.indexOf('.aspx'));
    }

    if (typeof (document.getElementById('sizes')) != "undefined" && document.getElementById('sizes') != null) {
        if (document.getElementById('sizes') != null) {
            size.goto_slide_by_id(document.getElementById('sizes').value);
            hidequantityforsoldout();
        }
    }
    else {
        size.goto_slide_by_id(mmid);
        hidequantityforsoldout();
    }

    //added by lava kumar to open the shadow box on clicking the product image - 13/June/2009
    if ($d("lnkX").childNodes[1] != undefined) {
        li($d("lnkX").childNodes[1].src, 0);
    }
    else {
        if ($d("lnkX").childNodes[0] != "undefined")
            li($d("lnkX").childNodes[0].src, 0);
    }

}


function hidequantityforsoldout() {
    if (document.getElementById('sizes') != null && typeof (document.getElementById('sizes')) != "undefined") {
        if (size.get_qty_by_id(document.getElementById('sizes').value) <= 0) {
            showORhide(false);
        }
        else {
            //document.getElementById("ctl00_CP_txtQuantity").value = size.get_qty_by_id(document.getElementById('sizes').value); 
            showORhide(true);
        }
    }
    else {
        if (document.getElementById('ctl00_CP_ddlColor') != null) {
            if (color.get_qty_by_id(document.getElementById('ctl00_CP_ddlColor').title) <= 0) {
                showORhide(false);
            }
            else {
                if (typeof (document.getElementById('ctl00_CP_ddlColor')) != "undefined" && document.getElementById('ctl00_CP_ddlColor') != null)
                    var mmid = document.getElementById("ctl00_CP_ddlColor").options[document.getElementById("ctl00_CP_ddlColor").selectedIndex].id;
                else
                    mmid = location.href.substring(location.href.indexOf('MID') + 3, location.href.indexOf('.aspx'));


                if (!isNaN(mmid)) {
                    if (size.get_qty_by_id(mmid) > 0) {
                        //document.getElementById("ctl00_CP_txtQuantity").value = size.get_qty_by_id(document.getElementById('sizes').value); 
                        showORhide(true);
                    }
                    else {
                        showORhide(false);
                    }
                }
                else {
                    //document.getElementById("ctl00_CP_txtQuantity").value = size.get_qty_by_id(document.getElementById('sizes').value); 
                    showORhide(true);
                }
            }
        }
        else {
            //var mmid1 = location.href.substring(location.href.indexOf('MID') + 3, location.href.indexOf('.aspx'));
            if (isNaN(location.href.substring(location.href.indexOf('MID') + 3, location.href.indexOf('.aspx'))))
                showORhide(false);
            else {
                if (size.get_qty_by_id(location.href.substring(location.href.indexOf('MID') + 3, location.href.indexOf('.aspx'))) > 0) {
                    showORhide(true);
                }
                else {
                    showORhide(false);
                }
            }

        }
    }
}

function showORhide(IsShown) {
    if (IsShown) {
        document.getElementById("ctl00_CP_trQuantity").style.cssText = "display:;";
        document.getElementById("trsold").style.cssText = "display:none;";
        document.getElementById("ctl00_CP_ibtnShoppingCart").style.cssText = "display:;";
        if (typeof (document.getElementById("ctl00_CP_lbtnWish")) != "undefined" && document.getElementById("ctl00_CP_lbtnWish") != null)
            document.getElementById("ctl00_CP_lbtnWish").style.cssText = "display:;";
        if (typeof (document.getElementById("ctl00_CP_lbtnGift")) != "undefined" && document.getElementById("ctl00_CP_lbtnGift") != null)
            document.getElementById("ctl00_CP_lbtnGift").style.cssText = "display:;";
        //Following condition was added by sunil on 06/04/2009 in order to hide the tr which holds the notes textfield
        if (typeof (document.getElementById("ctl00_CP_trDonationnotes")) != "undefined" && document.getElementById("ctl00_CP_trDonationnotes") != null)
            document.getElementById("ctl00_CP_trDonationnotes").style.cssText = "display:;";
    }
    else {
        document.getElementById("ctl00_CP_trQuantity").style.cssText = "display:none;";
        document.getElementById("ctl00_CP_ibtnShoppingCart").style.cssText = "display:none;";
        document.getElementById("trsold").style.cssText = "display:;";
        if (typeof (document.getElementById("ctl00_CP_lbtnWish")) != "undefined" && document.getElementById("ctl00_CP_lbtnWish") != null)
            document.getElementById("ctl00_CP_lbtnWish").style.cssText = "display:none;";
        if (typeof (document.getElementById("ctl00_CP_lbtnGift")) != "undefined" && document.getElementById("ctl00_CP_lbtnGift") != null)
            document.getElementById("ctl00_CP_lbtnGift").style.cssText = "display:none;";
        //Following condition was added by sunil on 06/04/2009 in order to hide the tr which holds the notes textfield
        if (typeof (document.getElementById("ctl00_CP_trDonationnotes")) != "undefined" && document.getElementById("ctl00_CP_trDonationnotes") != null)
            document.getElementById("ctl00_CP_trDonationnotes").style.cssText = "display:none;";
    }
}

function setsize() {

    var s = document.getElementById('sizes');

    if (document.getElementById('ctl00_CP_hdnGid') != "0" && s != null) {
        //document.getElementById('sizes').selectedvalue = document.getElementById('ctl00_CP_hdnGid');
        var v = document.getElementById('ctl00_CP_hdnGid').value;
        for (var i = 0; i < s.options.length; i++) {
            if (s.options[i].value == v) {
                s.options[i].selected = true;
                return;
            }
        }
    }

}


function setvalues(groupdetail1name) {

    if (document.getElementById('ctl00_CP_ddlColor') != null) {
        for (i = 0; i < document.getElementById('ctl00_CP_ddlColor').length; i++) {
            if (document.getElementById('ctl00_CP_ddlColor').options[i].value == groupdetail1name) {
                document.getElementById('ctl00_CP_ddlColor').selectedIndex = i
            }
        }
    }
    //included by lava kumar for swatch images
    setimageforonlycolor();
    applyCascadingDropdowns();
    //      color.goto_slide_by_id(groupdetail1name);
}


function productdetailsend() {
    color.set_image(document.images.colorIMG);
    color.set_image0(document.images.i0);
    color.set_image1(document.images.i1);
    color.set_image2(document.images.i2);
    color.set_image3(document.images.i3);
    color.set_image4(document.images.i4);
    color.set_image5(document.images.i5);
    //color.goto_slide(0);
    if (document.getElementById('ctl00_CP_ddlColor') != null) {
        color.goto_slide_by_id(document.getElementById('ctl00_CP_ddlColor').value);
    }
    else {
        color.goto_slide(0);
    }
    size.set_textid('PriceText');
    if (document.getElementById('sizes') != null) {
        size.goto_slide(document.getElementById('sizes').value);
    }
    hidequantityforsoldout();
}


