/**
 * 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;
	}
}

//=====================================================
function cursorWait() {
	var fe;
		for(var f=0;f<=(document.forms.length-1);f++) {
			for(var e=0;e<=(document.forms[f].elements.length-1);e++){
				fe=document.forms[f].elements[e];
				fe.style.cursor='wait';
				
				if(fe.type) {
					if(fe.type.indexOf("select-one")==0 || fe.type.indexOf("select")==0) fe.style.cursor='wait';
				}
			}
		}
}
//=====================================================
function resetCursor(objForm)
	{
		for (i=0; i<objForm.elements.length; i++) {
			objForm.elements[i].style.cursor = 'auto';
		}
	}
	
//=====================================================
function trim(strText)
	{
		//this will get rid of leading spaces
		while (strText.substring(0,1) == ' ')			
			strText = strText.substring(1, strText.length)
		//this will get rid of trailing spaces
		while (strText.substring(strText.length-1,strText.length) == ' ')			
			strText = strText.substring(0, strText.length-1)
		return strText
	}	//end trim()

//====================================================

//With nested layers for netscape, this function 
//hides the layer if it's visible and visa versa
//Toggles same div on/off

//'nest' arg is optional
function showHide(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0; 
	if(obj.visibility=='visible' || obj.visibility=='show') {
			obj.visibility='hidden';
			obj.display='none';
	}
	else {
		obj.visibility='visible';
		obj.display='block';
	}
}

//Shows the div
function show(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0; 
	obj.visibility='visible';
	obj.display='block';
}
//Hides the div
function hide(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0; 
	obj.visibility='hidden';
	obj.display='none';
}

//swaps between two divs
function swap(showObj,hideObj)
{
	show(showObj);
	hide(hideObj);
}
//===============================================================
// -------------------------------------------------------------------
// TabNext()
// Function to auto-tab phone field
// Arguments:
//   obj :  The input object (this)
//   event: Either 'up' or 'down' depending on the keypress event
//   len  : Max length of field - tab when input reaches this length
//   next_field: input object to get focus after this one

/*
Example:

<input type=text name="Phone_1" size=3 maxlength=3 onKeyDown="TabNext(this,'down',3)" onKeyUp="TabNext(this,'up',3,this.form.Phone_2)">
	-
	<input type=text name="Phone_2" size=3 maxlength=3 onKeyDown="TabNext(this,'down',3)" onKeyUp="TabNext(this,'up',3,this.form.Phone_3)">
	-
	<input type=text name="Phone_3" size=4 maxlength=4>
*/

// -------------------------------------------------------------------


/*
var phone_field_length=0;
function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		phone_field_length=obj.value.length;
		}
	else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length=obj.value.length;
			if (phone_field_length == len) {
				next_field.focus();
				}
			}
		}
	}
*/
/*
<input type="text" name="input1" maxlength="3" onkeyup="autoTab(this,
'input2');">
<input type="text" name="input2" maxlength="3" onkeyup="autoTab(this,
'input3');">
<input type="text" name="input3" maxlength="4" onkeyup="autoTab(this, null);">

*/
function autoTab(element, nextElement) {
    if (element.value.length == element.maxLength && nextElement != null) {
        element.form.elements[nextElement].focus();
    }
}



