/* ====================================================
//
// Client side script utilities.
//
=====================================================*/
// Page version: v1.30, released 2005-03-29

function GUID() {
	var guid = "{"; 
	
	for (var i = 1; i <= 32; i++) { 
	
		var n = Math.floor(Math.random() * 16.0).toString(16); 
		guid += n; 
		
		if ((i == 8) || (i == 12) || (i == 16) || (i == 20)) {
			guid += "-"; 
		}
	} 
	
	guid += "}"; 
	
	return guid;
}

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 setFocus(element) {
	if (!element.disabled) {
		try {
			element.focus();
			if (element.type == "text") {
				element.select();
			}
		} catch (e) {}
	}
}

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 + ",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);

	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;
			}					
		}		
	}

	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;
	newOption.innerText =  text;

	//toSelect.options.add(newOption);		
	toSelect.appendChild(newOption);		
}

function selectClear (list) {
	while (list.length > 0){
		//list.options.remove(0);
		list.removeChild(list.options[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 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) {
	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 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 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) {
	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) {
 
  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;
}

// Dynamically add an input field to a form
function addField (form, fieldType, fieldID, fieldName, fieldValue) {
  if (document.getElementById) {
	var input = document.createElement("INPUT");
	input.setAttribute("type", fieldType);
	input.setAttribute("id", fieldID);
	input.setAttribute("name", fieldName);
	input.setAttribute("value", fieldValue);
    form.appendChild(input);
  }
}

// add handler to window onload
function addWindowLoadHandler(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// add handler to form submit
function addFormSubmitHandler(func, form) {	
	var formRef = form
	if (formRef == null)
		formRef = document.forms(0);
	if (formRef)
	{
		var form = document.forms(0);
		var oldonsubmit = form.onsubmit;
		if (typeof form.onsubmit != 'function') {
			form.onsubmit = func;
		} else {
			form.onsubmit= function() {
				oldonsubmit();
				func();
			}
		}
	}
}

// preloads images
function registerImages () {
	var args = registerImages.arguments;
	var arrImages = new Array();
	var counter;
	for (counter=0; counter<args.length; counter++)
	{
		arrImages[counter] = new Image();
		arrImages[counter].src = args[counter];
	}
}

// registers a tri state button image
function registerButtonImage(btnID)
{
	// get the image
	var btnImage = document.getElementById(btnID);	
	if (!btnImage)
		return;	
	// get the source string for image
	var btnImageSource = btnImage.src;
	// get the image extension
	var btnImageFileExt = btnImageSource.substring(btnImageSource.lastIndexOf('.'));	
	// get the image path
	var btnImagePath = btnImageSource.substring(0, btnImageSource.lastIndexOf('/') + 1);	
	// get the image file name from source string	
	var btnImageFileName = btnImageSource.substring(btnImageSource.lastIndexOf('/') + 1, btnImageSource.lastIndexOf('.'));		
	// get the default state file name if we are using an image in an altered state
	if (btnImageFileName.lastIndexOf('_over') > 0 || btnImageFileName.lastIndexOf('_down') > 0) {
		btnImageFileName = btnImageFileName.substring(0, btnImageFileName.length - 5);
	}
	if (btnImageFileName.lastIndexOf('_on') > 0) {
		btnImageFileName = btnImageFileName.substring(0, btnImageFileName.length - 3);
	}	
	// create paths
	var overPath = btnImagePath + btnImageFileName + "_over" + btnImageFileExt;
	var downPath = btnImagePath + btnImageFileName + "_down" + btnImageFileExt;
	var outPath = btnImagePath + btnImageFileName + btnImageFileExt;
	registerImages(overPath, downPath);
	// register events
	btnImage.onmouseover = function() {
		btnImage.src= overPath;
	}
	btnImage.focus = function() {
		btnImage.src= overPath;
	}
	btnImage.onmousedown = function() {
		btnImage.src= downPath;
	}
	btnImage.onkeydown = function() {
		btnImage.src= downPath;
	}
	btnImage.onmouseout = function() {
		btnImage.src = outPath;
	}	
	btnImage.blur = function() {
		btnImage.src = outPath;
	}	
}

// registers tri state button images
function registerButtonImages()
{
	var args = registerButtonImages.arguments;
	var counter;
	for (counter=0; counter<args.length; counter++)
	{
		registerButtonImage(args[counter]);
	}		
}