Type.registerNamespace("Mccp.Basket");
Mccp.Basket.AddProduct = function(){
	Mccp.Basket.AddProduct.initializeBase(this);
	this.ParentProductId;
	this.PSP = "";
	this.PSPTermAccepted = false;
	this.Accessories = [];
	this.DetourToWarranty = false;
	this.BundlePSP = [];
}
Mccp.Basket.AddProduct.prototype = {
    initialize: function() {
        Mccp.Basket.AddProduct.callBaseMethod(this, "initialize");
    },
    get_ParentProductId: function() {
        return this.ParentProductId;
    },
    set_ParentProductId: function(value) {
        this.ParentProductId = value;
    },
    get_PSP: function() {
        return this.PSP;
    },
    set_PSP: function(value) {
        this.PSP = value;
    },
    get_PSPTermAccepted: function() {
        return this.PSPTermAccepted;
    },
    set_PSPTermAccepted: function(value) {
        this.PSPTermAccepted = value;
    },
    GenericUpdateAccessories: function(ProductId, selection) {
        if (selection == true)
            this.addAccessories(ProductId);
        else
            this.removeAccessories(ProductId);
    },
    UpdateWarrantyDetour: function(CheckboxControl) {
        this.DetourToWarranty = CheckboxControl.checked;
    },
    UpdateAccessories: function(CheckboxControl) {
        var productId = CheckboxControl.getAttribute("ProductID");
        var selection = CheckboxControl.checked;
        this.GenericUpdateAccessories(productId, selection);
    },
    addAccessories: function(accessories) {
        Array.add(this.Accessories, accessories);
    },
    removeAccessories: function(accessories) {
        Array.remove(this.Accessories, accessories);
    },
    get_Accessories: function() {
        return this.Accessories;
    },
    get_BundlePSP: function() {
        return this.BundlePSP;
    },
    addAtBundlePSP: function(index, aPSP) {
        this.BundlePSP[index] = aPSP;
        if (this.BundlePSP.length > 0) {
            this.PSP = "";
            for (var iPSP = 0; iPSP < this.BundlePSP.length; iPSP++) {
                bPSP = this.BundlePSP[iPSP];
                if (bPSP == "" || bPSP == null)
                    this.PSP = this.PSP.concat(",");
                else
                    this.PSP = this.PSP.concat(bPSP + ",");
            }
            if (this.PSP.charAt(this.PSP.length - 1) == ",")
                this.PSP = this.PSP.substr(0, this.PSP.length - 1)
        }
    },
    AddProductWithoutWarranty: function(InsertRootProduct, PCName) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + 1);
        document.cookie = "AddToCartSet=true;expires=" + exdate.toGMTString() + ";path=/;";
        document.cookie = "PreviousShoppingUrl=" + document.URL + ";expires=" + exdate.toGMTString() + ";path=/;";
        var queryString = "";
        if (PCName != "") {
            queryString += "&pcname=" + encodeURIComponent(PCName);
        }
        $redirect("/Order/AddToCart.ashx?SKU=" + encodeURIComponent(this.ParentProductId) + "&AddParentSku=" + encodeURIComponent(InsertRootProduct) + queryString);
        return false;
    },
    AddSelectedAccessoriesAndWarranty: function(InsertRootProduct, alertMsg, warrantyMsg, PCName, targetWindow) {
        if (!InsertRootProduct) {
            if ((this.Accessories == null || this.Accessories == "") && (this.PSP == null || this.PSP == "")) {
                DisplayTermsNotAcceptedMessage(alertMsg);
                return false;
            }
            if (this.PSP != null && this.PSP == "0") {
                this.redirectToBasket(this.ParentProductId, this.Accessories, InsertRootProduct, PCName, targetWindow)
            }
            else if (this.PSP != null && this.PSP != "" && !this.PSPTermAccepted) {
                DisplayTermsNotAcceptedMessage(warrantyMsg);
                return false;
            }
            if (this.PSP != null && this.PSP != "" && this.PSPTermAccepted)
                this.redirectToBasket(this.ParentProductId, this.Accessories, InsertRootProduct, PCName, targetWindow)
        }
        else {
            if ((this.Accessories == null || this.Accessories == "") && (this.PSP == null || this.PSP == "")) {
                DisplayTermsNotAcceptedMessage(alertMsg);
                return false;
            }
            if (this.PSP != null && this.PSP != "") {
                if ((this.PSP == "0") || this.PSPTermAccepted)
                    this.redirectToBasket(this.ParentProductId, this.Accessories, InsertRootProduct, PCName, targetWindow)
                else {
                    if (!(typeof DisplayTermsNotAcceptedMessage == 'function')) {
                        alert(warrantyMsg);
                    }
                    else {
                        DisplayTermsNotAcceptedMessage(warrantyMsg);
                    }
                    return false;
                }
            }
            else if (this.Accessories != null && this.Accessories != "") {
                this.redirectToBasket(this.ParentProductId, this.Accessories, InsertRootProduct, PCName)
            }
        }
    },
    ValidateAccessoriesAndWarranty: function(sender, args) {
        if ((this.Accessories == null || this.Accessories == "" || this.Accessories == "0") && (this.PSP == null || this.PSP == "" || this.PSP == "0")) {
            args.IsValid = false;
            return;
        }
    },
    ValidateWarranty: function(sender, args) {
        if (this.PSP != null && this.PSP != "" && this.PSP != "0" && !this.PSPTermAccepted) {
            args.IsValid = false;
            return;
        }
    },
    WarrantyPage_ValidateWarranty: function(sender, args) {
        basket.ValidateWarranty(sender, args);
        ManageAllAccSummary(sender.validationGroup, args.IsValid);
    },
    redirectToBasketWithDefault: function(InsertRootProduct, PCName) {
        this.redirectToBasket(this.ParentProductId, this.Accessories, InsertRootProduct, PCName)
    },
    redirectToBasket: function(paramProductID, paramAccessories, InsertRootProduct, PCName, targetWindow) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + 1);
        document.cookie = "AddToCartSet=true;expires=" + exdate.toGMTString() + ";path=/";
        document.cookie = "PreviousShoppingUrl=" + document.URL + ";expires=" + exdate.toGMTString() + ";path=/;";
        var queryStringParameters = "";
        if (paramAccessories != "") {
            queryStringParameters += "&Accessories=" + encodeURIComponent(paramAccessories)
        }
        if (this.PSP != "" && this.PSP != "0") {
            queryStringParameters += "&PSP=" + encodeURIComponent(this.PSP)
        }
        if (PCName != null && PCName != "") {
            queryStringParameters += "&pcname=" + encodeURIComponent(PCName)
        }
        var url;
        if (this.DetourToWarranty && this.PSP == "0") {
            var url = "/Catalog/Warranty.aspx?SKU=" + encodeURIComponent(paramProductID) + "&WarrantyMode=false" + queryStringParameters;
        }
        else {
            url = "/Order/AddToCart.ashx?SKU=" + encodeURIComponent(paramProductID) + "&AddParentSku=" + encodeURIComponent(InsertRootProduct) + queryStringParameters;
        }
        if (typeof (targetWindow) != 'undefined') {
            targetWindow.$redirect(url);
        }
        else {
            window.location.href = url;
        }
        return false;
    },
    redirectToWishList: function(paramProductID, PCName, isAuthenticated) {
        //var exdate = new Date();
        var wishListPath = "/Order/OrderWishList.aspx"
        //exdate.setDate(exdate.getDate() + 1);
        //document.cookie = "AddToWishlist=true;expires=" + exdate.toGMTString() + ";path=/";
        var queryString = "";
        if (PCName != null && PCName != "") {
            queryString += "&pcname=" + encodeURIComponent(PCName);
        }
        if (window.location.search.indexOf("WLParentURL") < 0) {
            queryString += "&WLParentURL=";
            queryString += encodeURIComponent(window.location.pathname);
        }

        var redirectPath = "";
        redirectPath = wishListPath + "?SKU=" + encodeURIComponent(paramProductID) + queryString;
/* Defect 184951
        if (isAuthenticated == "True")
        redirectPath = wishListPath + "?SKU=" + encodeURIComponent(paramProductID) + queryString;
        else
        redirectPath = "/Profile/SignIn.aspx?ReturnUrl=" + encodeURIComponent(wishListPath + "?SKU=" + encodeURIComponent(paramProductID) + queryString );
*/
        $redirect(redirectPath);
        return false;
    },

    redirectToPriceWatchList: function(paramProductID, PCName, isAuthenticated) {
        var exdate = new Date();
        var priceWatcherPath = "/Marketing/PriceWatcher.aspx";
//        exdate.setDate(exdate.getDate() + 1);
//        document.cookie = "AddToPriceWatchlist=true;expires=" + exdate.toGMTString() + ";path=/";
        var queryString = "";
        if (PCName != null && PCName != "") {
            queryString += "&pcname=" + encodeURIComponent(PCName)
        }
        if (window.location.search.indexOf("PWParentURL") < 0) {
            queryString += "&PWParentURL=";
            queryString += encodeURIComponent(window.location.pathname);
        }

        var redirectPath = "";
        redirectPath = priceWatcherPath + "?SKU=" + encodeURIComponent(paramProductID) + queryString;
/* Defect 184951
        if (isAuthenticated == "True")
        redirectPath = priceWatcherPath + "?SKU=" + encodeURIComponent(paramProductID) + queryString;
        else
        redirectPath = "/Profile/SignIn.aspx?ReturnUrl=" + encodeURIComponent( priceWatcherPath + "?SKU=" + encodeURIComponent(paramProductID) + queryString );
*/
        $redirect(redirectPath);
        return false;
    },
    dispose: function() {
        this.ParentProductId = null;
        this.PSP = null;
        this.PSPTermAccepted = null;
        this.Accessories = null;
        this.DefaultToWarranty = null;
        Mccp.Basket.AddProduct.callBaseMethod(this, "dispose");
    }
}

