/*
Parent - Child Select Lists Revisited: Multiple Child Selects
Copyright (C) 2009 Doug Vanderweide

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function bindList() {
	//function requires at least two parameters
	//first parameter MUST be ID of target list (i.e., list to be populated)
	//second parameter MUST be ID of sending list (i.e., immediate parent)
	//ALL parent select lists of target list must be passed to function as arguments
	
	if(arguments.length < 1) {
		//not enough parameters provided
		alert('You did not provide any arguments to the bindList() function');
	}
	else {
		//clear all lists that are not parents to invoking select list
		clearChildLists.apply(this, arguments);
		
		var qs = "http://www.easternwestern.co.uk/aaa/list.php?";
		var ok = true;
		var s; 
		
		//list to be bound with values
		var target = "#" + arguments[0];
		//list that is invoking this function
		var sender = "#" + arguments[1];
		
		//clear child list values
		$(target).attr("disabled", true);
		clearMessage(target);
		
		//change target list to indicate values being retrieved
		var s = document.getElementById(arguments[0]);
		s.options.length = 0;
		s.options[0] = new Option('Getting list ...', '');
		
		if(checkValue(sender)) {
			//build querystring
			for(var i = 1; i < arguments.length; i++) {
				qs += "&" + arguments[i] + "=" + escape($("#" + arguments[i]).val());
			}
			
			//http get call to helper page
			var theOptions = new Array();
			$.get(qs, function(data) {
					eval(data);
					//on success, bind list as options
					if(theOptions.length > 0) {
						addOptions(target, theOptions);
					}
				}
			);
		}
		else {
			//replace with error message on invalid parent option
			s.options.length = 0;
			s.options[0] = new Option('Select a valid parent value ...', '');
		}
	}
}

function clearChildLists() {
	//function clears values of all lists in parent-child chain that are not sent as arguments
	
	var lists = new Array();
	var children = new Array();
	var s;
	var tmp;
	
	//add all parent-child chain lists to array
	$(".ms").each(function() {
		lists.push(this.id);
	});
	
	//check every parent-child chain select ...
	for(var i = 0; i < lists.length; i++) {
		tmp = false;
		//against the invoking select list and its parents ...
		for(var x = 0; x < arguments.length; x++) {
			//if the chain list is the invoking list or its parent(s), stop
			if(arguments[x] == lists[i]) {
				tmp = true;
				break;
			}
		}
		//if the chain list is not the invoking list or its parent(s), it's a child of the invoking list
		if(!tmp) {
			children.push(lists[i]);
		}
	}
	
	//if there are any child lists to the invoking list, clear them
	if(children.length > 0) {
		for(var a = 0; a < children.length; a++) {
			addParentSelectMessage(children[a]);
		}
	}
}

function addParentSelectMessage(obj) {
	var s = document.getElementById(obj);
	//remove all options from list
	s.options.length = 0;
	//add option indicating a valid parent value is needed
	s.options[0] = new Option('Select previous value ...', '');
	//disable the list
	s.disabled = true;
}

function addOptions(s, cl) {
	//enable child select and clear current child options
	$(s).removeAttr("disabled");
	//repopulate child list with array from helper page
	var list = document.getElementById(s.replace('#', ''));
	list.options.length = 0;
	for(var i = 0; i < cl.length; i++) {
		list.options[i] = new Option(cl[i].text, cl[i].value);
	}
}

function checkValue(obj) {
	//remove current error message
	clearMessage(obj);
	//if there is an invalid choice, place error message, return false
	if($(obj).val() == "") {
		$(obj).css("background-color", "pink");
		$(obj + "msg").html("&laquo; Please make a valid choice");
		return false;
	}
	return true;
}
			
function clearMessage(obj) {
	//clear status message for element
	$(obj + "msg").html('');
	$(obj).css("background-color", "white");
}

function checkForm() {
	var obj;
	var ok = true;
	
	// for every list in the parent-child chain ...
	$(".ms").each(function() {
		obj = "#" + this.id;
		//if it does not have a valid value, return false
		if(!checkValue(obj)) {
			ok = false;
		}
	});
	
	return ok;
}
