// - - - - - - - - - - - - - -   Modifications  - - - - - - - - - - - - - - - -
//  who   date   description
//  sjf  100704  Process the set_focus correctly if there is a hidden div within
//               a visible page div
//  RHB  101210  Replaced var layers = document.all.tags("DIV");
//               with   	var layers = document.getElementsByTagName("DIV"); 
//               ifor Firefox to work.  The new way is the standard javascript
//               version for all browsers.
//
//
function SetFirstField(field_name) 
{

		var occurrence = 0;
		var i;
		var character;
		var length = field_name.length;
		var isNumeric;

		//If the field_name is blank, then position to the first enterable 
		//field on the screen.
		if (field_name.length == 0)
		{
			set_focus('1');
			return;
		}

		isNumeric = "T";
		for (i = 0; i < length; i++)
		{
			character = field_name.charAt(i).toUpperCase();
			if ((character < "0") || (character > "9"))
			{
				isNumeric = "F";
				break;
			}
		}

//	alert("Positioning: isNumeric = " + isNumeric);
		
		if (isNumeric == "F")
		{				    
	    	for (var e = 0; e < document.forms[0].elements.length; e++)
				{
	    		var strElementType = document.forms[0].elements[e].type;	
//	    		alert("Name = " + document.forms[0].elements[e].name + "\n" +
//	    		      "Type = " + strElementType);		  
	    		if (strElementType == 'text' || 
				    strElementType == 'select-one' || 
					strElementType == 'textarea' || 
					strElementType == 'file' || 
					strElementType == 'password') 
				{
//					   alert ("Name = " + document.forms[0].elements[e].name + "\n" +
//					          "field_name = " + field_name);
					 if (document.forms[0].elements[e].name == field_name)
					 {
		    			 document.forms[0].elements[e].focus();
		    			 break;
					 }
	    		}
	    	}
    	}
    	
    	if (isNumeric == "T")
    	{
    		set_focus(field_name);
    	}
}  //End of function SetFirstField


// This function will loop through all items of the DOM looking for <div> tags
// that have an id of "page".  When it finds one, it will check if the 
// visibility is "visible".  If it is, it will then check to find the first
// <input> tag that is not readonly and set focus to that field.
// This is used in add.inc.js and update.inc.js.

//062703 - This has been changed to expect a field number to be passed.  

function set_focus(field_number)
{
	var field_count;
	field_count = 0;
	for (var e = 0; e < document.forms[0].all.length; e++)
	{
		var node_type = document.forms[0].all[e].type; 		  
		var node_id = document.forms[0].all[e].id;
		var html_tag = document.forms[0].all[e].nodeName;
//		var div_visibility = document.forms[0].all[e].currentStyle.visibility;    //100704 - sjf
		var tag_readonly = document.forms[0].all[e].readOnly;
		var current_div_id; 
		var div_being_processed;
		
		if (html_tag == "DIV" && node_id.substring(0,4) == "page")
		{
			var div_visibility = document.forms[0].all[e].currentStyle.visibility;   		//100704
			// set the sub_div_visibility to the same as the div for the page so if there are no
			// sub div statements everything still works
			var sub_div_visibility = document.forms[0].all[e].currentStyle.visibility;   	//100704
			if (div_visibility == "visible")
			{
				current_div_id = node_id;
				div_being_processed = node_id;
			}
			else
			{
				div_being_processed = node_id;
			}
		}
		else
		{   																				//100704
			//  If we have a div under the page div, we have to check if it is visible	  	//100704
			if (div_visibility == "visible" && html_tag == "DIV") 							//100704
			{																				//100704
				var sub_div_visibility = document.forms[0].all[e].currentStyle.visibility;  //100704 
//				alert ("Set sub_div_visibility to " + sub_div_visibility);					//100704
			}																				//100704
		}																					//100704

//	   alert ("current_div_id = " + current_div_id + "\n" +
//				"HTML tag = " + html_tag + "\n" +
//				"ID = "  + node_id + "\n" +
//				"NodeType = " + node_type + "\n" +
//				"Visibility = " + div_visibility + "\n" +
//				"Sub Visibility = " + sub_div_visibility + "\n" +
//				"Class = " + document.forms[0].all[e].currentStyle.name);

		if (current_div_id != null && div_being_processed == current_div_id)
		{
			if (html_tag == "INPUT" || html_tag == "SELECT" || html_tag == "TEXTAREA")
			{

	    		if (node_type == 'text' || 
				    node_type == 'select-one' || 
					node_type == 'textarea' || 
					node_type == 'file' || 
					node_type == 'password') 
//				if (node_type == 'text' || node_type == 'select-one' || node_type == 'textarea' || node_type == 'file') 
				{
// 100704					if (!tag_readonly) 
					if (!tag_readonly && sub_div_visibility == "visible") //
					{
					   field_count++;

//						   alert ("current_div_id = " + current_div_id + "\n" + 
//						   		  "HTML tag = " + html_tag + "\n" + 
//						          "ID = "  + node_id + "\n" +
//						          "NodeType = " + node_type + "\n" +
//						          "Visibility = " + div_visibility + "\n" + 
//								  "Div processed = " + div_being_processed + "\n" +
//						          "Class = " + document.forms[0].all[e].currentStyle.name);
						if (field_count == field_number)
						{
							document.forms[0].all[e].focus();
							break;			
						}
					} // end of check for tag readonly
				}
			} // end of test for input or select
		} //end of check for current_div_id != null
		
	}
}

