//General JavaScript functions
//Include this file in all pages that use JS.

/**
 * General cross-browser DOM object referencer.
 * @param idDiv string with the object id (defined in the id attribute of the object's tag).
 * @param refDoc DO NOT USE, recursive Netscape layer lookup.
 * @return the object's reference, false if not found.
 */
function getDivRef(idDiv, refDoc) {
	if (!refDoc)
		refDoc = document;
	
	if (document.layers) {
		if (refDoc.layers[divID])
			return refDoc.layers[divID];
		else {
			for (var x = 0, y; !y && x < refDoc.layers.length; x++)
				y = getDivRef(divID, refDoc.layers[x].document);
			
			return y;
		}
	} else if (document.getElementById)
		return document.getElementById(idDiv);
	else if (document.all)
		return document.all[idDiv];
	
	return false;
}

/**
 * Allows to enter only numbers (and the period) in text boxes.
 * @param e event object (standards-compliant browsers)
 * @return true if the key is numeric.
 */
function justNum(e) {
	var evt = window.event ? window.event : e;
	var keyCode = evt.keyCode ? evt.keyCode : evt.which ? evt.which : false;
	
	if (keyCode) {
		if ((keyCode >= 48 && keyCode <= 57) || keyCode == 46 || keyCode == 8 || keyCode == 9)
			return true;
	}
	
	evt.returnValue = false;
	evt.cancelBubble = true;
	
	if (document.all)
		evt.keyCode = 0;
	else {
		evt.preventDefault();
		evt.stopPropagation();
	}
	
	return false;
}

function fmtNumberTyping(txtBox) {
	var intPart = txtBox.value;
	var decPart = "";
	
	if (intPart.indexOf(".") >= 0) {
		var splt = value.split(".");
		intPart = splt[0];
		decPart = splt[1];
	}
	
	var deformatted = deformatNumber(intPart);
	
	intPart = "";
	var cnt = 0;
	
	for (var i = deformatted.length - 1; i >= 0; i--) {
		intPart = deformatted.charAt(i) + ((cnt % 3) == 0 && cnt != 0 ? "," : "") + intPart;
		cnt++;
	}
	
	txtBox.value = intPart + (decPart != "" ? "." + decPart : "");
}

function deformatNumber(num) {
	var deformatted = "";
	
	for (var i = 0; i < num.length; i++)
		deformatted += (num.charAt(i) != "," ? num.charAt(i) : "");
	
	return deformatted;
}

/**
 * Formats a number in currency format (nnnnn.dd)
 * @param num number to format
 * @return formatted number
 */
function formatCurrency(num) {
	var num = num.toString().replace(/\$|\,/g,"");
	
	if (isNaN(num))
		num = "0";
	
	var sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	var cents = num % 100;
	num = Math.floor(num / 100).toString();
	
	if (cents < 10)
		cents = "0" + cents;
	
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + "," +
				num.substring(num.length - (4 * i + 3));
	
	return (((sign) ? "" : "-") + num + "." + cents);
}

/**
 * Adds a list element to any list.
 * @param selRef reference to a HTML list.
 * @param text text of the option
 * @param value value of the option
 */
function addOption(selRef, text, value) {
	var opt = document.createElement("option");
	opt.text = text;
	opt.value = value;
	
	selRef.options.add(opt);
}

/**
 * Removes all elements from any list
 * @param selRef reference to a HTML list.
 */
function removeAllOptions(selRef) {
	for (var i = selRef.options.length; i >= 0; i--)
		selRef.remove(i);
}

/**
 * Refills a form if validation did not pass, and
 * colors incorrect/missing fields.
 * @param valArray Array containing fields and refills.
 */
function refillForm(valArray) {
	var isInvalid = "";
	
	for (var i = 0; i < valArray.length; i++) {
		var pair = valArray[i].split("|");
		var ctrlRef = getDivRef(pair[0]);
		var ctrlType = (ctrlRef ? (ctrlRef.type ? ctrlRef.type.toLowerCase() : "unknown") : "");
		
		if (ctrlType == "text" || ctrlType == "textarea" || ctrlType == "password") {
			if (ctrlType != "password")
				ctrlRef.value = pair[1];
			
			if (pair.length > 2) {
				var isReqOrVal = pair[2];
				
				if (isReqOrVal) {
					if (isReqOrVal == "1") {
						ctrlRef.style.color = "#FFFFFF";
						ctrlRef.style.backgroundColor = "#ff6666";
					}
					
					if (isReqOrVal == "2") {
						ctrlRef.style.backgroundColor = "#FFFF99";
						isInvalid += "I";
					}
					
					if (isReqOrVal == "3") {
						ctrlRef.style.backgroundColor = "#FFFF99";
						isInvalid += "E";
					}
					
					if (isReqOrVal == "4") {
						ctrlRef.style.color = "#FFFFFF";
						ctrlRef.style.backgroundColor = "#ff6666";
						isInvalid += "P";
					}
					
					if (isReqOrVal == "5") {
						ctrlRef.style.backgroundColor = "#FFFF99";
						isInvalid += "U";
					}
				}
				
			}
			
		} else if (ctrlType == "select-one") {
			for (var j = 0; j < ctrlRef.options.length; j++) {
				if (ctrlRef.options[j].value == pair[1])
					ctrlRef.selectedIndex = j;
			}
		}
	}
	
	return isInvalid;
}

/**
 * General cross-browser XMLHttpRequest object creator.
 * @return a reference to a XMLHttpRequest object (AJAX).
 */
function getHttpRequestObject() {
	var xmlHttp;
	
	if (document.all) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ex) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (exc) {
				xmlHttp = false;
			}
		}
	} else {
		if (typeof XMLHttpRequest != "undefined") {
			try {
				xmlHttp = new XMLHttpRequest();
			} catch (ex) {
				xmlHttp = false;
			}
		}
	}
	
	return xmlHttp;
}