$(function() {
	//alert('Hola');
	//$("#signup").validate();	//	activates validate plugin for signup form - need another line for profile page
	
	// validate signup form on keyup and submit
	//$("#signup").validate();

	
	
	
	
	var $sub_types = $('#sub_types');
	
	
	
	var $sub_languages = $('id^=#sub_languages');
	//var $sub_language = $('id^=#lang');
	

	
	var $sub_languages_ind = $('#sub_languages_ind');
	
	//var $sub_languages_others = $('#sub_languages_others');
	
	var $sub_languages_sch = $('#sub_languages_sch');
	var $sub_languages_uni = $('#sub_languages_uni');
	
	
	var $sub_lengths = $('#sub_lengths');
	var $sub_students = $('#sub_students');
	var $uni_students = $('#uni_students');
	var $total_name = $('#total_name');
	var $id_show = $('#id_show');
	var $sub_sch_fr = $('#sub_sch_fr');
	var $sub_sch_ge = $('#sub_sch_ge');
	var $sub_sch_sp = $('#sub_sch_sp');
	var $sub_uni_fr = $('#sub_uni_fr');
	var $sub_uni_ge = $('#sub_uni_ge');
	var $sub_uni_sp = $('#sub_uni_sp');
	
	
	var $payment_system = $('#payment_system');
	
	var $da_price_info = $('#da_price_info');
	
	var $da_price = $('#da_price');
	var $picks = $('#picks');
	
	//var $choose_auth_sub_type_choice = $($choose_auth_sub_type_choice).selected;
	
	var $text_check = $("#text_check");
	//var $res_message = $('#res_message');
	
	//$sub_languages.hide();
	//$sub_languages.hide();
	//$($sub_languages_ind).hide();
	
	$($da_price_info).hide();
	
	//$sub_languages_others.hide();
	
	//$($sub_languages).hide();
	$($sub_languages_sch).hide();
	$($sub_sch_fr).hide();
	$($sub_sch_ge).hide();
	$($sub_sch_sp).hide();
	
	$($sub_languages_uni).hide();
	$($sub_uni_fr).hide();
	$($sub_uni_ge).hide();
	$($sub_uni_sp).hide();
	
	//$($payment_system).hide();
	
	
	
	$sub_languages.addClass('ui-state-disabled');	//	"turn off" language choice at start
	$('#sub_languages_ind input').attr("disabled", true);
	$sub_lengths.addClass('ui-state-disabled');		//	"turn off" length choice at start
	$('#sub_lengths input').attr("disabled", true);
	$sub_students.addClass('ui-state-disabled');		//	"turn off" length choice at start
	$('#sub_students input').attr("disabled", true);
	$($sub_students).hide();				//	hide extra student options by default
	$($uni_students).hide();
	$($total_name).hide();
	//$($id_show).hide();
	
	
	//	these functions highlight the row currently in use by the user - ROCKS!
	$("tr td :radio").bind("click",function() {
		$row = $(this).closest('tr');
		$($row).addClass("curFocus");
		$($row).siblings().removeClass("curFocus");
	});
	
	$("tr td :checkbox").bind("click",function() {
		$row = $(this).closest('tr');
		$($row).addClass("curFocus");
		$($row).siblings().removeClass("curFocus");
	});
	
	$("input").focus(function() {
		$row = $(this).closest('tr');
		$($row).addClass("curFocus");
		$($row).siblings().removeClass("curFocus");
	});
	
	$("select").mousedown(function() {
		$row = $(this).closest('tr');
		$($row).addClass("curFocus");
		$($row).siblings().removeClass("curFocus");
	});

	
	$('#sub_types th').click(function(){
		//alert('Moi');
		//$sub_types.removeClass('ui-state-disabled');
		//$('input[name=sub_type]').attr("disabled", false); 
		});
	
	
	//	initialise form variables
 	var $sub_type = '';
 	var $lang_choices = '';
 	var $sub_length = '';
 	var $tri_monthly = '';
 	var $sub_student = '';
 	
 	// for each select field on the page
	
	function reset_select($sub_type){
	 	$("[id^=sub_sch],[id^=sub_uni]").each( function(){	//	resets dropdowns so that no hidden values are passed at the end.
	  	// set its value to its first option
	  		var $id = $(this).attr('id');
	  		
	  		$(this).val( $("#" + $id + " option:first").val() );
	  		$($picks).html('');
	  		$($da_price).html('');
	  		
	  		var $error_div = "." + $id + "formError";
	  		//alert($error_div);
	  		$($error_div).remove();
	  		if ($("#" + $id +":visible").length != 0) {		// seems the opposite, but wherever it is occurring in the sequence, it finds what we need
	  			//$($error_div).remove();		//	get rid of validator div
	  			//alert($id);
	  			//$("#" + $id).addClass("validate[required]");
	  			}
	  		
		});
	};	

 	function add_class(){
 		//alert($sub_type);
 		$("[id^=sub_sch]:visible").each( function(){	//	resets dropdowns so that no hidden values are passed at the end.
	  	//alert($(this).attr('id'));
	  	$(this).addClass("validate[required]");      		
		});
	};


	function toggle_required($str)	{	//	
	  		
	  		var $id = $($str).attr('id');
	  		//alert($id);
	  		$($str).val( $("#" + $id + " option:first").val() );
	  		$($picks).html('');		//	empty selected choices area
	  		var $error_div = "." + $id + "formError";	//	div created by validator
	  		if ($("#" + $id +":visible").length != 0) {		// seems the opposite, but wherever it is occurring in the sequence, it finds what we need
	  			$($error_div).remove();		//	get rid of validator div
	  			$("#" + $id).removeClass("validate[required]");
	  			$("#" + $id).attr({	name: 'off' 	});		//	deactivate product
      			}
	  		else	{
	  			$("#" + $id).addClass("validate[required]");
	  			$("#" + $id).attr({	name: 'product_id[]' 	});		//	make this "live" by adding form name "product_id[]"
      		
	  			}
	  		//alert($("#" + $id).attr('name')); 	
	  		//$("#" + $id).addClass("validate[required]");		//	make this "live" by adding form name "product_id[]"
      	};


 	function reset_single_select($sub_type, $wich){
 		//alert($sub_type);
 		$("[id^=sub_"+$sub_type+"_"+$wich+"]").each( function(){	//	resets dropdowns so that no hidden values are passed at the end.
	  	toggle_required(this);      		
		});
	};
	

	function example_ajax_request(url) {
		alert ("pants!");
	    $('#example-placeholder').load(url, "",
	        function(responseText, textStatus, XMLHttpRequest) {
	            if(textStatus == 'error') {
	                $('#example-placeholder').html('<p>There was an error making the AJAX request</p>');
	            }
	        }
	    );
	   
	}

	//example_ajax_request('http://www.authentikinteractive.com/test-text.html');
	

 	function get_selected($kind){	// returns IDs of products selected
	 	var $langs = new Array();
	 	var $str = "";
	 	$i = 0;
	 	if ($kind == 'ind'){
	 		$('input[class^=lang]:checked').each( function()	{	//	
		  		$str = $(this).val();
		  		if ($str)	{
			  		$langs[$i] = $str;
			  		$i++;
			  	}
			});
	 	}
	 	else	{
	 		$("[id^=sub_"+$kind+"]").each( function()	{	//	
		  		$str = $(this).val();
			  	if ($str)	{
			  		$langs[$i] = $str;
			  		$i++;
			  	}
			});
		}
		return $langs;
	};	


	
	$("[id^=sub_sch],[id^=sub_uni]").change(function() { //	This detects when dropdown is changed - THIS LITTLE SNIPPET IS WHERE THE ACTION IS !!!
		get_total_name();
		});

	
 	function get_sub(){
      	//$('input[class^=lang]').attr({	name: 'nowt'	});		//		set name back to off before change
      	//$("[id^=sub_sch]").attr({	name: 'off'	});		//	set name back to off before change
      	//$("[id^=sub_uni]").attr({	name: 'off'	});		//		set name back to off before change
      	$sub_type = get_sub_type();
      	
      	if ($sub_type == 'ind')	{
      		$($sub_languages_sch).hide();
      		$($sub_languages_uni).hide();
      		$('input[class^=lang]').attr({	name: 'product_id[]' 	});		//	make this "live" by adding form name "product_id[]"
      		}
      	else if ($sub_type == 'sch')	{
      		$($sub_languages_sch).show();
      		$($sub_languages_uni).hide();
      		$('input[class^=lang]').attr({	name: 'nowt' 	});		//	make this "live" by adding form name "product_id[]"
      		//$("[id^=sub_sch]").attr({	name: 'product_id[]' 	});		//	make this "live" by adding form name "product_id[]"
      		}
      	else	{
      		$($sub_languages_sch).hide();
      		$($sub_languages_uni).show();
      		$('input[class^=lang]').attr({	name: 'nowt' 	});		//	make this "live" by adding form name "product_id[]"
      		//$("[id^=sub_uni]").attr({	name: 'product_id[]' 	});		//	make this "live" by adding form name "product_id[]"
      		}
      	return $sub_type;
     };
 	
 	
 	function get_langs($sub_type){
      	var $lang_choices = new Array();	//	reset choices for each count
      	$i=0;
      	$num_checked = $(':checkbox[class^=lang]:checked').size();	//	count checked for comma insertion
      	$(':checkbox[class^=lang]').each(function(){
		    $lang = '#sub_'+ $sub_type +'_' + $(this).attr('id');
		        if (this.checked == true) {
		        	$($lang).show();
		        	$lang_choices[$i] = $lang;
		        	$i++;
			       	}
			     else	{
			     	$($lang).hide();
			     	}
	     	 });
	    return $lang_choices;
     };

	
	function get_sub_type()	{
		$sub_type = $('input[name=sub_type]:checked').val();
      	return $sub_type;
	};



 	function get_length(){
      	$sub_length = $('input[name=sub_length]:checked').val();
      	return $sub_length;
      	
     };

 	function get_tri_monthly($price){
      	if (($sub_length == 'monthly') && $price)	{
	      	$tri_monthly = '(&euro;' + (3 * $price) + ' for first 3 months, &euro;' + $price + ' monthly after that. )';
	      	}
      	else	{
      		$tri_monthly = '';
      		}
      	return $tri_monthly;
      	
     };
 
 	function get_students(){
      	$sub_student = $('input[name=sub_student]:checked').val();
      	return $sub_student;
      	//$('#text_check').html($sub_type);
     };
 

	
 	function get_total_name()	{
      	
      	$subtype = get_sub();
      	$langs = get_langs($sub_type);
      	
      	if ($langs.length > 0)	{
      	      	
	      	$selected = get_selected($subtype);
	      	$how_many = $selected.length;
	      	
	      	var $total = "";
	      	var $details = "";
	      	var $i = 0;
	      	if ($selected.length > 0)	{
		      	$all_price = 0;
		      	$.each($selected, function(){
				      
				      $('#da_price_info').load('http://www.authentikinteractive.com/CI/index.php/utilities/amember_product_details/' + this ,  function() {
						$details = $(this).html();
						$total = $total + $details;
				       	$($picks).html($total);
				        $preis = parseInt($('.pick_price', this).text());		//	the 'this' parameter limits the selector to just this instance
				        $all_price = $all_price + $preis;
				        
				        $('#da_price').html($all_price);
				        });
				});
			}
		}
      	else	{
      		$total = '';
      		$($picks).text($total);
      		$total_name.show();
	 	}
	};
 	


	$('#sub_types input:radio').click(function(event)	{				//	actions when we click type radio button
		$sub_type = get_sub_type();
		$(':checkbox[class^=lang]').removeAttr("checked");	
		reset_select($sub_type);		//		resets the select dropdowns for plan options
      		
		if ( $('#sub_languages_ind input').attr('disabled') == true)	{	//	check if lang input is active yet
			$('#sub_languages_ind input').attr("disabled", false);			//	activate it
			$sub_languages.removeClass('ui-state-disabled');			//	un-disable class
			}
			
		get_total_name();
		
	});
	

 	$(':checkbox[class^=lang]').click(function(event)	{					//	actions when we click language checkboxes
		$sub_type = get_sub_type();
      	$wich = $(this).attr('id');
		
		if ($sub_type != "ind")	{	//	if lang checkbox is active for 
			reset_single_select($sub_type, $wich);		//		resets the select dropdowns for plan options
      	}
      	
		if ( $('#sub_lengths input').attr('disabled') == true)	{	//	check if length input is active yet
			$('#sub_lengths input').attr("disabled", false);			//	activate it
			}
		
		get_total_name();
	});


/*
	$('#sub_lengths input:radio').click(function(event)	{				//	actions when we click length radio button
		if ( $('#sub_students input').attr('disabled') == true)	{	//	check if lang input is active yet
			$('#sub_students input').attr("disabled", false);			//	activate it
			$sub_students.removeClass('ui-state-disabled');			//	un-disable class
			}
		$('#price_line').addClass('ui-widget-header dark');
		$($total_name).show();
		
		get_total_name();
	});
*/



	
});

