// LM HOME.JS

Error = function(){
	this.errors = [];
	this.errorsDef = {
		nombre_cliente:"Nombre Cliente / Razón Social",
		apellido:"Apellido",
		nif:"NIF/CIF",
		email:"Email para envo factura",
		nombre_viajero:"Apellido pasajero",
		orden:"Localizador Vuelo",
		emailFormat:"Formato de email incorrecto",
		ordenFormat:"Localizador debe ser un código alfanúmerico de 6 dígitos"
	};
	this.init = function(){
		this.errors = [];
		$("#errorContainer").hide();
		$("#errorContainer").css("padding","0");
		$("#errorContent").text("");
	};
	this.addError = function(id){
		var msg = this.errorsDef[id];
		this.errors[this.errors.length] = msg;
	};
	this.display = function(){
		var htmlErrors = "<ul>";
		$.each(this.errors, function(){
			htmlErrors+="<li>"+this+"</li>";
		});
		htmlErrors+="</ul>";
		
		
		$("#errorContainer").show();
		$("#errorContent").html(htmlErrors);
	};
	this.hasError = function(){
		return (this.errors.length>0);
	};
}

var o_err = new Error();

function send(){
	var ok = validateForm();
	if (ok){
		for(var i=0;i<document.B_Data.elements.length;i++){
			var el = document.B_Data.elements[i];
			if($(el).attr("value")){
				var _value = $(el).attr("value");
				_value = _value.replace(/,/g, "");
				$(el).attr("value",_value);
			}
		}
		
		//Submit the form
		document.B_Data.submit();
	};
}

function validateForm(){
	o_err.init();
	
	$("input.mandatory").each(function(){
		$(this).removeClass("error");
	});
	
	//Check mandatory inputs
	$("input.mandatory").each(function(){
		if(!$(this).attr("value")){
			$(this).addClass("error");
			o_err.addError($(this).attr("name"));
		}
	});
	
	//Check e-mail format
	regex=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
	var email = $(document.B_Data.email).attr("value");
	if (!regex.test(email) && email){
		$(document.B_Data.email).addClass("error");
		o_err.addError("emailFormat");
	}
	
	//Check PNR
	/*var orden = $(document.B_Data.orden).attr("value");
	if (trim(orden).length!=6 && orden){
		$(document.B_Data.orden).addClass("error");
		o_err.addError("ordenFormat");
	}*/
	
	if(o_err.hasError()){
		o_err.display();
		return false;
	}
	
	return true;
	
};

function trim(str) {
  var regExpBeginning = /^\s+/;
  var regExpEnd       = /\s+$/;
  
  if(str){
    return str.replace(regExpBeginning,"").replace(regExpEnd,"");
  }
  return "";
}

$(document).ready(function(){
    //Show/Hide Apellido help
    $("#nombre_viajero").focus(function(){
        $("#ayuda_apellido").css("display","block");
    });
    $("#nombre_viajero").blur(function(){
        $("#ayuda_apellido").css("display","none");
    });
    
    //Show/Hide PNR help
    $("#orden").focus(function(){
        $("#ayuda_pnr").css("display","block");
    });
    $("#orden").blur(function(){
        $("#ayuda_pnr").css("display","none");
    });
    
    $("#nombre_viajero").focus();
});

/*
$(document).ready(function(){
	$("input.mandatory").each(function(){
		$(this).blur(function(){
			if($(this).hasClass("error")){
				validateForm();
			}
		});
	});
});*/