var dom = (document.getElementById) ? 1 : 0;

function Cookie(document, name, hours, path, domain, secure) {
    this.$document = document;
    this.$name = name;
    if (hours)
      this.$expiration = new Date((new Date()).getTime() + hours*3600000);
    else this.$expiration = null;
    if (path) this.$path = path; else this.$path = "/";
    if (domain) this.$domain = domain; else this.$domain = null;
    if (secure) this.$secure = true; else this.$secure = false;
}

// This function is the store() method of the Cookie object
function _Cookie_store() {
    var cookieval = "";
    for(var prop in this) {
      if ((prop.charAt(0) == "$") || ((typeof this[prop]) == "function")) 
        continue;
      if (cookieval != "") cookieval += "&";
      cookieval += escape(this[prop]);
    }

		var cookie = this.$name + "=" + cookieval;
    if (this.$expiration)
      cookie += "; expires=" + this.$expiration.toGMTString();
    if (this.$path) cookie += "; path=" + this.$path;
    if (this.$domain) cookie += "; domain=" + this.$domain;
    if (this.$secure) cookie += "; secure";

    this.$document.cookie = cookie;
}

// This function is the load() method of the Cookie object
function _Cookie_load() {
    var colon;
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;

    var start = allcookies.indexOf(this.$name + "=");
    if (start == -1) return false;   // cookie not defined for this page.
    start += this.$name.length + 1;  // skip name and equals sign.
    var end = allcookies.indexOf(";", start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);

    var a = cookieval.split("&");  // break it into array of name/value pairs
    for(var i=0; i < a.length; i++) {   // break each pair into an array
      a[i] = unescape(a[i]);
      colon = a[i].indexOf(":");
      if (colon < 0) this[this.$name] = a[i]
      else this[a[i].substr(0, colon)] = a[i].substr(colon+1)
    }
    return true;
}

// This function is the remove() method of the Cookie object.
function _Cookie_remove() {
    var cookie;
    cookie = this.$name + "=";
    if (this.$path) cookie += "; path=" + this.$path;
    if (this.$domain) cookie += "; domain=" + this.$domain;
    cookie += "; expires=Thu, 01-Jan-2004 00:00:00 GMT";
    this.$document.cookie = cookie;
}

function cookRedir () {
	if (visitordata.load() && visitordata.pin) {
		this.location.replace ("/cgi/monitor/profily");
	} else {
		this.location.replace ("vstup.shtml");
	}
}

function loadPin() {
	if (!visitordata.load()) {
		alert("Cookie nebylo nalezeno.");
	} else if (!visitordata.pin) {
		alert("Cookie přečteno, PIN nebyl nalezen.");
	} else {
		alert("PIN OK - " + visitordata.pin);
	}
	if (false) {
		document.forms[0].USRPIN.value = visitordata.pin;
		if (dom) document.getElementById("ulozit").style.display = "inline";
		alert("PIN OK - " + document.forms[0].USRPIN.value);
	}
}

function checkPin() {
	if (document.forms[0].USRPIN.value) {
		if (dom) document.getElementById("ulozit").style.display = "inline";
	} else {
		openWindow("/cgi/monitor/logon?log=on","",315,260);
	}
}

function SetMainURL(url) {
	var op;
//	if (cook+0==0) url += znak+"USRPIN="+usrpin;
	if (window.opener.closed) { op=window.open(url, "default"); window.opener=op }
	else window.opener.location.href=url;
	if (COOK+0==0) window.opener.focus();
	else window.close();
}

function cookieAnketa(id) {
	//alert (document.cookie);
	if (!id) return;
	var kuk = "anketa"+id;
	var anketa = new Cookie(document, kuk, 720);
	if (!(anketa.load() && eval("anketa."+kuk))) {
		eval("anketa."+kuk+"=0")
		anketa.store();
	}	
}

function cookieInterst() {
	//alert (document.cookie);
	var rekl = new Cookie(document, "Reality.CZ.HP.interst", 24);
	if (rekl.load()) {
		if (rekl["Reality.CZ.HP.interst"] > 0) {
			rekl["Reality.CZ.HP.interst"] = -1;
			rekl.store();
			return false;
		}
		return true;
	} else {
		return false;
	}
}

function LogoffOK(alrt,zavri) {
	if (!window.opener.closed) {
		var op = window.opener.location;
		if (/\/monitor\//.test(op.href)) op.href="/";
		else {
			if (/USRPIN=/.test(op.href)) op.href=op.href.replace(/USRPIN=[^&]{26}/,"USRPIN=");
			else op.reload();
		}
	}
	if (alrt != '') alrt="\n"+alrt;
	alert("Uživatel odhlášen."+alrt);
	// if (zavri == '1') window.close();
	window.close();
}

function LogonOK(alert1) {
	var alert2 = "";
	var cookok = 1;
	if (COOK == "1") {
//		visitordata.pin = USRPIN;
//		visitordata.store();
		if (!visitordata.load() || !visitordata.pin) {
			alert2="\nPIN nelze uložit. Pravděpodobně máte zakázáno používání cookies.";
			cookok = !cookok;
			COOK = !COOK;
			if (alert1+alert2 != "") alert(alert1+alert2);
			window.location.href="/cgi/monitor/logon?usrtmp="+tmp;
		}
		else alert2="\nPIN uložen.";
	}
	if (USRPIN || (COOK == "1")) {
		var pin = (COOK == "1") ? "" : ("?USRPIN="+USRPIN)
		if (window.opener.closed) { 
			window.opener=window.open("/cgi/monitor/profily"+pin, "default"); 
			
		} else {
			var op = window.opener.location;
			if (/\/monitor\//.test(op.href)) op.href="/cgi/monitor/profily"+pin;
			else {
				if (/USRPIN=/.test(op.href)) op.href=op.href.replace(/USRPIN=/,"USRPIN="+USRPIN);
				else op.reload();
			}
		}
		if (alert1+alert2 != "") alert(alert1+alert2);
	}
}

// Create a dummy Cookie object, so we can use the prototype object to make
// the functions above into methods.
new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;
var visitordata = new Cookie(document, "pin", 720); 
//	var rkl = new Cookie(document, "Reality.CZ.HP.interst", 24);
//	rkl.remove();

