/* Settings you might want to define */
	//var waittime=100;		
	var waittime=setWaitTime("new");

/* Internal Variables & Stuff */
	//chatmsg.focus()
	document.getElementById("chatwindow").innerHTML = "ladataan...";

	var xmlhttp = false;
	var xmlhttp2 = false;

/* Request for Reading the Chat Content */
function ajax_read(url) {
	if(window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
		if(xmlhttp.overrideMimeType){
			xmlhttp.overrideMimeType('text/html');
		}
	} else if(window.ActiveXObject){
		try{
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try{
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e){
			}
		}
	}

	if(!xmlhttp) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}

	xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState==4) {
		document.getElementById("chatwindow").innerHTML = xmlhttp.responseText;

		zeit = new Date(); 
		ms = (zeit.getHours() * 24 * 60 * 1000) + (zeit.getMinutes() * 60 * 1000) + (zeit.getSeconds() * 1000) + zeit.getMilliseconds(); 
		intUpdate = setTimeout("ajax_read('./ajaxchat/r.php?x=" + ms + "')", setWaitTime(""))
		//waittime = ReadCookie('interval');
		//waittime = setWaitTime("");

		var foo = new Date; // Generic JS date object
                var unixtime_ms = foo.getTime(); // Returns milliseconds since the epoch
                var unixtime = parseInt(unixtime_ms / 1000);

			if(window.addEventListener) {
				window.addEventListener('focus', function() {
				document.title="Palasokeri.com V4";
  				createCookie('lastfocused',unixtime,1);
				},false);
			} else if (window.attachEvent) {
                                window.attachEvent('focus', function() {
                                document.title="Palasokeri.com V4";
                                createCookie('lastfocused',unixtime,1);
                                });
			}

			var lastwrite = ReadCookie('lastwrite');
			var lastfocused = ReadCookie('lastfocused')+60;
		
			//if(lastwrite > lastfocused) { document.title="V4 (+ chat)"; }

		}
	}
	
	xmlhttp.open('GET',url,true);
	xmlhttp.send(null);
}

/* Request for Writing the Message */
function ajax_write(url){
	if(window.XMLHttpRequest){
		xmlhttp2=new XMLHttpRequest();
		if(xmlhttp2.overrideMimeType){
			xmlhttp2.overrideMimeType('text/xml');
		}
	} else if(window.ActiveXObject){
		try{
			xmlhttp2=new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try{
				xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e){
			}
		}
	}

	if(!xmlhttp2) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}

	xmlhttp2.open('GET',url,true);
	xmlhttp2.send(null);

}

/* Submit the Message */
function submit_msg(){
	nick = document.getElementById("chatnick").value;
	msg = document.getElementById("chatmsg").value;

	if (nick == "") { 
		check = prompt("please enter username:"); 
		if (check === null) return 0; 
		if (check == "") check = "anonymous"; 
		document.getElementById("chatnic'k").value = check;
		nick = check;
	} 

	document.getElementById("chatmsg").value = "";
	ajax_write("./ajaxchat/w.php?m=" + encodeURIComponent(msg) + "&n=" + encodeURI(nick));
	createCookie('interval','1000',1);
	ms = (zeit.getHours() * 24 * 60 * 1000) + (zeit.getMinutes() * 60 * 1000) + (zeit.getSeconds() * 1000) + zeit.getMilliseconds();

        var foo = new Date; // Generic JS date object
        var unixtime_ms = foo.getTime(); // Returns milliseconds since the epoch
        var unixtime = parseInt(unixtime_ms / 1000);

        createCookie('lastfocused',unixtime+65,1);

	intUpdate = setTimeout("ajax_read('./ajaxchat/r.php?x=" + ms + "')", setWaitTime("new"))

}

/* Check if Enter is pressed */
function keyup(arg1) { 
	if (arg1 == 13) submit_msg(); 
}

/* Start the Requests! ;) */
var intUpdate = setTimeout("ajax_read('./ajaxchat/r.php')", setWaitTime("new"));

function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

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=/";
}

function setWaitTime($value) {
	
	var wtime;
	if($value=="new") { wtime=1000; }
	else {
	wtime = ReadCookie('interval');
	}
	return wtime;
}



