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=/";
        updatepage_old();
}

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 idexist(id){


/*
	var fchar = iid.substring(0,1);
	var aid;
	var id;
	if (fchar == 'i'){
		aid = 'a'+iid.substring(1);
         	var url = document.getElementById(aid).href;
         	id = getid(url);
	} else 
		id = iid.substring(1);
*/


//	var id = iid.substring(1);  f99999 or i999999

	ids = readCookie("fave");
	if (ids == null) return false;
	ids = unescape(ids);
	ida = ids.split(":");
	for (var i=0;i<ida.length;i++){
		if (id == ida[i]) return true;
	}
	return false;

}

function eraseCookie(name) {
        createCookie(name,"",-1);
}


function added(){


}

function remove(id){
     updatepage(id,"delete");

}

function initfave(){
	if (readCookie("fave") == null)
		return;
	updatepage(999999,"display");
}


var xmlhttp;

function updatepage(sturl,action){

xmlhttp = null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  var id;
  if (action == null) action = "add";
  if (action == "delete") id = sturl.substring(1);
  else if (action == "add"){
  	var vars = sturl.split(":",2);
	var suffix = vars[0];
	var id = vars[1];
	var iid = "i"+id;
        document.getElementById(iid).src="/st/img/added"+suffix+".gif";
  }
  else id = sturl; // don't need this now

  var url = "/myfave.php?id="+id+"&action="+action;
  xmlhttp.onreadystatechange=state_Change;
  xmlhttp.open("GET",url,true);
//  xmlhttp.send(null);
xmlhttp.setRequestHeader("If-Modified-Since","Sat, 1 Jan 2008 00:00:00 GMT");
dd = new Date();
tt = dd.getTime();
thetimestring=tt.toString(10);
xmlhttp.send(thetimestring);


  }
else
  {
    document.getElementById('favearea').innerHTML = "Your browser does not support XMLHTTP.";
  }
}





function state_Change() {
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    document.getElementById('favearea').innerHTML=xmlhttp.responseText;
    }
  else
    {
    document.getElementById('favearea').innerHTML = "Problem retrieving data:" + xmlhttp.statusText;
    }
    xmlhttp.close;
  }
}




function updatepage_old(){
        var fave=readCookie("fave");
 	var fa = fave.split('|');
	document.getElementById('favearea').innerHTML = "<a href='"+fa[0]+"'><img src='"+fa[1]+"'></a>";
}

function mouseOver(myimgid)
{
	var fchar=myimgid.substring(0,1);
	if (fchar == "f"){
        	document.getElementById(myimgid).src="/st/img/remove.gif";
	}
	else {
  		var vars = myimgid.split(":",2);
		var imgsuffix = vars[0];
		var id = vars[1];
		if (idexist(id))
        		document.getElementById("i"+id).src="/st/img/added"+imgsuffix+".gif";
		else
        		document.getElementById("i"+id).src="/st/img/add"+imgsuffix+".gif";

	}
}


function mouseOut(myimgid)
{


	var fchar=myimgid.substring(0,1);
	if (fchar == "f")
        	document.getElementById(myimgid).src="/st/img/blank.gif";
	else {
  		var vars = myimgid.split(":",2);
		var id = vars[1];
        	document.getElementById("i"+id).src="/st/img/blank.gif";
	}
}

function getid(sturl) {
  if (sturl == null) return '';
  var i = (sturl+'').indexOf('?');
  var query = sturl.substring(i+1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == 'id') {
      return pair[1];
    }
  } 
  return '';
}


var myImgs = ['/st/img/add1.gif', '/st/img/add3.gif', '/st/img/add7.gif','/st/img/blank.gif'];

function preload(imgs) {
    var img;
    for (var i = 0, len = imgs.length; i < len; ++i) {
        img = new Image();
        img.src = imgs[i];
    }
}

preload(myImgs)

