if (!PEOPLE) { var PEOPLE = new Object(); }

PEOPLE.login = {
	
	checkLogin	: function(form) {
		
		if (!formchecker.checkForm(form)) {
			return false;
		}
		
		$('login').style.background = "#059ED8";
		$('password').style.background = "#059ED8";	
		
		var mail = $('login').value;
		if(mail == "") mail = $('login2').value;
		
		var password = $('password').value;
		if(password == "") password = $('password2').value;
		
		var postBody	= "action=AJAX_checkLogin&u_email="+mail+"&password="+password;
		new Ajax.Request(
			'index.php?p=/login',
			{
				method		: "post",
				postBody	: postBody,
				onSuccess	: function(t) {
					PEOPLE.login.resultLogin(t.responseText);
				},
				onFailure	: function(t) {
					alert("Critical Error, please contact administration with this info:\n" + t.status + " -- " + t.statusText);
				}
			}
		);			
	},
	
	resultLogin	: function(responseText) {
		
		var json	= responseText.evalJSON();

		if(json.status.code == "success") {
			
			if($('return_url'))
				window.location = $('return_url').value;
			else
				window.location = "index.php";
				
		} else {
			
			if($('mandatory')) {
				$('mandatory').style.display = "block";
				$('mandatory').update(json.status.message);
			} else {
				alert(json.status.message);
			}					
			
		}			
	},
	
	checkEmail		: function(form, talent) {
		
		nickname = ($('u_nickname').value).toLowerCase();
		name = ($('u_name').value).toLowerCase();
		
		if(nickname.indexOf(name) >= 0 && nickname != "") {
			alert("Your nickname can't contain your surname");
			return false;
		}
		
		if (!formchecker.checkForm(form)) {
			return false;
		}
		
		if(talent)
			var postBody	= "action=AJAX_checkEmail&email="+$('email').value+"&u_nickname="+$('u_nickname').value;
		else
			var postBody	= "action=AJAX_checkCompanyEmail&email="+$('c_email').value+"&u_name="+$('c_name').value;
		
		new Ajax.Request(
			"index.php?p=/register",
			{
				method		: "post",
				postBody	: postBody,
				onSuccess	: function(t) {
					PEOPLE.login.doneCheck(form, t.responseText);
				},
				onFailure	: function(t) {
					alert("Critical Error, please contact administration with this info:\n" + t.status + " -- " + t.statusText);
				}
			}
		);	

	},
	
	doneCheck		: function(form, responseText) {

		var json = responseText.evalJSON();

		if(json.status.code == "success") {

			PEOPLE.login.doRegister(form);		
			
		} else {

			$('mandatory').style.display = "block";
			$('mandatory').update(json.status.message);
			
		}

	},
	
	doRegister	: function(form) {
		
		var postBody 	= Form.serialize(form);

		new Ajax.Request(
			"index.php?p=/register",
			{
				method		: "post",
				postBody	: postBody,
				onSuccess	: function(t) {
					PEOPLE.login.doneRegister(form, t.responseText);
				},
				onFailure	: function(t) {
					alert("Critical Error, please contact administration with this info:\n" + t.status + " -- " + t.statusText);
				}
			}
		);			
		
	},
	
	doneRegister	: function(form, responseText) {
		
		var json = responseText.evalJSON();

		if(json.status.code == "success") {
			
			$('stap1').style.backgroundImage = "url('images/layout/ok_off.jpg')";
			$('stap1b').style.backgroundImage = "url('images/layout/ok_off.jpg')";
			
			$('register').style.display = "none";
			$('companyRegister').style.display = "none";	
			
		} else {

			$('mandatory').style.display = "block";
			$('mandatory').update(json.status.message);
			
		}		
		
	},
	
	mailPassword		: function(form) {

		if (!formchecker.checkForm(form)) {
			return false;
		}
		
		var postBody	= "action=AJAX_sendPassword&email="+$('email').value;
		
		new Ajax.Request(
			window.location,
			{
				method		: "post",
				postBody	: postBody,
				onSuccess	: function(t) {
					PEOPLE.login.doneMailPassword(form, t.responseText);
				},
				onFailure	: function(t) {
					alert("Critical Error, please contact administration with this info:\n" + t.status + " -- " + t.statusText);
				}
			}
		);	

	},
	
	doneMailPassword	: function(form, responseText) {

		var json = responseText.evalJSON();

		if(json.status.code == "success") {

			$('success').style.display = "block";
			$('success').update("An email with your password has been succesfully sent to " + $('email').value);			
			
		} else {

			$('mandatory').style.display = "block";
			$('mandatory').update(json.status.message);
			
		}

	},
	
	swapView	: function(id) {
	
		$('companyRegister').style.display = "none";
		$('register').style.display = "none";
		
		$(id).style.display = "block";
	
	},
	
	checkAgency : function()
	{
		var success = true;
		var msg = "";
		if ( ($('u_phone').value == undefined  || $('u_phone').value.length == 0) )
		{
			msg += 'Please fill in your phone nr.<br/>';
			$('u_phone').style.background = "#059ED8"; 
			success = false;
		}
		if ( ($('u_contact').value == undefined || $('u_contact').value.length == 0) )
		{
			msg += 'Please fill in a contact person.<br/>';	
			$('u_contact').style.background = "#059ED8";
			success = false;
		}
		if ( ($('u_street').value == undefined || $('u_street').value.length == 0) )
		{
			msg += 'Please fill in your street.<br/>';	
			$('u_street').style.background = "#059ED8";
			success = false;
		}
		if ( ($('u_postal').value == undefined || $('u_postal').value.length == 0) )
		{
			msg += 'Please fill in your postal.<br/>';	
			$('u_postal').style.background = "#059ED8";
			success = false;
		}
		if ( ($('u_city').value == undefined || $('u_city').value.length == 0) )
		{
			msg += 'Please fill in your city.<br/>';	
			$('u_city').style.background = "#059ED8";
			success = false;
		}
		if ( ($('u_number').value == undefined || $('u_number').value.length == 0) )
		{
			msg += 'Please fill in your house number.<br/>';	
			$('u_number').style.background = "#059ED8";
			success = false;
		}		
		if ( ($('u_country').value == undefined || $('u_country').value.length == 0) )
		{
			msg += 'Please select your country.<br/>';	
			$('u_country').style.background = "#059ED8";
			success = false;
		}
		if ( !success )
		{
			$('mandatory').update(msg);
			$('mandatory').style.display = 'block';
		}
		
		return success;
	}
}
