// branch for native XMLHttpRequest object

var req;

function makeXMLHTTP(){
	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} 
		catch(e) {
			req = false;
		}
	} 
	// branch for IE/Windows ActiveX version
	else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e) {
				req = false;
			}
		}
	}
}

function processReqGetInfo() {
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			//actions if any
			document.getElementById("info").innerHTML = req.responseText
			//alert(req.responseText);
		} 
	}
}


/* Version that can be told which element to replace */
function AjaxPage(url, element) {
	getLinkElement(url, element);
}

/* Version that can be told which element to replace */
function getLinkElement(url, element) {
	makeXMLHTTP();  
	if(req) {
		//alert(url);
		req.onreadystatechange = function(){
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {
					//actions if any
					document.getElementById(element).innerHTML = req.responseText
				} 
			}
		}
		req.open("GET", url, true);
		req.send(null);
	}
}



/* Version that can be told which element to replace and present a loading screen */
function AjaxPageLoading(url, element, htmlStr) {
	getLinkElementLoading(url, element, htmlStr);
}

/* Version that can be told which element to replace and present a loading screen */
function getLinkElementLoading(url, element, htmlStr) {
	document.getElementById(element).innerHTML = htmlStr;
	makeXMLHTTP();  
	if(req) {
		//alert(url);
		req.onreadystatechange = function(){
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {
					//actions if any
					document.getElementById(element).innerHTML = req.responseText
				} 
			}
		}
		req.open("GET", url, true);
		req.send(null);
	}
}

/* Version that can be told which element to replace */
function fadeAjaxPage(url, element) {	
	  options = Object.extend({
	  duration: .2,
	  from: 1.0,
	  to:   0.0,
	  afterFinish: function(effect) 
	    { Element.hide(effect.element);
	      effect.setOpacity(1); 
	      getLinkElement(url, element); 
	      Effect.Appear(element,{ duration: 2.0, from: 0.0, to: 1.0 }); } 
	  });
	  
	  new Effect.Fade(element,options);		
}



/***********************************************
* this function will process a form and submit
* the fields onto an AJAX results page GET method
* 
* you must include a hidden field <form onSubmit="getInfo(this);">
* and use this form tag <input type="hidden" id="ToForm" name="ToForm" value="FormStart.asp">
***********************************************/

function getInfo(f) {
	qString = getFormValues(f);
	//alert(qString);
	theForm = document.getElementById("ToForm").value;
	
	//alert(document.getElementById("ToForm").value);
	
	document.getElementById("info").innerHTML = '<br /><br /><blink>Processing... Please Wait....</blink><br /><br />';
	makeXMLHTTP();  
	url = theForm + '?' + qString;
	if(req) {
		//alert(url);
		req.onreadystatechange = processReqGetInfo;
		req.open("GET", url, true);
		req.send(null);
		
	}
}

/***********************************************
* this function will process a form and submit
* the fields onto an AJAX results page POST method
* 
* you must include a hidden field <form onSubmit="getInfo(this);">
* and use this form tag <input type="hidden" id="ToForm" name="ToForm" value="FormStart.asp">
*
* This function depends on you also includingthe prototype.js libraries
* <script type="text/javascript" src="/includes/prototype.js"></script>
***********************************************/

function postInfo(f) {


	qString = Form.serialize(f) + '&';
	
	theForm = document.getElementById("ToForm").value;	
	document.getElementById("info").innerHTML = '<br /><br /><blink>Processing... Please Wait....</blink><br /><br />';
	makeXMLHTTP();  
	
	url = theForm;
	if(req) {
		req.onreadystatechange = processReqGetInfo;
		req.open("POST", url, true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		req.send(qString);
		
	}
}

/***********************************************
* this function replaces hyper links use it to AJAX link the content with no page reload
* Example: <a href="JavaScript: void(0);" OnClick="getLink('Form.asp?ID=66');">
***********************************************/

function getLink(url) {
	makeXMLHTTP();  
	if(req) {
		//alert(url);
		req.onreadystatechange = processReqGetInfo;
		req.open("GET", url, true);
		req.send(null);
	}
}


/* Slight variation on above function. This one has a place holder "loading" option */
function getLinkLoading(url,htmlStr) {
	document.getElementById("info").innerHTML = htmlStr;
	makeXMLHTTP();  
	if(req) {
		//alert(url);
		req.onreadystatechange = processReqGetInfo;
		req.open("GET", url, true);
		req.send(null);
	}
}	

			
/***********************************************
* This code scans a form for all fields
* It gathers them into a string ready to append
* to an URL for GET method submission
***********************************************/
	
function getFormValues(fobj){
    var str='';
    for(var i=0;i< fobj.elements.length;i++){
        str+=fobj.elements[i].name+'='+ escape(fobj.elements[i].value)+'&';
    }
    str=str.substr(0,(str.length-1));
    return str;
}




/***********************************************
* Ajax Includes script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

//To include a page, invoke ajaxinclude("afile.htm") in the BODY of page
//Included file MUST be from the same domain as the page displaying it.

var rootdomain="http://"+window.location.hostname

function ajaxinclude(url) {
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
	page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
	try {
		page_request = new ActiveXObject("Msxml2.XMLHTTP")
	} 
	catch (e){
	try{
		page_request = new ActiveXObject("Microsoft.XMLHTTP")
	}
	catch (e){}
	}
	}
else
	return false
	page_request.open('GET', url, false) //get page synchronously 
	page_request.send(null)
	writecontent(page_request)
}

function writecontent(page_request){
	if (window.location.href.indexOf("http")==-1 || page_request.status==200){
		document.write(page_request.responseText)
	}
}
