/*
'======================================================
' Parodia Internet Recruitment Software			
' �1998-2008 Cactusoft Ltd. www.parodia.net		
'======================================================
' All rights reserved.							
' Use of this code is covered by the terms and	
' conditions in the license agreement. No		
' unauthorized duplication or distribution is	
' permitted. Cactusoft's copyright notices must	
' remain in the ASP sections of the code.

Created by Dean Carslake @ Cactusoft Ltd - 02/10/2008
Some code and ideas copied from here:
- Chained Selects for jQuery 
- Copyright (C) 2008 Ziadin Givan www.CodeAssembly.com  
'======================================================

'Variables and their usage...
- blnChanged 			Flags if the dropdown is changed. We then switch to getting the selected ID from the dropdown and not the hidden field.
- blnErrorOccurred		This flags if we're submitting data that has errored. Again, needed so we know where to get the selected ID from.
- aryFields[]			An array of field names for ease of reference as we use similar names for CATs and REGs.
- numTier 			 	Int of the tier we're actioning i.e. if the T1 box changed we would set this to 2 as thats the next set of data to get.
- numRecordID			*SET IN ASP PAGE* 
*/
var blnChanged = false
var blnErrorOccurred = false
var strSelect
var numThisSelID = null
var numRecordID = (numRecordID!=null) ? numRecordID : 0

//Check if we're submitting data and there was an error. If so, we'll need to reload the dropdowns
blnErrorOccurred = (getFieldVal('blnError') == 'True') ? true : false

//This is used when there is an error, but the user has changed their regions. So we can reset the selected vals
blnChanged = (getFieldVal('blnChanged') == 'True') ? true : false

jQuery.fn.doChainedSelects = function(strType,numTier){
	var loadingMsg = '<img src="images/loading_circle.gif" alt="loading..." id="loadingMsg" />'
	//Show loading image
	$(this).after(loadingMsg);

	//create an array of field names for ease of reference
	var aryFields = new Array();
	for(i=1; i<4; i++) {	
		aryFields[i] = '#' + strType + '_T' + i;
	}

	//hide this tier. If this is tier 3 then hide tier 2 as well
	$("select" + aryFields[numTier]).css('visibility', 'hidden');
	$("select" + aryFields[numTier]).css('display', 'none');
	$("select" + aryFields[numTier]).html('');
	if(numTier==2){
		$("select" + aryFields[(numTier+1)]).css('visibility', 'hidden');
		$("select" + aryFields[(numTier+1)]).css('display', 'none');
		$("select" + aryFields[numTier+1]).html('');
	}
	/* 
	This is the main request. This is called onload and also onchange.
	The numRecordID is set to 0 if one isnt passed across.
	
	Here we're handling the Selected Values. If the boolean value is true we always get the ID's from
	the hidden fields, else we'll see if its a new or existing job
	*/

	//Here we're using the hidden fields - page load when error
	if(blnErrorOccurred == true && blnChanged == false){
		strSelect = $('[name="LAST_' + strType + '_T' + (numTier-1) +'"]').attr('value');
		numThisSelID = $('[name="LAST_' + strType + '_T' + (numTier) +'"]').attr('value');
	} 
	else if((strArea == 'search') && blnChanged == false){
		strSelect = $('[name="LAST_' + strType + '_T' + (numTier-1) +'"]').attr('value');
		numThisSelID = $('[name="LAST_' + strType + '_T' + (numTier) +'"]').attr('value');

	} else {
		strSelect = ($(this).length > 0 && $(this).val() != null)? $(this).val() : $(aryFields[(numTier-1)]).val();
	}
	$.get("includes/scripts/getData.asp",{
		json: 'true',
		type: strType,
		tier: numTier,
		numRecordID: numRecordID,
		numSelectedID: strSelect,
		numThisSelID: numThisSelID,
		blnErrorOccurred: blnErrorOccurred,
		area: strArea
		}, function(j){
			data = eval(j);//get json array
			
			var strSelected;
			var options = (strArea=="cv") ? '<option value="">Any</option>' : '<option value="">Select...</option>';

			if(data[0].id == 999999) {
				options = '<option value="' + data[0].id + '" selected="selected"></option>'
			}
			else {
				//create option elements
				for (var i = 0; i < data.length; i++) {
					strSelected = (data[i].selectID==data[i].id)? ' selected="selected"' : '';
					options += '<option value="' + data[i].id + '"' + strSelected + '>' + data[i].value + '</option>';
				}
			}
			//add them to the empty select boxes
			$("select" + aryFields[numTier]).html(options);

			//Remove the loading image as we're done
			$('#loadingMsg').remove();
			//now show the select
			if(data[0].id != 999999) {
				$("select" + aryFields[numTier]).css('visibility', 'visible');
				$("select" + aryFields[numTier]).css('display', 'inline');
			}
		},'json')
	
	if($(this).val()==0) {
		//Remove the loading image as we're done
		$('#loadingMsg').remove();
	}
};


jQuery.fn.doChainedSelects_cv = function(strType,numTier){
	var loadingMsg = '<img src="images/loading_circle.gif" alt="loading..." id="loadingMsg" />'
	//Show loading image
	$(this).after(loadingMsg);
	//create an array of field names for ease of reference
	var aryFields = new Array();
	for(i=1; i<4; i++) {	
		aryFields[i] = '#' + strType + '_T' + i;
	}

	//hide this tier. If this is tier 3 then hide tier 2 as well
	$("select" + aryFields[numTier]).css('visibility', 'hidden');
	$("select" + aryFields[numTier]).css('display', 'none');
	if(numTier==2){
		$("select" + aryFields[(numTier+1)]).css('visibility', 'hidden');
		$("select" + aryFields[(numTier+1)]).css('display', 'none');
	}
	/* 
	This is the main request. This is called onload and also onchange.
	The numRecordID is set to 0 if one isnt passed across.
	*/
	$.get("includes/scripts/getData.asp",{
		numSelectedID: ($(this).length > 0 && $(this).val() != null)? $(this).val() : $(aryFields[(numTier-1)]).val(), 
		json: 'true',
		type: strType,
		tier: numTier,
		numRecordID: (numRecordID!=null)? numRecordID : 0,
		area: strArea
		}, function(j){
			data = eval(j);//get json array
			var options = (numTier!=3) ? '<option value="">Any</option>' : '';
			var strSelected;
			//if(numTier==2 && strArea="cv"){options = (strArea=="cv") ? '<option value="">Any</option>' : '<option value="">Select...</option>'};
			//create option elements
			if(data[0].id == 999999) {
				options = '<option value="' + data[0].id + '" selected="selected"></option>'
			}
			else {
				//create option elements
				for (var i = 0; i < data.length; i++) {
					strSelected = (data[i].selectID==data[i].id)? ' selected="selected"' : '';
					options += '<option value="' + data[i].id + '"' + strSelected + '>' + data[i].value + '</option>';
				}
			}
			//add them to the empty select boxes
			$("select" + aryFields[numTier]).html(options);
			//Remove the loading image as we're done
			$('#loadingMsg').remove();
			//now show the select
			if(data[0].id != 999999) {
				$("select" + aryFields[numTier]).css('visibility', 'visible');
				$("select" + aryFields[numTier]).css('display', 'inline');
			}
		},'json')
	if($(this).val() ==0) {
		//Remove the loading image as we're done
		$('#loadingMsg').remove();
	}
};