// JavaScript Document

var scroll_up = 0;

function loginBox() {
	$('#loginOverlay').css("display", "block").fadeTo(500, 0.75);
	$('#loginBoxWrapper').css("display", "block").fadeTo(500, 1);
}

function exitLoginBox() {	
	$('#loginOverlay').css("opacity", 0).css("display", "none");	
	$('#loginBoxWrapper').css("opacity", 0).css("display", "none");	
	$('#errorBox').css("opacity", 0).css("display", "none");
}

function loginFormSubmit() {	
	$('#errorBox').css("opacity", 0).css("display", "none");
	
	$.ajaxSetup ({  
		cache: false  
	});
	
	var loginEmail = $('#lemail').val();
	var loginPass = $('#lpasswd').val();
	
	$.post("/direct/authCheck.php", {ajax: 1, lemail: loginEmail, lpasswd: loginPass }, function(responseText){ 
		var auth = parseInt(responseText);
		
		if(auth == 1) {
			$('#lemail2').val(loginEmail);
			$('#lpasswd2').val(loginPass);
			$('#loginForm2').submit();
		} else {
			$('#errorBox').css("display", "block").fadeTo(500, 1);
		}
	});  
}

function signupFormSubmit() {	
	$("#emailWait").css("opacity", 0);
	var error = 0;
	scroll_up = 0;
	
	$('#emailSent').animate({
		opacity: 0,
		height: "0px"
	}, 500);
	
	var fields = new Array('name', 'company', 'telephone', 'email');
	
	for(var i=0; i<fields.length; i++) {
	
		$('#error_'+(i+1)).animate({
			opacity: 0,
			height: "0px"
		}, 250);
	
		if($('#'+fields[i]).val() == "") {
			error = 1;
			scrollUp();
			$('#error_'+(i+1)).animate({
				opacity: 1,
				height: "30px"
			}, 500);
		}
	}
	
	if(error != 1) {
		$("#emailWait").css("opacity", 1);
		$.ajaxSetup ({  
			cache: false  
		});
		
		$.post("/direct/sendEmail.php", $('#signupForm').serialize(), function(responseText){ 
			var emailSent = parseInt(responseText);
			if(emailSent == 1) {
				$('#emailSent').animate({
					opacity: 1,
					height: "42px"
				}, 500);
				
				$("#emailWait").css("opacity", 0);
			}
		}); 		
	}
	
}

function scrollUp() {
	if(scroll_up == 0) {
		$.scrollTo('200px', 500);
		scroll_up = 1;
	}
}
