//
// "Compare Products" and "Email a Friend" support javascript
//
// Manages the cookies which maintain the state information concerning which items are selected
//
// Written by: Steve Fox  5/9/05
// Copyright Netizen Ltd 2005
//

var CompareSelectionListCookie = "compsel";
var ComparePrepend = "compareitem_";
var EmailFriendSelectionListCookie = "mailsel";
var EmailFriendPrepend = "emailfrienditem_";
var cookieSeparator = "|";

var CouponSelectionListArray = null;
var EmailFriendSelectionListArray = null;
var intLastMatchIndex = null;

function SaveWishListCookie(productid)
{
	eraseCookie('RS_' + productid);
	createCookie('RS_' + productid ,productid, 7);
	URL="add-to-wish-list.aspx?productid=" + productid;
	popup(URL, '400', '270', '0');
}

function showSelected() {
	// cycle through comparision cookie and mark the relevant selected coupons, if any
	if (CreateLocalSelectionArray(CompareSelectionListCookie)) {
		selectItems(CouponSelectionListArray, ComparePrepend)
	}
	// cycle through 2email a friend" cookie and mark the relevant selected coupons, if any
	if (CreateLocalSelectionArray(EmailFriendSelectionListCookie)) {
		selectItems(EmailFriendSelectionListArray, EmailFriendPrepend)
	}
	
}

function clearItems()  {
	var strList="", theform;
	theform=document.wishform;
	for (i=0; i<theform.elements.length; i++)   {
		thename = theform.elements[i].name.substring(3);
		strList =  strList + thename + "|";
		}
	if (strList!="")   {
		ClearAllCoupons(strList);
		document.location.reload();
		}	
	}

// strcouponIdproductId in the form: couponID1-productID1|couponID2-productID2|couponID3-productID3|
function ClearAllCoupons(strcouponIdproductId) {
	// remove final |
	strCP = strcouponIdproductId.substring(0, strcouponIdproductId.length - 1);
	// split into individual properties	
	cparray = strCP.split("|");
	for (i = 0; i < cparray.length; i++)   {
		// further split each property element into it's couponID and productID
		cpminiarray = cparray[i].split("-");
		// finally pass into removal function
		RemoveCoupon(cpminiarray[0], cpminiarray[1]);
		}
}

function RemoveCoupon(couponId, productId) {

//alert(couponId);
//alert(productId);

	// remove item from compare cookie state store
	if (ItemIsSelected(couponId, CompareSelectionListCookie)) {
		RemoveItemFromSelectionList(CouponSelectionListArray);
	} 
	// remove item from email a friend cookie state store
	if (ItemIsSelected(productId, EmailFriendSelectionListCookie)) {
		RemoveItemFromSelectionList(EmailFriendSelectionListArray);
	} 
	// commit changes back to cookie
	CommitSelectionList(CouponSelectionListArray, CompareSelectionListCookie);
	CommitSelectionList(EmailFriendSelectionListArray, EmailFriendSelectionListCookie); 
	return
}


function selectItems(selectionListArray, strPrepend) {
	for (var index in selectionListArray) {
		try {
			document.getElementById(strPrepend + selectionListArray[index]).checked = true;
		} catch (ex) {
			// just ignore it if the element doesn't exist
		}
	}
}

function UpdateCompareList(objSource, couponId){
	if (ItemIsSelected(couponId, CompareSelectionListCookie)) {
		// already selected so de-select it
		RemoveItemFromSelectionList(CouponSelectionListArray);
		objSource.checked = false;
	} else {
		if (CouponSelectionListArray == null) {
			CouponSelectionListArray = new Array();
		}	
		AddItemToSelectionList(couponId, CouponSelectionListArray);
		// should already be selected, just making sure
		objSource.checked = true;
	}
	CommitSelectionList(CouponSelectionListArray, CompareSelectionListCookie);
	return
}


