/* ====================================================
//
// Client side script utilities.
//
=====================================================*/
// Page version: v1.27, released 13 January 2004

var isIE = (browserSniff() == "IE");

function exEscape(str) {
	return escape(str).replace(/[+]/gi,"%2B");
}

function exUnescape(str) {
	return unescape(str.replace(/(%2B)/gi, "+"));
}

function formatMoney(n) {
	var num = nz(n, "&nbsp;");
	return ((!isNaN(num)) ? "$" + format(num, 2) : num);
}

function cloneObject() {

    var oNew = new Object();
    
    // iterate through the objects properties
    for (var i in this) {
        // don't process clone() method, which is part of every Object
        if (!(typeof(this[i]) == "function" && i == "clone")) {
        
            // if the property is an Object, clone it
            if (typeof(this[i]) == "object") {
				oNew[i] = this[i].clone();
			} else {
				oNew[i] = this[i].valueOf();
			}
        }
    }
    
    // return the new Object
    return oNew;
}

function goToNextElement(element) {
	var elements = element.form.elements;
	var i;
	var nextElement;
	
	//get element index
	for (i = 0; i < elements.length; i++) { 
		if (element == elements[i]) {
			break;
		}
	}

	//get next available element
	for (var j = 0; j < elements.length; j++) {
		i++;
		if (i == elements.length) {
			i = 0;
		}
		
		nextElement = elements[i];
		if (nextElement.type != "hidden" && nextElement.style.display != "none" && !nextElement.disabled) {
			try {
				nextElement.focus();
				if (nextElement.type == "text") {
					nextElement.select();
				}
				break;
			} catch(e) {}
		}
	}
}

function goToFirstAvailableElement(form) {
	var elements = form.elements;
	var j;
	var thisElement;

	//loop through elements collection
	for (var j = 0; j < elements.length; j++) {
		thisElement = elements[j];
		if (thisElement.type != "hidden" && thisElement.style.display != "none" && !thisElement.disabled) {
			try {
				thisElement.focus();
				if (thisElement.type == "text") {
					thisElement.select();
				}
				break;
			} catch(e) {}
		}
		
		if (j == elements.length) {
			j = 0;
		}
	}	
}

function setFocus(element) {
	if (!element.disabled) {
		try {
			// Go to the required div/tab
			/*if (typeof(isWizard) {
				if (currentStep != document.all(element.tab.replace(/^td/, "div")).step) {
					showStep(document.all(element.tab.replace(/^td/, "div")));
				}
			} else {
				if (tblTabs.currentTab != element.tab) { 
					document.all(element.tab).click();
				}
			}*/
			element.focus();
			if (element.type == "text") {
				element.select();
			}
		} catch (e) {}
	}
}

function getRadioGroupSelectedIndex(radio) {
	if (radio != null) {
		if (typeof(radio.length) == "undefined") {
			if (radio.checked)
				return -1;
		}else{
			for (var i = 0; i < radio.length; i++) {
				if (radio[i].checked)
					return i;
			}
		}
	} 
	
	return null;
}

function getRadioGroupValue(radio) {
	if (typeof(radio.length) == "undefined") {
		if (radio.checked)
			return radio.value;
	}else{
		for (var i = 0; i < radio.length; i++) {
			if (radio[i].checked)
				return radio[i].value;
		}
	}
}

function setRadioGroupValue(radio, value) {
	if (typeof(radio.length) == "undefined") {
        radio.checked = true;
	}else{
		for (var i = 0; i < radio.length; i++) {
		    if (radio[i].value == value)
			    radio[i].checked = true;
		}
	}
}

function isRadioGroupSelected(radio) {
	if (typeof(radio.length) == "undefined") {
		if (radio.checked)
			return true;
	}else{
		for (var i = 0; i < radio.length; i++) {
			if (radio[i].checked)
				return true;
		}
	}
	return false;
}

function addSeparator(str, strSeparator) {
	if (str.length > 0)
		str += strSeparator;

	return str;
}

function element(elementName, elementValue, attributeArray, twice) {
	var attributes = "";
    var xml;
	
    for (var attributeName in attributeArray) {
		if (twice) {
			attributes += " " + attributeName + "=\"" + formatXML(formatXML(attributeArray[attributeName])) + "\"";
		} else {
			attributes += " " + attributeName + "=\"" + formatXML(attributeArray[attributeName]) + "\"";
		}
    }
    
    if (elementValue == null || elementValue.length == 0) {
        xml = "<" + elementName + attributes + "/>";
    } else {
        xml = "<" + elementName + attributes + ">" + elementValue + "</" + elementName + ">";
    }
    
    return xml;
}

