//helper function to create the form
function getNewSubmitForm(){
 var submitForm = document.createElement("FORM");
 document.body.appendChild(submitForm);
 submitForm.method = "POST"; 
 return submitForm;
}
//helper function to add elements to the form
function createNewFormElement(inputForm, elementName, elementValue){
 var newElement = document.createElement('span');
 newElement.innerHTML = "<input type='hidden' name='"+ elementName +"' value='"+ elementValue +"'>";
 inputForm.appendChild(newElement);
 return newElement;
}
function AddToShoppingCart(ProductID, VariantID)
{
   var submitForm = getNewSubmitForm();
   createNewFormElement(submitForm, "ProductID", ProductID);
   createNewFormElement(submitForm, "VariantID", VariantID);
   createNewFormElement(submitForm, "Quantity", "1");
   submitForm.action = "/addtocart.aspx";
   submitForm.submit();
}

jQuery(document).ready(function () {
   jQuery(".AccAddtoCartPD .AddunCheck").click(function () {jQuery(this).toggleClass("AddCheck");});
 
   //for search input box
   jQuery('#ctl00_ctrlSearch_SearchText').attr('value', 'Search our products').attr('title', 'Search our products');
   jQuery('#ctl00_ctrlSearch_SearchText').click(function(){jQuery(this).val('');});
   jQuery("a[rel^='facebox']").facebox();
  
   /*For Minicart*/
   jQuery('#dvCartTextLink a:first').click(function() {jQuery('#divTempCart').css('display','block');});
   SetMiniCartClose();
   /*For Minicart ends*/
});

var MinicartPagePostBack = false;
function SetMiniCartClose() {
    //closeminicart
    try {
        if (MinicartPagePostBack) {
            jQuery('#divTempCart').css('display', 'block');
            scroll(0, 0);
        }
    }
    catch (er) { }

    jQuery("#MiniCartCloseAnchor").click(function () {
     try {
      if ($find("ctl00_ctl05_extCollapseMinicart") != null) {
         $find("ctl00_ctl05_extCollapseMinicart").set_Collapsed(true);
      }
      else {
        $find("ctl00_ctl06_extCollapseMinicart").set_Collapsed(true);
      }
     } catch (er) { }
   });
}
function AddMultiProducts() {
    var ProductIDs = '', VariantIDs = '';
    jQuery("#relatedProductsSlider a.AddCheck").each(function () {
        ProductIDs += jQuery(this).attr("productid") + ',';
        VariantIDs += jQuery(this).attr("variantid") + ',';
    })

    if (VariantIDs != '' && ProductIDs != '') {
        PostItemsToCart(ProductIDs, VariantIDs);
    }
    else {
        alert("Please select product(s).");
    }
}
function AddProductToMiniCart(ProductID, VariantID) {
    PostItemsToCart(ProductID, VariantID);
}

function PostItemsToCart(ProductIDs, VariantIDs) {
    jQuery.ajax({ type: "POST",
        url: "addtocart.aspx",
        cache: false,
        data: { "ProductID": ProductIDs, "VariantID": VariantIDs, "AddMultiple": true },
        timeout: 15000,
        error: function (xhr, ajaxOptions, thrownError) { alert("Error is occuring while adding product"); },
        success: function (response) {
            //Update minicart and open it        
            if (typeof (aspdnsf.Controls.Minicart) != "undefined") {
                var miniCart = aspdnsf.Controls.Minicart.getInstance();
                if (miniCart) {
                    MinicartPagePostBack = true;
                    var req = Sys.WebForms.PageRequestManager.getInstance();
                    var fx = function () {
                        miniCart.show();
                        miniCart.set_suppressAutoHide(false);
                        req.remove_endRequest(fx);
                        SetMiniCartClose();
                    }
                    req.add_endRequest(fx);

                    miniCart.refresh();
                }
            }
        }
    });
}
