//$Id: addtopackage.js,v 092184c50857 2011/12/10 07:53:20 mguan $

document.write("<script src=\"/js/ajaxfunctions.js\"></script>");
document.write("<script src=\"/ajax/addtocartfunctions.js\"></script>");

var inCall = false;
 
var callToArray = new Array();

function ChangeSPP(action, prId, index, type, poptgId)
{
	if (!poptgId)
		poptgId = 0;
	 
	if(action == 'add' && prId)
		AddItemInSPP(prId, index, type, poptgId);
	else 
		RemoveItemInSPP(prId, index, type, poptgId);
		
}


function ClickAcc(obj, index, prId)
{
	if(obj.checked == true && prId)
		AddItemInSPP(prId, index, 'acc', 0);
	else 
		RemoveItemInSPP(prId, index, 'acc', 0);
	
}

function AddItemInSPP(prId, index, type, poptgId)
{
	 
	callToArray.push( 'UpdateSPP(' + prId + ',' + index + ',' + 1 + ',\'add\'' + ',\'' + type + '\',' + poptgId + ')' );
	ExecuteCalls();
		
}

function RemoveItemInSPP(prId, index, type, poptgId)
{
	callToArray.push( 'UpdateSPP(' + prId + ',' + index + ',' + 1 + ',\'remove\'' + ',\'' + type + '\',' + poptgId + ')' );
	ExecuteCalls();
		
}


function UpdateSPP( prId, index, quantity, action, type, poptgId )
{
	c = CreateHTMLRequestor();

    // if the XMLHttpRequest object is in any state other than 0 (unitialized), then stop the current request.
    if (c && c.readyState!=0)
    {
        c.abort();
    }

    // Set the function that handles changes in state.
    c.onreadystatechange = onchangeactions;
	inCall = true;
	// Send the request.
	 
    c.open("GET", '/minicart/promopackage.php?type='+ type + '&action=' + action + '&prId=' + prId + '&index=' + index + '&quantity=' + quantity + '&poptgId=' + poptgId + '&noCache=' + Math.random(), true);
    c.send(null);
    return true;
	
}

if(!(typeof(accStorePriceArr) == 'object' && (accStorePriceArr instanceof Array)))
    var accStorePriceArr = new Array();

function ChangeDisplay(str)
{
	if (str != 'refresh')
	{
		var splitResult = str.split("||");
		
		if (splitResult[0] && document.getElementById('totalafterrebate'))
		{
			document.getElementById('totalafterrebate').innerHTML =  splitResult[0];
		 
		}
		 
		if (splitResult[1] == 'popt')
		{
			if (splitResult[2] && document.getElementById('monthlytotal'))
				document.getElementById('monthlytotal').innerHTML =  splitResult[2];
			 
			if (splitResult[3] && document.getElementById('monthlyFee'))
				document.getElementById('monthlyFee').innerHTML =  splitResult[3];
				 
			if (splitResult[4])
			{
				var ret2 = splitResult[4].split("##");
				var index = ret2[0];
				var priceHeader = ret2[1];
				var price = ret2[2];
				var expandedPriceId = index+'-devicePriceExpanded';
				var collapsedPriceId = index+'-devicePriceCollapsed';
			    
			    if(document.getElementById(expandedPriceId)) 
			    {
					document.getElementById(expandedPriceId).innerHTML = "<strong class=\"red superLg\">" + priceHeader + price + "</strong>";		
				}		
				
				if(document.getElementById(collapsedPriceId))
				{			
					document.getElementById(collapsedPriceId).innerHTML = "<strong class=\"LgClr3\">" + price + "</strong>";
				}	 
			}
		}
		else
		{
			if (splitResult[2])
			{
				var ret2 = splitResult[2].split("##");
				var index = ret2[0];
				var ret3 = ret2[1].split("%%%");
				var accArrName = ret3[0];
				var accArrPrice = ret3[1];
				var accArrNameDiv = index+'-accNameStr';
				var accArrPriceDiv= index+'-accPriceStr';
				 
				document.getElementById(accArrNameDiv).innerHTML = accArrName;
				document.getElementById(accArrPriceDiv).innerHTML = accArrPrice;
				
				if(splitResult[3])
				{
				    var carText = $('#totalCarrPrice strong').html();
				    if(carText)
				    {
    				    var replacePrice    = carText.replace(/\$/, '');
    				    var splitArr        = splitResult[3].split("##");
    				    var prId            = splitArr[0];
    				    var price           = parseFloat(splitArr[1]);
                    
                        if(price)
                        {   
                            if(!(typeof(accStorePriceArr[index]) == 'object' && (accStorePriceArr[index] instanceof Array)))
    				            accStorePriceArr[index]= new Array();
    				        
    				        accStorePriceArr[index][prId] = price;
    				    }
    				    else
    				    {
    				        if(typeof(accStorePriceArr[index]) == 'object' && (accStorePriceArr[index] instanceof Array))
    				        {   
    				            if( accStorePriceArr[index][prId] != 'undefined')
    				            {
    				                price = - accStorePriceArr[index][prId];
    				            }
    				        }
    				    }
    
    				    if( !isNaN(price))
    				    {
    					    replacePrice = parseFloat(replacePrice) + price;
    					    replacePrice = Math.round(replacePrice *100)/100;
    					    $('#totalCarrPrice strong').html('$'+replacePrice);
    				    }
				    }
			    }
			}
		}
	}
	else
	{
		window.location.reload( true );
	}
}

