var cookieXmlHttp;
var userid;



/*
 * the cookie function is called onload to set the user id
 */

function GetXmlHttpObject() {
  var xmlHttp = null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}


function cookie() {
	if (readCookie("wedography") == null) {
		getNewCookieId();
	} else {
		setViewerId(readCookie("askrandoms"));
	}
}

function setViewerId(id) {
	alert("setting viewer id to " + id);
	
}

function setCookie() {
	if (cookieXmlHttp.readyState == 4) {
		var id = cookieXmlHttp.responseText;
		createCookie("askrandoms", parseInt(id), 100);
		setViewerId(parseInt(id));
	}
}

function getNewCookieId() {
	cookieXmlHttp = GetXmlHttpObject();
	var url = "php/getuniqueuserid.php?";
	cookieXmlHttp.onreadystatechange = setCookie;
	cookieXmlHttp.open("GET", url, true);
	cookieXmlHttp.send(null);
}

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 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=/";
}