// This function will toggle the screen between multiple pages
// It is used when all the data does not fit on one screen
// If the field parameter is empty, then the function set_focus
// will be called to set focus to the first field on the page
function display_page(page, field)
{

// Loop through all the "div" tags and turn the visibility off
//	var layers = document.all.tags("DIV");                                       // 101210
	var layers = document.getElementsByTagName("DIV");                             // 101210

	for(var i=0;i<layers.length;i++)
	{
		var div_id = layers[i].id;
		
//		alert("positioning: div_id = " + div_id);
		
	   if (div_id.substring(0,4) == "page")
	   { 	   		
				layers[i].style.visibility = "hidden";
				layers[i].style.display = "none";
	   }
	}

	// Turn the visibility on the appropriate page on.	
	var current_page = "page" + page;
	eval(current_page).style.display='block';
	eval(current_page).style.visibility='visible';

	//Get all the input fields
	var input_flds = document.all.tags("input");

	for (var i=0;i<input_flds.length;i++)
	{
		var input_name = input_flds[i].name;
		var input_type = input_flds[i].type;
		
//		alert ("positioning: input_name = " + input_name + "\n" +
//		        "input_type = " + input_type);
		
		
		if (input_type == "button")
		{
	   		if (input_name.substring(0,6) == "b_page")
	   		{ 	   		
//				eval(input_name).className="PageButton";
				input_flds[i].className="PageButton";
								
			}
// -- added 040904 - sjf
			// Change the class on the selected button
			if (input_name == "b_" + current_page)
			{
				input_flds[i].className="SelectedPageButton";
			}
		}
	}
	
	// Change the class on the selected button
//040904
//	var button = "b_" + current_page;
//	eval(button).className="SelectedPageButton";
	
	if (field.length > 0)
	{
		SetFirstField(field);
	}
	else
	{
    set_focus('1');   //Set the cursor to field number 1
	}
}

//This function will determine the current page number of the
//"div" statements
function get_current_page()
{
	var layers = document.all.tags("DIV");
	
	for(var i=0;i<layers.length;i++)
	{
		var div_id = layers[i].id;
		
	   if (div_id.substring(0,4) == "page")
	   { 	   		
			var div_visibility = layers[i].currentStyle.visibility;
			if (div_visibility == "visible")
			{
				var current_page = div_id.substr(4);
				return current_page;
			}
	   }
	}
}

//This function will determine the number of "div" pages for the
// screen
function get_total_pages()
{
	var layers = document.all.tags("DIV");
	
	for(var i=0;i<layers.length;i++)
	{
		var div_id = layers[i].id;
		
	   if (div_id.substring(0,4) == "page")
	   { 	   		
//			var total_pages = div_id.substr(4,1);
			var total_pages = div_id.substr(4);
	   }
	}
	return total_pages;
}


