/*************************************************************
* Page: common_functions.js v 1.15  
* Description: Common Javascript functions
* 
* Hospitality Recruitment - Online Strategies
* Copyright (C) 2005 Online Strategies, All rights reserved
* Email  info@hospitalityonline.co.uk
* 
* Last modified: 25.07.2005
* - Added Check for DOM support to all funtions (26.06.2005)
* - Function: hightligh checked rows in message centre (12.07.2005)
* - Function: select all options in a select (25.07.2005)
* - Function: select all checkboxes in a certain div element with an id (25.07.2005)
* NOTES
* always use if (document.getElementById && document.createTextNode)
* to check that the script will work on the current browser
*
*************************************************************/
/* Version to Created on 29.09.2005 to get rid of old functions and create neater code */

// Functions to roll over and select rows
var activeRow = null;

// Funstion to set the roll over color 
function row_over(whichEl)
{
	if (document.getElementById && document.createTextNode){
		if ((!activeRow) || (activeRow && activeRow != whichEl)) whichEl.className = "row_over";
	}
}

// Funstion to set the roll Out color 
function row_off(whichEl)
{
	if (document.getElementById && document.createTextNode)
	{
		if ((!activeRow) || (activeRow && activeRow != whichEl)) whichEl.className = "row";
	}
}

// Funstion to set the Click on colours 
function row_click(whichEl) {
	if (document.getElementById && document.createTextNode)
	{
		if (activeRow && activeRow != whichEl) activeRow.className = "row";
		whichEl.className = "row_click";
		activeRow = whichEl;
	}
}

//--------------------------------------------------------------------------------------------------------
// Function that show's and hide one alternative Div
// Generally used when processing forms to show text.
// show = div you want to hide on submit
// hide = div you want to hide on submit
function showprocess(hide,show)
{
	// check for DOM suppport
	if (document.getElementById && document.createTextNode)
	{
		//lets set some vars
		var hideit = document.getElementById(hide);
		var showit = document.getElementById(show);
		location.href='#top';
		hideit.style.display = "none";
		showit.style.display = "block";
	}
}

//--------------------------------------------------------------------------------------------------------
// Function to open a popup window 
// name = name of the window
// URL = page you want to display
// width = width of window
// height = height of window
// scroll = 1 (scrollbars) 0 (no scrollbars)
function open_window(name,URL,width,height,scroll)
  {
	// Set Variables to position screen in the middle of the page
	var left = (screen.width) ? (screen.width-width)/2 : 0;
	var top = (screen.height) ? (screen.height-height)/2 : 0;
	
	// Open the new window
	window.open(URL,name,'left='+left+',top='+top+',toolbar=0,location=0,directories=0,status=0, menubar=0,scrollbars='+scroll+',resizable=0,width='+width+',height='+height);
  }
  
  //--------------------------------------------------------------------------------------------------------
// Function to open a popup window 
// name = name of the window
// URL = page you want to display
// width = width of window
// height = height of window
// scroll = 1 (scrollbars) 0 (no scrollbars)
function open_small_window(URL)
  {
	// Open the new window
	window.open(URL,name,'toolbar=1,location=1,directories=1,status=1, menubar=1,scrollbars=1,resizable=1,width=800,height=600');
  }

//--------------------------------------------------------------------------------------------------------
// Function to count charactors on a textarea
// field = field that needs counting
// countfield = input that counts down
// maxlimit = limit you want to count
function text_counter(field, countfield, maxlimit)
{
	// Check if browser supports DOM
	if (document.getElementById && document.createTextNode)
	{
		// Lets set come variables
		var f = document.getElementById(field);
		var c = document.getElementById(countfield);
		
		if (f.value.length > maxlimit) // if too long...trim it!
		{ 
			f.value = f.value.substring(0, maxlimit);
		}
		else
		{ 
			c.value = (maxlimit - f.value.length)
		}
	}
}

