﻿/*
javascript for product gallery page
*/

// by default on enter it was navigating to next page below script will disable enter key.
if (document.layers) document.captureEvents(Event.KEYDOWN);
document.onkeydown = doKey;
function doKey(e) {
    whichkey = (document.layers) ? e.which : event.keyCode;
    if (whichkey == 13) {
        return false
    }
}
var cookieName = "pgcompare" + unique_id.toLowerCase();
var cookieValue = "pgcomparevalue" + unique_id.toLowerCase();
var myImgArray;
function zoomZoom2(id, AliasItemCode) {
    if (Sys.Application._initialized == false) { return; }

    var Dom = YAHOO.util.Dom;
    var imgBig = Dom.get('bigimage');
    imgBig.src = 'images/white_background_400x400.jpg';
    Dom.get('thumbnail1').src = 'images/white_background_100x100.jpg';
    Dom.get('thumbnail2').src = 'images/white_background_100x100.jpg';
    Dom.get('thumbnail3').src = 'images/white_background_100x100.jpg';

    //waiting ..
    wait = new YAHOO.widget.Panel(
                    "wait",
                    { width: "240px",
                        fixedcenter: true,
                        close: false,
                        draggable: false,
                        zindex: 4,
                        modal: true,
                        visible: false
                    }
                );

    wait.setHeader("Processing.. Please wait..");
    wait.setBody("<img src='pics/ajax-loader3.gif' title='loading..' />");
    wait.render(document.body);
    wait.show();

    myImgArray = new Array();

    var fnSuccess = function(o) {
        var imgType = o.responseText.split('|');
        var thumbNails = imgType[0].split(',');
        var bigImages = imgType[1].split(',');
        var imgPrefix = "ImagesLibrary/";
        var found = false;
        var img;
        var bigSrc;
        if (top_category.toLowerCase() == "clearance") {
            imgPrefix = "";
        }

        //binding thumbNails              
        for (var i = 0; i < thumbNails.length; i++) {
            img = Dom.get('thumbnail' + (i + 1));
            if (thumbNails[i] != "") {
                img.src = imgPrefix + thumbNails[i];

                //resize
                myImgArray[myImgArray.length] = img;
                if (top_category.toLowerCase() == "clearance") {
                    sWSCall_WSonload = "WSCall_WSonload = new Function(\"\",\"myself = myImgArray[" + new String((myImgArray.length - 1)) + "];WSResize(myself, 100, 100, true,'clearance')\")";
                    eval(sWSCall_WSonload);
                    myImgArray[myImgArray.length - 1].onload = new WSCall_WSonload();
                } else {
                    myImgArray[myImgArray.length - 1].width = 100;
                    myImgArray[myImgArray.length - 1].height = 100;
                    var span = Dom.get("spanthumbnail" + (i + 1));
                    Dom.setStyle(span, "display", "");
                }
                Dom.setStyle(img, "display", "");

                bigSrc = bigImages[i];
                if (bigSrc != null && bigSrc != "") {
                    myImgArray[myImgArray.length - 1].longDesc = imgPrefix + bigSrc;
                    myImgArray[myImgArray.length - 1].onmouseover = function() {
                        var myBidImg = document.getElementById("bigimage");
                        if (myBidImg.longDesc != this.longDesc) {
                            myBidImg.src = this.longDesc;
                            myBidImg.longDesc = this.longDesc;

                            //resize big image
                            if (top_category.toLowerCase() == "clearance") {
                                myBidImg.onload = function() { WSResize(myBidImg, 400, 400, true); }
                            } else {
                                myBidImg.width = 400;
                                myBidImg.height = 400;
                                Dom.setStyle(Dom.get("spanbigimage"), "display", "");
                            }

                            //check no show reload again
                            if (myBidImg.lowsrc != this.longDesc && this.lowsrc != "X") {
                                myBidImg.lowsrc = this.longDesc;
                                this.lowsrc = "X";
                            }
                        }
                    }
                }

                if (bigSrc != null && bigSrc != "" && !found) {
                    imgBig.src = imgPrefix + bigSrc;
                    imgBig.longDesc = imgPrefix + bigSrc;

                    //resize big image
                    if (top_category.toLowerCase() == "clearance") {
                        WSResize(imgBig, 400, 400, true);
                    } else {
                        imgBig.width = 400;
                        imgBig.height = 400;
                        Dom.setStyle(Dom.get("spanbigimage"), "display", "");
                    }

                    found = true;
                }
            } else {
                Dom.setStyle(img, "display", "none");
            }

        } //end for

        if (found == false) { Dom.setStyle("loading", "display", "none"); }
        //Dom.setStyle("loading", "display", "none");
        wait.hide();
        Dom.get(btn_zoom_id).click();
    }                                    //end fnSuccess

    //start callback 
    YAHOO.util.Connect.asyncRequest(
                              'get'
                            , 'AjaxStation.aspx?method=popupimage&productid='
                            + id
                            + '&fc=' + top_category
                            + '&rnd=' + Math.random()
            , { success: fnSuccess
                , failure: function(o) {
                    alert(o.responseText);
                    Dom.setStyle("loading", "display", "none");
                }
            }
        );
}

