// JavaScript Document
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;
}


/*doAction methods are used to retrieve the html code of any file.  
For security reasons you can't retrieve html files off the current server. 
This prevents me from calling the method with file=www.google.com

the variable "file" is a string of the url of the file you wish to retrieve.
send is a string of the parameters you wish to send to the file.
id is the id of the div you wish to modify.  

Though the three following methods all make similar use of ajax by calling GetXmlHttpObject(),
what they do afterwards is different.  These distinctions are made below.
*/

//Does a direct replace of the div's html with that of the "file"
function doAction(file, send, id){
	var req = GetXmlHttpObject();
	req.open('POST',file,false);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	req.send(send);
	//alert(req.responseText);
	document.getElementById (id).innerHTML = req.responseText;
	//makeWidget();
}

function updateMsg(){
	var msg = document.getElementById('msg').value;
	if( msg.length >0 ){
		document.getElementById('msg_content_div').innerHTML += "<div style=\"float:left;\"><div style=\"color:blue;\">You</div></div><div> - " + msg +"</div>"; 
		document.getElementById('msg_content_div').scrollTop = document.getElementById('msg_content_div').scrollHeight;
		document.getElementById('msg').value = "";
	}
}

function doPollFirefoxSync(file, send, id){

}

function doPollFirefoxAsync(file, send, id){

}
var pollElapse=0;
var req;

function doPollFirefox(file, send, id){
	
	if( pollElapse > 3){
		doPollOthers(file, send, id);
		pollElapse = 0;
		return;
	}
	pollElapse++;
	
	req = GetXmlHttpObject();
	req.open('GET',file,true);
	//req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	req.onreadystatechange  = function(){ 
        if(req.readyState  == 4){
             if(req.status  == 200){ 
            	 if( req.responseText != null && req.responseText.length >4 ){
            		 document.getElementById(id).innerHTML += req.responseText;
            		 document.getElementById(id).scrollTop = document.getElementById(id).scrollHeight;
//            		 alert(req.responseText);
            		 pollElapse = 0;
            	 }
            	 
             }
        }
   }; 
	
	req.send(send);
	
	setTimeout("ajaxTimeout();",1700);
	
}

function doPollOthers(file, send, id){

	req = GetXmlHttpObject();
	req.open('POST',file,false);
	//req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	req.send(send);
	
	
//	setTimeout("ajaxTimeout();",300);
	
	
	//alert(req.responseText);
	var response_msg = req.responseText;
	if( response_msg != null && response_msg != "" ){
		document.getElementById (id).innerHTML += req.responseText;
		document.getElementById(id).scrollTop = document.getElementById(id).scrollHeight;
	}
	//makeWidget();
}

function ajaxTimeout(){
	req.abort();
}



//Will make request and then return immediately.  Page will not lock up.
function doActionAsycn(file, send, id){
	var req = GetXmlHttpObject();
	req.open('POST', file , true);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	req.onreadystatechange  = function(){ 
         if(req.readyState  == 4){
              if(req.status  == 200) 
                document.getElementById (id).innerHTML = req.responseText;
              else
			  	document.getElementById (id).innerHTML ="Error loading page";    
         }
    }; 
	req.send(send);
}



/*Calls the "file" but does not modify any div.  This is often used when file equals some .jsp that modifies SQL tables or other variables*/
function doActionWithoutDiv(file, send){
	var req = GetXmlHttpObject();
	req.open('POST',file,false);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	req.send(send);
	//alert(req.responseText);
}

/* This method appens the code of file to the div, rather than replacing it*/
function addDoAction(file, send, id){
	var req = GetXmlHttpObject();
	req.open('POST',file,false);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	req.send(send);
	//alert(req.responseText);
	document.getElementById (id).innerHTML += req.responseText;
	//makeWidget();
}

function create_request_string(theForm)
{
theForm = document.forms[theForm];
var reqStr = "";

for(i=0; i < theForm.elements.length; i++)
{
isFormObject = false;

switch (theForm.elements[i].tagName)
{
case "INPUT":

switch (theForm.elements[i].type)
{
case "text":
case "hidden":
reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
isFormObject = true;
break;

case "checkbox":
if (theForm.elements[i].checked)
{
reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
}else{
reqStr += theForm.elements[i].name + "=";
}
isFormObject = true;
break;

case "radio":
if (theForm.elements[i].checked)
{
reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
isFormObject = true;
}
}
break;

case "TEXTAREA":

reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
isFormObject = true;
break;

case "SELECT":
var sel = theForm.elements[i];
reqStr += sel.name + "=" + sel.options[sel.selectedIndex].value;
isFormObject = true;
break;
}

if ((isFormObject) && ((i+1)!= theForm.elements.length))
{
reqStr += "&";
}

}

return reqStr;
}

function create_save_network_request_string(theForm)
{
theForm = document.forms[theForm];
var reqStr = "";

for(i=0; i < theForm.elements.length; i++)
{
isFormObject = false;


if( theForm.elements[i].name == "allowedUsers"){
	var options = theForm.elements[i];
	for( j=1; j<options.length; j++){
		reqStr += theForm.elements[i].name + "=" + options[j].value;
		if( j <options.length -1){
			reqStr += "&";
		}
	}
	
	isFormObject = true;
	
}
switch (theForm.elements[i].tagName)
{
case "INPUT":

switch (theForm.elements[i].type)
{
case "text":
case "hidden":
reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
isFormObject = true;
break;

case "checkbox":
if (theForm.elements[i].checked)
{
reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
}else{
reqStr += theForm.elements[i].name + "=";
}
isFormObject = true;
break;

case "radio":
if (theForm.elements[i].checked)
{
reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
isFormObject = true;
}
}
break;

case "TEXTAREA":

reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
isFormObject = true;
break;

case "select-multiple":
reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
isFormObject = true;
break;
}

if ((isFormObject) && ((i+1)!= theForm.elements.length))
{
reqStr += "&";
}

}

return reqStr;
}