var req;
var lastUrl;
// var bulls defined in .tpl file

/** See: http://developer.apple.com/internet/webcontent/xmlhttpreq.html */
function loadSource(url) {
	lastUrl = url;
	loadXMLDoc(url);
}

function loadXMLDoc(url) {
	//alert("Getting "+url);
	req = false;
			
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
	// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
		}
	}
	if(req) {	
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	} else {
		return false;
	}
	return true;
}

function processReqChange() {
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...
			//alert("ok");
			var fields = req.responseText.split(",");
			var action = fields[0];
			var id = fields[1];
			if ( action == 'added' ) {
				//document.wl"+id+".submit();
				document.getElementById("wltxt"+id).innerHTML = "<input type=\"checkbox\" name=\"in_wish_list\" checked onClick=\"remove_from_wishlist('"+id+"');\"> Yact in wish list";
				document.forms["wl"+id].in_wish_list.checked = true;
				document.forms["wl"+id].cmd = "remove_from_wishlist";
			} else if ( action == 'removed' ) {
				//document.wl"+id+".submit();
				document.getElementById("wltxt"+id).innerHTML = "<input type=\"checkbox\" name=\"in_wish_list\"  onClick=\"add_to_wishlist('"+id+"');\"> Add to wish list";
				document.forms["wl"+id].in_wish_list.checked = false;
				document.forms["wl"+id].cmd = "add_to_wishlist";
			}
		} else {
			//alert("There was a problem retrieving the data:\n" +
			//	req.statusText);
			//document.getElementById("bullListing").innerHTML = "<br><p align=\"center\">Error: There was an error with the request ("+req.status+").</p>";
		}
	}
}

function add_to_wishlist(id) {
	// yachtsearch_addToWishList.php?cmd=add_to_wishlist&boat_id={$r.id}
	document.getElementById("wltxt"+id).innerHTML = "Adding...";
	var ok = loadXMLDoc("yachtsearch_addToWishList.php?cmd=add_to_wishlist&boat_id="+id);
	
	if ( !ok ) {
		// Browser can't handle AJAX, do it the old way
		document.forms["wl"+id].submit();
	}
}

function remove_from_wishlist(id) {
	// yachtsearch_addToWishList.php?cmd=remove_from_wishlist&boat_id={$r.id}
	document.getElementById("wltxt"+id).innerHTML = "Removing...";
	var ok = loadXMLDoc("yachtsearch_addToWishList.php?cmd=remove_from_wishlist&boat_id="+id);
	
	if ( !ok ) {
		// Browser can't handle AJAX, do it the old way
		document.forms["wl"+id].submit();
	}
}
