var xmlHTTP;

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function getxmlHTTP(){
	var xmlHTTP = null;
	// Firefox, Opera 8.0+, Safari
	try {xmlHTTP=new XMLHttpRequest();}
	catch (e) {
	// Internet Explorer
		try {xmlHTTP=new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e){
	    	try {xmlHTTP=new ActiveXObject("Microsoft.XMLHTTP");}
	// Browser does not support AJAX
	    	catch (e){ return false;}
	    }
	}
	return xmlHTTP;
}

function updateWatchIcons() {
	if (!document.getElementsByTagName || !(document.title.match(/(Catalog|Search Results|Watchlist)/) || String(document.location).match(/\/item.pl/))){ return; }
	var images = document.getElementsByTagName('img');

	// loop through all image tags
	for (var i=0; i<images.length; i++){
		var image = images[i];
		
		var imgurl = String(image.getAttribute('src'));
		
		// use the string.match() method to catch all watchlist links on the page
		if (imgurl.toLowerCase().match('auction_watchlist.jpg')){
			image.parentNode.onclick = (image.className.match(/iconinactive/)) ? deletefromwatchlist : addtowatchlist;
		}
	}

	var anchors = document.getElementsByTagName('a');

	// loop through all image tags
	for (var i=0; i<anchors.length; i++){
		var anc = anchors[i];
		
		var ancurl = String(anc.getAttribute('href'));
		
		// use the string.match() method to catch all watchlist links on the page
		if (ancurl.toLowerCase().match('deletewatch.pl')){
			anc.onclick = deletefromwatchlist;
			anc.setAttribute('rel','test');
		}
	}
}

function stateChanged() {
	// 	0	The request is not initialized
	// 	1	The request has been set up
	// 	2	The request has been sent
	// 	3	The request is in process
	// 	4	The request is complete
	
	if (xmlHTTP.readyState==4) { 
		alert(xmlHTTP.responseText); // for responseText to work, have to write/echo something 
	}
}

function processAdd() {
	// 	0	The request is not initialized
	// 	1	The request has been set up
	// 	2	The request has been sent
	// 	3	The request is in process
	// 	4	The request is complete
	
	if (xmlHTTP.readyState==4) { 
		rtxt = xmlHTTP.responseText
		if (rtxt.match(/success: /i)) {
			itemnum = rtxt.match(/\d*$/);
			updateIcon("inactive",itemnum);
		}
		else if (rtxt.match(/ERROR: You already have /i)) {
			itemnum = rtxt.match(/ \d* /); itemnum = String(itemnum).replace(/(\W|\s)/g,"");
			updateIcon("inactive",itemnum);
		}
	}
}

function processDelete() {
	// 	0	The request is not initialized
	// 	1	The request has been set up
	// 	2	The request has been sent
	// 	3	The request is in process
	// 	4	The request is complete
	
	if (xmlHTTP.readyState==4) { 
		rtxt = xmlHTTP.responseText
		if (rtxt.match(/success: /i)) {
			itemnum = rtxt.match(/\d*$/);
			if (!document.title.match(/Watchlist/)){ 
				updateIcon("active",itemnum);
			}
			else {
				var anchors = document.getElementsByTagName('a');
			
				// loop through all image tags
				for (var i=0; i<anchors.length; i++){
					var anc = anchors[i];
					
					var ancurl = String(anc.getAttribute('href'));
					
					// use the string.match() method to catch all watchlist links on the page
					if (ancurl.toLowerCase().match('deletewatch.pl') && ancurl.toLowerCase().match('&delete='+itemnum)){
						ancrow = anc.parentNode.parentNode;
						ancrow.parentNode.removeChild(ancrow);
						image.parentNode.onclick = addtowatchlist;
						titlechange = "Add item "+itemnum+" to your watchlist";
						image.parentNode.title = titlechange;
						image.setAttribute('alt',titlechange);
						image.title = titlechange;
						image.className = "";
					}
				}
			}

		}
	}
}

function addtowatchlist() {
var itemnum = this.getAttribute('title').match(/ \d* /);
itemnum=String(itemnum).replace(/ /g,"");

xmlHTTP = getxmlHTTP();
if (xmlHTTP==null) { return null; }

var url="addwatch.pl";
url=url+"?ajq=yes";
url=url+"&item="+itemnum;
url=url+"&username="+getCookie("Username");
url=url+"&password="+getCookie("Password");
url=url+"&sid="+Math.random();
xmlHTTP.onreadystatechange=processAdd;
xmlHTTP.open("GET",url,true);
xmlHTTP.send(null);
return false;
}

function deletefromwatchlist() {
var itemnum = this.getAttribute('title').match(/ \d* /);
itemnum=String(itemnum).replace(/ /g,"");

xmlHTTP = getxmlHTTP();
if (xmlHTTP==null) { return null; }

var url="deletewatch.pl";
url=url+"?ajq=yes";
url=url+"&delete="+itemnum;
url=url+"&username="+getCookie("Username");
url=url+"&password="+getCookie("Password");
url=url+"&sid="+Math.random();
xmlHTTP.onreadystatechange=processDelete;
xmlHTTP.open("GET",url,true);
xmlHTTP.send(null);
return false;
}

function updateIcon (state,itemnum) {
	var images = document.getElementsByTagName('img');

	// loop through all image tags
	for (var i=0; i<images.length; i++){
		var image = images[i];
		var imgurl = String(image.getAttribute('src'));
		var imgalt = String(image.getAttribute('alt'));
		
		// use the string.match() method to catch all watchlist links on the page
		if (imgurl.toLowerCase().match('auction_watchlist.jpg') && imgalt.match(" "+itemnum+" ")){
			image.parentNode.onclick = (state == "inactive") ? deletefromwatchlist : addtowatchlist;
			titlechange = (state == "inactive") ? "Remove item "+itemnum+" from your watchlist" : "Add item "+itemnum+" to your watchlist";
			image.parentNode.title = titlechange;
			image.setAttribute('alt',titlechange);
			image.title = titlechange;
			image.className = (state == "inactive") ? "iconinactive" : "";
		}
	}
}


Event.observe(window, 'load', updateWatchIcons, false);
