//THE FOLLOWING SCRIPTS ARE USED THROUGHOUT THE WEBSITE.  IT IS INCLUDED IN GLOBAL. MANY VARIABLES ARE DECLARED IN CONFIGURE.PHP

//Shorthand to commonly used DOM methods
function getValue(id) {
	return getObj(id).value;
}

function getObj(id) {
	return document.getElementById(id);
}

function getLength(id) {
	return getValue(id).length;
}

//FUNCTION VALIDATE_INPUT: validates form input; uses an optional error message and regular expression
//BY DEFAULT, if no regex is given, this function acts as "checkrequired" below
function validate_input( id, error , regex ) {
  var ret = true;
  obj = document.getElementById( id );
  
  //if no regular expression was passed
  if( !regex ) {
    if( obj.value.length < 1 ) {
      ret = false;
    }
  }
  //there was a regular expression
  else {
	//but no match was found
    if( regex && !obj.value.match( regex ) ) {
      ret = false;
    }
  }
  
  //no result yet
  if( !ret ) {
    obj.style.backgroundColor = "#ffdddd";
    obj.focus();
	
	//now check if a custom error message was passed in
    if( error && error.length > 0 ) {
      window.alert( error );
    }
    else {
      window.alert( 'Please fill out the ' + obj.name + ' field.' );
    }
  }
  else {
    obj.style.backgroundColor = "#ffffff";
  }

  return ret;
}

//FUNCTION CHECKREQUIRED: CHECKS IF REQUIRED FIELDS IN A FORM WERE CORRECTLY FILLED
function checkrequired(which) { //onSubmit="return checkrequired(this)"
	var pass=true;
	if (document.images) {
		for (i=0;i<which.length;i++) {
		var tempobj=which.elements[i];
		if (tempobj.id.substring(0,3)=="req") {
			if (((tempobj.type=="text"||tempobj.type=="textarea")&&
					tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
					tempobj.selectedIndex==0)) {
				
				pass=false;
				break;
				}
			}
		}
	}
	if (!pass) {
		shortFieldName=tempobj.name.substring(3,30).toUpperCase();
		alert("Please make sure the "+shortFieldName+" field was properly completed.");
		return false;
	}
	else {
		return true;
	}
}
//  End -->

//FUNCTION CHECKMAIL: CHECKS IF AN E-MAIL ADDRESS HAS BEEN CORRECTLY INTRODUCED 
function checkMail(frm, element) {
	var x = document.frm.element.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(x)){
		alert('Incorrect email address');
	}
}

//FUNCTION POPTASTIC: CREATES NEW WINDOWS
function poptastic(url) {
	var newwindow=window.open(url,'name','height=415,width=400,left=50, top=20,scrollbars=yes');
	if (window.focus) {newwindow.focus()}
}

//FUNCTION EDIT: DISPLAYS MESSAGES FOR EDIT/CHANGE RECORD IN DB 
function edit(url, cond) {
	if (cond=="delete"){
		if (confirm("Are you sure you want to delete this item?")){
			document.location.href=url;	
		}
	}
}

function expandCollapseFrame(name, value, path) {  //This function is used throughout the website to hide or show the left frame, which contains the main navigation panel
	var images_path = path;
	if (value==0){
		parent.document.body.cols='155,*';
		document.getElementById(name).src= images_path + "hide_nav_frame.gif";
		document.getElementById(name).alt="Click to Hide Navigation Panel";
		document.getElementById(name).value=1;
		
	} else if (value==1){
		parent.document.body.cols='0,*';
		document.getElementById(name).src= images_path + "show_nav_frame.gif";
		document.getElementById(name).alt="Click to Show Navigation Panel";
		document.getElementById(name).value=0;
	}
}

function setAllCheckBoxes(formName, fieldName, checkValue) {
	if(!document.forms[formName])
		return;
	var objCheckBoxes = document.forms[formName].elements[fieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = checkValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = checkValue;
}

/*Functions taken from http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=14&TOshow=KNQ */

function getRefToDiv(divID) {
	var returnVar = false;
    if( document.layers ) { //Netscape layers
        returnVar = document.layers[divID]; }
    if( document.getElementById ) { //DOM; IE5, NS6, Mozilla, Opera
        returnVar = document.getElementById(divID); }
    if( document.all ) { //Proprietary DOM; IE4
        returnVar = document.all[divID]; }
    if( document[divID] ) { //Netscape alternative
        returnVar = document[divID]; }

	return returnVar;
}

function showDiv(divID_as_a_string) {
    //get a reference as above ...
    myReference = getRefToDiv(divID_as_a_string);
    if( !myReference ) {
        return false; //don't go any further
    }
    //now we have a reference to it
    if( myReference.style ) { //DOM & proprietary DOM
        myReference.style.display = '';
    } else {
        if( myReference.visibility ) { //Netscape
            myReference.visibility = 'show';
        } else {
            return false; //don't go any further
        }
    }
    return true;
}

function hideDiv(divID_as_a_string) {
    //get a reference as above ...
    myReference = getRefToDiv(divID_as_a_string);
    if( !myReference ) {
        return false; //don't go any further
    }
    //now we have a reference to it
    if( myReference.style ) { //DOM & proprietary DOM
        myReference.style.display = 'none';
    } else {
        if( myReference.visibility ) { //Netscape
            myReference.visibility = 'hide';
        } else {
            return false; //don't go any further
        }
    }
    return true;
}

//This function validates a group of radio buttons to see if one was checked
function validate_radio(group,error) {
	var found_it; //initial value is null because we gave it no other value

	for (var i=0; i < group.length; i++)  {
		if (group[i].checked)  {
			found_it = group[i].value; //set found_it equal to checked button's value
			return true;
		}
	}
	
	if(!found_it){  //if found_it is equal to false or null, a button has NOT been checked
		alert(error);
		return false;
	}
}