Mccp.Basket.AddProduct.registerClass("Mccp.Basket.AddProduct", Sys.Component, Sys.IDisposable);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

function AddToCartButton(ButtonClicked){
	var productId = ButtonClicked.getAttribute("ProductID");
	var InAccessoriesMode = ButtonClicked.getAttribute("InAccessoriesMode");
	var PCName = ButtonClicked.getAttribute("PCName");
	if (productId != ""){
		if (InAccessoriesMode=='True'){
			basket.addAccessories(productId);
			basket.redirectToBasket(basket.get_ParentProductId(),productId, true,PCName, this.window);
		}
		else
		{
			basket.redirectToBasket(productId, basket.get_Accessories(), true,PCName, this.window);
		}
	}
}
function AddToWishListButton(ButtonClicked) {

	var productId = ButtonClicked.getAttribute("ProductID");
	var PCName = ButtonClicked.getAttribute("PCName");
	var isAuthenticated = ButtonClicked.getAttribute("AuthenticatedUser");
	if (productId != "") {
	    basket.redirectToWishList(productId, PCName, isAuthenticated);
	}
	return false;
}
function AddToPriceWatchListButton(ButtonClicked) {

	var productId = ButtonClicked.getAttribute("ProductID");
	var PCName = ButtonClicked.getAttribute("PCName");
	var isAuthenticated = ButtonClicked.getAttribute("AuthenticatedUser");
	if (productId != "") {
	    basket.redirectToPriceWatchList(productId, PCName, isAuthenticated);
	}
	return false;
}
function AddPSPToCart(ButtonClicked){
	var productId = ButtonClicked.getAttribute("warrantyId");
	if (productId != ""){
		return basket.set_PSP(productId)
	}
}
function getCookie(c_name){
	if (document.cookie.length>0){ 
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}
function GetPWItem(){		    
    UpdatePriceWatch("ctl00_uchead_PWC");
}
function showMoreCards(link, clientId){
	$get(clientId).style.display = 'block';
	link.onclick = function(){ return false;}
	link.style.cursor = 'default';
	link.style.color = '#CCC';
}

function ShowProcessingPageAfterValidate(contentControl, processingControl, sender) {
    if (window.postBackSource == null && sender != null) {
        window.postBackSource = sender;
    }

    ValidtionReset();
    var isValid = Page_ClientValidate('') && Page_ClientValidate('GiftCard');
    if (!isValid) {
        window.postBackSource = null;
        return false;
    }
    else {
        ShowProcessingPage(contentControl, processingControl);
        return true;
    }
}

function ShowProcessingPage(contentControl, processingControl){ 
	obj = $get(contentControl);
	if (obj != null)
		obj.style.display = 'none';
	obj = $get(processingControl);
	if(obj!=null){
		//IE Bug: Added a timestamp at the end of the url to reload the image and don't take it from the cache
		var img=$getElementsByClassName('processing-image','img',$get(processingControl))[0];
		img.src=img.src+'?'+new Date().getTime();
		obj.style.display='block';
		img.style.display='block';
	}
}
function CountLeft(field, count, max){
	if (field.value.length > max)
		field.value = field.value.substring(0, max);
	else
		count.value = field.value.length;
}
function ToggleExpandOrCollapse(tab, area, expand, selected) {
    tabObj = $get(tab);
    if (tabObj == null)
        return;
        
	imgObjs = tabObj.getElementsByTagName("img");
	pObjs = tabObj.getElementsByTagName("p");
	areaObj = $get(area);

	if (areaObj == null)
	    return;
	
	if ( tabObj != null )
	{
		if (selected){
			if(areaObj.style.display == "none"){
				pObjs[0].className = "on";
				imgObjs[0].src = "../images/common/buttons/minus.gif";
				areaObj.style.display = "block";
			}
			else{
				pObjs[0].className = "";
				imgObjs[0].src = "../images/common/buttons/plus.gif";
				areaObj.style.display = "none";
			}
		}
		else{
			pObjs[0].className = "";
			imgObjs[0].src = "../images/common/buttons/plus.gif";
			areaObj.style.display = "none";
		}
	}
}
function TabSelected(currentTab, tab1, tab2, currentArea, area1, area2, expand){
	ToggleExpandOrCollapse(tab1, area1, false, false);
	ToggleExpandOrCollapse(tab2, area2, false, false);
	ToggleExpandOrCollapse(currentTab, currentArea, expand, true);
}

function InitializeTab(imgObj, targetObj)
{
	targetControl = $get(targetObj);
	imgControl=$get(imgObj);
	if( targetControl != null && imgControl != null){
		parentObj=imgControl.parentNode;
		if(parentObj)
		{
		   targetControl.style.display == "none"?parentObj.className="":parentObj.className="on";
		}	
	}
}

function ToggleOnOff(imgControl, targetObj){
	targetControl = $get(targetObj);
	if( targetControl != null && imgControl != null){
		if (targetControl.style.display == "none"){
			imgControl.src = "../images/common/buttons/minus.gif";
			targetControl.style.display = "block";
			targetControl.Visible = "true";
		}
		else{
			imgControl.src = "../images/common/buttons/plus.gif";
			targetControl.style.display = "none"; 
			targetControl.Visible = "false";
		}
	}
}


function KioskPrint(isKioskMachine, isDuplicate) {
    if (isDuplicate == 'True') {
        $get('Duplicate').style.display = 'block';
    }

    if (isKioskMachine == 'True') {
        window.external.print();
    }
    else {
        var WebBrowser = '<object id="WebBrowser1" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
        document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
        if (WebBrowser1 != null) {
            WebBrowser1.ExecWB(6, 2, 3);
            WebBrowser1.outerHTML = "";
        }
    }

    if (isDuplicate == 'True') {
        $get('Duplicate').style.display = 'none';
    }
}

function KioskAutoPrint(isKioskMachine, numberOfCopies) {
    if (isKioskMachine == 'False') {
        var WebBrowser = '<object id="WebBrowser1" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
        document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    }

    for (i = 0; i < numberOfCopies; i++) {
        if (isKioskMachine == 'True') {
            window.external.print();
        }
        else {
            if (WebBrowser1 != null) {
                WebBrowser1.ExecWB(6, 2, 3);
            }
        }
    }
    
    WebBrowser1.outerHTML = "";
}

function OnProcessing(){
	ShowProcessingPage('pagecontentfull', 'divprocessing');
	document.aspnetForm.submit();
} 
 
function OpenReturnFormConfirmationPopUp(){
	popUp('ReturnFormPopUp.aspx',null,'left=200,top=200,height=300,width=600,status=yes,toolbar=no,scrollbar=yes,menubar=no,location=no');
}
function UpdateWishList(lblWishID){ 
	var obj = $get(lblWishID);
	if (userInformation.WishListItems > 0){
		if (obj != null){
			obj.innerHTML = "(" + userInformation.WishListItems + ")";
			obj.style.display = "block";
		}
	} 
	else{
		if (obj != null){
			obj.style.display = "none";
			obj.innerHTML = "";
		}
	}
}
function UpdatePriceWatch(lblPriceWatchID) {    
	var obj = $get(lblPriceWatchID);
	var priceWatchCookie = getCookie("PriceWatchCookie");
	if (priceWatchCookie != null && getCookie("FirstName") != "") {
		if (priceWatchCookie > 0) {
			if (obj != null){
				obj.innerHTML = "(" + escape(priceWatchCookie) + ")";
				obj.style.display = "block";
			}
        }
        else {
            if (obj != null) {
                obj.style.display = "none";
                obj.innerHTML = "";
            }
        }		
	} 
	else{
		if (obj != null){
			obj.style.display = "none";
			obj.innerHTML = "";
		}
	}
}
function UpdateWishListFromCookie(lblWishID) {
    var obj = $get(lblWishID);
    if (getCookie("WishListItems") > 0 && getCookie("FirstName") != "") {
        if (obj != null) {
            obj.innerHTML = "(" + escape(getCookie("WishListItems")) + ")";
            obj.style.display = "block";
        }
    }
    else {
        if (obj != null) {
            obj.style.display = "none";
            obj.innerHTML = "";
        }
    }
}
function UpdateQuickCart(lblOneLineModeQuantityID, lblSubtotalID){ 
	var objDiv = $get('itemqty');
	var obj = $get(lblOneLineModeQuantityID);
	var objTotal = $get(lblSubtotalID);
	var objRolloverLink = $get("cartitemqty");
	if (userInformation.CartItems > 0){
		if (obj != null)
			obj.innerHTML = userInformation.CartItems;
		if (objDiv != null)
			objDiv.style.display = "block";
	} 
	else{
		if (obj != null)
			obj.innerHTML = "";
		if (objDiv != null)
			objDiv.style.display = "none";
		if (objRolloverLink != null){
			objRolloverLink.onmouseover = null;
			objRolloverLink.onmouseout = null; 
		}
	}
	if (objTotal != null)
		objTotal.innerHTML = unescape(userInformation.CartTotal);
}
function UpdateQuickCartFromCookie(lblOneLineModeQuantityID, lblSubtotalID) {
    var objDiv = $get('itemqty');
    var obj = $get(lblOneLineModeQuantityID);
    var objTotal = $get(lblSubtotalID);
    var objRolloverLink = $get("cartitemqty");
    if (getCookie("CartItems") > 0) {
        if (obj != null)
            obj.innerHTML = escape(getCookie("CartItems"));
        if (objDiv != null)
            objDiv.style.display = "block";
    }
    else {
        if (obj != null)
            obj.innerHTML = "";
        if (objDiv != null)
            objDiv.style.display = "none";
        if (objRolloverLink != null) {
            objRolloverLink.onmouseover = null;
            objRolloverLink.onmouseout = null;
        }
    }
    if (objTotal != null)
        objTotal.innerHTML = unescape(getCookie("CartTotal"));
}

function UpdateCartPreviewRollOverSubtotal(LblSubTotalMessageID){
	var objTotal = $get(LblSubTotalMessageID);
	if (objTotal != null)
	objTotal.innerHTML = unescape(userInformation.CartTotal);
}
function UpdateCartPreviewRollOverSubtotalFromCookie(LblSubTotalMessageID) {
    var objTotal = $get(LblSubTotalMessageID);
    if (objTotal != null)
        objTotal.innerHTML = unescape(getCookie("CartTotal"));
}
function CallupBankUrl(bankUrl, termUrl, paReq, md) {
	document.BankRequestForm.PaReq.value = paReq;
	document.BankRequestForm.MD.value = md;
	document.BankRequestForm.TermUrl.value = termUrl;
	document.BankRequestForm.action = bankUrl;
	document.BankRequestForm.submit();
	window.focus();
}
function ValidateBankResponse(paRes, md) {
	parent.OnProcessing();
}


function PopupPrintBasket() {
   winStats = "width=800,height=700,location=0,menubar=0,scrollbars=yes,status=0,toolbar=0";
   popUp("PrintableCart.aspx", "_new", winStats);
   return false;
}

function PopulateCartPreviewRollover() {
    if (getCookie("CartItems") > 0) {
        JsLib.UI.InjectHTML("/Order/CartRolloverContent.aspx?QuickCartItemsIterationId=" + getCookie("QuickCartItemsIterationId"),
        "CartPreviewRolloverItemsDiv", "CartPreviewRolloverItemsDiv");
    }
}
