﻿ function ShoppingCartClientFunctions_AddProductToCart(productGuid, productTypeEnum, quantity) 
 {       
       var pnlAddingToCart = document.getElementById("ShoppingCartModal_PnlAddingItemToCart");
       if(pnlAddingToCart != null)
       {
            pnlAddingToCart.style.display = "block";       
       }
       var pnlDoneAddingToCart = document.getElementById("ShoppingCartModal_PnlDoneAddingItemToCart");
       if(pnlDoneAddingToCart != null)
       {
            pnlDoneAddingToCart.style.display = "none";       
       }
       
       ModalDialog_revealModal("modalPage");
       
       
       InksWebUI.WebServices.ShoppingCartService.AddProductToCart(productGuid, productTypeEnum, quantity, ShoppingCartClientFunctions_OnAddToCartSucceeded, ShoppingCartClientFunctions_OnAddToCartFailed);

       
       //var pnlLoadingIndicator = GetElementsByAttribute(document, "div", "name", "pnlLoadingIndicator" + productGuid)[0];
       //if(pnlLoadingIndicator != null)
       //{
            //pnlLoadingIndicator.style.visibility = "visible";
       //}
 }

 function ShoppingCartClientFunctions_OnAddToCartSucceeded(result, userContext, methodName) 
 {
       if(result != null)
       {
           this.RefreshShoppingCartDisplay(result.TotalItemsInCart, result.CartPrice);

           var pnlAddingToCart = document.getElementById("ShoppingCartModal_PnlAddingItemToCart");
           if(pnlAddingToCart != null)
           {
                pnlAddingToCart.style.display = "none";       
           }
           var pnlDoneAddingToCart = document.getElementById("ShoppingCartModal_PnlDoneAddingItemToCart");
           if(pnlDoneAddingToCart != null)
           {
                pnlDoneAddingToCart.style.display = "block";       
           }
           
           //ModalDialog_hideModal("modalPage");
            
            //this.RefreshAddToCartButtons(result.TotalItemsInCart);     
       }  
 }
 
 function ShoppingCartClientFunctions_OnAddToCartFailed(error, userContext, methodName) 
 {
       alert("add failed");
 }
 
function ModalDialog_revealModal(divID)
{
    window.onscroll = function () { document.getElementById(divID).style.top = document.body.scrollTop; };
    document.getElementById(divID).style.display = "block";
    document.getElementById(divID).style.top = document.body.scrollTop;
}

