/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}

/*
Functions for http requests.
reqType = "GET" or "POST"

doc methods:
	open( method, URL, async, user, password )	Creates a connection to the specified URL
	send( data )	Issue the request

check methods:
	xmlhttp.getAllResponseHeaders() - all headers

	xmlhttp.getResponseHeader("Last-Modified")) - to check last modified

	if (xmlhttp.status==200)
		alert("URL Exists!")
	else if (xmlhttp.status==404)
		alert("URL doesn't exist!")
	else
		alert("Status is "+xmlhttp.status)

	frm=document.forms[0]
	url="add.1?a="+frm.elements['a'].value+"&b="+frm.elements['b'].value

xmlhttp.onreadystatechange=function() {
   if (xmlhttp.readyState==4) {
    document.forms[0].elements['total'].value=xmlhttp.responseText
   }
  }



objHTTP = new ActiveXObject('Microsoft.XMLHTTP');
  objHTTP.open('POST',"OtherPage.asp",false);
  objHTTP.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
  objHTTP.send("id=1&user="+txtUser.value+"&password="+txtPassword.value);
  strResult=objHTTP.responseText;




*/

function CreateHttpRequest() {
	if ( window.XMLHttpRequest ) { // Mozilla-based browsers
		request = new XMLHttpRequest();
	} else if ( window.ActiveXObject ) { // IE-based browsers
		request = new ActiveXObject( "Msxml2.XMLHTTP" );
		if ( !request ) { // old version IE-based browsers
			request = new ActiveXObject( "Microsoft.XMLHTTP" );
		}
	}
	return request; // can still be null!
}

function httpRequest( reqType, url, asynch ) {
	request = CreateHttpRequest();
	if ( request == null ) {
		alert( "Your browser does not permit the use of all of this application's features!" );
		return;
	}
	// If the reqType param is POST, then the fifth arg is the POSTed data
	if ( reqType.toLowerCase() != "post" ) {
		httpRequestSend( reqType, url, asynch );
	} else {
		// The POSTed data
		var args = arguments[ 3 ];
		if ( args != null && args.length > 0 ) {
			httpRequestSend( reqType, url, asynch, args );
		}
	}
}

function httpRequestSend( reqType, url, asynch ) {
	try {
		// Specify the function that will handle the HTTP response
		request.onreadystatechange = httpRequestResponse;
		request.open( reqType, url, asynch );
		// If the reqType param is POST, then the fifth argument to the function is the POSTed data
		if ( reqType.toLowerCase() == "post" ) {
			// Set the Content-Type header for a POST request
			request.setRequestHeader( "Content-Type", "application/x-ww-form-urlencoded; charset=UTF-8" );
			request.send( arguments[ 3 ] );
		} else {
			request.send( null );
		}
	} catch ( errv ) {
		alert( "The application cannot contact the server at the moment.\nPlease try again in a few seconds.\n\nError detail:\n" + errv.message );
	}
}

function httpRequestResponse() {
/*
properties:
	readyState	State of the document
	status	HTTP response code
	responseText	Document as a string
	responseXML	Document as XML

response type: text/xml or text/plain

*/
	if ( xmlDoc.readyState != 4 )
		return;
	document.getElementById("output").value = xmlDoc.responseText;
}

