﻿var nTimeout;

function removeOption(nCartItemID) {
    $.get("/Cart/sideCart.aspx", "remove=" + nCartItemID, function(data) {
    });
}
 
function addProduct(nProductID, nStoreID, nAttachToID, szProductType) {
    
    if(typeof(GrayBlock)=="function")
    {
        //When add tracks, needn't to make album gray, except customer choose all of them
        if(Products[CONFIG.CurrentID] && Products[CONFIG.CurrentID]["DefaultChildID"]==nProductID)
            GrayBlock($("#CB_"+CONFIG.CurrentID).get(0));
    }
    
    nTimeout = setTimeout(setLoading,300)
    $.get("/Cart/sideCart.aspx", "productType="+szProductType+"&add=" + nProductID + "," + nStoreID + "," + nAttachToID + "&rnd="+Math.random(), function(data) {
        clearTimeout(nTimeout);
        $("#divSideCart").html(data);                
        if(document.getElementById("_SideCartStatus")!=null) {
            sStatusMessage = document.getElementById("_SideCartStatus").innerHTML;
            if (sStatusMessage != "OK") {
                alert(sStatusMessage);
                RefreshContentCovers();
            } else {
                J3Button.bind("div[name=divCart]"); 
                var arCapacityMsg=$("#hdCapacityMsg").val().split(",");
                if(typeof(UpdateCapacity)=="function")
                {
                    UpdateCapacity(arCapacityMsg[0],arCapacityMsg[1]);
                }
                
                if(typeof(Products)=="object" && typeof(Products[CONFIG.CurrentID])=="object" && nProductID==Products[CONFIG.CurrentID]["DefaultChildID"])
                {
                    //Update the "AlreadyBought" attribute, to be called in InitCover()
                    Products[CONFIG.CurrentID]["AlreadyBought"]=true;
                    HidePopup();
                }
            }
        }
        
    });
}

function setLoading() {            
    document.getElementById("spanSideCartHeader").innerHTML="<table cellpadding='0' cellspacing='1'><tr valign='center'><td><img src='/images/loading.gif' align='top'></td><td style='color:#808080;font-size:11px'>&nbsp;Refreshing Cart ...</td></tr></table>";
}

function showPopup(ctl) {
	var control=document.getElementById("mouseOverMessage");
	
	if (control!=null)
	{
		ctlStyle=control.style;
		var leftpos=0;
		var toppos=0;
		var aTag = ctl;
		
		do {
			leftpos += aTag.offsetLeft;
			toppos += aTag.offsetTop;
			aTag = aTag.offsetParent;
		} while((aTag.tagName!="HTML")&&((aTag.tagName!="BODY")));

		ctlStyle.left=(leftpos-30)+"px";
		ctlStyle.top=(toppos+20)+"px";
		ctlStyle.visibility="visible";
	}
}

function hidePopup() {				
	var ctl=document.getElementById("mouseOverMessage");
	if (ctl!=null)
	{
		ctl.style.visibility="hidden";
	}
}

var currentCTL=null;
var currentID='';
var previousLabel='';
var ignoreDocumentClick=true;
var timeout;

var base = (typeof(UserControls_sideCart)=="object") ? UserControls_sideCart : UserControls_ShoppingCart;

function convertToTextBox(id,ctl) {
    if(currentCTL!=ctl) {        
        if(currentCTL!=null) {
            convertToText(previousLabel);
        }
        
        currentCTL=ctl;
        currentID=id;
        previousLabel=currentCTL.innerHTML;
        currentCTL.innerHTML = "<input id='txtChangeName' onkeydown='getKey(event)' onblur='UpdateDeviceName()' type='text' size='15' style='font-size:9px;border:0px;padding:2px;background-color:#1386C0;color:#ffffff' value='" + currentCTL.innerHTML + "'>";
        document.getElementById("txtChangeName").select();
    }
    timeout = setTimeout("enableDocumentClick()",500);
}

function getKey(e) {
    var keynum;
    var keychar;
    var numcheck;

    if(window.event) // IE
    {
      keynum = e.keyCode;
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
      keynum = e.which
    }
      
    if(keynum==27){
        convertToText(previousLabel);
    }
    else if(keynum==13){   
        UpdateDeviceName();
    }
}

var bUpdatingLabel=false;

function UpdateDeviceName()
{
    if(bUpdatingLabel) return;
    
    if (document.getElementById("txtChangeName").value.trim()=="") {
        alert('Please give a label name for this device.');
    }
    if(document.getElementById("txtChangeName").value==previousLabel)
    {
        convertToText(previousLabel);
        return;
    }
    else 
    {
        bUpdatingLabel=true;
        base.changeName(currentID,document.getElementById("txtChangeName").value,
            function(data){
                if(data.value!=previousLabel) {
                    convertToText(data.value);
                }
                else {
                    convertToText(previousLabel);
                    alert("Unable to rename, the name already exists!");
                }
                bUpdatingLabel=false;
            }
        );
    }
}

function convertToText(newName) {
    if(currentCTL!=null) {
        currentCTL.innerHTML = newName;
        currentCTL=null;
    }
}

function enableDocumentClick() {
    ignoreDocumentClick=false;
}

function escapeRenaming() {
    if(!ignoreDocumentClick) {
        convertToText(previousLabel);
    }
}
