<!--// 
/*---- 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');
	}
}

/* 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) {
	var i;
	for ( i = 0; i < selobj.length; i ++ ) {
		if ( selobj.options[i].value == scode ) {                              
			selobj.options[i].selected = true;
			break;
		}
	}
}

/**
	Select several options from select box
*/
function selectOptions(selobj, slist) {
	var i, carray, sellist;
	sellist = slist.replace(' ', '');
	carray = sellist.split(',');    
	for ( i = 0; i < carray.length; i++ ) {
		if ( carray[i] != '' )                                   
			selectOption(selobj, carray[i]);   
	}
}

/**
	Sort a select list
*/
function sortSelectSpecific(selobj, itemname, itemvalue, idx, fresetselected) {
	var o = new Array();
	var i=0, j=0, curindex=0;
	var tmpopt = null;

	for (i=0, found=0; i<selobj.options.length; i++) {
		if (found==0) {
			if ((itemvalue!=null) && (itemvalue!="")) {
				if (selobj.options[i].value==itemvalue) {
					tmpopt = new Option( selobj.options[i].text, selobj.options[i].value, selobj.options[i].defaultSelected, selobj.options[i].selected);
					continue;	
				}
			}
			else if ((itemname!=null) && (itemname!="")) {
				if (selobj.options[i].text==itemname) {
					tmpopt = new Option( selobj.options[i].text, selobj.options[i].value, selobj.options[i].defaultSelected, selobj.options[i].selected);
					found = 1;
					continue;	
				}
			}
		}
			
		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 (i=0, j=0; i<o.length; i++, j++) {
		if ((tmpopt!=null) && (j==idx)) {
			selobj.options[j] = new Option(tmpopt.text, tmpopt.value, tmpopt.defaultSelected, tmpopt.selected);
			if (tmpopt.selected == true)
				selobj.selectedIndex = j;
			j++;
		}
				
		selobj.options[j] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		if ( o[i].selected == true )
			selobj.selectedIndex = j;
	}
	
	if (tmpopt!=null) {
		if (idx<0) {  
			curindex = selobj.length;
			selobj.options[curindex] = new Option(tmpopt.text, tmpopt.value, tmpopt.defaultSelected, tmpopt.selected);
			if (tmpopt.selected == true)
				selobj.selectedIndex = curindex;
		}
	}
	
	if (fresetselected)
		selobj.selectedIndex = 0;
	
}


/**
	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+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { 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++;
	}
	
	selArray = new Array(objSelTo.options.length);		
	objHidden.value = "";	
	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;
}

/**
	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 -----*/
//-->
