// JavaScript Ajax Helper 

var http_request = false;
var count = 6; // currently set to 5 seconds 
var elementid = ''; //hold element id value

function createRequestObject() {
	var ro;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		ro = new XMLHttpRequest();
		if (ro.overrideMimeType) {
			ro.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			ro = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			ro = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {  }
		}
	}

	return ro;
}


function makeRequest_gossip(url,formx) {
 
 // uid = formx.uid.value;
 // alteredacid = formx.alteredacid.value;
  //alert();		
  http_request = createRequestObject();
 
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	};

	http_request.onreadystatechange = showContents_gossip;

	http_request.open('GET', url+"?gx="+formx.gx.value+"&gid="+formx.gid.value+"&gusername="+formx.gusername.value+"&gossip="+formx.gossip.value, true);
	formx.gossip.value = '';
	http_request.send(null);
	//return false; 
}

function showContents_gossip() {

	document.getElementById('gossipcontent').innerHTML = '<img src="images/loading.gif" width="32" height="32" align="absmiddle" />'+" Saving chat....";
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.getElementById('gossipcontent').innerHTML = http_request.responseText;
			
		} else {
			alert('There was a problem with the request.');
		}
	}
	couter();
}

function makeRequest_sendmail(url,formx) {
 // uid = formx.uid.value;
 // alteredacid = formx.alteredacid.value;
  
  http_request = createRequestObject();
 
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	};
	
	http_request.onreadystatechange = showContents_sendmail;
	http_request.open('GET', url+"?type="+formx.type.value+"&id="+formx.id.value+"&sender="+formx.sender.value+"&mail="+formx.mail.value, true);
	http_request.send(null);
	
	//return false; 
}

function showContents_sendmail() {

	document.getElementById('sendmail').innerHTML = '<img src="images/loading.gif" width="32" height="32" align="absmiddle" />'+" Sending mail....";
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.getElementById('sendmail').innerHTML = http_request.responseText;
			
		} else {
			alert('There was a problem with the request.');
		}
	}
	couter();
}

//global ajax request format
//url : process form 
//formx : request come from form 
//id : element id to show output process
function makeRequest(url,formx,id) {
	elementid = id;
	http_request = createRequestObject(); 
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	};

	http_request.onreadystatechange = showContents;
	//http_request.open('GET', url+"?id="+formx.id.value+"&sender="+formx.sender.value+"&mail="+formx.mail.value, true);
	http_request.open('GET',url,true);
	http_request.send(null);
	
	//set element id 
	
	//return false; 
}

function showContents() {

	document.getElementById(elementid).innerHTML = '<img src="images/loading.gif" width="32" height="32" align="absmiddle" />'+" Updating Comment....";
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.getElementById(elementid).innerHTML = http_request.responseText;
			
		} else {
			alert('There was a problem with the request.'+http_request);
		}
	}
	//couter();
}


//preview news 
function previewnews(url,formx,id){
	//prepare url to expand
	url += "?fulltext="+escape(formx.fulltext.value)
	makeRequest(url,formx,id);
}

//post comment 
function makeComment(url,formx,id){
	//prepare url to expand
	url += "?rem_type="+formx.rem_type.value+"&rem_link="+formx.rem_link.value+"&rem_name="+escape(formx.rem_name.value)+"&rem_text="+escape(formx.rem_text.value)+"&rem_spam="+escape(formx.rem_spam.value);
	makeRequest(url,formx,id);
}

function counter(){ // this function must be called in the html document as follows: <body onload="counter()">
	count--;

	if(count == 0) 
	{ 
		window.location = "gossiptest.php"; // enter the location that you wish to redirect visitors to
	}

	setTimeout("counter()", 1000); // timeout function invokes and loops the function every second.
}