function resizeImage(oElem) {
    WSResize(oElem, 140, 140, false, top_category);
//    if (top_category.toLowerCase() == "clearance") {
//        WSResize(oElem, 140, 140, false, "clearance");
//    } else {
//        oElem.width = 140;
//        oElem.height = 140;
//        oElem.style.visibility = "visible";

//        var oParent = WSGetParentByTag(oElem, "SPAN", 4);
//        oParent.style.display = "";
//    }
}


function gotoPage(page, gennewuid) {
    var Dom = YAHOO.util.Dom;
    var sc = second_category;

    var new_uid = unique_id;
    if (gennewuid == true) new_uid = unique_id + "_" + Math.random();

    if (top_category.toLowerCase() == 'watches') {
        var ddlwatchcategory = Dom.get(ddl_watchcategory_id);
        if (ddlwatchcategory.selectedIndex > 0) {
            sc = ddlwatchcategory.options[ddlwatchcategory.selectedIndex].value;
        } else {
            sc = second_category;
        }

        document.location
                = "?query=" + query_string
                + "&fc=" + top_category
                + "&sc=" + sc
                + "&gender=" + Dom.get(ddl_gender_id).options[Dom.get(ddl_gender_id).selectedIndex].value
                + "&sort=" + Dom.get(ddl_sort_id).options[Dom.get(ddl_sort_id).selectedIndex].value
                + "&pageno=" + page
                + "&size=" + Dom.get(ddl_pagesize_id).options[Dom.get(ddl_pagesize_id).selectedIndex].value
                + "&pricerange=" + Dom.get(ddl_pricerange_id).options[Dom.get(ddl_pricerange_id).selectedIndex].value
                + "&uid=" + new_uid;
    }
    else if (top_category.toLowerCase() == 'clearance') {
        var ddlClearanceCategory = Dom.get(ddl_clearance_category_id);
        if (ddlClearanceCategory.selectedIndex > 0) {
            sc = ddlClearanceCategory.options[ddlClearanceCategory.selectedIndex].value;
        } else {
            sc = "";
        }

        var myMetal = Dom.get(ddl_clearance_producttype_id).options[Dom.get(ddl_clearance_producttype_id).selectedIndex].value;
        if (myMetal == metal) myMetal = "";

        document.location
                = "?query=" + query_string
                + "&fc=" + top_category
                + "&sc=" + sc
                + "&metal=" + myMetal
                + "&sort=" + Dom.get(ddl_sort_id).options[Dom.get(ddl_sort_id).selectedIndex].value
                + "&pageno=" + page
                + "&size=" + Dom.get(ddl_pagesize_id).options[Dom.get(ddl_pagesize_id).selectedIndex].value
                + "&pricerange=" + Dom.get(ddl_pricerange_id).options[Dom.get(ddl_pricerange_id).selectedIndex].value
                + "&uid=" + new_uid;
    }
    else if (top_category.toLowerCase() == 'instore') {
        var ddlwatchcategory = Dom.get(ddl_watchcategory_id);
        if (ddlwatchcategory.selectedIndex > 0) {
            sc = ddlwatchcategory.options[ddlwatchcategory.selectedIndex].value;
        } else {
            sc = "";
        }

        document.location
                = "?query=" + query_string
                + "&fc=" + top_category
                + "&sc="
                + "&inprodtype=" + Dom.get(ddl_in_producttype_id).options[Dom.get(ddl_in_producttype_id).selectedIndex].value
                + "&incatg=" + Dom.get(ddl_in_category_id).options[Dom.get(ddl_in_category_id).selectedIndex].value
                + "&style=" + Dom.get(ddl_style_id).options[Dom.get(ddl_style_id).selectedIndex].value
                + "&gender="
                + "&sort=" + Dom.get(ddl_sort_id).options[Dom.get(ddl_sort_id).selectedIndex].value
                + "&pageno=" + page
                + "&size=" + Dom.get(ddl_pagesize_id).options[Dom.get(ddl_pagesize_id).selectedIndex].value
                + "&pricerange=" + Dom.get(ddl_pricerange_id).options[Dom.get(ddl_pricerange_id).selectedIndex].value
                + "&uid=" + new_uid;
    }
    else if (top_category.toLowerCase() == 'colordiamonds') {
    var ddlCdColor = Dom.get(ddl_cd_color_id);
    var ddlCdShape = Dom.get(ddl_cd_shape_id);
    var ddlCdClarity = Dom.get(ddl_cd_clarity_id);
        var color,shape,clarity = "";

        if (ddlCdColor.selectedIndex > 0) {
            color = ddlCdColor.options[ddlCdColor.selectedIndex].value;
        } else { color = ""; }

        if (ddlCdShape.selectedIndex > 0) {
            shape = ddlCdShape.options[ddlCdShape.selectedIndex].value;
        } else { shape = ""; }

        if (ddlCdClarity.selectedIndex > 0) {
            clarity = ddlCdClarity.options[ddlCdClarity.selectedIndex].value;
        } else { clarity = ""; }
    
       

        document.location
                = "?query=" + query_string
                + "&fc=" + top_category
                + "&sc=" + second_category
                + "&cdcolor=" + color
                + "&cdshape=" + shape
                + "&cdclarity=" + clarity
                + "&sort=" + Dom.get(ddl_sort_id).options[Dom.get(ddl_sort_id).selectedIndex].value
                + "&pageno=" + page
                + "&size=" + Dom.get(ddl_pagesize_id).options[Dom.get(ddl_pagesize_id).selectedIndex].value
                + "&pricerange=" + Dom.get(ddl_pricerange_id).options[Dom.get(ddl_pricerange_id).selectedIndex].value
                + "&uid=" + new_uid;
    } 
    else {
        var myStle = style;
        var myTopCategory = Dom.get(ddl_top_category_id).options[Dom.get(ddl_top_category_id).selectedIndex].value;
        var mySecondCategory = second_category;
        var myMetalCode = Dom.get(ddl_metaltype_id).options[Dom.get(ddl_metaltype_id).selectedIndex].value;

        if (Dom.get(ddl_style_id).options.length > 0) {
            myStle = Dom.get(ddl_style_id).options[Dom.get(ddl_style_id).selectedIndex].value;
            if (myStle != style) {
                if (myMetalCode == '')
                    myMetalCode = "";
            }
        }

        if (Dom.get(ddl_second_category_id).options.length > 0) {
            mySecondCategory = Dom.get(ddl_second_category_id).options[Dom.get(ddl_second_category_id).selectedIndex].value;
            if (mySecondCategory.toLowerCase() != second_category.toLowerCase()) {
                myStle = "";
                if (myMetalCode == '')
                    myMetalCode = "";
            }
        }

        if (top_category.toLowerCase() != myTopCategory.toLowerCase()) {
            mySecondCategory = "";
            myStle = "";
            if (myMetalCode == '')
                myMetalCode = "";
        }

        if (is_filtered_extended == 'True') {
            document.location
                = "?query=" + query_string
                + "&fc=" + myTopCategory
                + "&sc=" + mySecondCategory
                + "&gender=" + Dom.get(ddl_gender_id).options[Dom.get(ddl_gender_id).selectedIndex].value
                + "&metal=" + Dom.get(ddl_metaltype_id).options[Dom.get(ddl_metaltype_id).selectedIndex].value
                + "&style=" + myStle
                + "&stonecolor=" + Dom.get(ddl_stonecolor_id).options[Dom.get(ddl_stonecolor_id).selectedIndex].value
                + "&stoneclarity=" + Dom.get(ddl_stoneclarity_id).options[Dom.get(ddl_stoneclarity_id).selectedIndex].value
                + "&stonecarat=" + Dom.get(ddl_stonecarat_id).options[Dom.get(ddl_stonecarat_id).selectedIndex].value
                + "&sort=" + Dom.get(ddl_sort_id).options[Dom.get(ddl_sort_id).selectedIndex].value
                + "&pageno=" + page
                + "&size=" + Dom.get(ddl_pagesize_id).options[Dom.get(ddl_pagesize_id).selectedIndex].value
                + "&pricerange=" + Dom.get(ddl_pricerange_id).options[Dom.get(ddl_pricerange_id).selectedIndex].value
                + "&uid=" + new_uid;
        }
        else {
            document.location
                = "?query=" + query_string
                + "&fc=" + myTopCategory
                + "&sc=" + mySecondCategory
                + "&gender=" + Dom.get(ddl_gender_id).options[Dom.get(ddl_gender_id).selectedIndex].value
                + "&metal=" + myMetalCode
                + "&style=" + myStle
                + "&sort=" + Dom.get(ddl_sort_id).options[Dom.get(ddl_sort_id).selectedIndex].value
                + "&pageno=" + page
                + "&size=" + Dom.get(ddl_pagesize_id).options[Dom.get(ddl_pagesize_id).selectedIndex].value
                + "&pricerange=" + Dom.get(ddl_pricerange_id).options[Dom.get(ddl_pricerange_id).selectedIndex].value
                + "&uid=" + new_uid;
        }
    }
    return true;
}

