﻿var g = 
{
    Load: false,
    dt:null,
    SelectField:"",
    addData : function ()
    {
        if(!g.Load)
        {
        
        
            $('#template4BuyItNowPopUp').append
            (
                $('<div></div>')
                .addClass("top")
                .append
                (
                    $('<div></div>').addClass("logo").append($('<img />').attr({"src":"../images/ShoppingCartDiv/it_logo.gif"}))
                )
                .append
                (
                    $('<div></div>').addClass("close").append($('<img />').attr({"src":"../images/ShoppingCartDiv/cks.gif"}))
                )
            )
            .append
            (
                $('<div></div>')
                .addClass("tarea")
                .append
                (
                    $('<div></div>')
                    .addClass("def grn")
                    .append
                    (
                        $('<strong></strong>').html("You have added this item to the shopping cart")
                    )
                )
            );
            if(g.dt.Rows.length > 0)
            {
                $('#template4BuyItNowPopUp div.tarea').append
                (    
                    $('<div></div>')
                    .addClass("def")
                    .append
                    (
                         $('<div></div>')
                         .addClass("mage").append($('<img />').attr({"src": $("input[id*='hfimagepath']").val() + g.dt.Rows[g.dt.Rows.length -1]["ItemImage"],"height":"100","width":"100"}))                
                        
                    )
                    .append
                    (   
                        $('<div></div>')
                        .addClass("rytarea")
                        .append
                        (   
                            $('<div></div>')
                            .addClass("nrm1").append($('<strong></strong>').html(g.dt.Rows[g.dt.Rows.length -1]["ItemDesc"]))
                        )
                        .append
                        (   
                            $('<div></div>')
                            .addClass("nrm1").append($('<strong></strong>').html("Item Price " + g.dt.Rows[g.dt.Rows.length -1]["ItemPrice"]))
                        )
                        .append
                        (   
                            $('<div></div>')
                            .addClass("nrm1").append($('<strong></strong>').html("Item Size " + g.dt.Rows[g.dt.Rows.length -1]["Size"]))
                            .attr({"id":"itemsize"})
                        )
                    )
                )
                .append
                (  
                    $('<div></div>')
                    .addClass("hrr").append($('<img />').attr({"src":"../images/ShoppingCartDiv/sp.gif"}))
                );
                
                if(g.dt.Rows[g.dt.Rows.length -1]["Size"] =="0")
                {                    
                    $('#itemsize').hide();     
                }
                
                $.each(g.dt.Rows, function(i, Row)
                {
                    if((i+1) <g.dt.Rows.length)
                    {
                        $('#template4BuyItNowPopUp div.tarea').append
                        (   
                            
                                $('<div></div>')
                                .addClass("def")
                                .append
                                (
                                     $('<div></div>')
                                     .addClass("mage").append($('<img />').attr({"src": $("input[id*='hfimagepath']").val() + g.dt.Rows[i]["ItemImage"],"height":"100","width":"100"}))                
                                    
                                )
                                .append
                                (   
                                    $('<div></div>')
                                    .addClass("rytarea")
                                    .append
                                    (   
                                        $('<div></div>')
                                        .addClass("nrm1").append($('<strong></strong>').html(g.dt.Rows[i]["ItemDesc"]))
                                    )
                                    .append
                                    (   
                                        $('<div></div>')
                                        .addClass("nrm1").append($('<strong></strong>').html("Item Price " + g.dt.Rows[i]["ItemPrice"]))
                                    )
                                    
                                    .append
                                    (   
                                        $('<div></div>')
                                        .addClass("nrm1").append($('<strong></strong>').html("Item Size " + g.dt.Rows[i]["Size"]))
                                        .attr({"id":"itemsize"+i})
                                    )

                                )
                        )
                        .append
                        (   
                            $('<div></div>')
                            .addClass("hrr").append($('<img />').attr({"src":"../images/ShoppingCartDiv/sp.gif"}))
                        );
                        if(g.dt.Rows[i]["Size"] =="0")
                        {                            
                            $('#itemsize'+i).hide();     
                        }
                    }
                
                });
                
                $('#template4BuyItNowPopUp div.tarea').append
                (
                    $('<div></div>')
                    .addClass("bto")
                    .append
                    (
                        $('<div></div>')
                        .addClass("btns")
                        .append
                        (
                            $('<div></div>')
                            .addClass("btn btnCS").append($('<img />').attr({"src":"../images/ShoppingCartDiv/cont_sp.gif","ID":"CSDiv"}).css({"cursor":"pointer"})).click(function (){
                            $('#template4BuyItNowPopUp').hide();$(g.SelectField).show();g.SelectField = "";
                        })
                        )
                        .append
                        (
                            $('<div></div>')
                            .addClass("btn btnCN")
                            .append
                            (
                                $("<a></a>").attr({"href":"../ShoppingCart/ShoppingBag.aspx"}).append($('<img />').attr({"src":"../images/ShoppingCartDiv/check_out_now.gif"}).css({"cursor":"pointer","border":"solid 0px white"}))
                            )
                        )
                    )
                );
            }
        }
    }
}


 

