//object detection to return the correct object depending upon broswer type. Used by the getAX(); function.
function getNewHttpObject() {
    var objType = false;
    try {
        objType = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            var xmlHttpVersions = new array('MSXML2.XMLHTTP.6.0',
                                            'MSXML2.XMLHTTP.5.0',
                                            'MSXML2.XMLHTTP.4.0',
                                            'MSXML2.XMLHTTP.3.0',
                                            'Microsoft.XMLHTTP');
            //try all IE possibilities
            for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++)
            {
              try
              {
                objType = new ActiveXObject(xmlHttpVersions[i]);
              }
              catch (e) {}
            }
        } catch(e) {
            objType = new XMLHttpRequest();
        }
    }
    return objType;
}

//Function used to update page content with new xhtml fragments by using a javascript object, the dom, and http.
function getAX(url,elementContainer){
	var theHttpRequest = getNewHttpObject();
	theHttpRequest.onreadystatechange = function() {processAX(elementContainer);};
	theHttpRequest.open("GET", url, true);
	theHttpRequest.setRequestHeader("Content-Type","text/javascript; charset=utf-8");
	theHttpRequest.send(true);

		function processAX(elementContainer){
		
		   if (theHttpRequest.readyState == 2){
		      // document.getElementById("loaderdiv").style.visibility = \'hidden\';
		       //document.getElementById("loaderdiv2").style.visibility = \'visible\';
		   }
		   
		   if (theHttpRequest.readyState == 1){
		      // document.getElementById("loaderdiv").style.visibility = \'hidden\';
		      // document.getElementById("loaderdiv2").style.visibility = \'visible\';
		   }
		   if (theHttpRequest.readyState == 3){
		       //document.getElementById("loaderdiv").style.visibility = \'hidden\';
		       //document.getElementById("loaderdiv2").style.visibility = \'visible\';
		   }

		   if (theHttpRequest.readyState == 4) {
			   if (theHttpRequest.status == 200) {
			        // document.getElementById("loaderdiv").style.visibility = \'visible\';
		              // document.getElementById("loaderdiv2").style.visibility = \'hidden\';
		               if(theHttpRequest.responseText != null || theHttpRequest.responseText != '')
		               {
				   document.getElementById(elementContainer).innerHTML = theHttpRequest.responseText;
				   }
				   else
				   {
				    document.location.href="index.php";
				   }
			   } else {
				   document.location.href="index.php";
			   }
		   }
		}

}


var http_request = false;
var elementdiv = "";
   function makeRequest(url, parameters, tempelement) {
      http_request = false;
      elementdiv = tempelement;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType(\'text/xml\');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = alertContents;
      http_request.open('GET', url + parameters, true);
      http_request.send(null);
   }

   function alertContents() {
      if (http_request.readyState == 2){
		      // document.getElementById("loaderdiv").style.visibility = \'hidden\';
		      // document.getElementById("loaderdiv2").style.visibility = \'visible\';
		   }
		   
		   if (http_request.readyState == 1){
		      // document.getElementById("loaderdiv").style.visibility = \'hidden\';
		      // document.getElementById("loaderdiv2").style.visibility = \'visible\';
		   }
		   if (http_request.readyState == 3){
		      /// document.getElementById("loaderdiv").style.visibility = \'hidden\';
		      // document.getElementById("loaderdiv2").style.visibility = \'visible\';
		   }
      
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
        // document.getElementById("loaderdiv").style.visibility = \'visible\';
	  // document.getElementById("loaderdiv2").style.visibility = \'hidden\';
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById(elementdiv).innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(url, obj, element) {
      var getstr = "&";
      for (i=0; i<obj.childNodes.length; i++) {
         if (obj.childNodes[i].tagName == "INPUT") {
            if (obj.childNodes[i].type == "text") {
               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            }
            if (obj.childNodes[i].type == "hidden") {
               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            }
            if (obj.childNodes[i].type == "password") {
               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            }
            if (obj.childNodes[i].type == "checkbox") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               } else {
                  getstr += obj.childNodes[i].name + "=&";
               }
            }
            if (obj.childNodes[i].type == "radio") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               }
            }
         } 
         if (obj.childNodes[i].tagName == "SELECT") {
            var sel = obj.childNodes[i];
            getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
         }
         if (obj.childNodes[i].tagName == "TEXTAREA") {
            getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
         }
         
      }
      makeRequest(url, getstr, element);
   }