function checkCompare(checkbox, itemid) {
    var Dom = YAHOO.util.Dom;

    var cookie = YAHOO.util.Cookie.get(cookieName);
    if (cookie == null) {
        YAHOO.util.Cookie.set(cookieName, "");
        cookie = "";
    }
    cookie = cookie.replace(cookieName, "");

    var cookie2 = YAHOO.util.Cookie.get(cookieValue);
    if (cookie2 == null) {
        YAHOO.util.Cookie.set(cookieValue, "");
        cookie2 = "";
    }
    cookie2 = cookie2.replace(cookieValue, "");

    var collection = cookie.split('|');
    var itemValue = itemid + "|";

    if (checkbox.checked) {
        if (collection.length == 5) {
            showAlert("Restrices to 4 items")
            checkbox.checked = false;
            return;
        } else {
            cookie += itemValue;
            var price;
            if (Dom.get("discountprice" + itemid).value != "0")
                price = Dom.get("discountprice" + itemid).value;
            else
                price = Dom.get("sellingprice" + itemid).value;

            cookie2 += itemValue
                        + Dom.get("aliasitemcode" + itemid).value + "|"
                        + Dom.get("image" + itemid).value + "|"
                        + Dom.get("producttitle" + itemid).value.replace('"', ' inch ') + "|"
                        + price + ";";
        }
    } else {
        cookie = cookie.replace(itemValue, "");
        var collvalue = cookie2.split(';');
        for (var i = 0; i < collvalue.length; i++) {
            var testCheck = collvalue[i].split('|');
            if (testCheck[0] == itemid) {
                // check for clear all
                if (i == 0)
                    collvalue[i] = "";
                else
                    collvalue[i] = ";";
                break;
            }
        }
        cookie2 = "";
        for (var i = 0; i < collvalue.length; i++) {
            if (collvalue[i] != "" && collvalue[i] != ";") {
                cookie2 += collvalue[i] + ";";
            }
        }
    }

    YAHOO.util.Cookie.set(cookieName, cookie);
    YAHOO.util.Cookie.set(cookieValue, cookie2);
    //alert(cookie);
    //alert(cookie2);
}

