<!--// 
/*---- Begin External JavaScript ----*/

function getGlobalValue(varname) {
	// this form is defined in tiles/main_layout.jsp, so it should always exist
	var formObj = document.getElementById('globalvars');
	if ( formObj != null ) {
		var elemObj = formObj.elements[varname];
		if ( elemObj != null ) {
			return elemObj.value;
		}
	}
	
	return '';
}

function getLocalValue(varname) {
	// this form should be defined in any page that wants jsp vars accessible from .js files.  If it isn't, this
	// function will simple return an empty string
	var formObj = document.getElementById('localvars');
	if ( formObj != null ) {
		var elemObj = formObj.elements[varname];
		if ( elemObj != null ) {
			return elemObj.value;
		}
	}
		
	return '';
}

function showStatus(isbusy, statusmsg) {
	/*
	var statusimage = document.getElementById('statusimage');
	//var statustext = document.getElementById('statustext');
	
	if ( (isbusy == true) || (isbusy == 'undefined') ) {
		
		if ( statusmsg == null || statusmsg == '' ) {
			statusmsg = getGlobalValue('LBL_DEFBUSY');
		}
		statustext.innerHTML = statusmsg;
		
		statusimage.src = getGlobalValue('BUSY_IMAGE');
	}
	else {
		//statustext.innerHTML = getGlobalValue('LBL_STATUS');
		statusimage.src = getGlobalValue('NONBUSY_IMAGE');
	}
	*/
}

/**
	When a checkbox is selected set the corresponding hidden field value to Y or N
	<input name="billshift"type="checkbox" value="<%=ADAPT_PREFIX+SHIFT_BILL_YN%>" onclick="setCheckbox(this.form,this.name);"/>
	<input name="<%=ADAPT_PREFIX+SHIFT_BILL_YN%>"  type="hidden" value="" />
*/
function setCheckbox(formobj,chkboxName) {
	var writeFld,hiddenFld;
	
	hiddenFld = formobj[chkboxName].value;
	if ( hiddenFld != null && hiddenFld != undefined ){
		writeFld = formobj[hiddenFld];
		writeFld.value = (formobj[chkboxName].checked == true)?'Y':'N';
	}
}

/* This function should be used to eliminate non-standard value attributes in form elements
** Status : Under Development
** Returns result for strName[elemValue] assigned in the array
** Example call getValues(this.form,this.value,'state,ID,val')
** elemValue="1,2,3" : assignVars = state,ID,val : on call arrValues.state returns 1
*/ 
function getValues(formObj,elemValue,assignVars){
	var retVals = elemValue.split(",");
	var retName = assignVars.split(",");
	//Test if the number of Values == Vars
	if (retVals.length != retName.length){
		alert('The number of Values '+ retVals.length+' != Number vars required '+retName.length)
		return;
	}
	
	var arrValues= new Array();
	for(i=0; i<retVals.length; i++){
		if(retVals[i]=='null' || retVals[i]=='undefined'){
			retVals[i]=''
		}
		arrValues[retName[i]] = retVals[i];
	}
	return arrValues;
}

function getFormElement(formname, elemname) {
	return eval('document' + '.' + formname + '.' + elemname);
}

/**
	Select an option from a select box
*/
function selectOption(selobj, scode, fselect) {
	var i;
	
	if ( fselect == undefined )
		fselect = true;
		
	for ( i = 0; i < selobj.options.length; i ++ ) {
		if ( selobj.options[i].value == scode.trim() ) {                              
			selobj.options[i].selected = fselect;
			break;
		}
	}
}

/**
	Select several options from select box
*/
function selectOptions(selobj, slist, fselect) {
	var i, carray, sellist;
	
	if ( fselect == undefined )
		fselect = true;
	
	if ( slist == '*' ) {
		for ( i = 0; i < selobj.options.length; i ++ )
			selobj.options[i].selected = fselect;
	}
	else {
		sellist = slist.replace(' ', '');
		carray = sellist.split(',');    
		for ( i = 0; i < carray.length; i++ ) {
			if ( carray[i] != '' )                                   
				selectOption(selobj, carray[i], fselect);   
		}
	}
}

