// get cookie
function getCookie(name) {
	var ret = '';
	var nameLenght = name.length;

	if (document.cookie.length > 0) {
  		cStart = document.cookie.indexOf(name + '=');
  		
  		if (cStart != -1) { 
    		cStart = cStart + nameLenght+1; 
    		cEnd = document.cookie.indexOf(';', cStart);

    		if (cEnd == -1) {
    			cEnd = document.cookie.length;
    		}
    		ret = unescape(document.cookie.substring(cStart, cEnd));
    	} 
  	}
  	
	return ret;
}

// login form
function sendForm_login(){
	var email = $('#email').val();
	var password = $('#password').val();
	var rememberMe = $('#remember:checked').val();
	// ajax 
	$.ajax({
		type: 'POST',
		url: '/developer-login.html',
		data:
			'submit=1' +
			'&email=' + email + 
			'&password=' + password +
			'&remember=' + rememberMe,
			
		success: function(result) {
			if(result !='') {
				$('#error').show();
				$('#error').html(result);
			}
			else{
				window.location.href = 'backend-navigation-campaigns.html';
			}
		}
	});
}

// recover password form
function sendForm_password(){
	var email = $('#emailPaswortForget').val();
	// ajax 
	$.ajax({
		type: 'POST',
		url: '/developer-password.html',
		data:
			'submit=1' +
			'&email=' + email,
			
		success: function(result) {
			if(result.substring(0,9) == ':SUCCESS:') {
				$('#contentFull').html(result.substring(9, result.length));
			}
			if(result !='') {
				$('#error').show();
				$('#error').html(result);
			}
		}
	});
	
}

//recover password form
function sendForm_newpassword(){
	var password = $('#passwordRecover').val();
	var passwordConfirm = $('#confirmPasswordRecover').val();
	var hash = $('#hash').val();
	// ajax 
	$.ajax({
		type: 'POST',
		url: '/developer-password.html',
		data:
			'setNewPassword=1' +
			'&newPassword=' + password +
			'&passwordConfirm=' + passwordConfirm +
			'&hash=' + hash,
			
		success: function(result) {
			if(result.substring(0,9) == ':SUCCESS:') {
				$('#contentFull').html(result.substring(9, result.length));
			}
			if(result !='') {
				$('#error').show();
				$('#error').html(result);
			}
		}
	});
	
}

// init
$(document).ready(function(){
	
	$('#send_login, .send_login').click(function(){
			sendForm_login();
			$('#error').hide();
			$('#error').html('');
	});
	
	$('#send_password').click(function(){
		sendForm_password();
		$('#error').hide();
		$('#error').html('');
	});
	
	$('#password').keydown(function(e){
		if(e.keyCode == 13) {
			sendForm_login();
			$('#error').hide();
			$('#error').html('');
		}
	});
	
	$('#emailPaswortForget').keydown(function(e){
		if(e.keyCode == 13) {
			sendForm_password();
			$('#error').hide();
			$('#error').html('');
		}
	});
	
	$('#send_newpassword').click(function(){
		sendForm_newpassword();
		$('#error').hide();
		$('#error').html('');
	});
	
	$('#passwordRecover, #confirmPasswordRecover').keydown(function(e){
		if(e.keyCode == 13) {
			sendForm_newpassword();
			$('#error').hide();
			$('#error').html('');
		}
	});
});