//--------------------------------------------------------------------------------------------------------
// Function to show and hide content with or without an image and text
// type 1 = img included to change
// type = type image or no image
// div = div that you are applying the function to
// img = img if type = 1 that you want to change
// imgname name of the image you want to change
function show_hide_divs(type, div, img, imgname)
{
	// Check for DOM support
	if (document.getElementById && document.createTextNode)
	{
		// Lets set come variables
		var divelem = document.getElementById(div);
		var imgelem = document.getElementById(img);
		
		if (divelem.style.display == "none")
		{
			if (type == 1)
			{
				divelem.style.display = "block";
				imgelem.src = "/images/icons/"+imgname+"_open.gif";
				imgelem.alt = "Open";
			}
			else
			{
				divelem.style.display = "block";
			}
		}
		else
		{
			if (type ==1){
				divelem.style.display = "none";
				imgelem.src = "/images/icons/"+imgname+"_close.gif";
				imgelem.alt = "Close";
			}
			else
			{
				divelem.style.display = "none";
			}
		}
	}
}

//--------------------------------------------------------------------------------------------------------
// Function to show and hide content with or without an image and text
// type 1 = img included to change
// type = type image or no image
// div = div that you are applying the function to
// img = img if type = 1 that you want to change
// imgname name of the image you want to change
function show_hide_divs_checkbox(e, c)
{
	if (document.getElementById && document.createTextNode)
	{
		var elem = document.getElementById(e);
		var chk = document.getElementById(c);
		(chk.checked)?elem.style.display = "none":elem.style.display = "block";
	}
}
//--------------------------------------------------------------------------------------------------------
// Function to show and hide content with or without an image and text
// type 1 = img included to change
// type = type image or no image
// div = div that you are applying the function to
// img = img if type = 1 that you want to change
// imgname name of the image you want to change
function show_hide_divs_radio(e, c, show)
{
	if (document.getElementById && document.createTextNode)
	{
		var elem = document.getElementById(e);
		var chk = document.getElementById(c);
		if (chk.checked)
		{
			if (show)
			{
				elem.style.display = "block";
			}
			else
			{
				elem.style.display = "none";
			}
		}
	}
}

//--------------------------------------------------------------------------------------------------------
// Function to show and hide an element on change with a select box
// element - the element  you want to show / hide
// selectbox - the select box you want to apply the function to
// selectindex - the selectedIndex that you want to show hide from
function show_hide_divs_select(element, selectbox, selectindex)
{
	if (document.getElementById && document.createTextNode)
	{
		var elem = document.getElementById(element);
		var sel = document.getElementById(selectbox);
		
		(sel.selectedIndex != selectindex)?elem.style.display = "none":elem.style.display = "block";
	}
}

//--------------------------------------------------------------------------------------------------------
// Function to change the text inside a label
function show_hide_div_label(div, span, texton, textoff)
{
	// Check for DOM support
	if (document.getElementById && document.createTextNode)
	{
		// Lets set come variables
		var d = document.getElementById(div);
		var s = document.getElementById(span);
		
		if (d.style.display == "block")
		{
			d.style.display = "none";
			s.innerHTML = texton;
		}
		else
		{
			d.style.display = "block";
			s.innerHTML = textoff;
		}
	}
}

//--------------------------------------------------------------------------------------------------------
// Function to change the status of the drop down menu's for the countries drop down
// If "United Kingdom" ia selected, then show UK Regions.
// If anything else is selected show nothing at all

function show_hide_regions()
{
	// Check for DOM support
	if (document.getElementById && document.createTextNode){
	
		// lets set some variables
		var c = document.getElementById('country');
		var ukr = document.getElementById('show_regions');
		var nr = document.getElementById('hide_regions');
		var r = document.getElementById('regions');
		var tr = document.getElementById('t_regions');
		var rlabel = document.getElementById('reg_label');
		var sr	   = document.getElementById('show_sub_regions');
		var sr_label	   = document.getElementById('sub_reg_label');
		if (c.selectedIndex == 0)
		{
			nr.style.display = "none";
			ukr.style.display = "none";
			rlabel.style.display = "none";
			r.selectedIndex = 0;
			tr.value = "";
			sr.style.display = "none";
			sr_label.style.display = "none";
		}
		else if (c.selectedIndex == 1)
		{
			nr.style.display = "none";
			ukr.style.display = "block";
			tr.value = "";
			rlabel.style.display = "block";
		}
		else 
		{
			nr.style.display = "block";
			ukr.style.display = "none";
			rlabel.style.display = "block";
			r.selectedIndex = 0;
			sr.style.display = "none";
			sr_label.style.display = "none";
		}
	}
}

