// 
//  Global Utility Javascript File 
//
//  	Index of Functions
//    	==================
//	
//		util_checkall		- checks/unchecks all or sets of checkboxes depending on args.
//		util_filterselect 	- filters the contents of one select depending on the value selected in another.
//  	popup				- creates new window and loads particular page.
//  	relocate			- navigates the current window to url specified.
//  	dodelete			- asks for user confirmation; if confirmed, navigates to url specified.
//  	dodelete_opener		- asks for user confirmation; if confirmed, closes window and opening window is navigated to url specified
//		set_datachanged 	- sets a global variable (data_has_changed).
//		check_datachanged 	- checks to see if data_has_changed is a 'Y' and confirms if the user wants to discard changes
//  	error				- alerts a message, sets focus (and selects) a form control.
//  	closewindow			- closes current window (typically for popups).
//		submit_to_action	- updates the value for the input specified (for use by the action page)
//							  and submits the form.
//      isbadentry			- ensures that the control doesn't contain any special characters.
//		permission_alert	- simply puts up a standard alert that the user doesn't have permission to this function.
//
//--------------------------------------------------------------------------------------------------------------------
// Begin Functions 
//--------------------------------------------------------------------------------------------------------------------

function util_checkall(formidx, checkuncheck, namepattern) {

//--------------------------------------------------------------------------------------------------------------------
// formidx 	- the index of the form in your document you want to process.
//	 	- send -1 for the entire document.
//
// checkuncheck	- send either true or false.
//
// namepattern	- send a portion of the name pattern for a set of checkboxes to process e.g. 'checkbox_productid_'
//		- send '' (empty string) to process all checkboxes in the specified form(s).
//--------------------------------------------------------------------------------------------------------------------

  if(formidx == -1) {
    for (formidx = 0; formidx < document.forms.length; formidx++) {
	for (var idx = 0; idx < document.forms[formidx].elements.length; idx++) {

		if(document.forms[formidx].elements[idx].type == 'checkbox') {
			if(namepattern == '')
				document.forms[formidx].elements[idx].checked = checkuncheck;
			else
				if(document.forms[formidx].elements[idx].name.indexOf(namepattern) == -1)
					;  // empty stmt
				else
					document.forms[formidx].elements[idx].checked = checkuncheck;
		} // End of if 
	} // End inner for
     }  // End of outer for
  }  // End process of all forms

  else {
	for (var idx = 0; idx < document.forms[formidx].elements.length; idx++) {

		if(document.forms[formidx].elements[idx].type == 'checkbox') {
			if(namepattern == '')
				document.forms[formidx].elements[idx].checked = checkuncheck;
			else
				if(document.forms[formidx].elements[idx].name.indexOf(namepattern) == -1)
					;
				else
					document.forms[formidx].elements[idx].checked = checkuncheck;
		} // End of if 
	} // End of for
  }  // End process of a specific form	

  return false;

} // End of function  
//--------------------------------------------------------------------------------------------------------------------

function popup(name, url, h, w, scroll, resize, menubar)
		{
//----------------------------------------------------------------------------------------
//	NAME 	- name of the window (string)
//  URL		- url that you wan't to load in the new window (string)
//  H		- height of the window (also used for centering the popup window) (string)
//  W		- width of the window (also used for centering the popup window) (string)
//  SCROLL	- yes/no for scroll bars in the new window (string)
//  RESIZE	- yes/no for ability to resize the window (string)
//  MENUBAR	- yes/no to show the menubar in the new window (string)
//----------------------------------------------------------------------------------------

	var win = null;
	var windowleft = (screen.width-w)/2;
	var windowtop = (screen.height-h)/2;
	var features  = 'height='+h+',';
		features += 'width='+w+',';
		features += 'top='+windowtop+',';
		features += 'left='+windowleft+',';
		features += 'scrollbars='+scroll+',';
		features += 'menubar='+menubar+',';
		features += 'resizable='+resize;
	
	win = window.open(url, name, features);
	
	win.focus();
	
//	if(parseInt(navigator.appVersion) >= 4){
//		win.window.focus();
//	}
	
} // END OF FUNCTION
//----------------------------------------------------------------------------------------

function relocate(url)
	{
//----------------------------------------------------------------------------------------
//  URL - the URL the window should navigate to (string)
//----------------------------------------------------------------------------------------

			window.location = url;
}//END OF FUNTION
//----------------------------------------------------------------------------------------

function dodelete(message, whereto)
	{
//----------------------------------------------------------------------------------------
//  MESSAGE - the message for user (string)
//  WHERETO - upon confirmation, where the window should navigate to (string)
//----------------------------------------------------------------------------------------
		
			if (confirm(message))
			{window.location=whereto;}

			return false;

}//END OF FUNCTION
//----------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------
//  MESSAGE - the message for user (string)
//  WHERETO - upon confirmation, where the "OPENING" window should navigate to (string)
//----------------------------------------------------------------------------------------
function dodelete_opener(message,whereto)
	{
		if(confirm(message))
			{
				opener.location=whereto;
				window.close();
			}
		return false;
	}
//END OF FUNCTION
//----------------------------------------------------------------------------------------