//onKeyPress function (does not prevent copy & paste)
function ValidateNumeric(){
	//Allows only integer values...
	//Used with the 'onkeypress' event of a textbox
	var keyCode = window.event.keyCode;
	if (keyCode > 57 || keyCode < 48){window.event.returnValue = false;}
}
//===============================================================
//Generating Pop-up Print Preview page
// USE:  <a href="javascript:void(0)" onFocus="this.blur()" onClick="javascript:getPrint('myDivTag','Sample Window Title');return false;">Print</a>
//===============================================================
function getPrint(print_area,strPageTitle)
{ 
	//Creating new page
	var pp = window.open();
	
	//Adding HTML opening tag with <HEAD> … </HEAD> portion 
	pp.document.writeln('<html><head><title>' + strPageTitle + '</title>');
	pp.document.writeln('<link href="/home.css" type="text/css" rel="stylesheet">');
	pp.document.writeln('<script language="JavaScript" type="text/javascript"> function kill(){} </script>');		//hack to satisfy function call
	pp.document.writeln('<base target="_self"></head>');
	//Adding Body Tag
	pp.document.writeln('<body>');
	//Adding form Tag
	pp.document.writeln('<form method="post">');
	//Creating two buttons Print and Close within a HTML table
	pp.document.writeln('<table width="100%" cellpadding="4" cellspacing="0" style="margin:0px"><tr><td></td></tr><tr><td align="right">');
	pp.document.writeln('<input id="print" type="button" value="Print" ');
	pp.document.writeln('onclick="javascript:location.reload(true);window.print();">');
	pp.document.writeln('<input id="close" type="button" value="Close" onclick="javascript:window.close();">');
	pp.document.writeln('</td></tr><tr><td>&nbsp;</td></tr></table>');
	//Writing print area of the calling page
	pp.document.writeln(document.getElementById(print_area).innerHTML);
	//pp.document.writeln('</div>');
	//Ending Tag of </form>, </body> and </HTML>
	pp.document.writeln('</form></body></html>'); 
} 


/*
======= POP-UP WINDOW Function ===========================
Author: Fred Scales

An example of the code used to fire this function:
<A HREF="somepage.asp" 

onclick="openPopupWindow(this.href,'theWindow','640','480','scrollbars=no,menubar=yes,resizable=no,status=no,toolbar=no');return false;">Click Here open Popup Window</A>
-- OR --
openPopupWindow('somepage.asp','theWindow','640','480','scrollbars=no,menubar=yes,resizable=no,status=no,toolbar=no');

Optional parameter values:
	w = 'full'
	h = 'full'
Will create a width or a height that occupies the full screen of that user's available space.
=========================================================
*/
	var win = null
	var winl;
	var wint;
	function openPopupWindow(mypage, myname, w, h, strother) {

	// if w = 'full' and h = 'full'
	if(h == "full") {
		//h = screen.height - 170;		//To adjust for windows taskbar
		h = window.screen.availHeight - 130;	//To adjust for windows taskbar
		wint = 0;
	}
	else{
		wint = 20;
	}
	
	if(w == "full") {
		//w = screen.width - 10		//Just a little space
		w = window.screen.availWidth - 10
		strother += ' fullscreen=yes';
		winl = 0;
	}
	else{
		winl = (screen.width - w) / 2;	
	}
	
	// default if no w or h specified
	if(isNaN(h) || (h == "")) {
		h = 420;
	}
	if(isNaN(w) || (w == "")) {
		w = 620;
	}
	
	
	winprops = 'width='+w+',height='+h+',top='+wint+',left='+winl+','+strother+''
	win = window.open(mypage, myname, winprops)
	
	
	//Check if window already exists
		if (!win || win.closed)	{
			//store new window object in global variable
			win = window.open(mypage, myname, winprops);
		}
		else {
			//Window already exists, so bring it forward
			win.focus()
		}
		
}

//=============================================
  var xmlhttp = false;

function openXMLHttpObj() {
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        xmlhttp = false;
      }
    }
  }
	return xmlhttp;
}

//=============================================================
//////////////////////////////////////////////////
//      XML Data Traversal                      //
//      (c) 2003 Premshree Pillai               //
//      http://www.qiksearch.com                //
//      http://premshree.resource-locator.com   //
//      Email : qiksearch@rediffmail.com        //
//////////////////////////////////////////////////

var xmlDoc;

function loadXML(xmlFile) {
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");	
        xmlDoc.async="false";
        xmlDoc.onreadystatechange=verify;
        xmlDoc.load(xmlFile);
}
function verify() { 
        if(xmlDoc.readyState!=4)
                return false; 
}
//Traverse() is the function that performs XML data traversal. 
//It is a recursive function that takes a node as it's argument: 
function traverse(tree) {
        if(tree.hasChildNodes()) {
                document.write('<ul><li>');
                document.write('<b>'+tree.tagName+' : </b>');
                var nodes=tree.childNodes.length;
                for(var i=0; i<tree.childNodes.length; i++)
                        traverse(tree.childNodes(i));
                document.write('</li></ul>');
        }
        else
                document.write(tree.text);
}

function initTraverse(file) {
        loadXML(file);
        var doc=xmlDoc.documentElement;
        traverse(doc);
}


