registration=0;
window.addEvent('load',function (){//domready
	alto =  window.getHeight();		
	if($("container")){
		if($("container").offsetHeight<alto){
			margen = (alto-$("container").offsetHeight)/2;
			$("container").style.marginTop = margen+"px";
		}
	}
	
	$$('img.iconImage').each(function(item, index){
		item.addEvent('click', function(){
			if(lang == "en"){
				if(confirm("Delete selected period? ")){
					window.location.href = "actions/deletePeriod/" + this.id.substring(1);
				}
			}else{
				if(confirm("Eliminar periodo seleccionado? ")){
					window.location.href = "actions/deletePeriod/" + this.id.substring(1);
				}
			}
		});
	});
	if($('formuDate')){
		$('formuDate').addEvent('submit', function(e) {
				new Event(e).stop();
				validateDate();
		});
	}
});
window.addEvent("resize", function(){
	alto =  window.getHeight();
	if($("container")){
		if($("container").offsetHeight<alto){
			margen = (alto-$("container").offsetHeight)/2;
			$("container").style.marginTop = margen+"px";
		}
	}						 
});

function changeContactStatus(uname, upass){
	var endpoint = "../actions/logIn";
	var jsonRequest = new Request.JSON({url: endpoint, onComplete: function(result){
		if(result.status == "home"){
			window.location.href="../actions/home";	
		}else{
			$("msgError").innerHTML="Invalid User or Password";
		}
	}}).post({'username': uname, 'password': upass});
}