//--------------------------------------------------------------------------------------------------------
// Function to change the status of the drop down menu's for the countries drop down
// If "United Kingdom" ia selected, then show UK Regions.
// If anything else is selected show nothing at all

function show_hide_regions_jobsearch()
{
	// Check for DOM support
	if (document.getElementById && document.createTextNode){
	
		// lets set some variables
		var c = document.getElementById('country');
		var ukr = document.getElementById('show_regions');
		var r = document.getElementById('regions');
		var rlabel = document.getElementById('reg_label');
		if (c.selectedIndex == 0)
		{
			ukr.style.display = "none";
			rlabel.style.display = "none";
			r.selectedIndex = 0;
		}
		else if (c.selectedIndex == 1)
		{
			ukr.style.display = "block";
			rlabel.style.display = "block";
		}
		else 
		{
			ukr.style.display = "none";
			rlabel.style.display = "none";
			r.selectedIndex = 0;
		}
	}
}
//--------------------------------------------------------------------------------------------------------
// Function to insert text into a text field from a popup window
// elem = target element by ID that you want to send the txt to
// text = text that you are going to be inserting into the database
// split = if you want to add multiple words using the same function
// split 1 = add comma between words
// split 0 = just add one value and close window
function insert_text(elem,text,split)
{
	// Check for DOM support
	if (document.getElementById && document.createTextNode)
	{
		if (!window.opener) // If there is no opener window, then send an alert
		{
			alert("No Target");
		}
		else // If the opener exsists, then process
		{
		// Set variables for popup window
		var e = window.opener.document.getElementById(elem);
		var s = ",";
			
			if (split == 1) // If you want to split multiple words with a comma
			{
				// If there is no value then insert the first value
				if (e.value == "")
				{
					e.value = text;
				}
				// If there is already a value on the box, then split the rest by comma
				else if (e.value !== "")
				{
					e.value = e.value + s + text;
				}
			}
			else if (split == 0) // If there is no splitter
			{
				e.value = text;
				window.close();
			}
			else
			{
				e.value = "error";
			}
		}
	}
}

//--------------------------------------------------------------------------------------------------------
// Fucntion to disable date boxes if if checkbox is checked
// day = dat dropdown
// month = month dropdown
// year = year dropdown
// trigger = element in which to add the function
function disable_date_dropdowns(day,month,year,trigger)
{
	// Check for DOM support
	if (document.getElementById && document.createTextNode)
	{
		// Lets set come variables
		var d = document.getElementById(day);
		var m = document.getElementById(month);
		var y = document.getElementById(year);
		var trig = document.getElementById(trigger);
		
		// check is the relevant checkbox is checked, then disable the select boxes
		if (trig.checked){
			// Disable the fields and set their selection to 0
			d.disabled = true; d.selectedIndex = 0;
			m.disabled = true; m.selectedIndex = 0;
			y.disabled = true; y.selectedIndex = 0;
		}
		// if the checkbox is not selected, then enable the fields
		else {
			// Enable the fields
			d.disabled = false;
			m.disabled = false;
			y.disabled = false;
		}
	}
}

//--------------------------------------------------------------------------------------------------------
// Function to set the focus onto another field
// from - field that the data is coming from
// field that the data is going to
// length before move
function move_focus(fromfield, tofield, maxlength)
{
	// Check for DOM support	
	if (document.getElementById && document.createTextNode)
	{
		// Lets set some variables
		var from = document.getElementById(fromfield);
		var to   = document.getElementById(tofield);
		var len  = document.getElementById(maxlength);
		
		// If the length is up, then lets move to the next one.
		if (from.value.length >= maxlength)
		{
			to.focus();
		}
	}
}

//--------------------------------------------------------------------------------------------------------
// Function that highlights the row relevant to the checkbox, inside message centre
// row = the relecant row that needs to be higlighted
function highlight_row(row, revert)
{
	// Check for DOM support
	if (document.getElementById && document.createTextNode)
	{
		// find the table to select from
		var tr = document.getElementById('tbl').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
		var chk = document.getElementById('tbl').getElementsByTagName('tbody')[0].getElementsByTagName('input');
		
		// If its row change back to row
		if (chk[row].checked == true)
		{
			tr[row].className = 'row_click';
		}
		else
		{
			tr[row].className =  revert;
		}
	}
}

