/***********************************   
	VALIDATION
************************************/	
	
		//DO NOT DELETE!!! Used by other scripts to test for existence of this library on page
		function validationLibraryLoaded(){
			return true;
		}
		
		function validateAll(formHandle,validationArray){
		//This function expects an array containing all the field names for a form that want to be validated
		//Each field that wants to be validated needs a matching function  "String FIELDNAME_validate(formField)"
		//which returns a string: either of zero length or containing a description of why that field failed validation.
		//You can use this framework IF you name your validation functions correctly and do a javascript AFTER each formfield and script like : 
		/*
			<input .... name="FIELDNAME">
			<script>
				function FIELDNAME_validate(formField){
					....
				}
				if(typeof(validationArray) == typeof(undef)){validationArray = new Array('FIELDNAME');}
				else{validationArray[validationArray.length] = 'FIELDNAME';}
			</script>
		*/
			var alertMsg ='';
			var undef;
			if(typeof(validationArray) != typeof(undef)){//if no validationArray exists don't bother processing
				//alert('in validateall.  valary length = ' + validationArray.length);
				for(var i =0;i < validationArray.length; i++){
                    //alert('running ' + validationArray[i] + '_validate(formHandle,validationArray[i])...');
					alertMsg+=eval(validationArray[i] + '_validate(formHandle,validationArray[i])');
				}
				if( alertMsg.length){
					alert(alertMsg);
					return false;
				}else{
					return true;
				}
			}else{//there was no validationArray... nothing to validate: return true
				return true;
			}
		}
	
	//test for the existence of a field.  this can be useful when you use conditional clauses in building forms in CF
		function fieldExists(formField){
			var undef;
			if(typeof(formField) == typeof(undef)){return false;}
			else{return true;}
		}		
			
		function isRadioChecked(radioGroup){
			for (var i=0;i<radioGroup.length;i++){
				if(radioGroup[i].checked){ return true;}
			}
			return false;
			
		}
		function compileChecked(radioGroup){//returns a comma delimited list
			var returnList = '';
			for (var i=0;i<radioGroup.length;i++){
				if(radioGroup[i].checked){ 
					if(returnList.length >0){returnList += ',';}
					returnList += radioGroup[i].value;
				}
			}
			return returnList;
		}

		
		function isNumeric(formField){//alert('in isnumeric');
				if(! isBlank(formField)){
					if(!isNaN(formField.value)){
						return true;
					}else return false;
				}else return false;
		}
		
		function isBlank(formField){
				if(formField.value == null || formField.value.length < 1){
					return true;
				}else return false;
		}
		
		function isEmail(formField){
			var EmailCheck = /[\w\.\-\_]+@[\w\.\-]+\.\w{2,3}/;
			var strEmail = formField.value	;
		
			if (strEmail.search(EmailCheck) == -1){
				return false;
			}else return true;
		}
	