$(document).ready(function () 
{
    $('#template4BuyItNowPopUp').hide();
    $("#template4btnBuyBig").click(function ()
    {
 
    // start 1
     var ItemEstimatedArrival2 = $("input[id*='hdnItemEstimatedArrival']").val();
     var OvernightShippingDays =$("input[id*='hidOvernightShippingDays']").val();
     try
     {  
        var StrOvernightShippingDays = OvernightShippingDays ;// "1"; // 
        var myRegExp = /Est. Ship/;
        var matchPos1 = "";
        var shippingdate=""; 
        
        //ring
        var trSize = document.getElementById("ctl00_ContentPlaceHolder1_trSize");
        if(trSize!=null && trSize.style.visibility=="visible")
        {         
            shippingdate = document.getElementById("ctl00_ContentPlaceHolder1_ddlSize").options[document.getElementById("ctl00_ContentPlaceHolder1_ddlSize").selectedIndex].text;
            matchPos1 = shippingdate.search(myRegExp);
            if(matchPos1 != -1)
            {     
               ItemEstimatedArrival2  = StrOvernightShippingDays ;
            } 
            else
            {
               ItemEstimatedArrival2 = $("input[id*='hdnItemEstimatedArrival']").val();
            }
        }
            
        //ddlBackingType
        var trBackingType = document.getElementById("ctl00_ContentPlaceHolder1_trBackingType");
        if(trBackingType!=null && trBackingType.style.visibility=="visible")
        {    
            shippingdate = document.getElementById("ctl00_ContentPlaceHolder1_ddlBackingType").options[document.getElementById("ctl00_ContentPlaceHolder1_ddlBackingType").selectedIndex].text;
            matchPos1 = shippingdate.search(myRegExp);
            if(matchPos1 != -1)
            {     
               ItemEstimatedArrival2  = StrOvernightShippingDays ;
            } 
            else
            {
               ItemEstimatedArrival2 = $("input[id*='hdnItemEstimatedArrival']").val();
            }
        } 
        
        // other item
        var trlblstockaws = document.getElementById("ctl00_ContentPlaceHolder1_lblstockaws");
        if(trlblstockaws!=null && trlblstockaws.style.visibility=="visible")
        {    
            shippingdate = document.getElementById("instockshipping").innerHTML;
            myRegExp = /Overnight Shipping/;
            matchPos1 = shippingdate.search(myRegExp);
            if(matchPos1 != -1)
            {     
               ItemEstimatedArrival2  = StrOvernightShippingDays ;
            } 
            else
            {
               ItemEstimatedArrival2 = $("input[id*='hdnItemEstimatedArrival']").val();
            }
        }                   
     }
      catch(e){}

     // end 1
     
        var itemID = $("span[id*='lblItemID']").html();
        var ItemName = $("span[id*='lblItemTitle']").html();
        //var itemDesc = $("span[id*='lblItemDesc']").html();
        var itemDesc = $("span[id*='lblItemMainTitle']").html();
        
        
        
        var itemPrice = $("span[id*='lblTotalPrice']").html();
        var IsFeatured = $("input[id*='hdnIsFeatured']").val();
        var ItemEstimatedArrival = $("input[id*='hdnItemEstimatedArrival']").val();
        
        
        var CategoryID = $("input[id*='hdnCategoryID']").val();
        var TrackID = $("input[id*='hdnTrackID']").val();
        var ItemImage = $("input[id*='hdnItemImage']").val();
        var Color = $("input[id*='hdnColor']").val();
        var ItemType= $("input[id*='hdnItemType']").val();
        var IsAvailable= $("input[id*='hdnIsAvailable']").val();
        var VendorID = $("input[id*='hdnVendorID']").val();
        var isGiftWrap= $("input[id*='hdnisGiftWrap']").val();
        var isGiftMessage= $("input[id*='hdnisGiftMessage']").val();
        var ShippingCharges= $("input[id*='hdnShippingCharges']").val();
        
        var itemSize="0";
        var itemSize =$("OPTION:selected", $("select[id*='ddlSize']")).val();
        /*var itemSize_store =$("OPTION:selected", $("select[id*='ddlSize']")).val();
        try
        {
            if (itemSize_store.indexOf("Est. Ship")!="-1")
            {
                itemSize = itemSize_store.substring(0,itemSize_store.indexOf("-")-1);
            }
            else
            {
              itemSize =itemSize_store;
            }
        }
        catch(e){}
        */
        
        var itemSize
        var ITBackingPrice = "0";
        var ITBackingType = "";
        //alert(ItemEstimatedArrival);
        if(itemSize != null)
        {
            if(itemSize == "-Select Size-")
            {
                PopMessageBox.title = "Item Size";
                PopMessageBox.message = "Please select item size first";
                PopMessageBox.ShowMessage();
                return false;
            }
        }
        else
        {
            itemSize = "0";
        }
        //alert(itemSize);
        var trBackingType = document.getElementById("ctl00_ContentPlaceHolder1_trBackingType");
        if(trBackingType!=null && trBackingType.style.visibility=="visible")
        {      
            var Index = document.getElementById("ctl00_ContentPlaceHolder1_ddlBackingType").selectedIndex;
            ITBackingPrice = document.getElementById("ctl00_ContentPlaceHolder1_ddlBackingType").options[Index].value;
            ITBackingType = document.getElementById("ctl00_ContentPlaceHolder1_ddlBackingType").options[Index].text;
            if(Index == 0)
            {
                PopMessageBox.title = "Backing Type";
                PopMessageBox.message = "Please select backing type";
                PopMessageBox.ShowMessage();
                return false;
            }
        }
        var IsEngrave = document.getElementById("chkEngrave").checked;
        var EngravePrice = document.getElementById("ctl00_ContentPlaceHolder1_lblEngravePrice").innerHTML;
        EngravePrice = EngravePrice.replace("$","");
        var InitialMonogram = "";
        if(document.getElementById("rbItInfoEngInitial").checked) 
        InitialMonogram = "Initial";
        else
        InitialMonogram = "Monogram";
        var EngravingFont = "";
        if(document.getElementById("rbItInfoEngraveRoman").checked)
        EngravingFont = "Roman";
        if(document.getElementById("rbItInfoEngraveScript").checked)
        EngravingFont = "Script";
        if(document.getElementById("rbItInfoEngravBlock").checked)
        EngravingFont = "Block";
        var Initials = document.getElementById("txtItInfoEngInitialFirst").value + document.getElementById("txtItInfoEngInitialMiddle").value + document.getElementById("txtItInfoEngInitialLast").value;
        if(Initials.length < 3 && IsEngrave == true)
        {
            PopMessageBox.title = "Initials are required";
            PopMessageBox.message = "Please enter all the three initials";
            PopMessageBox.ShowMessage();
            return false;
        }
        
        //alert(ITBackingPrice +"/"+ ITBackingType +"/"+ IsEngrave +"/"+ EngravePrice +"/"+ InitialMonogram +"/"+ EngravingFont +"/"+ Initials);
        Templates_Template4.addItem(parseInt(itemID),itemSize,parseFloat(itemPrice),itemDesc,ItemName,IsFeatured,CategoryID,TrackID,ItemImage,VendorID,Color,ItemType,IsAvailable,isGiftWrap,isGiftMessage,parseFloat(ITBackingPrice),ITBackingType,IsEngrave,parseFloat(EngravePrice),InitialMonogram,EngravingFont,Initials,ItemEstimatedArrival,ItemEstimatedArrival2,CheckAddItem);
        //
    })
});


