/* ====================================================
//
// Client side script utilities.
//
=====================================================*/
// Page version: v1.24, released 23 September 2003

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 setFocus(element) {
	if (!element.disabled) {
		element.focus();
		if (element.type == "text") {
			element.select();
		}
	}
}

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 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 formatHTML(str) {
	if (str == null)
		return str;
		
	str = new String(str);
		
	str = str.replace(/"/g, "&quot;");
	str = str.replace(/ /g, "&nbsp;");
	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;
	}

	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);
	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 messageBox(title, buttons, width, height, args) {
	return window.showModalDialog("/Common/MessageBox.asp?Title=" + title + "&Buttons=" + buttons, args, "center:yes;dialogWidth:" + width + "px;dialogHeight:" + height + "px;status:no;");
}

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 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 getSelectTextValue(list) {
	return list.options[list.selectedIndex].text;
}

function getSelectValue(list) {
	return list.options[list.selectedIndex].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 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 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 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 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) {
 
  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;

  // Put it all back together
  return sign + whole + "." + frac;
}

function CBitToChecked(val){
	if(val==1)
		return "checked";
	else
		return "";
}

//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 PCase(STRING)
{
	var strReturn_Value = "";
	var iTemp = STRING.length;
	
	if(iTemp == 0)
	{
		return"";
	}
	
	var UcaseNext = false;
	
	strReturn_Value += STRING.charAt(0).toUpperCase();
	
	for(var iCounter=1;iCounter < iTemp;iCounter++)
	{
		if(UcaseNext == true)
		{
			strReturn_Value += STRING.charAt(iCounter).toUpperCase();
		}
		else
		{
			strReturn_Value += STRING.charAt(iCounter).toLowerCase();
		}
		
		var iChar = STRING.charCodeAt(iCounter);
		
		if(iChar == 32 || iChar == 45 || iChar == 46)
		{
			UcaseNext = true;
		}
		else
		{
			UcaseNext = false
		}
		
		if(iChar == 99 || iChar == 67)
		{
			if(STRING.charCodeAt(iCounter-1)==77 || STRING.charCodeAt(iCounter-1)==109)
			{
				UcaseNext = true;
			}
		}
	}

	return strReturn_Value;
}
