/**
*    Chained Selects for jQuery 
*    Copyright (C) 2008 Ziadin Givan www.CodeAssembly.com  
*
*    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/
*
*    
*   settings = { usePost : true, before:function() {}, after: function() {}, default: null, parameters : { parameter1 : 'value1', parameter2 : 'value2'} }	
*   if usePost is true, then the form will use POST to pass the parameters to the target, otherwise will use GET
*   "before" function is called before the ajax request and "after" function is called after the ajax request.
*   If defaultValue is not null then the specified option will be selected.
*   You can specify additional parameters to be sent to the the server in settings.parameters.
*
*/
jQuery.fn.chainSelect = function( target, valo1, valo2, valo3, valo4, url, settings ) 
{
  return this.each( function()
  {
	$(this).change( function( ) 
	{
		settings = jQuery.extend(
		{
			after : null,
			before : null,
			usePost : false,
			defaultValue : null,
		//	parameters : {'_id' : $(this).attr('id'), '_name' : $(this).attr('name')}
	parameters : {'course' : $("#course").val()}
        } , settings);
/*
settings.parameters.course = $("#course").val();
settings.parameters.period = $("#period").val();
settings.parameters.lang = $("#lang").val();
*/
//settings.parameters.course = $(this).closest("form").find("input.period").val();
settings.parameters.course = $(valo1).val();

settings.parameters.period = $(valo2).val();
settings.parameters.lang = $(valo3).val();

		//settings.parameters._value =  $(this).val();

		if (settings.before != null) 
		{
			settings.before( target );
		}

		ajaxCallback = function(data, textStatus) 
		{
			$(target).html("");//clear old options
			data = eval(data);//get json array
			for (i = 0; i < data.length; i++)//iterate over all options
			{
			  for ( key in data[i] )//get key => value
			  {	
					$(target).get(0).add(new Option(data[i][key],[key]), document.all ? i : null);
              }
			}

			if (settings.defaultValue != null)
			{
				$(target).val(settings.defaultValue);//select default value
			} else
			{
				$("option:first", target).attr( "selected", "selected" );//select first option
			}

			if (settings.after != null) 
			{
				settings.after(target);
			}

			$(target).change();//call next chain
		};

		if (settings.usePost == true)
		{
			$.post( url, settings.parameters, ajaxCallback );
		} else
		{
			

if  ($(this).val() != 0) {
$.get( url, settings.parameters, ajaxCallback );
} else {data = [["-------"]]; ajaxCallback(data);}
if (settings.parameters._action == "getlang") {$(valo4).html($(this).val());}
		}
	});
  });
};


/*----------RECUP HORAIRES -----------------------------------*/

jQuery.fn.tofrecup = function( targeto, valo1, valo2, valo3, url, settings ) 
{
  return this.each( function()
  {
	$(this).change( function( ) 
	{
		settings = jQuery.extend(
		{
			after : null,
			before : null,
			usePost : false,
			defaultValue : null,
	parameters : {'course' : $(valo1).val()}
        } , settings);
settings.parameters.period = $(valo2).val();

if  ($(valo1).val() != "intensif") {
settings.parameters.lang = $(valo3).val();
settings.parameters.level =  $(this).val();
settings.parameters.crs_lang =  $("#crs_lang").val();}
else {settings.parameters.lang = 'all';
settings.parameters.level =  '0';
settings.parameters.crs_lang =  $("#crs_lang").val();}

		if (settings.before != null) 
		{
			settings.before( targeto );
		}

		ajaxCallbacko = function(data, textStatus) 
		{
			$(targeto).html("");//clear old options
$(targeto).html(data);
	//		data = eval(data);//get json array



			if (settings.after != null) 
			{
				settings.after(targeto);
			}

			//$(targeto).change();//call next chain
		};


			

if  ($(this).val() != 0) {
$.get( url, settings.parameters, ajaxCallbacko );
} else {data = ""; ajaxCallbacko(data);}

		
	});
  });
};

