//global event binding
window.onload = window_onLoad;

function window_onLoad(){
	//cause the form to render all widgets with a gradient
	if(typeof(goGradient) == 'function'){
		goGradient();
	}

	//runs the "set focus" function if there is one
	if(typeof(setFocus) == 'function'){
		setFocus();
	}
	
	//run the page specific "page load" function if there is one
	if(typeof(pageLoad) == 'function'){
		pageLoad();
	}
}

//loop through all the elements on the page applying the horizontal
//gradient to all the text boxes
function goGradient(){
	for(var intA = 0; intA < document.all.length; intA++){
		e = document.all(intA);
		if((e.tagName == "INPUT" && (e.getAttribute("type") == "text" || e.getAttribute("type") == "password"))
		|| (e.tagName == "TEXTAREA" || e.tagName == "SELECT")) {
			e.className = "gradientFormControl";
		}
	}
}

//used so we can have buttons that function as <a> tags
function hrefButton_OnClick(objButton){
	var strURL;
	var strTarget;
	
	strURL = objButton.getAttribute("href");
	strTarget = objButton.getAttribute("target");
	if (strTarget != null){
		if (strTarget == "_new"){
			window.open(strURL);
		}
	}
	else{ 
		window.navigate(strURL);
	}
}

//used to navigate the toolbar without requiring the user to actualy click on the link
function toolBar_OnClick(){
	var eSrc;
	var strHREF;
	eSrc = window.event.srcElement;
	if (eSrc.tagName == "TD" && eSrc.className !== "notButton"){
		eSrc.children[0].click();
	}
}

//validates each field on the form
function validateForm(objForm){
	for(var intA = 0; intA < objForm.length; intA++){
		objElement = objForm(intA);
		validateField(objElement);
	}
}

//validates a single field
function validateField(objElement){
	var strErrorMsg = "This field contains an invalid entry.  Please correct and try again.";

	//get the "custom" error message for this element if there is one
	if(objElement.getAttribute("errormsg") != null){
		strErrorMsg = objElement.getAttribute("errormsg");
	}
	
	//validate the element
	if(!isValid(objElement)){
		alert(strErrorMsg);
		objElement.focus();
		objElement.select();
		event.returnValue = false;
		event.cancelBubble = true;
	}
}

//tests an form element according to its "regexp" attribute
function isValid(objElement){
	var blnIsValid = true;
	
	if((objElement.getAttribute("regexp") != null) && (objElement.value != "")){
		if(objElement.value.match(objElement.getAttribute("regexp")) == null){
			blnIsValid = false;
		}
	}
	return blnIsValid
}