// cookie code used from http://www.quirksmode.org/
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);
}
// end of cookie code





function processWishlist()
{
	var u = document.URL;
	var r = new Array();
	var property = u.match(/(add|remove)=([0-9]+)/i);
	if (property == null)
	{
		/*
		if (u.search(/htm$/) == -1)
		{
			alert("Unable to process property");
		}
		*/
		return false;		
	}
	else
	{
		if (property[1] == "add")
		{
			addtoWL(property[2]);
			return true;
		}
		else if (property[1] == "remove")
		{
			removefromWL(property[2]);
			return true;
		}
		else
		{
			return false;
		}
	}
}





var cookiename = "aaWL";
var container = "wishlisttd";
var separator = "-";

function cookietoarray()
{
	// not implemented yet!
}

function addtoWL(id) {
	var wl = readCookie(cookiename);
	
	if (wl == null  ||  wl == "") { 
		wl = new Array();
		wl.push(id);
	}
	else {
		//explode
		wl = wl.split(separator);
		
		// limit to 3 properties
		var limitqty = 3;
		if (wl.length >= limitqty) {
			window.onload = function() {
				var answer = confirm("To save more than " + limitqty + " properties in your wishlist you must registered with our site. Click OK to login or register for FREE, or Cancel to stay here.");
				if (answer) {
					window.location = "/login.htm";
				}
			}
			
			return false; // return false and exit function as not to add to wishlist
		}
		
		var found = false;
		for (var i = 0; i < wl.length; i++) {
			if (wl[i] == id) {
				found = true;
				break;
			}
		}
		if (!found) {
			wl.push(id);
		}
	}
	
	wl = wl.join(separator);
	
	createCookie(cookiename,wl,7);
	
	// was there a link for the add?
	l = document.getElementById("wla"+id);
	if (l != null) {
		l.href = "javascript:void(0);";
		l.innerHTML = "! Added !";
	}
}



function removefromWL(id) {
	var wl = readCookie(cookiename);
	
	if (wl == null  ||  wl == "") { 
		return false;
	}

	wl = wl.split(separator);
	
	for (var i = 0; i < wl.length; i++) {
		if (wl[i] == id) {
			wl.splice(i,1);
			i--;
		}
	}
	
	wl = wl.join(separator);
	
	createCookie(cookiename,wl,7);
}



function clearWL() {
	eraseCookie(cookiename);	
}


function retrieveWL(wl_container) {
	wl_container = document.getElementById(wl_container);
	
	if (wl_container != null) {
		
		wl = readCookie(cookiename);
		
		if (wl == null) {
			return false;
		}
		
		var url = "/ajax.wishlist.php?c=" + wl_container + "&wl=" + wl;
		//alert(url);
		
		var ajax = new GLM.AJAX();
		
		function pageCallback(response) {
			wl_container.innerHTML = response;
		}
		ajax.callPage(url, pageCallback);
	}
	else {
		alert("Unable to locate container: " + wl_container);	
	}
}