/*********************************************************
	Dealing with select lists
***********************************************************/
	function isSelectedValueNULL(selectElement){
	//for single selects (with no MULTIPLE attribute) returns FALSE if the selected OPTION has a value other than 'NULL' or no OPTION selected.  
	//for multi-selects returns TRUE if any selected OPTION has a value of 'NULL', otherwise returns FALSE (no selected value will return FALSE)
		if(selectElement.type == 'select-one'){
			if(getSelectedValue(selectElement) == 'NULL'){ return true;}
			else{ return false;}
		}else{
			mySelectedOptionsArray = selectedOptionsArray(selectElement);
			for(i=0;i<mySelectedOptionsArray.length;i++){
				if(mySelectedOptionsArray[i] == 'NULL'){
					return true;
				}
			}
			return false;
		}
	}
	
	
	//ensures that at least one element is selected in a SELECT
	function isValueSelected(selectElement){
		if(selectElement.selectedIndex != -1){
			return true;
		}else{return false}
	}
	
	function clearSelect(selectElement){
				selectElement.options.length=0;
	}
	
	function addOption(mytext,myvalue,selectElement){
		if(mytext.length && myvalue.length){
			myOption = new Option(mytext,myvalue);
			selectElement.options[selectElement.options.length] = myOption;
		}else return false;
	}
		
	function deleteOption(selectElement){
		if(selectElement.selectedIndex >= 0){
			selectElement.options[selectElement.options.selectedIndex]=null;
		}else return false;
	}	
		
	//allow for adding all selected options in a multi-select list into one list
	//this can be useful if you want to bung these into a WDDX object
	function compileSelectedOptions(selectElement){
		var undef;
		var i;
		var returnList = '';
		if(typeof(selectElement) != typeof(undef)){
			if(selectElement.type != 'select-one'){
				for(i=0;i < selectElement.length;i++){
	              if(selectElement.options[i].selected && selectElement.options[i].value.length > 0){
				  	if(returnList.length >0){returnList += ',';}
					returnList += selectElement.options[i].value;
				  }
	            }
				//alert(returnList);
				return returnList;
			}else{
				return getSelectedValue(selectElement);
			}
		}else{
			alert('The first parameter of compileSelectedOptions was undefined.  You probably mispelt the select element name.');
		}
	}	
	
	
	function selectedOptionsArray(selectElement){
		var undef;
		var i;
		var returnList = new Array();
		if(typeof(selectElement) != typeof(undef)){
			if(selectElement.type != 'select-one'){
				for(i=0;i < selectElement.length;i++){
	              if(selectElement.options[i].selected && selectElement.options[i].value.length > 0){
					returnList[returnList.length] = selectElement.options[i].value;
				  }
	            }
			}else{
				returnList[0] = getSelectedValue(selectElement);
			}
			return returnList;
		}
	}	
	
	
	
	//simplify returning the value of a select list
	function getSelectedValue(selectElement){ 
		if(selectElement.type == 'select-one'){
			if(isValueSelected(selectElement)){
				return selectElement.options[selectElement.selectedIndex].value;
			}else{return null;}
		}else{alert('function getSelectedValue only works on single-select form elements. Try using selectedOptionsArray instead');}
	}
	function getSelectedText(selectElement){
		if(isValueSelected(selectElement)){
			return selectElement.options[selectElement.selectedIndex].text;
		}else{return null;}
	}
	
	function setSelectedValue(selectElement,inputArray){
		var undef;
		var i;
		var j;
		if(typeof(selectElement) != typeof(undef)){
			if(selectElement.type == 'select-one' || selectElement.type == 'select-multiple'){
				for(j=0;j < inputArray.length;j++){
					for(i=0;i < selectElement.length;i++){
						if(selectElement.options[i].value == inputArray[j]){
							selectElement.selectedIndex= i;
							break;
						}
					}
				}
			}else{
				alert('The first parameter of setSelectedValue must be a form select element');
			}
		}else{
			alert('The first parameter of setSelectedValue was undefined.  You probably mispelt the select element name.');
		}
	}
	
	//get only the first selected element of a multi select list
	function getFirstSelectedOptionText(selectElement){
		var undef;
		var i;
		var returnList = '';
		if(typeof(selectElement)!= typeof(undef)){
			for(i=0;i < selectElement.length;i++){
	             if(selectElement.options[i].selected && selectElement.options[i].value.length > 0){
				returnList += selectElement.options[i].text;
				break;
			  }
	           }
		}
		return returnList;
	}	
		
  function moveOptionBetweenSelects(from_selectElement,to_selectElement) {//only for single-selects
			if(from_selectElement.options.selectedIndex >=0){
				addOption(to_selectElement.options[to_selectElement.options.selectedIndex].text,to_selectElement.options[to_selectElement.options.selectedIndex].value,from_selectElement);
				deleteOption(to_selectElement);
				from_selectElement.options.selectedIndex=from_selectElement.options.length-1;
			}else return false;
	}



	//direction: -1=down, 1=up
	function moveOptionUpDown(selectElement,direction) {
		var moveBy = 0;
		moveBy = parseInt(direction);
		
		if(typeof(selectElement) != 'object'){alert('moveOptionUpDown takes a SELECT object as its first argument');return false;}
	
		if(!isNaN(moveBy) || moveBy == 0 ){//passing isNaN(null) returns false. If no howMany was passed stop now
			if(howMany == null){return true;}
		}else{alert('moveOptionUpDown takes an integer as its second element');return false;}
		
	
		with (selectElement.options){
		  Sel   = selectedIndex;
			SelTo = selectedIndex - direction;
			Last  = length;
		}		
		
		//can't go past the end or beginning
		if(SelTo >= selectElement.length || SelTo < 0){
			return false;
		}
		
		myOptionText = new Array();
		myOptionValue = new Array();
		//Loop over elements
		for(i=0;i<Last-1;i++){
			with (selectElement) {
				switch (i){
					case SelTo:
						myOptionText[i+direction]=options[i].text;
						myOptionValue[i+direction]=options[i].value;
					break;
					case Sel:
						myOptionText[i-direction]=options[i].text;
						myOptionValue[i-direction]=options[i].value;
					break;
					default:
						myOptionText[i]=options[i].text;
						myOptionValue[i]=options[i].value;
					break;
				}
				
			}
		}
		//rebuild
		selectElement.options.length=0;
		selectElement.options.length=Last;
		for(i=0;i<Last-1;i++){
			with (selectElement) {
				options[i].text=myOptionText[i];
				options[i].value=myOptionValue[i];
			}
		}
		selectElement.options.selectedIndex=SelTo;
		return true;
	}

	

	
  function SelectAll(selectElement) {
	  	if(selectElement.type=='select-multiple') {
			for (i=0; i<selectElement.options.length; i++) {
			  selectElement.options[i].selected = true;
			}
		}
	}