/// <summary>
/// common.js contains all common javascript used throughout the worktrainWeb application.
/// With the exception of focus setting javascript which is done on a page by page basis all
/// javascript used within the site is contained within this file.
/// </summary>

//define member variables
var m_searchClicked=false;
var m_hccSearchClicked=false;
var m_confirmSubjectClicked=false;


/// <summary> 
/// This key press function is used for simple key press functionality i.e. not HCC Dialogue
/// </summary>
/// <param name="e">The key press event</param>
/// <param name="buttonRef">The id of the button to be invoked</param>
function onKeyPressSimple(e, buttonRef)
{
	//check if the enter key has been pressed
	if(e.keyCode == 13)
	{
		//set return value to false and invoke click event of button
		e.returnValue=false;
		var obj = document.getElementById(buttonRef);
		obj.click();
		return false;
	}
}


/// <summary>  
/// This key press function is used by HCC Dialogue
/// </summary>
/// <param name="e">The key press event</param>
/// <param name="buttonRef">The id of the button to be invoked</param>
function onKeyPressHCC(e, buttonRef)
{
	//check if the enter key has been pressed
	if(e.keyCode == 13)
	{
		//set return value to false and invoke click event of button
		e.returnValue=false;
		var obj = document.getElementById(buttonRef);
		obj.click();
		return false;
	}
	else
	{
		//user has entered a different key stroke
		var btn = document.getElementById(buttonRef);
		//if button is disabled, enable it and reset button clicked var
		if (btn.disabled == true)
		{
			btn.disabled=false;
			btn.className="HCCSearchBtn";
			m_hccSearchClicked=false;
			return true;			
		}
	}
}


/// <summary>  
/// Function to set Search buttons to rollover state and disable on second click,
/// to set the cursor to hourglass on all elements of page and to set status bar text.
/// </summary>
/// <param name="id1">The id of the first search button to be affected</param>
/// <param name="id1">The id of the second search button to be affected</param>
/// <param name="statusBarText">Text, in the correct language, to be displayed in the status bar</param>
function searchOptionSearchClick(id1,id2, statusBarText)
{
	//set cursor style
	document.body.style.cursor="wait";
	document.getElementById(id1).style.cursor="wait";
	document.getElementById(id2).style.cursor="wait";
	//set cursor on fieldsets
	setFieldsetCursors()
	//set status bar text
	status=statusBarText;
	
	//disable button if appropriate
	if(m_searchClicked)
	{
		document.getElementById(id1).disabled=true;	
		document.getElementById(id2).disabled=true;
		document.getElementById(id1).className="SearchRollover";
		document.getElementById(id2).className="SearchRollover";
		return false;
	}
	else
	{
		m_searchClicked = true;
		return true;
	}	
}

		
/// <summary>  
/// Function called by HCCDialogue to set Search button to rollover state and disable on second click
/// the status bar text will also be set and the cursor changed to it's 'wait' status e.g. hourglass.
/// it requires the id of the button, and the statusBarText in the correct language to be passed to it.
/// </summary>
/// <param name="id">The id of the button on the HCCDialogue</param>
/// <param name="statusBarText">Text, in the correct language, to be displayed in the status bar</param>
function hccDialogueSearchClick(id, statusBarText)
{
	//set cursor style
	document.body.style.cursor="wait";
	document.getElementById(id).style.cursor="wait";
	
	//if hcc dialogue fieldset present set cusor style within this
	if(document.getElementById('fsHCCDialogue')!= null)
	{
		document.getElementById('fsHCCDialogue').style.cursor='wait';
	}
	
	//set status bar text
	status=statusBarText;
	
	//prevent secondary click
	if(m_hccSearchClicked)
	{
		document.getElementById(id).disabled=true;	
		document.getElementById(id).className="HCCSearchBtnRollover";
		return false;
	}
	else
	{
		m_hccSearchClicked=true;
		return true;
	}
}


/// <summary>  
/// Function called from subject selection pages to limit number of subjects selected
/// </summary>
/// <param name="checkboxRef">The id of the checkbox list of subjects</param>
/// <param name="errorText">Text, in the correct language, to be displayed in the message box</param>
function selectedSubjectsCheck(checkboxRef, errorText)
{
	var selected = 0;
	//retrieve number of subjects in list
	var itemCount = document.getElementsByTagName("input").length;
    
    //loop through subjects
	for	(var count = 0; count < itemCount; count++) 
	{
		chkName = checkboxRef + "_" + count;
		//retrieve check box item for checking
		var objcheck = document.getElementById(chkName);
		if (objcheck != null)
		{
			if (objcheck.checked == true) 
			{
				//increment number of selected subjects
				selected += 1;
				if (selected > 3)
	            {
		            //display message and return false to uncheck box
		            alert(errorText);
		            return false;
	            }
			}
		}
	}	
}


