
// set event handlers

function setEventHandlers() {
	var inputs = document.body.getElementsByTagName("INPUT")
	for (var i=0; i<inputs.length; i++) {
		if (inputs[i].type == "text") {
			if (inputs[i].className == "numeric") {
				inputs[i].onkeypress = chkNumeric
			    inputs[i].onfocus = chgStyle
			    inputs[i].onblur = chgStyle
			}
			if (inputs[i].className == "decimal") {
				inputs[i].onkeypress = chkDecimal
			    inputs[i].onfocus = chgStyle
			    inputs[i].onblur = chgStyle
			}
			if (inputs[i].className == "date") {
			    inputs[i].onhelp = function() {
			                        popUpCalendar(this.nextSibling,this, 'd mmm yyyy')
			                       }
			    inputs[i].onchange = chkDate
			}
		}
		if (inputs[i].type == "button" || inputs[i].type == "submit") {
		    if (inputs[i].value == "Back") {
		        inputs[i].onclick = quit
		    }
		    if (inputs[i].value == "Close") {
		        inputs[i].onclick = closeWin
		    }
        }
	}
	
	var imgs = document.body.getElementsByTagName("IMG")
	for (var i=0; i<imgs.length; i++) {
	    if (imgs[i].className == "dateSearch") {
	        imgs[i].onclick = function() {
	                            popUpCalendar(this, this.previousSibling, 'd mmm yyyy')
	                          }
	    }
	}
}


//change style
function chgStyle() {
	el = event.srcElement;
	if (event.type == "focus") {
		el.style.textAlign = "left"
	}
	if (event.type == "blur") {
		el.style.textAlign = "right"
		if (hasValue(el) && (el.className == "decimal")) {
			el.value = toChar(toNumber(el.value))
		}
	}
}

// tabs functions
function setSpace() {
	var tabs = document.getElementById("tabs");
	var tab1 = document.getElementById("tab1");
	var divContent = document.getElementById("divContent");
	var space = document.getElementById("space");

	tabs.style.pixelWidth = divContent.offsetWidth;
	if (document.getElementById("tab2")) {
		space.style.pixelWidth = divContent.offsetWidth - (80 * 5) - 6
	} else {
		space.style.pixelWidth = divContent.offsetWidth - (80 * 1) - 10
	}
//	while(Math.abs(space.offsetTop - tab1.offsetTop) > 2) {
//		space.style.pixelWidth -= 1
//	}
}

function msOver() {
	var el = event.srcElement;
	if (el.tagName == "A") {
		el = el.parentElement;
	}
	if (el.tagName != "SPAN" || el.className != "tab") {
		return;
	}
	el.className = "tabO"
}

function msOut() {
	var el = event.srcElement;
	if (el.tagName == "A") {
		el = el.parentElement;
	}
	if (el.tagName != "SPAN" || el.className != "tabO") {
		return;
	}
	el.className = "tab"
}

// get random number between x and y
function getRandom(x, y) {
	var range = y - x + 1;
	return Math.floor(Math.random() * range) + x;
}


// check that a field has value
function hasValue(fld) {
	if (!fld || !fld.value)
		return false;
	var vEntered = false;
	for (p=0; p<fld.value.length; p++) {
		if (fld.value.charAt(p) != " ") {
			vEntered = true;
			break;
		}
	}
	if (vEntered && (p > 0))
		fld.value = fld.value.substr(p);
	return vEntered;
}


// on key press (check numeric key)
function chkNumeric() {
	if (event.keyCode < 48 || event.keyCode > 57) 
		event.returnValue = false;
}


// on key press (check decimal number)
function chkDecimal() {
	if (((event.keyCode < 48) || (event.keyCode > 57)) && (event.keyCode != 45) && (event.keyCode != 46))
		event.returnValue = false;
}

// open centralized pop-up window
function openPopUp(height, width, winName, url) {
	var x_pos = ((screen.availWidth - 10) - width)/2;
	var y_pos = ((screen.availHeight - 30) - height)/2;

	var features = "scrollbars, resizable, status,";
	features += "height=" + height + ", ";
	features += "width=" + width;
	features += ", left=" + x_pos + ",screenX=" + x_pos;
	features += ", top=" + y_pos + ",screenY=" + y_pos;
	  
	popUp2 = window.open(url, winName, features);
	setTimeout("popUp2.focus()", 500)
    
}

// convert text to number
function toNumber(txt) {
	if (!txt)
		return 0;
	var p = txt.indexOf(",");
	while (p > -1) {
		txt = txt.substr(0, p) + txt.substr(p + 1);
		p = txt.indexOf(",", p);
	}
	return parseFloat(txt);
}


// convert float number to character
function toChar(nbr) {
	if (nbr == 0)
		return "0";
	if (nbr > 0 ) {
		var nbrChr = (nbr + 0.005).toString();
	} else {
		nbrChr = (nbr - 0.005).toString();
	}
	var p = nbrChr.indexOf(".");
	if (p == -1) {
		nbrChr += ".00";
	} else {
		if (p == nbrChr.length - 2) {
			nbrChr += "0";
		} else {
			nbrChr = nbrChr.substr(0, p + 3);
		}
	}
	if (nbrChr == "-0.00") {
		nbrChr = "0.00"
	}

	if (nbrChr.length > 6) {
		var p = nbrChr.indexOf(".")
		var dec = nbrChr.substr(p)
		var whole = nbrChr.substring(0, p)
		var l = whole.length
		var chr3Ary = new Array()
		var i = 0
		while (l > 3) {
			l = l - 3
			chr3Ary[i] = whole.substr(l, 3)
			i += 1
		}
		chr3Ary[i] = whole.substr(0, l)

		nbrChr = ""
		for (var n = i; n >= 0; n--) {
			nbrChr += chr3Ary[n]
			if (n > 0) {
				nbrChr += ","
			} 
		}
		nbrChr = nbrChr + dec
	}
	
	return nbrChr;
}

// exit page
function quit() {
	if (window.history.length > 0) {
		window.history.back()
		setTimeout("self.close()", 500)
	} else {
		self.close()
	}
}

// close window
function closeWin() {
    self.close()
}
