var gIsNav = (navigator.appName.indexOf("Netscape") >= 0);
var gIsIE  = (navigator.appName.indexOf("Microsoft") >= 0);
var gAltMask = "/[^<\">@{}=#%&\$£;*\^!|]/";
var gAlphaMask = "/[a-z,A-Z, ]/";
var gAlphaNumericMask = "/[a-z,A-Z,0-9, ]/";
var gAlphaNumericMaskHyphen = "/[a-z,A-Z,0-9,-]/";
var gTelMask = "/[ 0-9]/";
var gEmailRegEx = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var gDelimiter = new String("|");
var gPopUpWindow;

function checkEmail(email) {
	if (!gEmailRegEx.test(email.value))
		return false;
	return true;
}

function buildSelection(options, values, selectedIdx, asString) {
	// Build option and value arrays
	var optArr =options.split("|");
	var valArr = '';
	if (values==null) {
		valArr = opt;
	} else {
		valArr = values.split("|");
	}

	// Set selected option if applicable
	var selectedOpt = (selectedIdx==null) ? -1 : selectedIdx;

	// Build options and highlight selected option if applicable
	var str = '';
	for (var i=0; i < optArr.length; i++) {
		str += '<option ' + ((i==selectedOpt) ? 'SELECTED ' : '') + 
				'value="' + valArr[i] + '">' + optArr[i];
	}
	str += '</SELECT>';

	// Return option string or write to document
	if (asString) {
		return str;
	}
	document.writeln(str);
}

function buildNumericSelection(from, count, minSize, asString, increment, suppressBlank) {
	var s="  ";
	var delta=1;
	if(increment)
		delta=increment;
	var doSep=true;
	if (suppressBlank!=null) {
		doSep=!suppressBlank;
		s='';
	}
	var max = from + count;
	for (var i=from; i<max; i=i+delta) {
		if (doSep)
			s+="|";
		s+=pad(String(i),"0",minSize);
		doSep = true;
	}
	return buildSelection(s,null,null,asString);
}

function getSelectionValue(obj) {
	if (obj && obj.selectedIndex >= 0) {
		return obj.options[obj.selectedIndex].value;
	}
	return '';
}

function getSelectionTextValue(obj) {
	if (obj && obj.selectedIndex >= 0) {
		return obj.options[obj.selectedIndex].text;
	}
	return '';
}

function setSelectionValue(obj,value) {
	if (!obj) return;
	if (value!=null) {
		if (value.length>0) {
			obj.selectedIndex= getIndexOfOption(obj, value);
		} else {
			obj.selectedIndex= -1;
		}
	}
}

function changeSelection(obj, val, newOption, newValue) {
	with (obj) {
		for (var idx= 0; idx< options.length; idx++) {
			if (options[idx].value == val) {
				options[idx].text  = newOption;
				options[idx].value = newValue;
				break;
			}
		}
	}
}

function getRadioValue(obj) {
	if (obj) {
		for (var i=0; i<obj.length; i++) {
			if (obj[i].checked)
				return obj[i].value;
		}
	}
	return null;
}

function setRadioValue(obj,value) {
	if (!obj || value==null)
		return;
	if (value.length>0) {
		for (var i=0; i<obj.length; i++) {
			if (obj[i].value==value) {
				obj[i].checked=true;
				return;
			}
		}
	}
}

function showField(field,show) {
	var obj=getLayer(field);
	if (!obj) return;
	if (obj.style) {
		obj.style.visibility=show ? "visible" : "hidden";
	} else {
		obj.visibility=show ? "show" : "hide";
	}
}

function dspField(field,show) {
	var obj=getLayer(field);
	if (!obj) return;
	if (obj.style) {
		obj.style.display=show ? "inline" : "none";
	} else {
		obj.display=show ? "block" : "none";
	}
	return false;
}

function dspTableRow(field,show) {
	var obj=getLayer(field);
	if (!obj) 
		return;
	if (obj.style) {
		if (gIsIE) {
			obj.style.display=show ? "inline" : "none";
		} else {
			obj.style.display=show ? "table-row" : "none";
		}
	} else {
		obj.display=show ? "block" : "none";
	}
	return false;
}