function compareProduct() {
    if (Sys.Application._initialized == false) { return; }
    var Dom = YAHOO.util.Dom;
    var cookie = YAHOO.util.Cookie.get(cookieName);
    var cookie2 = YAHOO.util.Cookie.get(cookieValue);
    var top_category_fordetail = top_category;

    // detail require only wathes or jewelry
    if (top_category != "watches" && top_category != "instore") {
        top_category_fordetail = "";
    }

    if (cookie != null) cookie = cookie.replace(cookieName, "");
    if (cookie2 != null) cookie2 = cookie2.replace(cookieValue, "");
    if (cookie == null || cookie == "" || cookie2 == null || cookie2 == "") {
        showAlert("please select product");
        return;
    }

    //initial to invisible
    Dom.get('cmp_compare1').style.display = 'none';
    Dom.get('cmp_compare2').style.display = 'none';
    Dom.get('cmp_compare3').style.display = 'none';
    Dom.get('cmp_compare4').style.display = 'none';

    var items = cookie2.split(";")
    var found = false;
    for (var i = 1; i <= items.length; i++) {
        var data = items[i - 1].split('|');
        if (data[0] != "" && data[0] != null) {
            Dom.get('cmp_compare' + i).style.display = '';
            Dom.get('cmp_aliasitemcode' + i).innerHTML = data[1];
            Dom.get('cmp_image' + i).src = 'ImagesLibrary/' + data[2];
            Dom.get('cmp_comparetitle' + i).innerHTML = data[3].replace('"', ' inch ');
            Dom.get('cmp_sellingprice' + i).innerHTML = addCommas(data[4]);
            if (top_category == "watches" || top_category == "instore")
                Dom.get('a' + i).href = "Detail.aspx?fc=" + top_category_fordetail + "&ProdId=" + data[0];
            else
                Dom.get('a' + i).href = "Detail.aspx?ProdId=" + data[0];
            found = true;
        }
    }

    if (found)
        Dom.get(btn_compare_id).click();
    else
        showAlert("please select product");
}

function moveToWishlish(productid, producttype) {
    YAHOO.util.Connect.asyncRequest(
            'get'
            , "AjaxStation.aspx?method=addtowishlistfrompg&productid=" + productid + '&producttype=' + producttype + '&rnd=' + Math.random()
            , {
                success: function(o) {
                    if (o.responseText == success)
                        document.location = "WishList.aspx";
                    else if (o.responseText == soldout)
                        document.location = "SoldOut.aspx";
                    else {
                        showAlert(o.responseText);
                    }
                }
                , failure: function(o) {
                    showAlert(o.responseText);
                }
            }
        );
    return false;
}

function showAlert(msg) {
    var conf = new YAHOO.widget.SimpleDialog(
                    "conf",
			         { width: "300px",
			             fixedcenter: true,
			             visible: false,
			             draggable: false,
			             modal: true,
			             close: false,
			             text: msg,
			             icon: YAHOO.widget.SimpleDialog.ICON_WARN,
			             constraintoviewport: true,
			             buttons: [{ text: "Ok", handler: function() { this.hide(); }, isDefault: true}]
			         });

    conf.setHeader("Alert");
    conf.render(document.body);
    conf.show();
}