function util_filterselect(formname, selectname, keyvalue, arrayname, showempty){

//--------------------------------------------------------------------------------------------------------------------
// This utility function will load the options in a select from a javascript array depending on a value that is passed.
// You can call it from the onChange event of another select if you like.  For example, when you select from a list of
// product categories, you may then reload a select with the proucts that belong to the selected category.   
//
//- Argument ----------------------------------------------------------------------------------------------------------
//
// formname 	- the name of the form that the select control you are populating is in. String.
//
// selectname	- the name of the select control. String.
//
// keyvalue	- the value you will match to in the javascript array to populate new options. Number.
//
// arrayname	- the name of the javascript array that you pre-loaded all possible options into.
//
// showempty	- boolean value to indicate whether you want an empty option at the top of the select control.
//--------------------------------------------------------------------------------------------------------------------
//
// To use this function you must pre-load a multi-dimensional array with all the possible options for population.
// The array can be named anything you like but it must have the following structure
//
//	var select2values    = new Array();		// change select2values to anything you want.
//
//	select2values[0]     = new Array();
//	select2values[0][0]  = 1;			// keyvalue to match to e.g. categoryid for polo shirts
//	select2values[0][1]  = 13;			// option value e.g. productid for large blue polo shirts
//	select2values[0][2]  = "Large Blue Polo";	// option display value e.g. product name 
//
//	select2values[1]     = new Array();
//	select2values[1][0]  = 1;			// keyvalue to match to e.g. categoryid for polo shirts
//	select2values[1][1]  = 14;			// option value e.g. productid for large red polo shirts
//	select2values[1][2]  = "Large Red Polo";	// option display value e.g. product name 
// 
//      and so on ....
//--------------------------------------------------------------------------------------------------------------------
	
	eval('document.' + formname + '.' + selectname + '.options.length = 0');  // Remove all existing options! 

	var opt = 0;

	if(showempty){  // Add empty option if specified!
	   eval('document.' + formname + '.' + selectname + '.options[' + opt + '] = new Option("", "")');
	   opt++;
	}

	for(var idx = 0; idx < eval(arrayname + '.length'); idx++){  // Load options that correspond to the key value that was sent in!
	   if(eval(arrayname + '[idx][0]') == keyvalue){
	      eval('document.' + formname + '.' + selectname + '.options[' + opt + '] = new Option("' + eval(arrayname + '[idx][2]') + '", ' + eval(arrayname + '[idx][1]') + ')');
	      opt++;
	   }
	}	

	return;

} //END OF FUNCTION

//--------------------------------------------------------------------------------------------------------------------

var data_has_changed = false;  // Global variable for indicating when a form's data has changed - false by default! 

function set_datachanged(datachanged){

//--------------------------------------------------------------------------------------------------------------------
// This function sets the global data_has_changed variable to the argument value (true/false) 
// Call this from onChange or other events that modify form data.  e.g. onChange="set_datachanged(true);" 
//--------------------------------------------------------------------------------------------------------------------
	
	data_has_changed = datachanged;

} //END OF FUNCTION

//--------------------------------------------------------------------------------------------------------------------

function check_datachanged(){

//--------------------------------------------------------------------------------------------------------------------
// This function checks to see if data_has_changed was turned on (true).  If so it will confirm if user wants to discard changes.
// Call this function from activites used to leave a page like links eg. href="nextpage.html" onClick="return check_datachanged();" 
//--------------------------------------------------------------------------------------------------------------------

	var discard_changes = true;
	
	if(data_has_changed == true) 
		discard_changes = confirm('The data on this page has changed.  If you click OK your changes will NOT be saved.  Continue?');

	return discard_changes; 

} //END OF FUNCTION

//--------------------------------------------------------------------------------------------------------------------

function error(msg, form, control)
{
//----------------------------------------------------------------------------------------
//  MSG		- the message for the user (string)
//  FORM	- the form the control belongs to  (string)
//  CONTROL - the name of the control that did not pass the check (string)
//
//  notes: preface the call to this function with return (as it is returning false)
//----------------------------------------------------------------------------------------

	alert(msg);

	if( eval('document.' + form + '.' + control + '.type') == 'select-one' || eval('document.' + form + '.' + control + '.type') == 'checkbox') 
		return false;

	eval('document.' + form + '.' + control + '.select();');
	eval('document.' + form + '.' + control + '.focus();');
 
	return false;

}//END FUNCTION
//----------------------------------------------------------------------------------------

function closewindow()	
//----------------------------------------------------------------------------------------
//  NO PARMS
//----------------------------------------------------------------------------------------
{
	window.close();
}//END FUNCTION
//----------------------------------------------------------------------------------------

function submit_to_action(form, control, action)
//----------------------------------------------------------------------------------------
//  1) form  	-  name of the form (string)
//	2) control 	-  should be a input type="hidden" (string)
//  3) action 	- value will be checked in the action page to 
//             	  determine which action to take. (string)
//----------------------------------------------------------------------------------------
{
	eval('document.' + form + '.' + control + '.value = "' + action + '";');
	eval('document.' + form + '.submit();');
	return false;
}//END FUNCTION
//----------------------------------------------------------------------------------------

function isbadentry(formname, controlname, listofchars)
//----------------------------------------------------------------------------------------
//  1) formname  	-  name of the form (string)
//	2) controlname 	-  should be a input type="hidden" (string)
//----------------------------------------------------------------------------------------
{
	var thelength = eval('document.' + formname + '.' + controlname + '.value.length');
	var thevalue = eval('document.' + formname + '.' + controlname + '.value');
	for (var i=0; i<thelength; i++)
	{
		if(listofchars.indexOf(thevalue.charAt(i)) > 0)
		{
			return error("Please Enter valid Characters. The following characters are invalid.\t\r" + listofchars, formname, controlname);
			return false;
		}
	return true;

	}

}//END FUNCTION
//----------------------------------------------------------------------------------------

function permission_alert()
//----------------------------------------------------------------------------------------
{
	alert("Sorry, you do not have the permissions required to use this feature."); 
}//END FUNCTION
//----------------------------------------------------------------------------------------

  