function formatXML(str) {
	if (str == null)
		return str;
		
	str = new String(str);
		
	str = str.replace(/&/g, "&amp;");
	str = str.replace(/"/g, "&quot;");
	str = str.replace(/'/g, "&apos;");
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
    
    return str;
}

function formatHTML(str) {
	if (str == null)
		return str;
		
	str = new String(str);
		
	str = str.replace(/"/g, "&quot;");
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	str = str.replace(/\r\n/g, "<br>");
	
	return str;
}

function formatJS(str) {
	if (str == null)
		return str;
		
	str = new String(str);
		
	str = str.replace(/\\/g, "\\\\");
	str = str.replace(/"/g, "\\\"");
	str = str.replace(/'/g, "\\\'");
	str = str.replace(/\r/g, "\\r");
	str = str.replace(/\n/g, "\\n");
	
	return str;
}

function formatSQL(str) {
	if (str == null)
		return str;
		
	str = new String(str);
		
	str = str.replace(/'/g,"\''");
	
	return str;
}

function openWindow(page, pagename, width, height, windowfeatures) {
	if (windowfeatures != "") {
		//windowfeatures = "," + windowfeatures + ",status";
		windowfeatures = "," + windowfeatures;
	}
	
	var left = screen.availWidth/2 - width/2;	
	var top = screen.availHeight/2 - height/2;	
		
	pagename = pagename.toString().replace(/[\s-&.]/g, "");

	var w  = window.open(page, pagename,"LEFT=" + left + ",TOP=" + top + ",HEIGHT=" + height + ",WIDTH=" + width + windowfeatures);	

	try {
		if (windowfeatures.indexOf('maximized') != -1) {
			w.moveTo(0,0);
			if (document.all) {
				w.resizeTo(screen.availWidth,screen.availHeight);
			} else if (document.layers||document.getElementById) {
				if (w.outerHeight < screen.availHeight||w.outerWidth < screen.availWidth) {
					w.outerHeight = screen.availHeight;
					w.outerWidth = screen.availWidth;
				}					
			}		
		}
	} catch(e) {}

	var browser = browserSniff();
	
	if (browser != "NS" && browser != "OPR") {
		eval("try {w.focus();} catch(e) {}");
	}
	
	return w;
}

function browserSniff() {
	if (document.layers) {
		return "NS";
	}
	if (document.all) {
		var agt = navigator.userAgent.toLowerCase();
		var is_opera = (agt.indexOf("opera") != -1);
		
		if(is_opera) {
			return "OPR";
		} else {
			return "IE";
		}
	}
	if (document.getElementById) {
		return "MOZ";
	}
	return "OTHER";
}

function centreWindow(w, width, height) {
	w.resizeTo(width, height);
	w.moveTo(screen.availWidth/2 - width/2, screen.availHeight/2 - height/2);
}

function setRowEvents(table, evt, func, start) {
	for (var i = start; i < table.rows.length; i++)
		eval("table.rows[" + i + "]." + evt + " = " + func +";");
}

function trimNonNumericChars(varStr) {
	return varStr.replace(/\D/g,"");
}

function trimWhiteSpace(varStr) {
	return varStr.replace(/\s/g,"");
}

function lTrim(varStr) {
	return varStr.replace(/^\s+/,"");
}

function rTrim(varStr) {
	return varStr.replace(/\s+$/,"");
}

function trim(varStr) {
	return varStr.replace(/^\s+|\s+$/g,"");
}

function maxLength(field, length) {
	if (field.value.length > length){
		alert("You have exceeded the maximum number of characters (" + length + ") for this field by " + (field.value.length - length) + " character(s).");
		field.focus();
		return false;
	}else{
		return true;
	}
}

function hide(x){
	x.style.display = "none";
}

function show(x){
	x.style.display = "block";
}

//used for moving the selected item in fromSelect to toSelect - where they are exclusive from each other.
function moveSelection (fromSelect, toSelect) {
	var index = fromSelect.selectedIndex;

	for (var i = 0; i< fromSelect.length; i++)
		if (fromSelect.options[i].selected){
		
			var newOption = document.createElement("OPTION");
			newOption.value = fromSelect.options(i).value;
			newOption.text =  fromSelect.options(i).text;

			toSelect.add(newOption);		
			fromSelect.remove(i);
			i--;
		}
		
	if (index != fromSelect.length)
		fromSelect.selectedIndex = index;
	else if (fromSelect.length != 0)
		fromSelect.selectedIndex = index - 1;
}

function selectAdd (toSelect, value, text) {
	var newOption = document.createElement("OPTION");
	newOption.value = value;
	newOption.text =  text;

	toSelect.add(newOption);		
}

function selectClear (list) {
	while (list.length > 0){
		list.options.remove(0);
	}
}

function selectRemoveOption(list, optionValue) {
    var options = list.options;
 
    for (var i = 0; i < options.length; i++) {
		if (options[i].value == optionValue) {
			options.remove(i);
			break;
		}
    } 
}

function selectSelectAll (list){
    var options = list.options;
 
    for (var i = 0; i < options.length; i++) {
        options[i].selected = true;
    } 
}

function selectReset (list){
    var options = list.options;
 
    for (var i = 0; i < options.length; i++) {
        options[i].selected = false;
    } 
}

function setSelectTextValue (list, text){
	var options = list.options;
	
	for (var i=0; i < options.length; i++)
		if (options[i].text == text){
			list.selectedIndex = i;
			return;
		}	
}

function setSelectValue (list, value){
	var options = list.options;
	
	for (var i=0; i < options.length; i++)
		if (options[i].value == value){
			list.selectedIndex = i;
			return;
		}	
}

function setMultiSelectTextValue (list, text){
	var options = list.options;
	
	for (var i=0; i < options.length; i++)
		if (options[i].text == text){
			options[i].selected = true;
			return;
		}	
}

function setMultiSelectValue (list, value){
	var options = list.options;
	
	for (var i=0; i < options.length; i++)
		if (options[i].value == value){
			options[i].selected = true;
			return;
		}	
}

function getSelectTextValue(list) {
	var text = ""
	if (list.options.length > 0) {
		text = list.options[list.selectedIndex].text;
	}
	return text
}

function getSelectValue(list) {
	var value = ""
	if (list.options.length > 0) {
		value = list.options[list.selectedIndex].value;
	}
	return value
}

function getMultiSelectValue(list) { 
	var options = list.options; 
	var values = new Array(); 
	var x = 0;  
	
	for (var i = 0; i < options.length; i++) {
		if (options[i].selected) {   
			values[x++] = options[i].value;   
		} 
	}
 
	return values;
}

function getMultiSelectTextValue(list) { 
	var options = list.options; 
	var values = new Array(); 
	var x = 0;  
	
	for (var i = 0; i < options.length; i++) {
		if (options[i].selected) {   
			values[x++] = options[i].text;   
		} 
	}
 
	return values;
}

function phoneOnly() {
	if ((window.event.keyCode < 48 || window.event.keyCode > 57) && window.event.keyCode != 32) {
		window.event.returnValue = false;
	}
}

function numericOnly(point) {
	if (!point && window.event.keyCode == 46) {
		window.event.returnValue = false;
	}
	if ((window.event.keyCode < 45 || window.event.keyCode > 57) && window.event.keyCode != 47) {
		window.event.returnValue = false;
	}
}

function checkBounds(field, minNumber, maxNumber) {
	if (field.value.length == 0) {
		return true;
	}
	
	if (parseFloat(field.value) < minNumber) {
		alert("The minimum valued allowed for this field is " + minNumber + ".");
		field.focus();
		field.select();
		return false;
	}
	if (parseFloat(field.value) > maxNumber) {
		alert("The maximum valued allowed for this field is " + maxNumber + ".");
		field.focus();
		field.select();
		return false;
	}

	return true;
}

function decimalPlaces(field, dp){
	var endRange;

	if (dp == 0 && window.event.keyCode == 46){
		window.event.returnValue = false;
		return;
	}
	
	// CATCH INVALID CHARACTERS
	if ((window.event.keyCode < 47 || window.event.keyCode > 57) && window.event.keyCode != 45 && window.event.keyCode != 46) {
		window.event.returnValue = false;
		return;
	}	
	
	if (document.selection.type == "None") {
		// WHEN TEXT NOT SELECTED
		
		endRange = document.selection.createRange();
		endRange.moveEnd("textedit");

		// CATCH MULTIPLE -'S
		if (window.event.keyCode == 45 && (field.value.indexOf("-") != -1 || (field.value.length - endRange.text.length) != 0)) {
			window.event.returnValue = false;
			return;
		}
		
		if (dp != 0) {	
		
			// CATCH DECIMAL POINTS 
			if (window.event.keyCode == 46 && (field.value.indexOf(".") != -1 || endRange.text.length > dp)) {
				window.event.returnValue = false;
				return;
			}	

			// CATCH DECIMAL PLACES 
			if (window.event.keyCode != 45 && window.event.keyCode != 46 && endRange.text.indexOf(".") == -1 && getDecimalPlaces(field.value) >= dp) {
				window.event.returnValue = false;
				return;
			}
		}
		
	} else if (document.selection.type == "Text"){
		// WHEN TEXT SELECTED
	
		endRange = document.selection.createRange();	

		// CATCH MULTIPLE -'S
		if (window.event.keyCode == 45 && (field.value.indexOf(endRange.text) > 0) || (endRange.text.indexOf("-") == -1 && field.value.indexOf("-") != -1)) {
			window.event.returnValue = false;
			return;
		}
		
		if (dp != 0) {
		
			// CATCH DECIMAL POINTS 
			if (window.event.keyCode == 46 && endRange.text.indexOf(".") == -1 && field.value.indexOf(".") != -1) {
				window.event.returnValue = false;
				return;
			}	
					
		}
	}
}

function formatC(value){
	value = value + "";
	var start = 0;

    if (value.length > 0) {
	    if (value.substr(0, 1) == "-") {
		    start = 1;
	    }
    	
	    if (value.indexOf(".") == -1) {
		    value = value + ".";
	    }
    	
	    if (value.substr(start, 1) == ".") {
		    value = value.substr(0, start) + "0" + value.substr(start, value.length - start);
	    }
    	
	    while (value.length - value.indexOf(".") - 1 < 2) {
		    value += "0";
	    }
    }

    return "$" + value;
}

function formatCurrency(field, dp) {
	var start = 0;
	
	if (field.lastvalue != field.value && field.value.length > 0) {
	
		if (field.value.substr(0, 1) == "-") {
			start = 1;
		}
			
		if (dp > 0 && field.value.indexOf(".") == -1) {
			field.value += ".";
		}
		
		if (field.value.substr(start, 1) == ".") {
			field.value = field.value.substr(0, start) + "0" + field.value.substr(start, field.value.length - start);
		}
		
		while (field.value.length - field.value.indexOf(".") - 1 < dp) {
			field.value += "0";
		}
		
		field.lastvalue = field.value;
	}
}

function excludeCharacters(chars) {
	for (var i = 0; i < chars.length; i++) {
		if (chars.charCodeAt(i) == window.event.keyCode) {
			window.event.returnValue = false;
			return;
		}
	}
}

function getDecimalPlaces(num) {
	if (num.indexOf(".") == -1) {
		return 0;
	} else {
		return num.length - num.indexOf(".") - 1;
	}
}

function nz(val, rtn) {
	return cnull(val, rtn);
}

function mt(val, rtn) {
	return MT(val, rtn);
}

function MT(val, rtn) {
	if (val.length == 0) {
		return rtn;	
	} else {
		return val;	
	}
}

function cnull(val, rtn){
	if (val == null  || val == 'null' || val == 'undefined' || typeof(val) == 'undefined')
		return rtn;	
	else
		return val;		
}

function charToUpper(){
	var k = window.event.keyCode;
	
	if (k > 96 && k < 123){
		window.event.keyCode = k - 32;
	}
	return;
}

function format(n, d, wholeNumberDelim, showDollarSymbol) {
 
	with (Math) {
		// Separate the sign from the number
		var sign  = (n < 0) ? "-" : "";
		var num  = abs(n);

		num = floor((num * pow(10, d + 1) + 5) / 10) / pow(10, d);
		var whole = floor(num).toString();
		var frac  = round((num - whole) * pow(10, d)).toString();
	}

	// Zero-fill decimal
	for ( ; frac.length < d ; ) {
		frac = "0" + frac;
	}

	if (wholeNumberDelim) {
		for (var i = 0; i < Math.floor((whole.length - (1 + i)) / 3); i++) {
			whole = whole.substring(0, whole.length - (4 * i + 3)) + wholeNumberDelim + whole.substring(whole.length - (4 * i + 3));
		}
	}
	
	if (showDollarSymbol) {
		sign = "$" + sign;
	}

	// Put it all back together
	if (d == 0) {
		return sign + whole;
	} else {
		return sign + whole + "." + frac;
	}
}

function round(value, direction, decimalPlaces) {
	var result;
	var sign = sgn(value);
	var adjustment = 0;
	var multiplier = Math.pow(10, decimalPlaces);
	
	if (direction.toLowerCase() == "up") {
		adjustment = 0.5;
	}
	
	result = sign * parseInt((Math.abs(value) * multiplier) + adjustment) / multiplier;
	
	return result;
}

function sgn(value) {	
	var result;
	
	if (value != Math.abs(value)) {
		result = -1;
	} else {
		result = 1;
	}
	
	return result;
}


function attachJSEvent(eventName, func) {
	eval(eventName + "Func = func;");
}

function raiseJSEvent(eventName, params) {
	var func;		
	var result;
	
	eval("func = " + eventName + "Func;");

	if (func) 
		result = func(params);

	return result;
}	

//object of fields names for a recordset from remote script
function fieldNames(rtnValue){
	var flds = new Array();
	
	for(var i = 0; i < rtnValue[0].length; i++){
		flds[rtnValue[0][i].toString()] = i;
	}
	
	this.Fields = flds;
}

function escapeEx(str) {
    return escape(str).replace(/[+]/gi, "%2B");
}

function unescapeEx(str) {
    return unescape(str.replace(/(%2B)/gi, "+"));
}