//--------------------------------------------------------------------------------------------------------
// Function that checks all checkboxes in a table, and highlights their rows
// row = the relecant row that needs to be higlighted
function select_all_for_delete()
{
	// Check for DOM support
	if (document.getElementById && document.createTextNode)
	{
		// find the table to select from
		var tr 			= document.getElementById('tbl').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
		var chk_array 	= document.getElementById('tbl').getElementsByTagName('tbody')[0].getElementsByTagName('input');
		var trigger		= document.getElementById('checkall');
		var row;
		//alert (chk_array.length);
		
		if(trigger.checked)
		{
			for (i=0; i < chk_array.length; i++)
			{
				
				chk_array[i].checked = true;
				tr[i].className = 'row_click';
			}					
		}
		else
		{
			for (i=0; i < chk_array.length; i++)
			{
				(row == "row")?row = "row_over":row = "row";
				chk_array[i].checked = false;
				tr[i].className = row;
			}	
		}
	}
}

//--------------------------------------------------------------------------------------------------------
// Function that selects and deselects a range of multiple select options
// selectid - the select tag that you are looking to "select" all of
// inner_html - the href with the text inside
function select_deselect(selectid, inner_html)
{
	// Check for DOM support
	if(document.getElementById && document.createTextNode)
	{
		// Lets set some variables
		var all_options = document.getElementById(selectid).getElementsByTagName('option');
		var inner    	= document.getElementById(inner_html);
		
		
		// If the a href reads "select all" then change it to "deselect all" and select all option elements
		if(inner.innerHTML == 'select all')
		{
			// Set the inner HTML
			inner.innerHTML = 'deselect all'
			// Loop through all the elements and select them
			for(var i = 0; i < all_options.length; i++)
			{		
				all_options[i].selected = true;		
			}
		}
		
		// If the a href reads "deselect all" then change it to "select all" and de select all option elements
		else
		{
			// Set the innter HTML
			inner.innerHTML = 'select all'
			// Loop through all the elements and deselect select them
			for(var i = 0; i < all_options.length; i++)
			{
				all_options[i].selected = false;
			}
		}
	}
}

//--------------------------------------------------------------------------------------------------------
// Function that that will check and uncheck checkboxes
// container - the element that contains the checkboxes
// inner_html - the href with the text inside
function check_uncheck(container, inner_html)
{
	// Check for DOM support
	if(document.getElementById && document.createTextNode)
	{
		// Lets set some variables
		var sel_elem 	= document.getElementById(container);
		var inner    	= document.getElementById(inner_html);
		var check_them	= sel_elem.getElementsByTagName('input');
		
		// If the a href reads "select all" then change it to "deselect all" and select all option elements
		if(inner.innerHTML == 'select all'){
			// Set the inner HTML
			inner.innerHTML = 'deselect all';
			// Run through the loop and check all checkboxes
			for(var i = 0; i < check_them.length; i++)
			{
				check_them[i].checked = true;	
			}				
		}
		
		// If the a href reads "deselect all" then change it to "select all" and de select all option elements
		else
		{
			// Reset the InnerHTML
			inner.innerHTML = 'select all';
			// Run through the loop and uncheck all the checkboxes
			for(var i = 0; i < check_them.length; i++)
			{
				check_them[i].checked = false;	
			}				
		}
	}
}

// Fucntion to change the action of the form to delete all messages
// form = form you want to the action to change
// action = the action you want it changed to
function change_form_action (form, action)
{
	// Check for DOM support
	if(document.getElementById && document.createTextNode)
	{
		var frm = document.getElementById(form);
		frm.setAttribute('action',action);
	}
}

// Function swap heights for text area's in STD replies	
function swap_textarea_height(element, sml, height)
{
	// Check for DOM support
	if(document.getElementById && document.createTextNode)
	{
		// Lets set some variables
		var elem = document.getElementById(element);
		var s = document.getElementById(sml);
		var h = document.getElementById(height);
		if(elem.height == s)
		{
			elem.height = h;
		}
		else
		{
			elem.height = s;
		}
	}
}