function changeContactStatus_es(uname, upass){
	var endpoint = "../../actions/logIn";
	var jsonRequest = new Request.JSON({url: endpoint, onComplete: function(result){
		if(result.status == "home"){
			window.location.href="../../actions/home_es";	
		}else{
			$("msgError").innerHTML="Usuario o Clave inv&aacute;lidos";
		}
	}}).post({'username': uname, 'password': upass});
}
function validate_profile(password, email, country, city, uid){
	var error=0;
	if($("password").value.trim() == "" || $("email").value.trim() == "" || $("country").value.trim() == "" || $("city").value.trim() == ""){
		$("msgError").innerHTML="All fields are mandatory";
		error=1;
	}
	if(error==0){
		if ($('email').value.indexOf('@')==-1 || $('email').value.indexOf('.')==-1 ) {
			$("msgError").innerHTML="Invalid Email"; 
			error=1;
		}
	}
	if(error==0){
		registration=1;
		
		var endpoint = "actions/do_save_profile";
		var jsonRequest = new Request.JSON({url: endpoint, onComplete: function(result){
			if(result.status == "1"){
				//window.location.href="thanks.html";	
				window.location.href="actions/home";	
			}else{
				$("msgError").innerHTML="No se pudo guardar los datos.";
			}
		}}).post({'email': email, 'name': name, 'password': password, 'country': country, 'city': city, "uid": uid});
			
	}
}
function validate_profile_es(password, email, country, city, uid){
	var error=0;
	if($("password").value.trim() == "" || $("email").value.trim() == "" || $("country").value.trim() == "" || $("city").value.trim() == ""){
		$("msgError").innerHTML="Todos los campos son obligatorios";
		error=1;
	}
	if(error==0){
		if ($('email').value.indexOf('@')==-1 || $('email').value.indexOf('.')==-1 ) {
			$("msgError").innerHTML="Mail inv&aacute;lido"; 
			error=1;
		}
	}
	if(error==0){
		registration=1;
		
		var endpoint = "actions/do_save_profile";
		var jsonRequest = new Request.JSON({url: endpoint, onComplete: function(result){
			if(result.status == "1"){
				//window.location.href="thanks.html";	
				window.location.href="actions/home_es";	
			}else{
				$("msgError").innerHTML="No se pudo guardar los datos.";
			}
		}}).post({'email': email, 'name': name, 'password': password, 'country': country, 'city': city, "uid": uid});
			
	}
}
function validate_registration(uname, password, email, country, city, date){
	/*
		All fields are mandatory
    	Invalid Email
    	This User Name is not available
	*/
	var error=0;
	if($("username").value.trim()=="" || $("password").value.trim()=="" || $("email").value.trim()=="" || $("country").value.trim()=="" || $("city").value.trim()==""){
		$("msgError").innerHTML="All fields are mandatory";
		error=1;
	}
	if(error==0){
		if ($('email').value.indexOf('@')==-1 || $('email').value.indexOf('.')==-1 ) {
			$("msgError").innerHTML="Invalid Email"; 
			error=1;
		}
	}
	if(error==0){
		registration=1;
			if(validateDate()==1){
		var endpoint = "../actions/checkKeyJS";
		var jsonRequest = new Request.JSON({url: endpoint, onComplete: function(result){
			if(result.status == "ok"){
				window.location.href="thanks.html";	
			}else{
				if(result.status == "user"){
					$("msgError").innerHTML="This User Name is not available";
				}else{
					if(result.status == "ErrorSendedMail"){
						$("msgError").innerHTML="Error sending emails..";
					}else{
						$("msgError").innerHTML="That Email is already in use.";
					}
				}
			}
		}}).post({'username': uname, 'email': email, 'name': name, 'password': password, 'country': country, 'city': city, 'date': date, "lang": lang});
			}
	}
}
function validate_registration_es(uname, password, email, country, city, date){
	/*
		All fields are mandatory
    	Invalid Email
    	This User Name is not available
	*/
	var error=0;
	if($("username").value.trim()=="" || $("password").value.trim()=="" || $("email").value.trim()=="" || $("country").value.trim()=="" || $("city").value.trim()==""){
		$("msgError").innerHTML="Todos los campos son obligatorios";
		error=1;
	}
	if(error==0){
		if ($('email').value.indexOf('@')==-1 || $('email').value.indexOf('.')==-1 ) {
			$("msgError").innerHTML="Mail inv&aacute;lido"; 
			error=1;
		}
	}
	if(error==0){
		registration=1;
		if(validateDate()==1){
		
				var endpoint = "../../actions/checkKeyJS";
				var jsonRequest = new Request.JSON({url: endpoint, onComplete: function(result){
					if(result.status == "ok"){
						window.location.href="thanks.html";	
					}else{
						if(result.status == "user"){
							$("msgError").innerHTML="Nombre de usuario no disponilbe";
						}else{
						if(result.status == "ErrorSendedMail"){
							$("msgError").innerHTML="Error al enviar el Mail..";
						}else{
							$("msgError").innerHTML="E-mail ya existe en nuestra base de datos";
						}
					}
				}
			}}).post({'username': uname, 'email': email, 'name': name, 'password': password, 'country': country, 'city': city, 'date': date, "lang": lang});
		}
	}
}
function validateForgot(username){
	var endpoint = "../actions/forgot";
	if (username.indexOf('@')==-1 || username.indexOf('.')==-1 ) {
		$("msgError").innerHTML="Inavlid Mail"; 
		error=1;
	}else{
		var jsonRequest = new Request.JSON({url: endpoint, onComplete: function(result){
			if(result.status == "ok"){
				window.location.href="forgot_pass_ok.html";	
			}else{
				$("msgError").innerHTML="Inavlid Mail";
			}
		}}).post({'email': username, "lang": lang});
	}
}

function validateForgot_es(username){
	var endpoint = "../../actions/forgot";
	if (username.indexOf('@')==-1 || username.indexOf('.')==-1 ) {
		$("msgError").innerHTML="Mail inv&aacute;lido"; 
		error=1;
	}else{
		var jsonRequest = new Request.JSON({url: endpoint, onComplete: function(result){
			if(result.status == "ok"){
				window.location.href="forgot_pass_ok.html";	
			}else{
				$("msgError").innerHTML="Mail inv&aacute;lido";
			}
		}}).post({'email': username, "lang": lang});
	}
}
function saveMail(mail, id){
	var endpoint = "actions/saveMail";
	var jsonRequest = new Request.JSON({url: endpoint, onComplete: function(result){
 
	}}).post({'mail': mail, "id": id});
	
}
function validateDate(dia){
	if($("date").value.trim()==""){
		if(lang == "en"){
				alert("Select a day.");
				return 0;
		}else{
				alert("Seleccione un dia.");
				return 0;
		}
	}else{
		if(registration==0){
			$("formuDate").submit();
		}else{
			return 1;
		}
	}
		
}