
function showMessage(error,msg){
	$("#statusmsg .modal-body").html("<p>"+msg+"</p>");
	$("#statusmsg").modal({show:true})
}
$().ready(function(){
	$("#statusmsg").hide();
	
	$("#txtbda,#txteda").datepicker({

		dateFormat: "dd/mm/yy",
      	minDate:new Date(2018,6,23),
      	maxDate:new Date(2018,6,27)
	});
	
	$("#pais").select2({
		data: data.paises,
		placeholder: "Select Country",
		width: "100%",
	});
	
	$("#roomtype").select2({
		data: data.roomtypes,
		placeholder: "Select Rooming Preference",
		width:"100%",
		minimumResultsForSearch: -1,
		dropdownCssClass: "bigdrop",
		
	});
	

	$('#registerform').on('submit',function(e) {
		e.preventDefault();
	});
	$('#registerform').validate({
		ignore: '',
		rules: {
			nombre: {
				required: true,
				minlength: 2
			},
			apellido: {
				required: true,
				minlength: 2
			},
			email:{
				required: true,
				email: true
			},
			invited:{required:true},
			financiacion:"required",
			letterinvited:"required",


		},
		messages: {
			nombre: {
				required: "Please enter your firstname",
				minlength: "Your firstname must be at least 2 characters long"
			},
			apellido: {
				required: "Please enter your lastname",
				minlength: "Your lastname must be at least 2 characters long"
			},
			email: "Please enter a valid e-mail address",
			invited: "Select one option please",
			financiacion: "Select one option please",
			letterinvited: "Select one option please"

				
		},
		submitHandler: function(form){
			var formdata = $(form).serializeArray();
			formdata.find(item => item.name === "pais").value = $("#pais").select2("data")[0].text;
			formdata.find(item => item.name === "roomtype").value = $("#roomtype").select2("data")[0].text;
			var parameters = $.param(formdata);
			console.log(parameters);
			var post = $.post('api/register', parameters);
			post.done(function(data){
				//var res = $.parseJSON(data);				
				console.log(data);

				if(data.success){

					alert("yupiiii, te registraste ok");
				}
				else{
					var errorMsg=""
					$.each(data.errors, function(idx,val){
						
						errorMsg += val+"\n";						
					});
					console.log(errorMsg);
					console.log(data.errors);
					showMessage("true",errorMsg);


				}
				
			});
			//console.log(parameters);

		},		
		errorElement: "em",
		errorPlacement: function ( error, element ) {
			// Add the `help-block` class to the error element			
			error.addClass("help-block");
			var esradio = $(element).prop("type") == "radio";
			var padre = element.parents(".input-container");
			padre.addClass("has-feedback");
			padre.append(error);
			var iconstatus = $("<span class='glyphicon glyphicon-remove form-control-feedback'></span>");
			if(!$.contains(padre,"span.glyhpicon")){
				error.before(iconstatus);				
			}
			if(esradio){
				error.css("display", "inline");
				$(iconstatus).css("display", "inline");
				$(iconstatus).css("position", "initial");
			}			
		},
		success: function(label,element){
			var esradio = $(element).prop("type") == "radio";
			var padre = $(element).parents(".input-container");
			var iconstatus = padre.children("span.glyphicon");			
			if(!iconstatus){
				label.before("<span class='glyphicon glyphicon-ok form-control-feedback'></span>");				
			}
		},
		highlight: function ( element, errorClass, validClass ) {
			var padre = $(element).parents(".input-container");
			var iconstatus = padre.children("span.glyphicon");
			padre.addClass("has-error").removeClass("has-success");
			iconstatus.addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
			padre.addClass( "has-error" ).removeClass( "has-success" );			
		},
		unhighlight: function (element, errorClass, validClass) {
			var padre = $(element).parents(".input-container");			
			var iconstatus = padre.children("span.glyphicon");
			padre.addClass("has-success").removeClass("has-error");									
			iconstatus.addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
			padre.addClass( "has-success" ).removeClass( "has-error" );			
		}    	
    });

    var finan;
	$('input:radio[name="financiacion"]').change(function(){
		finan=true;
		if($(this).is(':checked') && $(this).val() == "si"){
			$("#detallefinan").show();			
			$("#detallefinan textarea").addClass("has-error");			
		}
		else
			$("#detallefinan").hide();
		
		$('#sifinan').tooltip("hide");
	  	$('#sifinan').tooltip("destroy");
	});		
    

	
});
	
