function form_focus(form_num, field_num)
{
  if (document.forms.length > 0)
  {
    document.forms[form_num].elements[field_num].focus();
  }
}

function load_images()
{
  if (document.images)
  {
    if (typeof(document.kraemer) == 'undefined')
    {
      document.kraemer = new Object();
    }

    document.kraemer.loaded_images = new Array();
    var num_arg = load_images.arguments.length;

    for(i=0;i<num_arg;i++)
    {
      document.kraemer.loaded_images[i] = new Image();
      document.kraemer.loaded_images[i].src = load_images.arguments[i];
    }
  }
}

var photopWin;
var photolWin;

function popUpp(url)
{
  if (photolWin != null)
  {
    if (!photolWin.closed)
    {
      photolWin.close();
      photolWin = null;
    }
  }
  photopWin=window.open(url,"Photo_Album",'width=330,height=500');
  photopWin.focus(); 
}

function popUpl(url)
{
  if (photopWin != null)
  {
    if (!photopWin.closed)
    {
      photopWin.close();
      photopWin = null;
    }
  }
  photolWin=window.open(url,"Photo_Album_",'width=500,height=330');
  photolWin.focus(); 
}

function get_quarry_info(quarry,id)
{
  document.getElementById('quarry_link').innerHTML = "Click here to find out more about the <b>" + quarry + "</b> quarry";
  document.getElementById('quarry_link').style.visibility = "visible";
  document.getElementById('quarry_link').href = "kraemer-quarry.php?" + id;
  document.getElementById('quarry_info').innerHTML = quarry;
}

function delete_user(name, active)
{
	if (active==0)
	{
		var where_to = confirm("Are you sure you want to delete user '" + name + "'?");
		
		if (where_to==false)
		{
			return false;
		}
	}
	else
	{
		alert("Please deactivate user '" + name + "' before deletion");
		return false;
	}
}


function highlight_row(id, action, previous_classes)
{
	if(action==1)
	{
		new_class = 'siso_highlighted_row ' + previous_classes;
		change=document.getElementById(id);
		change.className=new_class;
	}
	else
	{
		new_class = previous_classes;
		change=document.getElementById(id);
		change.className=new_class;
	}
}


function aReload() { location.reload(true); }
function startReload() { setTimeout("aReload()", 200000); }
  
  
  /*GLOBAL*/
  var last_selected_row_id ='';
  
  
var current_tab = 'siso_front_tab';
var current_tab_state = 0;
var last_tab_opened = 0;

function show_tab(show_tab,id,date,close)
{
	
	
	show_tab = show_tab + "_" + id;
	//Show latest tab
	/*
	if(document.getElementById(show_tab).className == 'siso_tab_hide')
	{
	*/
		//Closed last tab
		if(last_tab_opened!=0)
		{
			new_class = 'siso_tab_hide';
			change=document.getElementById(show_tab);
			change.className=new_class;
		}
		
		if(close!="" && last_tab_opened!=0)
		{
			new_class = 'siso_tab_hide';
			change=document.getElementById('siso_today_tab_' + last_tab_opened);
			change.className=new_class;
		}
		
		if(last_tab_opened==id)
		{
			new_class = 'siso_tab_hide';
			change=document.getElementById('siso_today_tab_' + last_tab_opened);
			change.className=new_class;
			last_tab_opened=0;
		}
		else
		{
			
			new_class = 'siso_tab_hide';
			change=document.getElementById('siso_schedule_tab');
			change.className=new_class;
			
			new_class = 'siso_tab_show';
			change=document.getElementById(show_tab);
			change.className=new_class;
			current_tab = show_tab;
			current_tab_state = 1;
			last_tab_opened = id;
		}
		
		
		
		/*
	}
	else
	{
		*/
		/*
		alert("closing 3");
		new_class = 'siso_tab_hide';
		change=document.getElementById(show_tab);
		change.className=new_class;
		current_tab = show_tab;
		current_tab_state = 0;
		
	}
	*/
}

function expand_activities(id,action)
{
	if(document.getElementById('short_destination_'+id).className == 'siso_hide')
	{
		new_class = 'siso_hide';
		change=document.getElementById('long_destination_'+id);
		change.className=new_class;
		
		new_class = 'siso_show';
		change=document.getElementById('short_destination_'+id);
		change.className=new_class;
	}
	else
	{
		new_class = 'siso_hide';
		change=document.getElementById('short_destination_'+id);
		change.className=new_class;
		
		new_class = 'siso_show';
		change=document.getElementById('long_destination_'+id);
		change.className=new_class;
	}
}