// Function to insert common job title into a text area
// text_box = text box that we are inserting to
// text = text we are insterting
function insert_job(text)
{
	// Check for DOM support
	if(document.getElementById && document.createTextNode)
	{
		// Lets set some variables
		var tb = window.opener.document.getElementById('insert_into');
		if (tb)
		{
			tb.value = text;
			window.close();
		}
		else
		{
			return false;
		}
	}
}

// Function to close a popup and refresh the opener window
function close_it()
{
	// Check for DOM support
	if(document.getElementById && document.createTextNode)
	{
		var loc = window.opener.location.href;
		window.opener.location.reload();
		
		window.opener.location.href = loc+'#note';
		window.close();
	}
}

//------------------------------------------------------------------
// Function that creates an array from the elements that we need to SHOW
function show_elements()
	{
	if (document.getElementById && document.createTextNode)
	{
		for (var i = 0; i < arguments.length; i++)
		{
			var elements = document.getElementById(arguments[i]);
			// Check the elements, and then set them to hide or not
			if(elements)
			{
				//alert('show');
				elements.style.display = 'block';
			}
		}	
	}
}

//------------------------------------------------------------------
// Function that creates an array from the elements that we need to HIDE
function hide_elements()
	{
	if (document.getElementById && document.createTextNode)
	{
		for (var i = 0; i < arguments.length; i++)
		{
			var elements = document.getElementById(arguments[i]);
			// Check the elements, and then set them to hide or not
			if(elements)
			{
				//alert('hide');
				elements.style.display = 'none';
			}
		}	
	}
}
//------------------------------------------------------------------
// Function that creates an array from the elements that we need to HIDE
function show_hide_multiple()
	{
	if (document.getElementById && document.createTextNode)
	{
		for (var i = 0; i < arguments.length; i++)
		{
			var elements = document.getElementById(arguments[i]);
			// Check the elements, and then set them to hide or not
			if(elements)
			{
				if(elements.style.display == 'none')
				{
					elements.style.display = 'block';
				}
				else
				{
					elements.style.display = 'none';
				}
				
			}
		}	
	}
}

// Fucntion to stretch 2 elements that are floating to the same accoriing to which one is longer
// left = left clumn that needs to be checked.
// right = right coloumn that needs to be checked.
function fix_height(left,right)
{	
	// Check for DOM
	if (document.getElementById && document.createTextNode)
	{	
		// Lets set some variables
		var lh=document.getElementById(left).offsetHeight;
		var rh=document.getElementById(right).offsetHeight;
		
		// 
		document.getElementById(right).style.height=(lh>rh)? lh+"px" : rh+"px";
		document.getElementById(left).style.height=(rh>lh)? rh+"px" : lh+"px";
	}
}

// Fucntion to stretch 2 elements that are floating to the same accoriing to which one is longer
function fix_width(elem)
{
	// Check for DOM support
	if (document.getElementById && document.createTextNode)
	{	
		var element = document.getElementById(elem);
		var final_width = (screen.width - 450);
		element.style.width = final_width+"px";
		alert(final_width);
	}
}

// Fucntion to stretch 2 elements that are floating to the same accoriing to which one is longer
// image  = the ID of the image that needs to be changed.
// src_to = the new source for the image

function swap_image(image,src_to)
{
	// Check for DOM support
	if (document.getElementById && document.createTextNode)
	{	
		// Lets set some variables
		var image = document.getElementById(image);
		if (image)
		{
			image.src = src_to;
		}
	}
}

// Fucntion that add's to favorites or to Firefox Bookmarks
function add_to_bookmarks(name,url)
{
	if (window.sidebar)
	{
		window.sidebar.addPanel(name, url,"");
	}
	else if (document.all)
	{
		window.external.AddFavorite(url, name);
	}
	else{
		alert("Sorry your browser does not support this script");
	}
}

// Function to change a + and - inside a span
function plus_minus(id)
{
	if (document.getElementById && document.createTextNode)
		{	
		// Lets set some variables
		var element = document.getElementById(id);
		(element.innerHTML == "-") ? element.innerHTML = "+" : element.innerHTML = "-";
	}
}

