/*
	This is the JavaScript file for the AJAX Suggest Tutorial

	You may use this code in your own projects as long as this 
	copyright is left	in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.DynamicAJAX.com
	
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.	

*/
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function statInsFav(ID) {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		searchReq.open("GET", 'act_ins_fav.php?ID=' + ID, true);
		searchReq.send(null);
	}		
}

function statInsOFav(ID) {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		searchReq.open("GET", 'act_ins_ofav.php?ID=' + ID, true);
		searchReq.send(null);
	}		
}

function listingAddFav(ID) {
	DelCookie(ID);
	var expr = new Date();
	expr.setTime(expr.getTime() + 31104000000);
	FavID = "";
	if (document.cookie.indexOf("FavID=") != -1) {
		p = document.cookie.indexOf(" FavID=") + 1;
		p1 = document.cookie.indexOf(";", p+5);
		if (p1 == -1) p1 = document.cookie.length;
		FavID = document.cookie.substring(p+6, p1);
	}
	if (FavID == "")
		FavID = ID;
	else
		FavID = FavID + "," + ID;
	document.cookie = "FavID=" + FavID + ";expires=" + expr.toGMTString() + ";path=/";
	statInsFav(ID);
	return false;
}

function AddFav(ID) {
	listingAddFav(ID);
	alert('Listing was added to your favorites');
}

function AddOFav(ID) {
	DelOCookie(ID);
	var expr = new Date();
	expr.setTime(expr.getTime() + 31104000000);
	OFavID = "";
	if (document.cookie.indexOf("OFavID=") != -1) {
		p = document.cookie.indexOf(" OFavID=") + 1;
		p1 = document.cookie.indexOf(";", p+6);
		if (p1 == -1) p1 = document.cookie.length;
		OFavID = document.cookie.substring(p+7, p1);
	}
	if (OFavID == "")
		OFavID = ID;
	else
		OFavID = OFavID + "," + ID;
	document.cookie = "OFavID=" + OFavID + ";expires=" + expr.toGMTString() + ";path=/";
	statInsOFav(ID);
	alert('Listing was added to your favorites');
	return false;
}

function DelFav(ID) {
	DelCookie(ID);
	return false;
}

function DelOFav(ID) {
	DelOCookie(ID);
	return false;
}

function DelCookie(ID) {
	var expr = new Date();
	expr.setTime(expr.getTime() + 31104000000);
	FavID = "";
	if (document.cookie.indexOf("FavID=") != -1) {
		p = document.cookie.indexOf(" FavID=") + 1;
		p1 = document.cookie.indexOf(";", p+5);
		if (p1 == -1) p1 = document.cookie.length;
		FavID = document.cookie.substring(p+6, p1);
	}
	if (FavID == "")
		FavID = "";
	else {
		p = FavID.indexOf("," + ID + ",");
		if (p != -1)
			FavID = FavID.substring(0, p) + FavID.substring(p + ID.length + 1, FavID.length);
		else
			if (FavID.substring(0, ID.length + 1) == ID + ",")
				FavID = FavID.substring(ID.length + 1, FavID.length);
			else
				if (FavID.substring(FavID.length - ID.length - 1, FavID.length) == "," + ID)
					FavID = FavID.substring(0, FavID.length - ID.length - 1);
				else
					if (FavID == ID)
						FavID = "";
	}
	document.cookie = "FavID=" + FavID + ";expires=" + expr.toGMTString() + ";path=/";
}

function DelOCookie(ID) {
	var expr = new Date();
	expr.setTime(expr.getTime() + 31104000000);
	OFavID = "";
	if (document.cookie.indexOf("OFavID=") != -1) {
		p = document.cookie.indexOf(" OFavID=") + 1;
		p1 = document.cookie.indexOf(";", p+6);
		if (p1 == -1) p1 = document.cookie.length;
		OFavID = document.cookie.substring(p+7, p1);
	}
	if (OFavID == "")
		OFavID = "";
	else {
		p = OFavID.indexOf("," + ID + ",");
		if (p != -1)
			OFavID = OFavID.substring(0, p) + OFavID.substring(p + ID.length + 1, OFavID.length);
		else
			if (OFavID.substring(0, ID.length + 1) == ID + ",")
				OFavID = OFavID.substring(ID.length + 1, OFavID.length);
			else
				if (OFavID.substring(OFavID.length - ID.length - 1, OFavID.length) == "," + ID)
					OFavID = OFavID.substring(0, OFavID.length - ID.length - 1);
				else
					if (OFavID == ID)
						OFavID = "";
	}
	document.cookie = "OFavID=" + OFavID + ";expires=" + expr.toGMTString() + ";path=/";
}

function openPopup(page) {
	window.open(page, 'vrpopup', 'resizable=yes,scrollbars=yes,width=500,height=500');
	return false;
}
function openPopup2(page) {
	window.open(page, 'vrpopup', 'resizable=yes,scrollbars=yes,width=630,height=630');
	return false;
}