
/* 	Right now this script is configured for only handling form elements that are text textarea and checkbox
	It would be nice to create a loading div...
 */
var READY_STATE_UNINITIALIZED = 0;
var READY_STATE_LOADING = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_INTERACTIVE = 3;
var READY_STATE_COMPLETE = 4;

var xmlHttpRequest = null;
var console = null;
var recDivId = null;
var original = null;


/* Get the XMLHttpRequest object */
function getXmlHttpRequest(){
  if (window.XMLHttpRequest){
    xmlHttpRequest = new XMLHttpRequest();
  }else if (typeof ActiveXObject != "undefined"){
     xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return xmlHttpRequest;
}

/* Send the Http Request object to server*/
function sendHttpRequest(url, params, httpMethod){
  if(httpMethod == null){
    httpMethod = "GET";
  }
  xmlHttpRequest = getXmlHttpRequest();
  if (xmlHttpRequest != null){
    xmlHttpRequest.onreadystatechange = readyStateChangeCallback;
    xmlHttpRequest.open(httpMethod, url, true);
    xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttpRequest.send(params);
  }
}

/* Call back to monitor and receive data from server*/
function readyStateChangeCallback(){
  var readyState = xmlHttpRequest.readyState;
  var data = 'loading...';
  if (readyState == READY_STATE_COMPLETE){
		data = xmlHttpRequest.responseText;
		if(original == null){
			displayData(data, recDivId);
		}else{
			concatData(data, recDivId, original);	
		}

  }else{
		displayLoading(data, recDivId);
  }
}

// Display Loading
function displayLoading(data, id){
/*   if( console != null){
    var newLine = document.createElement("div");
    console.appendChild(newLine);
    var txt = document.createTextNode(data);
 */	var loading = '<div style=" font-weight:bold; font-size:14px;	position: fixed;	top: 5px;	right: 5px;	color: #FFFFFF;	background-color: #FF0000;	padding: 4px;">Loading...</div>';

 	div = document.getElementById(id);
	div.style.position = 'relative';
	div.innerHTML = loading+div.innerHTML;
	
	
    //newLine.appendChild(txt);
  //}
  // IF I was to add a function to parse javascript it would go here
}


/* Display the received data in the */
function displayData(data, id){
/*   if( console != null){
    var newLine = document.createElement("div");
    console.appendChild(newLine);
    var txt = document.createTextNode(data);
 */	
 	div = document.getElementById(id);
	div.innerHTML = data;
	
	
    //newLine.appendChild(txt);
  //}
  // IF I was to add a function to parse javascript it would go here
}

function concatData(data, id, orig){
/*   if( console != null){
    var newLine = document.createElement("div");
    console.appendChild(newLine);
    var txt = document.createTextNode(data);
 */	
 	div = document.getElementById(id);
	div.innerHTML = orig +"<BR>"+data;
	
	
    //newLine.appendChild(txt);
  //}
  // IF I was to add a function to parse javascript it would go here
}


function encodeform(formID){
	var form = document.getElementById(formID);
	count = form.length;
//	alert(count);
	var pst = "";
	for(i=0;i<count;i++){
		if(i != 0)
		pst += "&";
		if(form.elements[i].type == 'checkbox'){
			if(form.elements[i].checked == true)
			pst += escape(form.elements[i].name) + "=" + escape(form.elements[i].value);

		}
		else{
		pst += escape(form.elements[i].name) + "=" + escape(form.elements[i].value);
		}
	}
	return pst;
}


/* Get the data when the button is clicked */
function getData(page, div, form){
  recDivId = div;		
  console = document.getElementById(div);
  original = null;
  if(form != ''){
  sendHttpRequest(page, encodeform(form), "POST");
  }else{
  sendHttpRequest(page, '', "GET");
  }
}

function addData(page, div, form){
  recDivId = div;		
  console = document.getElementById(div);
  original = console.innerHTML;
  if(form != ''){
  sendHttpRequest(page, encodeform(form), "POST");
  }else{
  sendHttpRequest(page, '', "GET");
  }
}

/* If you just think you need to load an iFrame Instead */
function loadIframe(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;   
    return false;
  }
  else return true;
}