function show_schedule_tab()
{
	
	//Close last opened 'today' tab
	if(last_tab_opened!=0)
	{
		new_class = 'siso_tab_hide';
		change=document.getElementById('siso_today_tab_' + last_tab_opened);
		change.className=new_class;
		last_tab_opened=0;
		unhighlight_row();
	}
	
	//Show latest tab
	if(document.getElementById('siso_schedule_tab').className == 'siso_tab_hide')
	{
		new_class = 'siso_tab_show';
		change=document.getElementById('siso_schedule_tab');
		change.className=new_class;
	}
	else
	{
		new_class = 'siso_tab_hide';
		change=document.getElementById('siso_schedule_tab');
		change.className=new_class;
	}
}

//VARIABLE ABOVE!!!
function select_row(id)
{
	//Hide the previous information, if showing
		//First, "deselect the last row"
		if(id!=last_selected_row_id && last_selected_row_id!="")
		{
			document.getElementById("row_"+last_selected_row_id).style.backgroundColor='white'; 
		}
		//Second, hide the last "Today's Activities" box
		if(id!=last_selected_row_id && last_selected_row_id!="")
		{
			document.getElementById("siso_today_tab_"+last_selected_row_id).className = 'siso_hide'; 
		}
		//Third, Makes sure the "schedule" tab is gone
		new_class = 'siso_tab_hide';
		change=document.getElementById('siso_schedule_tab');
		change.className=new_class;
	
	if(document.getElementById("row_"+id).style.backgroundColor!='lightblue') 
	{ 
		document.getElementById("row_"+id).style.backgroundColor='lightblue';
		last_selected_row_id = id;
	} 
	else 
	{ 
		document.getElementById("row_"+id).style.backgroundColor='white'; 
	} 
}

function unhighlight_row()
{
	document.getElementById('row_' + last_selected_row_id).style.backgroundColor='white';
}

//Formats the time if the person did not do so
function format_time(time_field)
{
	var raw_time = time_field.value;
	var formatted_time = '';
	//if the field is empty
	if(raw_time.length==0)
	{
		//(do nothing)
		time_field.value = '';
	}
	//else, if the time is only one number
	else if (raw_time.length==1 || (raw_time.length==2 && isNumeric(raw_time,"0123456789")))
	{
		//set the formatted time;
		formatted_time = raw_time + ":00";
		time_field.value = formatted_time;
	}
	//else, the time is either words or something like 8:3 or 12.3 (8:30 and 12:30)
	else
	{
		//Find out if there's a delimiter
		raw_time = raw_time.replace('.',':');
		del_index = raw_time.lastIndexOf(':');
		
		if(del_index!=-1)
		{
			//Find out if, without the delimiter, the string is numeric
			var is_a_number = isNumeric(raw_time,"0123456789:");
			
			//Check to see if it's a number without the delimiter
			if(is_a_number == true)
			{
				//check to see if the last character is the ':'
				if(del_index+1 == raw_time.length)
				{
					formatted_time = raw_time + "00";
					time_field.value = formatted_time;
				}
				else
				{
					//check to see if the string ends with ':00'
					var difference = raw_time.length - del_index+1;
					if(difference<4)
					{
						formatted_time = raw_time + "0";
						time_field.value = formatted_time;
					}
				}
			}
			//else, it's probably a string and doesn't need to be touched
			else
			{
				
			}
		}
		else
		{
			//check for 11 or 12
			if(isNumeric(raw_time,"0123456789")==true)
			{
				formatted_time = raw_time + ":00";
				time_field.value = formatted_time;
			}
		}
	}
}

function isNumeric(sText,ValidChars)
{
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;

}






//DPER

function tally_production_hours()
{
	ferm = document.forms[0];
	
	//tally the regs
	tally = ferm.ho_production.value*1 + ferm.ho_maintenance.value*1 + ferm.ho_downtime.value*1 + ferm.ho_mobilization.value*1 + ferm.ho_moved_to.value*1 + ferm.ho_other.value*1;
	ferm.ho_total.value = tally*1;
	
	//tally the downs
	tally = ferm.ho_main_from.value*1 + ferm.ho_down_to.value*1 + ferm.ho_mobil_from.value*1 + ferm.ho_moved_to_2.value*1 + ferm.ho_other_from.value*1;
	ferm.ho_total_to.value = tally*1;
}