$(document).ready(function () 
{
    $("#CSDiv").click(function (){
        alert("Hello");
            $('#CSDiv').hide();
    })
});
            
function CheckAddItem(response)
{
    if(response.value == "true")
    {
            PopMessageBox.title = "Buy Now";
            PopMessageBox.message = "Product is already added to shopping cart";
            PopMessageBox.ShowMessage();
            return;
    }
    else
    {
        $("span[id*='lblShopCartCount']").html(parseInt(parseInt($("span[id*='lblShopCartCount']").html()) + 1));
    }
    Templates_Template4.ItemTable(ShowAddItem);
}

function ShowAddItem(response)
{
    var dt = response.value;
    g.dt = dt;
    $('#template4BuyItNowPopUp').html("");
    g.addData();
    var TopLeft = $.fn.popCenter($("#template4BuyItNowPopUp").width(),$("#template4BuyItNowPopUp").height());
     var pHeight = parseInt($("#template4BuyItNowPopUp").height());
    $.each($("select"), function(i, sel)
    {   
        var offset = $(sel).offset();                
        if(
            (
            parseInt(offset.top)>=parseInt(TopLeft[0]) && 
            parseInt(offset.top)<=(parseInt(TopLeft[0])+pHeight) && 
            parseInt(offset.left)>=parseInt(TopLeft[1]) &&
            parseInt(offset.left)<=(parseInt(TopLeft[1]) +parseInt($("#template4BuyItNowPopUp").width()))
            )
            ||
            (
            (parseInt(offset.top)+parseInt($(sel).height()))>=parseInt(TopLeft[0]) && 
            (parseInt(offset.top)+parseInt($(sel).height()))<=(parseInt(TopLeft[0])+pHeight) && 
            (parseInt(offset.left)+parseInt($(sel).width()))>=parseInt(TopLeft[1]) &&
            (parseInt(offset.left)+parseInt($(sel).width()))<=(parseInt(TopLeft[1]) +parseInt($("#template4BuyItNowPopUp").width()))
            )
        )
        {
            $(sel).hide(); // alert(+"\n"+$(sel).height());
            g.SelectField += (g.SelectField=="")?("select[id='"+$(sel).attr("id")+"']"):(",select[id='"+$(sel).attr("id")+"']");
        }
    });
    $('#template4BuyItNowPopUp').show("slow");
    
    $('#template4BuyItNowPopUp div.top div.close').css({"cursor":"pointer"}).click(function(){
        
        $("#template4BuyItNowPopUp").hide("slow");
        $("#template4BuyItNowPopUp").html("");
        $(g.SelectField).show();g.SelectField = "";
    });
    
    $(document).bind('keypress', function(e)
    { 
        var code = (e.keyCode ? e.keyCode : e.which);
        if(code == 13) 
        {
            $(g.SelectField).show();g.SelectField = "";
            $(document).unbind( "keypress" );
        }
        
        if(code == 27)
        {
            $(g.SelectField).show();g.SelectField = "";
            $("#template4BuyItNowPopUp").hide("slow");
            $("#template4BuyItNowPopUp").html("");
            $(document).unbind( "keypress" );
        }
    });
}
