var MsgNombre = "<Escriba su nombre>";
var MsgEmpresa = "<Su empresa>";
var MsgMail = "<Su e-mail>";
var MsgCiudad = "<Su Ciudad>";
var MsgTel = "<Su nro. telefónico>";
var MsgTexto = "<Cuéntenos su necesidad>";

var NomObjNombre = "#TxtNombrePersona";
var NomObjEmpresa = "#TxtEmpresa";
var NomObjMail = "#TxtMailPersona";
var NomObjCiudad = "#TxtCiudad";
var NomObjTel = "#TxtTelefonoPersona";
var NomObjMsg = "#TxtMensaje";
var NomObjOrigen = "#TxtOrigen";

//Establecemos los autocomplete=off. Se hace con Js porque en HTML no es valido para la W3c
/*document.getElementById(NomObjNombre).autocomplete = 'off';
document.getElementById(NomObjEmpresa).autocomplete = 'off';
document.getElementById(NomObjMail).autocomplete = 'off';
document.getElementById(NomObjCiudad).autocomplete = 'off';
document.getElementById(NomObjTel).autocomplete = 'off';*/

//Establecemos los value por defecto de cada input
if($(NomObjNombre).val() == '' || $(NomObjNombre).val() == MsgNombre ) {
	$(NomObjNombre).val(MsgNombre);
	$(NomObjNombre).attr("title",MsgNombre);
	$(NomObjNombre).addClass("marcaagua");
}	
if($(NomObjEmpresa).val() == '' || $(NomObjEmpresa).val() == MsgEmpresa) {
	$(NomObjEmpresa).val(MsgEmpresa);
	$(NomObjEmpresa).attr("title",MsgEmpresa);
	$(NomObjEmpresa).addClass("marcaagua");
}				
if($(NomObjMail).val() == '' || $(NomObjMail).val() == MsgMail) {
	$(NomObjMail).val(MsgMail);
	$(NomObjMail).attr("title",MsgMail);
	$(NomObjMail).addClass("marcaagua");
}
if($(NomObjCiudad).val() == '' || $(NomObjCiudad).val() == MsgCiudad) {
	$(NomObjCiudad).val(MsgCiudad);
	$(NomObjCiudad).attr("title",MsgCiudad);
	$(NomObjCiudad).addClass("marcaagua");
}
if($(NomObjTel).val() == '' || $(NomObjTel).val() == MsgTel) {
	$(NomObjTel).val(MsgTel);
	$(NomObjTel).attr("title",MsgTel);
	$(NomObjTel).addClass("marcaagua");
}
if($(NomObjMsg).val() == '' || $(NomObjMsg).val() == MsgTexto) {
	$(NomObjMsg).val(MsgTexto);
	$(NomObjMsg).attr("title",MsgTexto);
	$(NomObjMsg).addClass("marcaagua");
}

//Programamos los eventos al focus y al focusout
$(NomObjNombre).focus(function () {
	 if($(NomObjNombre).val() == MsgNombre) {
		$(NomObjNombre).val('') ;
		$(NomObjNombre).removeClass("marcaagua");
	 }
});
$(NomObjNombre).focusout(function () {
	 if($(NomObjNombre).val() == '') {
		$(NomObjNombre).val(MsgNombre) ;
		$(NomObjNombre).addClass("marcaagua");
	 }
});

$(NomObjEmpresa).focus(function () {
	 if($(NomObjEmpresa).val() == MsgEmpresa) {
		$(NomObjEmpresa).val('') ;
		$(NomObjEmpresa).removeClass("marcaagua");
	 }
});
$(NomObjEmpresa).focusout(function () {
	 if($(NomObjEmpresa).val() == '') {
		$(NomObjEmpresa).val(MsgEmpresa) ;
		$(NomObjEmpresa).addClass("marcaagua");
	 }
});					

$(NomObjMail).focus(function () {
	 if($(NomObjMail).val() == MsgMail) {
		$(NomObjMail).val('') ;
		$(NomObjMail).removeClass("marcaagua");
	 }
});
$(NomObjMail).focusout(function () {
	 if($(NomObjMail).val() == '') {
		$(NomObjMail).val(MsgMail) ;
		$(NomObjMail).addClass("marcaagua");
	 }
});				