/// <summary>  
/// Function which is virtually identical to btnSearchClickHCC, but as the 2 buttons both appear on 
/// the ConfirmSubject page 2 separate functions are required  
/// </summary>
/// <param name="id">The id of the button which confirms subject selection</param>
/// <param name="statusBarText">Text, in the correct language, to be displayed in the status bar</param>
function confirmSubjectClick(id, statusBarText)
{        
	//set cursor style
	document.body.style.cursor="wait";
	document.getElementById(id).style.cursor="wait";
	
	//set status bar text
	status=statusBarText;
	//if Subjects fieldsets present set cusor style within these
	if(document.getElementById('fsOptions')!= null)
	{
		document.getElementById('fsOptions').style.cursor='wait';
	}
	if(document.getElementById('chklbPreferredTerms')!= null)
	{
		document.getElementById('chklbPreferredTerms').style.cursor='default';
	}
	if(document.getElementById('chklBroadTerms')!= null)
	{
		document.getElementById('chklBroadTerms').style.cursor='default';
	}
	//prevent secondary click
	if(m_confirmSubjectClicked)
	{
		document.getElementById(id).disabled=true;	
		document.getElementById(id).className="SearchRollover";
		return false;
	}
	else
	{
		m_confirmSubjectClicked = true;
		return true;
	}
}


/// <summary>  
/// This function sets the cursor to 'wait' style e.g. hourglass when a hyperlink is clicked
/// <summary>
/// <param name="id">The id of the hyperlink being clicked</param>
/// <param name="statusBarText">Text, in the correct language, to be displayed in the status bar</param>
function hyperlinkClick(id, statusBarText)
{
	var hplClicked = document.getElementById(id);
	//set cursor style
	hplClicked.style.cursor='wait'; 
	document.body.style.cursor='wait';
	
	//set cusor style for such fieldsets as are present on the page
	setFieldsetCursors();
	//if hcc dialogue fieldset present set cusor style within this
	if(document.getElementById('fsHCCDialogue')!= null)
	{
		document.getElementById('fsHCCDialogue').style.cursor='wait';
	}	
	
	//set status text
	status=statusBarText;
	
	//prevent multiple clicking of link
	if (!hplClicked.clicked) 
	{ 
		hplClicked.clicked = true; 
		return true; 
	}
	else 
	{
		return false;
	}
}


/// The following functions are effectively private functions only called from within this script ///

/// <summary> 
/// Common function for setting of fieldset cursors used by button and hyperlink click functions 
/// </summary>
function setFieldsetCursors()
{
	//set cusor style for such fieldsets as are present on the page
	if(document.getElementById('fsRadius')!= null)
	{
		document.getElementById('fsRadius').style.cursor='wait';
	}
	//job specific
	if(document.getElementById('fsPermTemp')!= null)
	{
		document.getElementById('fsPermTemp').style.cursor='wait';
	}
	if(document.getElementById('fsHoursPerWeek')!= null)
	{
		document.getElementById('fsHoursPerWeek').style.cursor='wait';
	}
	if(document.getElementById('fsAgeOfVacancy')!= null)
	{
		document.getElementById('fsAgeOfVacancy').style.cursor='wait';
	}
	if(document.getElementById('fsSource')!= null)
	{
		document.getElementById('fsSource').style.cursor='wait';
	}
	//training specific
	if(document.getElementById('fsLearningMethod')!= null)
	{
		document.getElementById('fsLearningMethod').style.cursor='wait';
	}
	if(document.getElementById('fsQualification')!= null)
	{
		document.getElementById('fsQualification').style.cursor='wait';
	}
	//childcare specific
	if(document.getElementById('fsTypeOfChildcare')!= null)
	{
		document.getElementById('fsTypeOfChildcare').style.cursor='wait';
	}
	if(document.getElementById('fsAvailability')!= null)
	{
		document.getElementById('fsAvailability').style.cursor='wait';
	}
	//voluntary specific
	if(document.getElementById('fsInterest')!= null)
	{
		document.getElementById('fsInterest').style.cursor='wait';
	}
	if(document.getElementById('fsActivity')!= null)
	{
		document.getElementById('fsActivity').style.cursor='wait';
	}
	if(document.getElementById('fsTypeOfOpportunity')!= null)
	{
		document.getElementById('fsTypeOfOpportunity').style.cursor='wait';
	}
}