function UpdateEmailFriendList(objSource, propertyId){
	if (ItemIsSelected(propertyId, EmailFriendSelectionListCookie)) {
		// already selected so de-select it
		RemoveItemFromSelectionList(EmailFriendSelectionListArray)
		objSource.checked = false;
	} else {
		if (EmailFriendSelectionListArray == null) {
			EmailFriendSelectionListArray = new Array()
		}	
		AddItemToSelectionList(propertyId, EmailFriendSelectionListArray)
		// should already be selected, just making sure
		objSource.checked = true;
	}
	CommitSelectionList(EmailFriendSelectionListArray, EmailFriendSelectionListCookie);
	return
}


 function CommitSelectionList(selectionListArray, cookieName) {
	var selectionList;
	var intSaveDuration = 7;
	
	if (selectionListArray != undefined) {
		selectionList = selectionListArray.join(cookieSeparator);
		createCookie(cookieName, selectionList, intSaveDuration);
	}
	return
}

function RemoveItemFromSelectionList(selectionListArray) {
	// using the previously set matching index remove the required item from the array
	delete selectionListArray[intLastMatchIndex];
	// move truncated item to end of array
	selectionListArray.sort();
	// truncate array to delete item
	selectionListArray.length = selectionListArray.length - 1;
	return
}

function AddItemToSelectionList(itemId, selectionListArray) {
	selectionListArray[selectionListArray.length] = itemId;
	return
}

function ItemIsSelected(itemId, cookieName) {
// returns a boolean indicating if the supplied item id is present in the selection list
	var intArrayIndex = 0;
	var intArraySize;
	var blnFound = false;
	var selectionListArray;
	
	
	if (CreateLocalSelectionArray(cookieName)) {
		if (cookieName.toLowerCase() == CompareSelectionListCookie.toLowerCase()) {
			selectionListArray = CouponSelectionListArray;
		} else if (cookieName.toLowerCase() == EmailFriendSelectionListCookie.toLowerCase()) {
			selectionListArray = EmailFriendSelectionListArray;
		}
		intArraySize = selectionListArray.length;
		
		while ((blnFound == false) && (intArrayIndex < intArraySize)) {
			if (parseInt(itemId) == parseInt(selectionListArray[intArrayIndex])) {
				// found the item
				blnFound = true;
				intLastMatchIndex = intArrayIndex;
			} else {
				intArrayIndex++;
			}
		} 
	}
	
	return blnFound;
}

function CreateLocalSelectionArray(cookieName) {
// creates an array containing the cookie items if required. If the cookie does not exist the function return false
// indicating the array hasn't been created otherwise it returns true. If the cookiePassed in is not recognised the 
// function also returns true
	var LocalSelectionList = null;
	var blnResult = true;
	var selectionListArray;
	var blnContinue = true;
	
	if (cookieName.toLowerCase() == CompareSelectionListCookie.toLowerCase()) {
		selectionListArray = CouponSelectionListArray;
	} else if (cookieName.toLowerCase() == EmailFriendSelectionListCookie.toLowerCase()) {
		selectionListArray = EmailFriendSelectionListArray;
	} else {
		// bail - unknown cookie name
		blnContinue = false
	}
	if (blnContinue) {
		if (selectionListArray == null) {
			LocalSelectionList = readCookie(cookieName);
			if (LocalSelectionList != null) {
				if (cookieName.toLowerCase() == CompareSelectionListCookie.toLowerCase()) {
					CouponSelectionListArray = LocalSelectionList.split(cookieSeparator);
				} else if (cookieName.toLowerCase() == EmailFriendSelectionListCookie.toLowerCase()) {
					EmailFriendSelectionListArray = LocalSelectionList.split(cookieSeparator)
				}
			} else {
				// cookie doesn't exist - so current item is definitely not already set 
				blnResult = false;
			}
		}
	}
	
	return blnResult;
}


function refreshParent() {
	opener.location.reload();
	this.close();
}