function ModalDialog_hideModal(divID)
{
    document.getElementById(divID).style.display = "none";
}
 
 
 /*
 function ShoppingCartClientFunctions_RemoveProductFromCart(productGuid) 
 {
       InksWebUI.WebServices.ShoppingCartService.RemoveProductFromCart(productGuid, ShoppingCartClientFunctions_OnRemoveFromCartSucceeded, ShoppingCartClientFunctions_OnRemoveFromCartFailed);
       
       //var pnlLoadingIndicator = document.getElementsByName("pnlLoadingIndicator" + productGuid)[0];
       var pnlLoadingIndicator = GetElementsByAttribute(document, "div", "name", "pnlLoadingIndicator" + productGuid)[0];
       if(pnlLoadingIndicator != null)
       {
            pnlLoadingIndicator.style.visibility = "visible";
       }
 }
 
 function ShoppingCartClientFunctions_OnRemoveFromCartSucceeded(result, userContext, methodName) 
 {
       if(result != null)
       {
           var pnlLoadingIndicator = GetElementsByAttribute(document, "div", "name", "pnlLoadingIndicator" +  result.ProductId)[0];
           //var pnlLoadingIndicator = document.getElementsByName("pnlLoadingIndicator" + result.ProductId)[0];
           if(pnlLoadingIndicator != null)
           {
                pnlLoadingIndicator.style.visibility = "hidden";
           }
           
           //var pnlRemoveItem = document.getElementsByName("pnlRemoveItem" + result.ProductId)[0];
           var pnlRemoveItem = GetElementsByAttribute(document, "div", "name", "pnlRemoveItem" +  result.ProductId)[0];
           if(pnlRemoveItem != null)
           {
                pnlRemoveItem.style.display = "none";
           } 
           
           this.RefreshShoppingCartDisplay(result.TotalItemsInCart, result.CartPrice);   
           this.RefreshAddToCartButtons(result.TotalItemsInCart);      
       } 
 }
 
 function ShoppingCartClientFunctions_OnRemoveFromCartFailed(error, userContext, methodName) 
 {
       alert("remove failed");
 }
 */
 
 function RefreshShoppingCartFromServer()
 {
        InksWebUI.WebServices.ShoppingCartService.GetShoppingCartStatus(ShoppingCartClientFunctions_OnRefreshShoppingCartFromServerSucceeded, ShoppingCartClientFunctions_OnRefreshShoppingCartFromServerFailed);
 } 
 
 function ShoppingCartClientFunctions_OnRefreshShoppingCartFromServerSucceeded(result, userContext, methodName)
 {
        if(result != null)
        {
            this.RefreshShoppingCartDisplay(result.TotalNumberOfItems, result.ItemsTotalPrice);
        }
 }
 
 function ShoppingCartClientFunctions_OnRefreshShoppingCartFromServerFailed(error, userContext, methodName)
 {
        //alert("refresh failed");
 }

 function RefreshShoppingCartDisplay(numberOfItems, itemsTotalPrice) 
 {
    var pnlNumberOfItems = null;
    var pnlItemsTotalPrice = null;
    var pnlShoppingCartInfo = null;
    var pnlShoppingCartLoadingIndicator = null;
    var linkCheckOut = $get("ctl00_HeaderNew_linkCheckOut");

    if ($get("ctl00_Header1_lblTotalItemsInCurrentOrder") != null)
        pnlNumberOfItems = $get("ctl00_Header1_lblTotalItemsInCurrentOrder");
    else if ($get("ctl00_HeaderNew_lblTotalItemsInCurrentOrder") != null)
        pnlNumberOfItems = $get("ctl00_HeaderNew_lblTotalItemsInCurrentOrder");
    else
        pnlNumberOfItems = $get("ctl00_HeaderCRO_lblTotalItemsInCurrentOrder");

    if ($get("ctl00_Header1_lblTotalOrderPrice") != null)
        pnlItemsTotalPrice = $get("ctl00_Header1_lblTotalOrderPrice");
    else if ($get("ctl00_HeaderNew_lblTotalOrderPrice") != null)
        pnlItemsTotalPrice = $get("ctl00_HeaderNew_lblTotalOrderPrice");
    else
        pnlItemsTotalPrice = $get("ctl00_HeaderCRO_lblTotalOrderPrice");

    if ($get("ctl00_Header1_pnlCurrentOrderInfo") != null)
        pnlShoppingCartInfo = $get("ctl00_Header1_pnlCurrentOrderInfo");
    else if ($get("ctl00_HeaderNew_pnlCurrentOrderInfo") != null)
        pnlShoppingCartInfo = $get("ctl00_HeaderNew_pnlCurrentOrderInfo");
    else
        pnlShoppingCartInfo = $get("ctl00_HeaderCRO_pnlCurrentOrderInfo");

    if ($get("ctl00_Header1_pnlShoppingCartLoadingIndicator") != null)
        pnlShoppingCartLoadingIndicator = $get("ctl00_Header1_pnlShoppingCartLoadingIndicator");
    else if ($get("ctl00_HeaderNew_pnlShoppingCartLoadingIndicator") != null)
        pnlShoppingCartLoadingIndicator = $get("ctl00_HeaderNew_pnlShoppingCartLoadingIndicator");
    else
        pnlShoppingCartLoadingIndicator = $get("ctl00_HeaderCRO_pnlShoppingCartLoadingIndicator");

    if(pnlShoppingCartLoadingIndicator != null)
    {
        pnlShoppingCartLoadingIndicator.style.display = "none";
    }
    
    if(pnlShoppingCartInfo != null)
    {
        if(numberOfItems > 0)
        {
            pnlShoppingCartInfo.style.visibility = "visible";
        }
        else
        {
            pnlShoppingCartInfo.style.visibility = "hidden";
        }
    }

    if (linkCheckOut != null) {
        if (numberOfItems > 0) {
            linkCheckOut.style.display = "block";
        }
        else {
            linkCheckOut.style.display = "none";
        }
    }
    
    if(pnlNumberOfItems != null)
    {
        if(numberOfItems > 1)
        {
            pnlNumberOfItems.innerHTML = numberOfItems + " Items";
        }
        else if(numberOfItems == 1)
        {
            pnlNumberOfItems.innerHTML = numberOfItems + " Item";
        }
    }
    
    if(pnlItemsTotalPrice != null)
    {
        pnlItemsTotalPrice.innerHTML = itemsTotalPrice;
    }
 }
 
 /*
 function RefreshAddToCartButtons(numberOfItemsInCart)
 {
    //var addToCartPanels = document.getElementsByName("pnlViewCartButton");
    var addToCartPanels = GetElementsByAttribute(document, "div", "name", "pnlViewCartButton");

    if(addToCartPanels != null && addToCartPanels.length > 0)
    {
        var display = "none";
        if(numberOfItemsInCart > 0)
        {
            display = "inline";
        }
        
        for(var i = 0; i<addToCartPanels.length; i++)
        {
            addToCartPanels[i].style.display = display;
        }
    }
    //alert("add to cart panels: " + addToCartPanels.length);
 }
 */
 
 
 
function GetElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue)
{
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++)
    {
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
        if(typeof oAttribute == "string" && oAttribute.length > 0)
        {
	        if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute)))
	        {
		        arrReturnElements.push(oCurrent);
	        }
        }
    }

    return arrReturnElements;
}

//photobook
 function ShoppingCartClientFunctions_AddProductToCart_PlatinumPhotobook(platinumProductGuid, addOnItemGuid1, addOnItemGuid2, addOnItemGuid3,
    addOnItemGuid4, addOnItemGuid5, addOnItemGuid6, addOnItemGuid7, numberOfPages, thumbnailImageName, jobId) {
     
     InksWebUI.WebServices.ShoppingCartService.AddPlatinumProductDetails(platinumProductGuid, addOnItemGuid1, addOnItemGuid2, addOnItemGuid3,
        addOnItemGuid4, addOnItemGuid5, addOnItemGuid6, addOnItemGuid7, numberOfPages, thumbnailImageName, jobId, PlatinumProductDetailsResultPhotobook);
 }

 function PlatinumProductDetailsResultPhotobook(result) {
    try {
         var pnlAddingToCart = document.getElementById("ShoppingCartModal_PnlAddingItemToCart");
         if (pnlAddingToCart != null) {
             pnlAddingToCart.style.display = "block";
         }
         var pnlDoneAddingToCart = document.getElementById("ShoppingCartModal_PnlDoneAddingItemToCart");
         if (pnlDoneAddingToCart != null) {
             pnlDoneAddingToCart.style.display = "none";
         }

         ModalDialog_revealModal("modalPage");
     }
     catch (err) { }

    if (result != null) 
    {

        //default to 1 PhotoBook
        var numberOfPhotoBook = 1;

        InksWebUI.WebServices.ShoppingCartService.AddProductToCart_Platinum(result.ProductId, result.Guid, numberOfPhotoBook, ShoppingCartClientFunctions_OnAddToCartSucceeded, ShoppingCartClientFunctions_OnAddToCartFailed);

        //window.setTimeout("window.location.href = '/viewShoppingCart.aspx'", 3000); 
    }
}

//photos
function ShoppingCartClientFunctions_AddProductToCart_PlatinumPhoto(platinumProductGuid, addOnItemGuid1, addOnItemGuid2, addOnItemGuid3,
    addOnItemGuid4, addOnItemGuid5, addOnItemGuid6, addOnItemGuid7, batchQty, thumbnailImageName, jobId) {

    InksWebUI.WebServices.ShoppingCartService.AddPlatinumProductDetails(platinumProductGuid, addOnItemGuid1, addOnItemGuid2, addOnItemGuid3,
        addOnItemGuid4, addOnItemGuid5, addOnItemGuid6, addOnItemGuid7, batchQty, thumbnailImageName, jobId, PlatinumProductDetailsResultPhoto);
}

