// JavaScript Document

/*FUNCTION confirmDelete - Confirm Item Delete before it happens*/
function confirmDelete(sItem, sCatID, sPage){ 
	if( confirm("Are you sure you'd like to delete <" + sItem + ">?") ){
		window.location = sPage + ".php?action=delete&catID=" + sCatID;  
	}
}

/*FUNCTION validateForm - pass the formID and make sure all required fields are filled out*/
function validateForm(sFormID){
	//alert("validating form");
	var oCurrForm = document.forms[sFormID]; //Select Current Form
	var oLabels = document.getElementsByTagName("label"); //Get all labels in that form
	
	/*Initialize Variables*/
	var hasError = false;
	var errorMsg = "Please fill in the following required elements:\n";
		
  	
	/*Loop through all form elements, looking for requried elements*/	
	for(i=0; i<oCurrForm.length; i++){

			if(oCurrForm.elements[i].className == "required"){ //Check to see if the element is required
				
				/*Check status of form elements depending on their type*/
				if(oCurrForm.elements[i].type == "select-one" && oCurrForm.elements[i].value==-1) { //Check to see if the element is a select box and if the deault state is selected
					var currElement = matchLabel(oCurrForm.elements[i].id, oLabels); //Get the label for the element to build the error message
					hasError = true;
				  errorMsg += "- " + currElement + "\n" ;
				}
				
				if( (oCurrForm.elements[i].type == "text" || oCurrForm.elements[i].type == "textarea") && oCurrForm.elements[i].value==""){//Check to see if the element if a text box
					var currElement = matchLabel(oCurrForm.elements[i].id, oLabels);
					hasError = true;
					errorMsg += "- " + currElement + "\n" ;
				}
		
			}//END REQUIRED CHECK
			
			//Check if the field is an e-mail address and that it's valid
			if(oCurrForm.elements[i].id == "email"){ 
				if(oCurrForm.elements[i].value){
					if( !isEmail(oCurrForm.elements[i].value) ){
						hasError = true;
						errorMsg = "You have entered an invalid e-mail address!";
					}
				}
			}//END EMAIL VALIDATE
		
		}//END LOOP

	if(hasError) {
		alert(errorMsg); 
	} else {
		//alert("submitting");
		oCurrForm.submit();
	}
	

	/*FUNCTION matchLabel - take a form element (from validateForm) and determine it's label for error reporting*/
	function matchLabel(sElementID, sLabels){
		if(BrowserDetect.browser=="Explorer"){
			//alert(sElementID + " " + sLabels.length );
			for(j=0; j<sLabels.length; j++){
			//alert(sLabels[j].getAttribute('htmlFor') + " " + sElementID );
				if( sLabels[j].getAttribute('htmlFor') == sElementID){
					return sLabel = sLabels[j].innerHTML;
				}
			}
		} else {
			for(j=0; j<sLabels.length; j++){
				if( sLabels[j].getAttribute('for') == sElementID){
					return sLabel = sLabels[j].innerHTML;
				}
			}
		}
	}
		
	/*FUNCTION validateEmail - take the email form element and determine if the address is valid*/	
	function isEmail(who) {
		var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
		return(email.test(who));
	}

	
}//END FUNCTION validateForm