/**
	Sort a select list
*/
function sortSelect(selobj) {
	var o = new Array();

	//if (!hasOptions(selobj)) { return; }	
	for (var i=0; i<selobj.options.length; i++) {
		o[o.length] = new Option( selobj.options[i].text, selobj.options[i].value, selobj.options[i].defaultSelected, selobj.options[i].selected);		
	}
	
	if (o.length==0) { return; }
	
	o = o.sort( 
		function(a,b) { 
			if ((a.text.toLowerCase()+"") < (b.text.toLowerCase()+"")) { return -1; }
			if ((a.text.toLowerCase()+"") > (b.text.toLowerCase()+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		selobj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		if ( o[i].selected == true )
			selobj.selectedIndex = i;
	}
}

/**
	Move an item from one select list to another
*/
function moveBasketSelection(formName, sourceName, destName, hiddenName, maxAdd) {	
	var i, cnt, objOptions, selArray;
	
	var objSelFrom = getFormElement(formName, sourceName);
	var objSelTo = getFormElement(formName, destName);
	var objHidden = getFormElement(formName, hiddenName);
	
	cnt = objSelTo.options.length;		
	if ( (maxAdd > 0) && (cnt >= maxAdd) )
		return;
		
	for ( i = 0; i < objSelFrom.options.length ;  ) {
		if ( objSelFrom.options[i].selected ) {
			objOptions = objSelFrom.options[i];
			objSelTo.options[cnt] = new Option(objOptions.text, objOptions.value, false, false);
			objSelFrom.options[i] = null;
			cnt++;				
			if ( (maxAdd > 0) && (cnt >= maxAdd) )
				break;
		}
		else
			i++;
	}
	
	objHidden.value = "";	
	if(sourceName.indexOf('to') > -1){ //Adding items
		selArray = new Array(objSelFrom.options.length);	
		for ( i = 0; i < objSelFrom.options.length; i++ ) {
			if ( objSelFrom.options[i].value != "" )
				selArray[i] = objSelFrom.options[i].value;
		}
		
	}else{ //removing items
		selArray = new Array(objSelTo.options.length);	
		for ( i = 0; i < objSelTo.options.length; i++ ) {
			if ( objSelTo.options[i].value != "" )
				selArray[i] = objSelTo.options[i].value;
		}	
	}
	objHidden.value = selArray.join();
	sortSelect(objSelTo);
}

/**
	trim() function, since javascript doesn't seem to have one for some reason
*/
String.prototype.trim = function () {
	return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}

/**
	Format numbers (strip out all unwanted chars
*/
function formatNumber(obj) {
	if ( obj == null )
		return;
		
	obj.value = obj.value.replace(/[^0-9\\.]*/g, '');
	return;
}

/**
	Format document text by stripping out tags, etc.
*/
function cleanText(text) {
	var newtext = text;
	
	var regexp = new RegExp("<br />", "gi");
	newtext = newtext.replace(regexp, "");
	regexp = new RegExp("<br>", "gi");
	newtext = newtext.replace(regexp, "");
	regexp = new RegExp("\\&nbsp;", "gi");
	newtext = newtext.replace(regexp, " ");
	regexp = new RegExp("<p>", "gi");
	newtext = newtext.replace(regexp, "");
	
	return newtext;
}

/**
	Limit the amount of text allowed in a textarea.  Use 
	onkeyup="limitText(this, <n>);" to implement
*/
function limitText(textfld, maxlen) {
	if ( textfld.value.length > maxlen )
		textfld.value = textfld.value.substring(0, maxlen);
}

/*
	Pop up window. 
 */
function showWindow(displaypage,event) {
	var width = 425, height = 375;
	var xPos = event.screenX;
	var yPos = event.screenY;
	//incase they are using netscape
	if (xPos == "undefined" || xPos == null){
		xPos=(eventObj.screenX);
		yPos=(eventObj.screenY);
	}	
	var left = xPos, top = yPos - (height + 50);
	if ( top < 20 )	top = 20;		
	if ( (left + width) > screen.width )
		left = screen.width - (width + 10);

	var windowprop = 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',status=no,menubar=no,scrollbars=no,toolbar=no';
	var win = window.open(displaypage, 'DISPLAY', windowprop);
	win.focus();
}

/*
	Collapsible tabs for div's
*/
function changeTab(divId,context,imgId){
	divobj = document.getElementById(divId);
	if( imgId !='' )
		imgobj = document.getElementById(imgId);
	
	if (divobj.style.visibility == 'visible'){
		divobj.style.display = "none";
		divobj.style.visibility = "hidden";
		if( imgId !='' )
			imgobj.setAttribute('src',context + '/images/expand.jpg'); 
	}else{
		divobj.style.display = "block";
		divobj.style.visibility = 'visible';
		if( imgId !='' )
			imgobj.setAttribute('src',context + '/images/collapse.jpg'); 
	}
}

/*
	Collapsible tabs for tables
*/
function changeTR(divId,context,imgId){
	divobj = document.getElementById(divId);
	if( imgId !='' )
		imgobj = document.getElementById(imgId);
	
	if (divobj.style.visibility == 'visible'){
		divobj.style.display = "none";
		divobj.style.visibility = "hidden";
		if( imgId !='' )
			imgobj.setAttribute('src',context + '/images/expand.jpg'); 
	}else{
		try { divobj.style.display = 'table-row'; }
		catch (e) { divobj.style.display = 'block'; }
		divobj.style.visibility = 'visible';
		if( imgId !='' )
			imgobj.setAttribute('src',context + '/images/collapse.jpg'); 
	}
}

/**
	General validator function
	@param: value - the value to validate
	@param: flags - Any combination of the following flags: r=required, d=date, t=time, e=email, p=phone, n=numeric, z=zip/post code, m=monetary
					OR
					A comparison directive ('l' for length or 'v' for value) followed by any of the following comparison operators: >, >=, <, <=, <>
	@param: comparator1 - A number to which a comparison operator is to compare against.
	@param: comparator2 - A second comparator used for range checks.
	@return: 	
				Return values:
				- '0' if the validation is successful.
				- The flag on which the validation fails - for example, is a field is missing, 'r' is returned, or if it fails a numeric test, 'n' is returned, etc.
				- '-1' if the value is undefined or invalid for comparison.
				- '-2' if the comparator is not valid and a comparison operation is requested.
*/
function fieldValidator(value, flags, comparator1, comparator2) {
	if ( value == 'undefined' )
		return '-1';

	// we'll need length for most validation
	var len = value.length;
	// make it easier to parse the flags
	flags.toLowerCase();
	
	// required field check
	if ( flags.indexOf('r') >= 0 ) {
		if ( len == 0 )
			return 'r';
	}
	
	// format checking (if there's any data)
	if ( len > 0 ) {		
		// number
		if ( flags.indexOf('n') >= 0 ) {
			if ( isNaN(value) )
				return 'n';
		}
		// date
		else if ( flags.indexOf('d') >= 0 ) {
			var dcheck = Date.parse(value);
			if ( dcheck <= 0 )
				return 'd';
		}
		// time
		else if ( flags.indexOf('t') >= 0 ) {
			var tcheck = Date.parse(value);
			if ( tcheck <= 0 )
				return 't';
		}
		// money
		else if ( flags.indexOf('m') >= 0 ) {
			if ( isNaN(value) )
				return 'm';
		}
		// phone
		else if ( flags.indexOf('p') >= 0 ) {
			var formatter = eval(getGlobalValue('FORMATTER_PHONE'));
			if ( formatter.test(value) == false )
				return 'p';
		}
		// zip/post code
		else if ( flags.indexOf('z') >= 0 ) {
			var formatter = eval(getGlobalValue('FORMATTER_POSTCODE'));
			if ( formatter.test(value) == false ) {
				formatter = eval(getGlobalValue('FORMATTER_POSTCODE_EX'));
				if ( formatter.test(value) == false ) {
					return 'z';
				}
			}
		}
		// e-mail
		else if ( flags.indexOf('e') >= 0 ) {
			var n = value.indexOf('@');
			if ( (n <= 0) || (value.indexOf('.', n) <= 0) )
				return 'e';
		}
	}
	
	// comparison checking
	if ( isNaN(comparator1) )
		return '-2';
		
	var comp = Number(comparator1);

	// length comparison
	if ( flags.indexOf('l<>') >= 0 ) {
		if ( isNaN(comparator2) )
			return '-2';
		var comp2 = Number(comparator2);		
		if ( (len < comp1) || (len > comp2) )
			return 'l<>';
	}
	else if ( flags.indexOf('l>=') >= 0 ) {
		if ( len < comp )
			return 'l>=';
	}
	else if ( flags.indexOf('l<=') >= 0 ) {
		if ( len > comp )
			return 'l<=';
	}	
	else if ( flags.indexOf('l<') >= 0 ) {
		if ( len >= comp )
			return 'l<';
	}
	else if ( flags.indexOf('l>') >= 0 ) {
		if ( len >= comp )
			return 'l>';
	}
	
	// value comparison
	if ( isNaN(value) )
		return '-1';
		
	var nval = Number(value);	
	
	if ( flags.indexOf('v<>') >= 0 ) {
		if ( isNaN(comparator2) )
			return '-2';
		var comp2 = Number(comparator2);		
		if ( (nval < comp1) || (nval > comp2) )
			return 'v<>';
	}
	else if ( flags.indexOf('v>=') >= 0 ) {
		if ( nval < comp )
			return 'v>=';
	}
	else if ( flags.indexOf('v<=') >= 0 ) {
		if ( nval > comp )
			return 'v<=';
	}	
	else if ( flags.indexOf('v<') >= 0 ) {
		if ( nval >= comp )
			return 'v<';
	}
	else if ( flags.indexOf('v>') >= 0 ) {
		if ( nval >= comp )
			return 'v>';
	}
	
	
	return true;	
}

/**
	Performs validation (using fieldValidator) and displays failures with a multi-lingual alert message
*/
function vAlert(displayname, value, flags, comparator1, comparator2) {
	var flag = fieldValidator(value, flags, comparator1, comparator2);
	var key = '';
	var n1 = getGlobalValue('NUM_SUB');
	var n2 = getGlobalValue('NUM_SUB2');

	switch ( flag ) {
		case 'r':
			key = getGlobalValue('ALERT_VALIDATE_REQD');
			break;
		case 'd':
			key = getGlobalValue('ALERT_VALIDATE_DATE');
			break;
		case 't':
			key = getGlobalValue('ALERT_VALIDATE_TIME');
			break;
		case 'e':
			key = getGlobalValue('ALERT_VALIDATE_EMAIL');
			break;
		case 'p':
			key = getGlobalValue('ALERT_VALIDATE_PHONE');
			break;
		case 'z':
			key = getGlobalValue('ALERT_VALIDATE_POSTCODE');
			break;
		case 'n':
			key = getGlobalValue('ALERT_VALIDATE_NUMBER');
			break;
		case 'm':
			key = getGlobalValue('ALERT_VALIDATE_MONEY');
			break;
		case 'l>=':
			key = getGlobalValue('ALERT_VALIDATE_LEN_GTE_N').replace(n1, comparator1);			
			break;
		case 'l<=':
			key = getGlobalValue('ALERT_VALIDATE_LEN_LTE_N').replace(n1, comparator1);
			break;
		case 'l>':
			key = getGlobalValue('ALERT_VALIDATE_LEN_GT_N').replace(n1, comparator1);
			break;
		case 'l<':
			key = getGlobalValue('ALERT_VALIDATE_LEN_LT_N').replace(n1, comparator1);
			break;
		case 'l<>':
			key = getGlobalValue('ALERT_VALIDATE_LEN_RANGE_NN').replace(n1, comparator1);
			key = key.replace(n2, comparator2);
			break;
		case 'v>=':
			key = getGlobalValue('ALERT_VALIDATE_VAL_GTE_N').replace(n1, comparator1);
			break;
		case 'v<=':
			key = getGlobalValue('ALERT_VALIDATE_VAL_LTE_N').replace(n1, comparator1);
			break;
		case 'v>':
			key = getGlobalValue('ALERT_VALIDATE_VAL_GT_N').replace(n1, comparator1);
			break;
		case 'v<':
			key = getGlobalValue('ALERT_VALIDATE_VAL_LT_N').replace(n1, comparator1);
			break;
		case 'v<>':
			key = getGlobalValue('ALERT_VALIDATE_VAL_RANGE_NN').replace(n1, comparator1);
			key = key.replace(n2, comparator2);
			break;
	}
	
	if ( key != '' ) {
		var msg = displayname + key;	
		alert(msg);
		return false;
	}
	
	return true;	
}

/*----- End External JavaScript -----*/
//-->