function PlatinumProductDetailsResultPhoto(result) {
    if (result != null) 
    {
        //default to 1 PhotoPack
        var numberOfPhotoPack = 1; 

        InksWebUI.WebServices.ShoppingCartService.AddProductToCart_Platinum(result.ProductId, result.Guid, numberOfPhotoPack, ShoppingCartClientFunctions_OnAddToCartSucceeded, ShoppingCartClientFunctions_OnAddToCartFailed);
        
        //window.setTimeout("window.location.href = '/viewShoppingCart.aspx'", 3000); 
    }
}

function LoginUser(emailAddress, password, chkRememberMe, redirectUrl) {
    InksWebUI.WebServices.ShoppingCartService.LoginButtonClicked(emailAddress, password, chkRememberMe, redirectUrl, ShoppingCartClientFunctions_LoginUserSucceeded, ShoppingCartClientFunctions_OnAddToCartFailed);
}

function ShoppingCartClientFunctions_LoginUserSucceeded(result, userContext, methodName) {
    if (result != null) {
        var lblErrorMessage = $get("ctl00_ContentPlaceHolder1_loginModalDialog1_lblErrorMessage");
        if (result != "") { window.location.href = result; }
        else { lblErrorMessage.innerHTML = "Incorrect username or password." }
    }
}

function CreateNewCustomer(newCustomerFirstName, newCustomerLastName, newCustomerEmailAddress, newCustomerPassword, newCustomerPasswordVerify, redirectUrl) {
    InksWebUI.WebServices.ShoppingCartService.CreateNewCustomerButtonClicked(newCustomerFirstName, newCustomerLastName, newCustomerEmailAddress, newCustomerPassword, newCustomerPasswordVerify, redirectUrl, ShoppingCartClientFunctions_CreateNewCustomerSucceeded, ShoppingCartClientFunctions_OnAddToCartFailed);
}

function ShoppingCartClientFunctions_CreateNewCustomerSucceeded(result, userContext, methodName) {
    if (result != null) {
        var lblErrorMessage = $get("ctl00_ContentPlaceHolder1_loginModalDialog1_lblNewCustomerErrorMessages");
        if (result.toString().indexOf("###") != -1) { lblErrorMessage.innerHTML = result.toString().replace("###","") }
        else { window.location.href = result; }
    }
}

function ForgetPassword(forgetPasswordEmailAddress, redirectUrl) {
    InksWebUI.WebServices.ShoppingCartService.ForgetPasswordButtonClicked(forgetPasswordEmailAddress, redirectUrl, ShoppingCartClientFunctions_ForgetPasswordSucceeded, ShoppingCartClientFunctions_OnAddToCartFailed);
}

function ShoppingCartClientFunctions_ForgetPasswordSucceeded(result, userContext, methodName) {
    if (result != null) {
        var pnlForgotPassword = $get("ctl00_ContentPlaceHolder1_loginModalDialog1_pnlForgotPassword");
        var pnlConfirmation = $get("ctl00_ContentPlaceHolder1_loginModalDialog1_pnlConfirmation");
        var lblForgetPasswordErrorMessage = $get("ctl00_ContentPlaceHolder1_loginModalDialog1_lblForgetPasswordErrorMessage"); 
        if (result == "success") {
            pnlConfirmation.style.display = "block";
            pnlForgotPassword.style.display = "none";
        }
        else { lblForgetPasswordErrorMessage.innerHTML = result }
    }
}


function ClientFunctions_GenericForm() {
    var pnlAddingToCart = document.getElementById("GenericFormModal_PnlForm");
    if (pnlAddingToCart != null) {
        pnlAddingToCart.style.display = "block";
    }
    var pnlDoneAddingToCart = document.getElementById("GenericFormModal_PnlConfirmation");
    if (pnlDoneAddingToCart != null) {
        pnlDoneAddingToCart.style.display = "none";
    }

    ModalDialog_revealModal("modalPage");
}
