// Global Variables
//var ajaxServerUrl = "http://localhost:9814/SwissOutPost/site/SwissOutPostAJAXServer.aspx";
var ajaxServerUrl = "http://www.swissoutpost.com/site/SwissOutPostAJAXServer.aspx";
var vItemGUId = '';
// Called when the COLOR DDL is changed
function selectColor(oDDLColor) {
    var vDDLSizeValue = '';
    var oDDLSize = $('#' + g_IDs.SizeDDLId)[0];
    var oDDLColor = $('#' + g_IDs.ColorDDLId)[0];
    var oItemRegPrice = $('#' + g_IDs.RetailPrice)[0];
    var oItemSalePrice = $('#' + g_IDs.Price)[0];
    var oYouSavePrice = $('#' + g_IDs.YouSavePrice)[0];
    var oItemCode = $('#' + g_IDs.ItemCode)[0];
    var pTrAddToCart = $('#' + g_IDs.trAddToCart)[0];
    var oMainItemCode = $('#' + g_IDs.MainItemCode)[0];
    var oMainImage = $('#' + g_MainImage.MainImage)[0];
    var oMainImageUrl = $('#' + g_MainImage.MainImageUrl)[0];
    var oMainImgHref = $('#' + g_MainImage.MainImgHref)[0];
    var oLinkMain = $('#' + g_MainImage.LinkMain)[0];
    var oItemTitle = $('#' + g_IDs.ItemTitle)[0];
    var oMainItemTitle = $('#' + g_IDs.MainItemTitle)[0];
    var oWishList = $('#' + g_IDs.WishList)[0];

    SetInnerText(oItemCode, oMainItemCode.value);
    SetInnerText(oItemTitle, oMainItemTitle.value);
    SetInnerText(oItemRegPrice, "");
    SetInnerText(oItemSalePrice, "");
    SetInnerText(oYouSavePrice, "");

    if (oDDLColor.selectedIndex == 0) {
        alert('Please select Color');
        pTrAddToCart.style.display = "none";
        oWishList.style.display = "none";
        
        if (oMainImage != undefined && oMainImage != null && oMainImageUrl != undefined && oMainImageUrl != null) {
            oMainImage.src = oMainImageUrl.value;
        }
        if (oLinkMain != undefined && oLinkMain != null && oMainImgHref != undefined && oMainImgHref != null) {
            oLinkMain.href = oMainImgHref.value;
        }
        return false;
    }
    pTrAddToCart.style.display = "block";
    oWishList.style.display = "block";
    var reqXml = '<reqXml Id="2" item_guid="' + g_IDs.ItemGUId + '"><variation name="Color" value="'
            + GetInnerText(oDDLColor.options[oDDLColor.selectedIndex]) + '"';

    if (oDDLSize != null && oDDLSize != undefined && oDDLSize.options.length > 0 && oDDLSize.selectedIndex > 0)
        vDDLSizeValue = GetInnerText(oDDLSize.options[oDDLSize.selectedIndex]);
    else {
        if (oDDLSize != null && oDDLSize != undefined) {
            alert("Please select Size");
            return false;
        }
    }    
    if (vDDLSizeValue != '') {
        reqXml = reqXml + ' size_value = "'
        + vDDLSizeValue + '"';
    }
    else
        reqXml = reqXml + ' size_value = ""';
        
    reqXml = reqXml + '></variation></reqXml>';
    
    $.ajax({
        url: ajaxServerUrl
        , type: "POST"
        , contentType: "text/xml"
        , processData: false
        , data: reqXml
        , dataType: "xml"
        , success: function(response) {
            populateItemOptions(response.documentElement);
        }
    });
}
// Called when the SIZE DDL is changed
function selectSize(oDDLSize, pItemColor) {
    var oDDLSize = $('#' + g_IDs.SizeDDLId)[0];
    var oDDLColor = $('#' + g_IDs.ColorDDLId)[0];
    var oItemRegPrice = $('#' + g_IDs.RetailPrice)[0];
    var oItemSalePrice = $('#' + g_IDs.Price)[0];
    var oYouSavePrice = $('#' + g_IDs.YouSavePrice)[0];
    var oItemCode = $('#' + g_IDs.ItemCode)[0];
    var pTrAddToCart = $('#' + g_IDs.trAddToCart)[0];
    var oMainItemCode = $('#' + g_IDs.MainItemCode)[0];
    var oMainImage = $('#' + g_MainImage.MainImage)[0];
    var oMainImageUrl = $('#' + g_MainImage.MainImageUrl)[0];
    var oMainImgHref = $('#' + g_MainImage.MainImgHref)[0];
    var oLinkMain = $('#' + g_MainImage.LinkMain)[0];
    var oItemTitle = $('#' + g_IDs.ItemTitle)[0];
    var oMainItemTitle = $('#' + g_IDs.MainItemTitle)[0];
    var oWishList = $('#' + g_IDs.WishList)[0];

    SetInnerText(oItemCode, oMainItemCode.value);
    SetInnerText(oItemTitle, oMainItemTitle.value);
    SetInnerText(oItemRegPrice, "");
    SetInnerText(oItemSalePrice, "");
    SetInnerText(oYouSavePrice, "");

    var reqXml = '<reqXml Id="3" item_guid="' + g_IDs.ItemGUId + '"><variation name="Size" value="'
            + GetInnerText(oDDLSize.options[oDDLSize.selectedIndex]) + '"';
        
    reqXml = reqXml + '></variation></reqXml>'

    if (oDDLSize.selectedIndex == 0) {
        alert('Please select Size');

        // Clearing the COLOR DDL if any
        if (oDDLColor != null && oDDLColor != undefined) {
            for (var count = oDDLColor.options.length - 1; count > -1; count--) {
                oDDLColor.options[count] = null;
            }
            ddlValue = "0";
            ddlText = " -- Select -- ";
            optionItem = new Option(ddlText, ddlValue, false, false);
            oDDLColor.options[oDDLColor.length] = optionItem;
        }

        SetInnerText(oItemCode, oMainItemCode.value);
        SetInnerText(oItemTitle, oMainItemTitle.value);
        pTrAddToCart.style.display = "none";
        oWishList.style.display = "none";
        SetInnerText(oItemRegPrice, "");
        SetInnerText(oItemSalePrice, "");
        SetInnerText(oYouSavePrice, "");

        if (oMainImage != undefined && oMainImage != null && oMainImageUrl != undefined && oMainImageUrl != null) {
            oMainImage.src = oMainImageUrl.value;
        }
        if (oLinkMain != undefined && oLinkMain != null && oMainImgHref != undefined && oMainImgHref != null) {
            oLinkMain.href = oMainImgHref.value;
        }
        
        return false;
    }
    pTrAddToCart.style.display = "none";

    $.ajax({
        url: ajaxServerUrl
        , type: "POST"
        , contentType: "text/xml"
        , processData: false
        , data: reqXml
        , dataType: "xml"
        , success: function(response) {
            //Loading Color DropDown
            var xmlResponse = response.documentElement;
            var iRecordCount = parseInt(xmlResponse.getAttribute("record_count"));
            if (iRecordCount > 0) {
                var loadDDL = xmlResponse.getAttribute("loadDDL");
                if (loadDDL == "COLOR") {
                    var oDDLColor = $('#' + g_IDs.ColorDDLId)[0];
                    if (oDDLColor != null && oDDLColor != undefined) {
                        //ClearAndPopulateDDL(xmlResponse, oDDLSize, "Sizes");
                        if (pItemColor != null)
                            BindDDLFromXml(xmlResponse, "Colors", oDDLColor.id, "textField", "valueField", pItemColor);
                        else {
                            ClearAndPopulateDDL(xmlResponse, oDDLColor, "Colors");
                        }
                    }
                    else {
                        return populateItemOptions(xmlResponse);
                    }
                }
                else {
                    return populateItemOptions(xmlResponse);
                }
            }
        }
    });
}
//Called when the Variation images are clicked
function clickVariationImage(oImage, pItemCode, pItemColor, pItemSize) {
    var oWishList = $('#' + g_IDs.WishList)[0];
    var reqXml = '<reqXml Id="4" item_guid="' + g_IDs.ItemGUId + '"><variation name="VarItemCode" value="' + pItemCode + '"';
    reqXml = reqXml + '></variation></reqXml>'

    var oDDLSize = $('#' + g_IDs.SizeDDLId)[0];
    var oDDLColor = $('#' + g_IDs.ColorDDLId)[0];
    var pTrAddToCart = $('#' + g_IDs.trAddToCart)[0];
    oDDLSize.value = pItemSize;
    selectSize(oDDLSize, pItemColor);
    pTrAddToCart.style.display = "block";
    oWishList.style.display = "block";

    $.ajax({
        url: ajaxServerUrl
        , type: "POST"
        , contentType: "text/xml"
        , processData: false
        , data: reqXml
        , dataType: "xml"
        , success: function(response) {
            populateItemOptions(response.documentElement);
        }
    });
}
//Load items into the specified dropdownlist from XML Reponse
function BindDDLFromXml(xmlResponse, xmlKey, ddlId, textField, valueField, defaultSelectedValue) {
    var options = [];
    //options.push('<option value="">' + '-- Select --' + '</option>');
    var ddlNodes = xmlResponse.getElementsByTagName(xmlKey);
    var ddlValue, ddlText;
    var optionItem, optionNode;
    for (var iCounter = 0; iCounter < ddlNodes[0].childNodes.length; iCounter++) {
        optionNode = ddlNodes[0].childNodes[iCounter];
        var optValue = optionNode.getAttribute(valueField);
        var optText = optionNode.getAttribute(textField);
        if (defaultSelectedValue != null && optValue == defaultSelectedValue)
            options.push('<option SELECTED=SELECTED value="' + optValue + '" title="' + optText + '">' + optText + '</option>');
        else
            options.push('<option value="' + optValue + '" title="' + optText + '">' + optText + '</option>');
    }
    $('#' + ddlId).html(options.join(''));
}
function populateItemOptions(resXmlRoot) {
    var oHdnItemGUId = $('#' + g_IDs.CartItemGUId)[0];
    var oItemRegPrice = $('#' + g_IDs.RetailPrice)[0];
    var oItemSalePrice = $('#' + g_IDs.Price)[0];
    var oYouSavePrice = $('#' + g_IDs.YouSavePrice)[0];
    var oItemCode = $('#' + g_IDs.ItemCode)[0];
    var nodeItemData = resXmlRoot.firstChild;
    var oMainImage = $('#' + g_MainImage.MainImage)[0];
    var oMainImageUrl = $('#' + g_MainImage.MainImageUrl)[0];
    var olnkEnlarge = $('#' + g_MainImage.lnkEnlarge)[0];
    var oMainImgHref = $('#' + g_MainImage.MainImgHref)[0];
    var oLinkMain = $('#' + g_MainImage.LinkMain)[0];
    var oItemTitle = $('#' + g_IDs.ItemTitle)[0];
    var oMainItemTitle = $('#' + g_IDs.MainItemTitle)[0];

    if (nodeItemData != null && nodeItemData != undefined) {
        var itemGuid = nodeItemData.getAttribute("item_guid");
        var regPrice = nodeItemData.getAttribute("item_reg_price");
        var salePrice = nodeItemData.getAttribute("item_sale_price");
        var youSavePrice = nodeItemData.getAttribute("you_save_price");
        var youSavePer = nodeItemData.getAttribute("you_save_percentage");
        var itemCode = nodeItemData.getAttribute("item_cd");
        var imageUrl = nodeItemData.getAttribute("item_image_url");
        var enlargeImgUrl = nodeItemData.getAttribute("item_enlarge_image_url");
        var itemTitle = nodeItemData.getAttribute("item_description_title");

        if (itemGuid != null && itemGuid != undefined)
            oHdnItemGUId.value = itemGuid;
        SetInnerText(oItemRegPrice, regPrice);
        SetInnerText(oItemSalePrice, salePrice);
        SetInnerText(oItemCode, itemCode);

        if (itemTitle != null && itemTitle != "")
            SetInnerText(oItemTitle, itemTitle);
        else
            SetInnerText(oItemTitle, oMainItemTitle.value);
            
        if (youSavePrice != null && youSavePrice != undefined && youSavePer != null && youSavePer != undefined)
            SetInnerText(oYouSavePrice, youSavePrice + ' (' + youSavePer + '%)');

        if (oMainImage != undefined && oMainImage != null) {
            if (imageUrl != null && imageUrl != undefined && imageUrl != "")
                oMainImage.src = imageUrl;
            else
                oMainImage.src = oMainImageUrl.value;
        }

        if (oLinkMain != undefined && oLinkMain != null) {
            if (enlargeImgUrl != null && enlargeImgUrl != undefined && enlargeImgUrl != "")
                oLinkMain.href = enlargeImgUrl;
            else
                oLinkMain.href = oMainImgHref.value;
        }        
        
        if (olnkEnlarge != undefined && olnkEnlarge != null) {
            if (enlargeImgUrl != null && enlargeImgUrl != undefined && enlargeImgUrl != ""){
                olnkEnlarge.href = "http://www.swissoutpost.com/site/EnlargeImage.aspx?item_guid=" + itemGuid;
            }
        }
    }
    return true;
}
function ClearAndPopulateDDL(resXmlRoot, pObjDDL, pNodeName) {
    if (pObjDDL != null && pObjDDL != undefined) {
        for (var count = pObjDDL.options.length - 1; count > -1; count--) {
            pObjDDL.options[count] = null;
        }
        var ddlNodes = resXmlRoot.getElementsByTagName(pNodeName);
        var ddlValue, ddlText;
        var optionItem, optionNode;
        for (var iCounter = 0; iCounter < ddlNodes[0].childNodes.length; iCounter++) {
            optionNode = ddlNodes[0].childNodes[iCounter];
            ddlValue = optionNode.getAttribute("valueField");
            ddlText = optionNode.getAttribute("textField");
            optionItem = new Option(ddlText, ddlValue, false, false);
            pObjDDL.options[pObjDDL.length] = optionItem;
        }
        return true;
    }
    return false;
}
//Returns the node text value
function SetInnerText(node, text) {
    //alert("Node --> " + node);
    //alert("Text --> " + text);
    if (node != null && node != undefined && text != null && text != undefined) {
        if (node.textContent != null && node.textContent != undefined)
            node.textContent = text;
        if (node.innerText != null && node.innerText != undefined)
            node.innerText = text;
        if (node.text != null && node.text != undefined)
            node.text = text;
    }
}
//Assigns the node text content
function GetInnerText(node) {
    return (node.textContent || node.innerText || node.text);
}

