jQuery.noConflict();

jQuery(document).ready(function(){
	//disable places dropdown on load
	jQuery("#city").attr("disabled", true);
	
	//hack to loads all the cities from previus sessions
	var province = jQuery('#province_id').val();
	if (!isNaN(province)){
		findCitiesAjaxRequest(province);
	}
	
	//allow only numebrs inputed in  aantal personen
	jQuery("#persons_number").keypress(function (e){
		  //if the letter is not digit then display error and don't type anything
		  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){
			return false;
		  }
	});

	//binds on change event
	jQuery('#province_id').bind('change', function(){
		findCitiesAjaxRequest(this.value);
	});

	//on load count locations
	countLocations();

	//binds on change event for all elements on form to ocunt locations
	jQuery('input,select').bind('change', function(){
		countLocations();
	});
	
	//when enter is pressed post the form
	jQuery('input').bind('keyup', function(e){
		if (e.which == 13){
			jQuery('form').submit();
		}
	});
});

function findCitiesAjaxRequest(id){
//if no value then disable places dropdown
	if (id == ""){
		jQuery("#city").attr("disabled", true);
		return false;
	}

	jQuery.ajax({
		url: "/index/ajax-request-places/?province_id="+id,
		cache: false,
		success: function(html){
			eval("var options="+html);
			//removes all options
			jQuery("#city > option").remove();
			
			//append options returned by php if any, otherwise insert default text and disable dropdown
			if (options == '') {
				jQuery("#city").append('<option value="">kies de plaats</option>');
				
				// disable places dropdown
				jQuery("#city").attr("disabled", true);
			} else {
				jQuery("#city").append('<option value="">kies de plaats</option>');
				jQuery("#city").append(options);
				
				//enables places dropdown
				jQuery("#city").attr("disabled", false);
			}			
		}
	});
}

function countLocations(){
	//URL generation
	var url = "";

	//count checked elements
	if (jQuery("input:checkbox[name='features[]']:checked").size() > 0){
		var url = "features=";

		//selects checked features
		jQuery("input:checkbox[name='features[]']:checked").each(function() {
			url=url+this.value+",";
		});
	}

	//count checked elements
	if (jQuery("input:checkbox[name='atmosphere[]']:checked").size() > 0){
		url=url+"&atmosphere=";
		//selects checked atmospheres
		jQuery("input:checkbox[name='atmosphere[]']:checked").each(function() {
			url=url+this.value+",";
		});
	}

	//selects all imputs that are not empty and not checkboxes
	jQuery(':input', jQuery('form')).each(function() {
		if (this.type=="checkbox" || this.value==""){
			return;
		}else{
			url = url+"&"+this.id+"="+ this.value+",";
		}
	});
	//replaces extra commas, global search!
	url = url.replace(/,&/g,"&");

	//replaces last comma
	url = url.substr(0,url.length-1);

	//ajax call
	jQuery.ajax({
		url: "/index/ajax-request-location-count/?"+url,
		cache: false,
		success: function(html){
			eval("var count="+html);
			jQuery("span.count").html(count);
		}
	});
}

jQuery(document).ready(function(){
	jQuery(".clearForm").bind("click", function(e){
		clearForm(jQuery('form'));
		return false;
    });
});

function clearForm(form) {
	  // iterate over all of the inputs for the form
	  // element that was passed in
	 jQuery(':input', form).each(function() {
	 var type = this.type;
	 var tag = this.tagName.toLowerCase(); // normalize case
	 // it's ok to reset the value attr of text inputs,
	 // password inputs, and textareas
	 if (type == 'text' || type == 'password' || tag == 'textarea')
	   this.value = "";
	 // checkboxes and radios need to have their checked state cleared
	 // but should *not* have their 'value' changed
	 else if (type == 'checkbox' || type == 'radio')
	   this.checked = false;
	 // select elements need to have their 'selectedIndex' property set to -1
	 // (this works for both single and multiple select elements)
	 else if (this.name == "city"){
		jQuery("#city > option").remove();
		jQuery("#city").append('<option value="">kies de plaats</option>');
		this.disabled = true;
	 }
	 else if (tag == 'select')
	   this.selectedIndex = 0;
  });
};