function getLayer(layer) {
	if (document.all)
		return eval('document.all.'+layer);
	if (document.getElementById)
		return document.getElementById(layer);
	return eval('document.'+layer);
}

function writeToLayer(layer,text) {
	if (!layer) return;
	if (document.layers) {
		layer.document.write(text);
		layer.document.close();
	} else {
		layer.innerHTML=text;
	}
}

function statusMsg(msg) {
	window.status=msg;
	parent.canScroll = false;
	return true;
}

function hideMsg() {
	window.status='';
	parent.canScroll = true;
	return true;
}

function trim(text) {
	return trimL(trimR(text));
}

function trimL(text) {
	if (text==null || text.length==0)
		return '';
	var start=0;
	while(text.charAt(start) == ' ')
		start++;
	return text.substring(start, text.length);
}

function trimR(text) {
	if (text==null || text.length==0)
		return '';
	var end = text.length;
	do {end--;} 
		while (end>=0 && text.charAt(end) == ' ');
	if (end < 0)
		return '';
	return text.substring(0, end+1);
}

function pad(string, padChar, minLen, padRight) {
	var count = minLen - string.length;
	while (count-- > 0)
		string = (padRight)? (string + padChar) : (padChar + string);
	return string;
}

function isEmpty(text) {
	return (trim(text).length == 0);
}

function getIndexOfElement(arr, val) {
	for (var idx=0; idx < arr.length; idx++) {
		if (arr[idx] == val) {
			return idx;
		}
	}
	return null;
}

function addOption(obj, newOption, newValue) {
	obj.options[obj.options.length] = new Option(newOption, newValue);
	return obj.options.length - 1;
}

function getIndexOfOption(obj, value) {
	if (obj.options) {
		for (var idx= 0; idx < obj.options.length; idx++) {
			if (obj.options[idx].value == value)
				return idx;
		}
	}
	return -1;
}

function getKeyEvent(event) {
	if (event.keyCode)
		return event.keyCode;
	if (event.which)
		return event.which;
	return 0;
}

function editKey(event, exprStr) {
	var key = getKeyEvent(event);
	if (key==8 || key==9) 
		return true;
	var chr=String.fromCharCode(key);
	if (exprStr==null)
		exprStr = '/\\d/';
	var exp = eval(exprStr);
	return chr.match(exp)!=null;
}

function checkPaste(event){
	if(!gIsIE){
		return !(event.ctrlKey && editKey(event, "/v/i"));
	}
}

function makeArray(numberOfElements, initialValue) {
  	if (numberOfElements==null)
    	numberOfElements = 1;
  	if (initialValue==null)
    	initialValue = "";
  	var arr = new Array(numberOfElements);
  	for (var i=0; i<numberOfElements; i++) {
    	arr[i] = initialValue;
  	}
  	return arr;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop  = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop  += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function openPopUp(url,event,width,height,move,centre,options) {
	var win = gPopUpWindow;
	if(width==null)
		width = 500;
	if(height==null)
		height = 500;
	if (!win || win.closed) {
		if (!options) {
			options="resizable,scrollbars";
		}
		win=window.open(url,"","width=" + width + ",height=" + height + ","+options);
		gPopUpWindow=win;
		var y=(event!=null)?event.screenY:9999;
		if (move!=false)
			if(centre){
				moveWin(win,(screen.availWidth-width)/2,(screen.availHeight-height)/2)
			}else{
				moveWin(win,screen.availWidth-width-20,20);
			}
	} else {
		if (win.closed) win.open();
		win.focus();
		win.location.replace(url);
	}
	return false;
}


function moveWin(win,x,y) {
	win.moveTo(x,y);
}

function getYaxis() {
	return window.screenTop!=undefined ? window.screenTop : window.screenY;
} 

function getXaxis() {
	return window.screenLeft!=undefined ? window.screenLeft : window.screenX;
} 

function rollImage(imgName,rollImage) {
	document.images[imgName].src = rollImage;
}

function rollOnImage(imgName,rollImage) {
	document.images[imgName].src = rollImage;
}
function rollOffImage(imgName,rollImage) {
	document.images[imgName].src = rollImage;
}

