var strSPCookiesNames = "SPSuits1,SPSuits2,SPSuits3,SPShirts1,SPShirts2,SPShirts3,SPTies1,SPTies2,SPTies3,SPBelts1,SPBelts2,SPBelts3,SPSocks1,SPSocks2,SPSocks3";
var strSPDCookiesNames;
	
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function addAllSPItems()
{
	var currentUrl;
	var params;
	
	if (readCookie("isSSPSuitAdded") == "true") {
		strSPCookiesNames = "SSPSuit,SSPShirt,SSPTie,SSPBelt,SSPSocks,SSPShoes";
	}
	else if (readCookie("isSHPShoesAdded") == "true") {
		strSPCookiesNames = "SHPShoes1,SHPShoes2,SHPShoes3";
	}
	else if (readCookie("isSTPTuxAdded") == "true") {
		strSPCookiesNames = "STPTux,STPShirt,STPTie,STPCummerbund";
	}
	
	// Add the 15 product/skus user selected for the suit package		
	arrSPCookiesNames = strSPCookiesNames.split(",");
	
	if (arrSPCookiesNames.length > 0)
	{
		for (i = 0; i < arrSPCookiesNames.length; i++)
		{				
			if (readCookie(arrSPCookiesNames[i]) != null)
			{ 
				//alert("BEFORE " + arrSPCookiesNames[i] +" = "+ readCookie(arrSPCookiesNames[i]));		
								
				currentUrl = readCookie(arrSPCookiesNames[i]);
				params = '';
				
				new Ajax.Request(
					currentUrl,
					{
						method: 'get',
						parameters: params,
						asynchronous: false,
						onSuccess: addSuccess,
						onFailure: addFailure
					}
				);
				
				eraseCookie(arrSPCookiesNames[i]);
						
				//alert("AFTER " + arrSPCookiesNames[i] +" = "+ readCookie(arrSPCookiesNames[i]));			
			}		
		}
		
		window.location.reload();		
	}	

}

function addSuccess(reponse){}

function addFailure(response){}

function processSPItems(intPCt,intCCt){	
	var intParentCt = intPCt;
	var intChildCt = intCCt;
	var strCookiePNamePrefix = "SPParent";
	var strCookiePNameEnum;
	var strCookieCNamePrefix = "SPChild";
	var strCookieCNameEnum;
	var objSPPDUrl;
	var objSPCDUrl;		
	var tempStrHelper = "";
	var arrtempStrHelper;
	var tempStrHelper2 = "";	
	var currentSPParent = "";
	var currentSPParent2 = "";
	var arrCurrentSPParent;
	var arrCurrentSPParent2;
	var activeSPParent = "";	
	var ctrDUrls = 0;
	var moreDUrls = 0;
	
	if (intParentCt > "0" && intChildCt > "0") {	
	
		if (intParentCt == "1" && intChildCt == "15") {	
			
			objSPPDUrl = document.getElementById("SPParent0").className;			
			if (readCookie(objSPPDUrl) == null) {
				createCookie(objSPPDUrl, objSPPDUrl, 0);	
				createCookie("SPParentNames", objSPPDUrl, 0);			
			}			
			for (i=0;i<15;i++){						
				strCookieCNameEnum = strCookieCNamePrefix + i;
				objSPCDUrl = document.getElementById(strCookieCNameEnum).className;					
				if (tempStrHelper == "") tempStrHelper = objSPCDUrl
				else tempStrHelper = tempStrHelper + "," + objSPCDUrl;
			}			
			tempStrHelper = readCookie(objSPPDUrl) + "," + tempStrHelper; 			
			arrtempStrHelper = tempStrHelper.split(",");
			if (arrtempStrHelper.length == 16){
				createCookie(objSPPDUrl, tempStrHelper, 0);							
				tempStrHelper = "";		
			}	
			
			//alert(readCookie(objSPPDUrl))
			//alert(readCookie("SPParentNames"))			
			
		}
		else {
						
			//alert("SP1: "+readCookie("SPParentNames"))										
			for (j=0;j<intParentCt;j++){
				strCookiePNameEnum = strCookiePNamePrefix + j;
				objSPPDUrl = document.getElementById(strCookiePNameEnum).className;	
				if (readCookie("SPParentNames").indexOf(objSPPDUrl) == -1){
					//alert("new SPParent cookie = " + objSPPDUrl)
					createCookie(objSPPDUrl, objSPPDUrl, 0);
					tempStrHelper2 = readCookie("SPParentNames") + "," + objSPPDUrl;
					createCookie("SPParentNames", tempStrHelper2, 0);		
				}
			}
						
			arrCurrentSPParent = readCookie("SPParentNames").split(",");						 
			//alert(readCookie(arrCurrentSPParent[0]))
																																									
		}		
	
	}
	
	// Debugger:
	//for (n=0;n<intParentCt;n++){
		//strCookiePNameEnum = strCookiePNamePrefix + n;	
		//strCookiePNameEnum = document.getElementById(strCookiePNameEnum).className;	
		//currentSPParent = readCookie(strCookiePNameEnum);
		//if (currentSPParent) arrCurrentSPParent = currentSPParent.split(",");
		//alert("FINAL "+strCookiePNameEnum + " length ==== " + arrCurrentSPParent.length);
	//}		
	
}

function deleteAllSPItems(strThisDCookie){ //alert(strThisDCookie)
	var currentUrl;
	var params;
	
	// Delete the 15 product/skus user selected for the suit package that goes with the suit package parent item that was clicked
	strSPDCookiesNames = readCookie(strThisDCookie);	
	if (strSPDCookiesNames) {
		arrSPDCookiesNames = strSPDCookiesNames.split(",");
		//alert(arrSPDCookiesNames[8])
		if ( arrSPDCookiesNames.length > 0)
		{
			for (i = 0; i < arrSPDCookiesNames.length; i++) //alert(arrSPDCookiesNames[i])
			{				
				if (arrSPDCookiesNames[i] != null && arrSPDCookiesNames[i] != '') 
				{ 
					//alert("BEFORE " + arrSPDCookiesNames[i]);		
								
					currentUrl = arrSPDCookiesNames[i];
					params = '';
					
					new Ajax.Request(
						currentUrl,
						{
							method: 'get',
							parameters: params,
							asynchronous: false,
							onSuccess: deleteSuccess,
							onFailure: deleteFailure
						}
					);								
							
					//alert("AFTER " + arrSPCookiesNames[i]);			
				}		
			}
			
			eraseCookie(strThisDCookie);
			window.location.reload();
		}	
	}	

}

function deleteSuccess(reponse){}

function deleteFailure(response){}

function updateContShopButton(){ 
	strCSBHref = document.getElementById("continueShoppingLink").href;		
	if (strCSBHref.indexOf("SPProdBrowse.asp") != -1 || strCSBHref.indexOf("SSPProdBrowse.asp") != -1 || strCSBHref.indexOf("SHPProdBrowse.asp") != -1 || strCSBHref.indexOf("STPProdBrowse.asp") != -1){
		strCSBHref = '/';
	}	
	document.getElementById("continueShoppingLink").href = strCSBHref;
}

if (readCookie("isSPMainAdded") == "true")
{ 	
	addAllSPItems();
	eraseCookie("isSPMainAdded");
	eraseCookie("isSSPSuitAdded");
	eraseCookie("isSHPShoesAdded");
	eraseCookie("isSTPTuxAdded");
}	

