<!--
//Misc utilities for use in site

/*
	Toggles the visibility of a DOM element
	
	@param elem = DOM element
	@param state = valid: none, hidden
*/
function toggleVisibility(source,state) {
	//source = document[source];
	if (state == undefined){
		if (source.style.display == 'none') {
			source.style.display = '' ;
			} else {
			source.style.display = 'none' ;
		}
	} else {
		if (state == 'visible'){
			state = '';
		}
		if (source.style.display != state) {
			source.style.display = state;
		}
	}
}


/*
	Changes the class name of a DOM element.
	Optionally it will revert the name onMouseOut and use Hand Cursors
	
	@param elem = DOM element
	@param newClass = class name to assign to element
	@revertOnMouseOut = revert the element to it's previous class name onMouseOut
	@useHandCursor = use the hand cursor when hovering over this element
*/
function changeClassName( elem, newClass, revertOnMouseOut, useHandCursor ) 
{
	// set this to be true unless false is passed
	revertOnMouseOut = (revertOnMouseOut == false)? false : true ;
	// use the hand pointer by default
	useHandCursor = (useHandCursor == false)? false : true ;
	// use the hand pointer if required
	if (useHandCursor)
	{
		elem.style.cursor='hand';
		elem.style.cursor='pointer';
	}
	// if reverting, get the current class name 
	// and set it to revert onMouseOut
	if (revertOnMouseOut)
	{
		var currentClass = elem.className;
		elem.onmouseout = function(){ changeClassName( elem, currentClass, false, useHandCursor ); }
		/* None of these methods work:
		//var currentClass = 'CM_EditableContentRegion_btn_off';
		//elem.onmouseout = "changeClassName(" + elem +", '"+ currentClass +"', false )";
		//elem.onmouseout = function(){ elem.className = currentClass; };
		*/
	}
	else {
		// remove the onMouseOut function
		elem.onmouseout = null ;
	}
	// finally set the new class name
	elem.className = newClass ;
}

/*
Toggles a class name
It changes it to the opposite class name
*/
function toggleClassName( elem, class1, class2, useHandCursor ) 
{
	// use the hand pointer by default
	useHandCursor = (useHandCursor == false)? false : true ;
	// use the hand pointer if required
	if (useHandCursor)
	{
		elem.style.cursor='hand';
		elem.style.cursor='pointer';
	}
	// get the current class and change it to the opposite class
	var currentClass = elem.className;
	if (currentClass == class1)
	{
		// set the new class name
		elem.className = class2 ;
	}
	else {
		// set the new class name
		elem.className = class1 ;
	}
}


/*
* 
* Browser detection scripts
* 
*/
function isIE()
{
	var _isIE = document.all?true:false;
	return _isIE;
}


/*
*
* Math functions
*
*/
function randRange( _min, _max )
{
	// set defaults
	_min = (_min!=null)? _min: 1000;
	_max = (_max!=null)? _max: 10000;
	// make sure that the max is greater than the min
	if (_min>_max)
	{
		var _maxtmp = _max;
		_min = _max;
		_max = _maxtmp;
	}
	return _min + Math.floor( Math.random()*(_max-_min+1) );
}



/****************************************************
	FORM VALIDATION FUNCTIONS
	***************************************************/
	
function validateForm(formName,requiredFieldsArray) {
	var ret=true;
	
	//loop through fields array
	for (i in requiredFieldsArray){
		var fieldName = requiredFieldsArray[i];
		var fieldSource = document[formName][fieldName];
		
		if((fieldSource.value.length < 1 || fieldSource.value == 'required') && fieldSource.selectedIndex != 0 ){
			setTextFocus(fieldSource);
			//alert(fieldName + " is required.");
			ret=false;
		}/**/
		if(fieldSource.selectedIndex == 0){
			setSelectFocus(fieldSource);
			//alert(fieldName " is required.");
			ret=false;
		}
	}
	
	if(ret==false){
		alert("Please fill in all required fields.");
	}
	return ret;
}
function setTextFocus(source){
	source.style.backgroundColor = 'FFFFCC';
	source.style.color = 'red';
	source.value = 'required';
}
function resetFocus(source){
	if (source.style.color=='red'  ){/*|| source.selectedIndex == 0*/
		source.parentNode.style.display = '';
		source.style.backgroundColor='';
		source.style.color = '';
		source.value = '';
	}
}
function setSelectFocus(source){
	//source = document.ClientInfo.BreedID;
	source.parentNode.style.display = '';
	source.style.color = 'red';
	source.style.backgroundColor = 'FFFFCC';
	//source.text = 'required';
}

	/***** for updating passwords **********/
	
function confirmPassword( field1,field2){
	/* 	The fields should follow the following format:
			document.Form_Name.Field_Name */
	pNew = field1.value; //document.formName.field1.value
	pConf = field2.value;// document.formName.field2.value
	
	// confirm that the passwrod is at least 4 chars long
	if (confirmPasswordLength(pNew,pConf)== false){
		return false;
	}
	
	// confirm that the passwords match
	if (pNew != pConf){
		field2.value = '';
		alert("The passwords did not match.  Please verify your password.");
		return false;
	}
	
	// otherwise alls well
	return true;
}

function confirmPasswordLength(field1value,field2value){
	// confirm that the password is at least 4 chars long
	if (field1value.length < 4 ){
		field2value = '';
		alert("Your Password must be at least 4 characters long.");
		return false;
	}
}

//-->