$(NomObjCiudad).focus(function () {
	 if($(NomObjCiudad).val() == MsgCiudad) {
		$(NomObjCiudad).val('') ;
		$(NomObjCiudad).removeClass("marcaagua");
	 }
});
$(NomObjCiudad).focusout(function () {
	 if($(NomObjCiudad).val() == '') {
		$(NomObjCiudad).val(MsgCiudad) ;
		$(NomObjCiudad).addClass("marcaagua");
	 }
});					

$(NomObjTel).focus(function () {
	 if($(NomObjTel).val() == MsgTel) {
		$(NomObjTel).val('') ;
		$(NomObjTel).removeClass("marcaagua");
	 }
});
$(NomObjTel).focusout(function () {
	 if($(NomObjTel).val() == '') {
		$(NomObjTel).val(MsgTel) ;
		$(NomObjTel).addClass("marcaagua");
	 }
});					
$(NomObjMsg).focus(function () {
	 if($(NomObjMsg).val() == MsgTexto) {
		$(NomObjMsg).val('') ;
		$(NomObjMsg).removeClass("marcaagua");
	 }
});
$(NomObjMsg).focusout(function () {
	 if($(NomObjMsg).val() == '') {
		$(NomObjMsg).val(MsgTexto) ;
		$(NomObjMsg).addClass("marcaagua");
	 }
});

//Esta funcion valida que todos los campos del formulario estén llenos y correctos
function validarCampos() {
	var MsgErr = "";
	if(IsEmpty(NomObjNombre,MsgNombre)) {
		MsgErr = MsgErr + "Su Nombre es requerido. \n";
	}
	if(IsEmpty(NomObjEmpresa,MsgEmpresa)) {
		MsgErr = MsgErr + "El nombre de su Empresa es requerido. \n";
	}
	if(IsEmpty(NomObjMail,MsgMail)) {
		MsgErr = MsgErr + "Su E-Mail es requerido. \n";
	}
	else {
		if(!IsEmail($(NomObjMail).val())) { 
			MsgErr = MsgErr + "Su E-Mail es incorrecto. \n";
		}
	}
	if(IsEmpty(NomObjCiudad,MsgCiudad)) {
		MsgErr = MsgErr + "Su Ciudad es requerida. \n";
	}
	if(IsEmpty(NomObjTel,MsgTel)) {
		MsgErr = MsgErr + "Su nro. telefónico es requerido. \n";
	}	
	if(IsEmpty(NomObjMsg,MsgTexto )) {
		MsgErr = MsgErr + "Por favor, cuéntenos un poco su necesidad. \n";
	}	
	
	if(MsgErr.length == 0) {
		return true; 
	} 
	else {
		alert(MsgErr);
		return false;
	}
}

function IsEmpty(pCampo, pMsgDefaul) { 
	if($(pCampo).val() == pMsgDefaul || $(pCampo).val().length == 0 ) {
		return true;
	}
	else {
		return false;
	}
}

function getVal(pCampo) {
	return $(pCampo).val();
}

function IsEmail(pValor){
    re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
    if(!re.exec(pValor))    {
        return false;
    }else{
        return true;
    }
}

$(document).ready(function(){
	$("#cmdEnviar").click(function(){
		if(validarCampos()) {
			$(this).hide();
			$("#DivProgress").show();
			var UrlPeticion = $("#varAppSendMail").val();
			var UrlToRedirec = $("#varRedirectTo").val();
			
			$.get(UrlPeticion, 
				{TxtNombrePersona: getVal(NomObjNombre), TxtEmpresa: getVal(NomObjEmpresa), TxtMailPersona: getVal(NomObjMail), TxtCiudad: getVal(NomObjCiudad), TxtTelefonoPersona: getVal(NomObjTel), TxtOrigen: getVal(NomObjOrigen), TxtMensaje: getVal(NomObjMsg) }, 
				function(data){
					$("#DivProgress").hide();
					window.location = UrlToRedirec;
				}
			);
		}
	});
});

