// JavaScript Document
// JavaScript Document

/*
	Description: 
*/
function byId(lookIn, id){
	return lookIn.getElementById(id);	
}

/*
	Description: 
*/
function byTagName(lookIn, name) {
	return lookIn.getElementsByTagName(name);	
}

/*
	Description: 
*/
function byName(lookIn, name) {
	return lookIn.getElementsByName(name);	
}

/*
	Description: Return the next sibling of this node with spacified tagName
*/
function nextNode(node, nextTagNameNode) {
	var node = node.parentNode;
	var next;
	var len = node.parentNode.childNodes.length;

	for( var i=0; i<len; i++) {
		next = node.nextSibling;
		
		if(next.tagName == nextTagNameNode) {
			return next;
		}
	}
}

/*
	Description:
*/
function childElement(node, index, tagName) {
	len = node.childNodes.length;
	iteration = 1;
	
	for(var i=0; i<len; i++) {
		child = node.childNodes[i];
		
		if(child.nodeType == 1) { //if you are an element node
			
			if(iteration == index || typeof index == "undefined") {
				return child
			}
			
			iteration++;
		}
	}
	
	return false
}


function childVal(node, index) {
	child = childElement(node, index);
	len = child.childNodes.length;
	
	for(var i=0; i<len; i++) {
		son = child.childNodes[i];
		
		if(son.nodeType == 3) {
			return son.nodeValue;
		}
	}
	
	return false;
}

function childTagName(node) {
	val = childElement(node).tagName;
	
	if(typeof val != "undefined") {
		return val;
	}
	
	return false;
}

function grandchildVal(node, chIndex, grIndex) {
	
	if(val = childElement(node, chIndex)) {
		return childVal(val, grIndex)
	}
}
/*
	Description: Shortcut to retrieve a nodes value, assuming the node
	your looking at has an inner node of type 'text'
	Test: <div>Hello</div>
	e.g. nodeVal(document.getElementByTagName('div')[0]);
	
*/
function nodeVal(node) {
	return node.firstChild.nodeValue;	
}

/*

*/
function nextSiblingVal(node) {
	return node.nextSibling.nodeValue	
}
/*
	Descriptions: sends the params paramater string to a fileName,
	which upon completion of loading returns the contents of the 
	page called back to the method variable which is a method to run
*/
function doPost( fileName, params, method) {
	xmlHttp = returnXmlHttp();

	if (xmlHttp == null)  {
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	xmlHttp.open("POST", fileName, true);

	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.onreadystatechange = method;
	xmlHttp.send(params);
}

/*
	Description:
*/
function returnXmlHttp() {
	//Setup the xmlHttp variable for the appropriate browser
	var xmlHttp;
	try	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e)	{
		// Internet Explorer
		try	{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)	{
			try	{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e)	{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

/*
	Description: Create a parameter string to send to a dynamic webpage
	@param validation: number, string, anything
*/
function concatParams(id, validation) {
	var el = byId(document,id).value;
	var valid = false;
	
	if(validation == "number") { valid = !isNaN(el) || el == ""; }
	else if(validation == "string") { valid = isNaN(el) || el == ""; }
	else { valid = true; }
	
	string = id + "=" + el;
	return valid ? string : "";
}
function handleIntegerOnlyField(id) {
	var element = byId(document,id);
	var value = element.value;

	if(value.length > 0) {
		
		if(value.charAt(0) == 0 || !isFinite(value.charAt(0))) {
			element.value = "";
		}
		
		if(!isFinite(value.charAt(value.length - 1)) || value.charAt(value.length - 1) == " ") {
			element.value = value.substr(0, value.length - 1);
		}
		
		for(var i = 0; i < value.length - 1; i++) {
		
			if(!isFinite(value.substr(i,1))) {
				element.value = "";
				break;
			} 
		}
	}
}
