
function print(s) {
   var deb = document.getElementById('debug');
   if (deb) {
   	  deb.innerHTML = deb.innerHTML + s + "<br>\n";
   }
}

function readCookie(name) {
    //alert('Leyendo Cookie : '+name + "\nDominio : "+document.domain);
    print('leyendo cookie:'+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) {
			//alert('Leyendo - encontrada ('+name+') = ' + c.substring(nameEQ.length,c.length));
			print('Leyendo - encontrada ('+name+') = ' + c.substring(nameEQ.length,c.length));
			return c.substring(nameEQ.length,c.length);
		}
	}
	//alert('No esta retornando nada');
	print('No esta retornando nada:' + name);
	return null;
}

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 = "";
	}
	//alert('Antes : '+document.cookie);
	print('Antes : '+document.cookie);
	ncookie = name+"="+value; //+expires+"; path=/"+"; domain="+document.domain;
	//document.cookie = name+"="+value+expires+"; path=/"+"; domain=boliviamall.com";
	//document.cookie = name+"="+value+expires+"; path=/"+"; domain="+document.domain;
	//document.cookie = document.cookie + ';' + ncookie ;
	document.cookie = ncookie + ";" + document.cookie;
	//alert('Cookie Creada :' +ncookie+"\n\n"+
	//      'Despues : '+document.cookie);
	print('Cookie Creada :' +ncookie+"<br>\n\n"+
	       'Despues : '+document.cookie);
}

function doCompare(obj, base_pid) {
	var bp = base_pid;
	var cc = readCookie('bmCompCookie');

	//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 > 5) {
				//if there's more than 10 items in the array, delete the last one
				sku.pop();
			}

			cc = sku.join(',');
			createCookie('bmCompCookie', cc, 1);
		}
		//they don't have a cookie, add the checked sku
		else{
			createCookie('bmCompCookie', bp, 1);
		}
	}
	//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('bmCompCookie',cc, 1);
			}
		}
	}
	//this shouldn't happen, but this is when check/uncheck fail
	else {
		alert('There was an error when trying to compare this item.');
	}
}


