$(document).ready(function() {

	//fix forms without placeholders()

	/*$(".form:not(.form-placeholder)").each(function(e){
		$(this).find("input, textarea, select").removeAttr("placeholder");
	});*/


	$(".form-label-option").each(function(e){
		var max = 0;
		$(this).find(".form-line-group-label").each(function(e){ if ($(this).outerWidth() > max) max = $(this).outerWidth(); });
		var moreMax = 10;
		var finalMax = max + moreMax;
		$(this).find(".form-line-group-label").width(finalMax);
	});


	$(".form:not(.form-label-option) .form-line-group-label").remove();



	$("select[data-click=true]").change(function(e){
		var link = $(this).find("option:selected").attr("data-link");

		$(location).attr('href',link);
	});
	
	$(document).on(
		"click" , 
		".form-item-captcha-img",
		function(e) {
			e.preventDefault();
			$(this).closest(".form-line").find("input[name=image_code]").val("");
			$(this).find("img").attr("src" , $(this).attr("href") + "?" + $.now() );		
		}
	);


	$("select[data-selected]").each(function(){
		$(this).val($(this).attr("data-selected"));
	});


	$("input[data-calendar]").datetimepicker({
		 format: "DD MMM YYYY"  
	});

	$("input[data-calendar-time]").datetimepicker({
		format: "DD MMM YYYY H:mm A" 

	});

	$("input[data-time]").datetimepicker({
		 format: "H:mm A" 
	});

	$("input[data-calendar]").change(function(e){ $(this).blur();} );



	$('select:not(.nochosen)').chosen();

	$('.form .form-item').placeholder();

	//all forms with class ajax will return the response in responsebox element
	var ajaxForm = false;
	$(document).on(
		"submit" , 
		"form.ajax",
		
		function(e){
			e.preventDefault();

			var form = $(this);

			//add suppport for oneclick
			if (form.hasClass("oneclick")){
			}

			if(ajaxForm!= false)
			  ajaxForm.abort();

			if (typeof form.attr("data-sending") != "undefined"){
				form.find(".responsebox").html('<div class="alert alert-warning"><p>' + form.attr('data-sending') + '</p></div>');
			}

			ajaxForm = $.ajax({

				url:form.attr("action"),
				type:form.attr("method"),

				data:form.serialize(),
			}).done(function(msg){
				
				ajaxObject = false;

				//redirect message
				if (msg.indexOf("redirect:") != -1){
					$(location).attr('href',msg.replace("redirect:" , "" ));
				} else {
					
					//capcha reload
					if (msg.indexOf("captcha:") != -1){

						if ($(".g-recaptcha").length){
							grecaptcha.reset();
							//Recaptcha.reload();
						} else {
							form.find(".form-item-captcha-img").trigger("click");
						}

						form.find(".responsebox").html(msg.replace("captcha:" , ""));
					} else {

						//execute function
						if (msg.indexOf("execute:") != -1 ) {
							eval( msg.replace("redirect:" , "" ) );
						} else if (msg.indexOf("reset:") != -1 ) {							
							//reset this form

							form.find('input:text, input:password, input:file, select, textarea').val('');
							form.find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
							$(".form-capcha-img").trigger("click");
							//form[0].reset();

							//show the message
							form.find(".responsebox").html(msg.replace("reset:" , "" ));
						} else {
							//full message
							form.find(".responsebox").html(msg);
						}
					}
				}

			});				
		}
	);


});
