//*************************//
//Ajax engine for xero//
//Author: genoxide//
//**********************//

//globalize the timerID
var timerID = setTimeout('var bogus;',1);

//find out what browser the visitor has and return the appropriate result.

function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}

var http = createRequestObject();

function gx_load(section)
{
	//alert(section);
	if ( (window.current) && (window.current != section) ) window.previous = window.current;
	sndReq(section + '&gx=1','main');
    
    window.current = section;
    //toolbar
    var webtitle = section.replace('?s=', '' );
    var webtitle = webtitle.replace('?mod=', '' );
    var webtitle = webtitle.replace('main', 'Home' );
    var patt1=/&/g;

    if (patt1.test(webtitle) != false)
    {
    var webtitle = webtitle.substr(0,+patt1.lastIndex-1);
    }
    if (patt1.test(webtitle) != false)
    {
    var webtitle = webtitle.substr(0,+patt1.lastIndex-1);
    }
    
    var webtitle = getwebtitlephp + webtitle;
    document.title = webtitle;
    if (window.previous) document.getElementById('tool_back').innerHTML = '<a onclick="gx_go(-1)" title="Go back"><img src="resources/images/back.png"></a>';
    document.getElementById('tool_refresh').innerHTML = '<a onclick="gx_go(0)" title="Refresh"><img src="resources/images/refresh.png"></a>';
    document.getElementById('tool_direct').innerHTML = '<a onclick="gx_go(1)" title="Direct link"><img src="resources/images/direct.png"></a>';


}
function chg_opac(rgs, oc, dc) {
	//alert(rgs);
		el 						= document.getElementById(rgs);
		el.style.opacity 		= oc;
		el.style.MozOpacity 	= oc;
		el.style.KhtmlOpacity 	= oc;
		el.style.filter 		= "alpha(opacity='"+(oc * 100)+"')";
		if (dc==1)
			oc-=0.1;
		else
			oc+=0.09;
		
	if ((dc==0 && oc<1.4) || (dc==1 && oc>0.2)) 
	timerID = self.setTimeout("chg_opac('"+rgs+"',"+oc+", "+dc+")", 1);  //1s
}

//back,refresh,direct
function gx_go(tohell) 
{
if ( (tohell==-1) && (window.previous.match('s=')) ) //back
gx_load(window.previous);
else if ( (tohell==0) && (window.current.match('s=')) ) //refresh
gx_load(window.current);
else if ( (tohell==1)  && (window.current.match('s=')) )//direct link
document.location.href= 'http://'+location.host+window.current; //Change localhost to the place you are hosting this.
}

//GET ajax
function sndReq(section,myfield,rf)
 {
//alert(section);
xContent = myfield; //set as global :)
http.open("get", section);
http.onreadystatechange = handleResponse;
http.send(null);
if (rf == '1') 
{
	var s=setTimeout("sndReq(section,myfield,rf)",5000);
}
}
var olddiv;

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
if (response != responseold || responsecheck != 1) {
var responsecheck = 1;
/*
var newdiv = document.createElement(xContent);
newdiv.innerHTML = http.responseText;

var container = document.getElementById(xContent);

if (olddiv) container.removeChild(olddiv);
container.appendChild(newdiv);
olddiv = newdiv;
*/
//document.createElement(xContent);
//document.getElementById(xContent).innerHTML = 'hi'
if (!response) document.getElementById(xContent).innerHTML = '';
document.getElementById(xContent).innerHTML = response;
clearTimeout(timerID);
chg_opac(xContent,0.1,0);

var responseold = response;
}
}
}

//post ajax
function sndPREQ(section,myfield,myfield2) {
//alert(section);
//alert(myfield);
//alert(myfield2);

var gmt = getquerystring(myfield2);
finalurl = section +  gmt + '&rs=1';
http.open("POST", section + '&rs=1');
//protect reg flood
var isreg = /create_account/;
var searchin = section.search(isreg);
//alert(finalurl);
if(searchin == 14) c_registered();
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", gmt.length);
http.setRequestHeader("Connection", "close");
http.send(gmt);
//alert(gmt);
//alert('hi');
//alert(document.getElementById(myfield2).outerHTML)
var loading_ = '<img align="center" src="resources/images/loader.gif">';
document.getElementById(myfield).innerHTML = loading_;
http.onreadystatechange = handleResponse;
xContent = myfield;
xContent2 = myfield2;
tempa = section;
tempb = myfield;

}
function getquerystring(myfield2) {
    var frm    = document.forms[myfield2];
    var pineapple = frm.elements;
    var iistr = '';
    var varlen = (pineapple ? pineapple.length : 0);
    tinyNO = 0;
    //alert(pineapple);
for(var i = 0;i < varlen; i++) 
{
	if (pineapple[i].name == 'hassend')  pineapple[i].value = pineapple[i].value+1;
	
	if (pineapple[i].type == "checkbox") { 
		if (pineapple[i].checked) pineapple[i].value = '1'; else pineapple[i].value = '0';
		}
			if (pineapple[i].type == "radio") { 
	if (pineapple[i].checked) pineapple[i].name= 'x' + pineapple[i].name;
		}
//TinyMCE
//alert(pineapple[i].name);
if (pineapple[i].name == 'notiny') tinyNO = 1;

if (tinyNO == 0)
{
if(typeof(tinyMCE) == 'object')
{
for (id in tinyMCE.editors){
 if (tinyMCE.getInstanceById(id) && (!xes))
 {
 var xes = 1;
 tinyMCE.execCommand('mceRemoveControl', false, id);
 tinyMCE.execCommand('mceAddControl', false, id);
}
}

}
}


   var str = escape(encodeURI(pineapple[i].value));
if (!iistr) var iistr =  pineapple[i].name + "=" + str;
else var iistr = iistr + '&' + pineapple[i].name + "=" + str;
if (pineapple[i].type == "radio") { 
if (pineapple[i].checked) pineapple[i].name=  pineapple[i].name.substring(1,pineapple[i].name.length);
}
	}
    return iistr;
}

//function to prevent massive registrations in registration form
function c_registered()
{
	document.getElementById("registered").value = '1';
}

function print_window(url)
{
window.open(url,'FYP WBMS',"status=0,location=0,scrollbars=0,width=800,height=300");
}

var editor;

function check_maxlength(myfield2, element, maxvalue)
     {
   var frm    = document.forms[myfield2];
    var pineapple = frm.elements;
     var q = pineapple[element].value.length;
     var r = q - maxvalue;
     var msg = "Sorry, you have input "+q+" characters into the "+
       "text area box you just completed. It can return no more than "+
       maxvalue+" characters to be processed. Please abbreviate "+
       "your text by at least "+r+" characters";
     if (q > maxvalue) alert(msg);
     document.getElementById('check_chars').innerHTML = (maxvalue - q);
 }


function xero_expand(myfield)
{

var expandable = document.getElementById(myfield);
if (expandable.style.display == 'none') {
expandable.style.display = 'block';
} else {
expandable.style.display = 'none';
}
}

