﻿/*
'======================================================
' 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.
*/
var blnErrorOccurred = false;

var rdmString = +new Date;
var strSelected;

//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(numCatParentID, strChildSelectID) {
	var numThisSelID = null;
	//Here we're using the hidden fields if the page errorred we get the vals from the them rather then the var passed to this function
	if (blnErrorOccurred == true && blnChanged == false) {
		numThisSelID = $('#LAST_DCAT_CategoryIDs').val();
	}

	$("select#" + strChildSelectID).attr('disabled', 'disabled').html('<option value="">loading...</option>');

	$.get("scripts/getCategories.asp", {
		json: 'true',
		numSelectedID: numCatParentID,
		numThisSelID: numThisSelID,
		blnErrorOccurred: blnErrorOccurred,
		s: rdmString
	}, function(j) {

		data = eval(j); //get json array
		var options = '';

		if (data[0].id != 999999) {
			//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#" + strChildSelectID).html(options);
			$("select#" + strChildSelectID).removeAttr('disabled');
		}
		else {
			$("select#" + strChildSelectID).html('');
			$("select#" + strChildSelectID).attr('disabled', 'disabled');
		}
	}, 'json')
};
