/**************************************************/
/* ahill 6/27/07 compare with cookies code 		   */
/* this piece is for the checkboxes				   */
/**************************************************/

window.onload=markChecked;

//this function automatically checks the checkboxes that match the skus in the cookie when the page loads
function markChecked(){
	
	//alert('There was an error while trying to compare this item.\n Error: 321.\n Please contact us at webmaster@musiciansfriend.com.');
	
	var mc = readCookie('comparecookie');
	if(mc){
		//array for the skus in the cookie
		var chksku = new Array();
		chksku = mc.split(',');
		//array for all the compare checkboxes on the page
		var chkboxes = new Array();
		chkboxes = document.getElementsByName('base_pid');

		for (var i = 0; i <= chksku.length-1; i++) {
			for (var x = 0; x <= chkboxes.length-1; x++) {
				if (chksku[i] == chkboxes[x].value) {
					chkboxes[x].checked = true;
				}
			}
		}
	}
}

function doCompare(obj,base_pid){
	var bp = base_pid;
	var cc = readCookie('comparecookie');

	//this block is for when they check
	if (obj.checked == true) {
		//do they have a cookie?
		if (cc) {
			//converts the cookie to the sku.array
			var sku = new Array();
			sku = cc.split(',');
			//insert base_pid into the first slot of the sku.array
			sku.unshift(bp);
			
			if (sku.length > 10) {
				//if there's more than 10 items in the array, delete the last one
				sku.pop();
			}

			cc = sku.join(',');
			createCookie('comparecookie',cc,0);
		}
		//they don't have a cookie, add the checked sku
		else{
			createCookie('comparecookie',bp,0);
		}
	}
	//this block is for deselecting a checkbox
	else if (obj.checked == false) {
		
		var sku = new Array();
		sku = cc.split(',');
		
		for (var i = 0; i <= sku.length-1; i++) {
			if (sku[i] == bp ){
				//this deletes the array index['s']
				sku.splice(i,1);
				//converts the array back to a string and rewrites the new cookie
				cc = sku.join(',');
				createCookie('comparecookie',cc,0);
			}
		}
	}
	//this shouldn't happen, but this is when check/uncheck fail	
	else {
		alert('There was an error when trying to compare this item. Error: 321.');
	}
}

/**************************************************/
/* this piece is for the form action submit 	   */
/**************************************************/
function compareUrl(ow){
	var url = 'http://' + document.domain + '/compare?';
	
	//this writes out the cookie into the correct QS values
	var qsc = readCookie('comparecookie');
	var qsOutput = new Array();
	qsOutput = qsc.split(',');
	
	var newQString = qsOutput.join('&base_pid=');
	
	
	//storing the user's current page so they can click "go back" from the compare page
	if(ow !== 'dont'){
		var backURL = encodeURI(document.URL);
		createCookie('compareBackURL',backURL,0);
	}
	//alert(startURL);
	//window.location.href = decodeURI(startURL);
	
	
	//rewrites the url to reflect whats in the user's cookies
	window.location = url + 'base_pid=' + newQString;
	return false;
	
	
}





