jQuery(document).ready(function() {

	if (typeof processingHide == 'function') {
		function processingHide() {
		}
		;
	}

	jQuery('#password-clear').show();
	jQuery('#login_password').hide();

	jQuery('#login_username').focus(function() {
		jQuery('#password-clear').hide();
		jQuery('#login_password').show();
	});

	jQuery('#password-clear').focus(function() {
		jQuery('#password-clear').hide();
		jQuery('#login_password').show();
		jQuery('#login_password').focus();
	});
	jQuery('#login_password').blur(function() {
		if (jQuery('#login_password').val() == '') {
			jQuery('#password-clear').show();
			jQuery('#login_password').hide();
		}
	});
})

function showLoginDetail(dir) {
	if (!dir) {
		// jQuery('#remember_me').fadeIn();
		jQuery('#login_detail').slideDown();
	} else {
		// jQuery('#remember_me').fadeOut();
		jQuery('#login_detail').slideUp();
	}
}

function doLoginKeyPressEnter(e, url) {

	var unicode = e.keyCode ? e.keyCode : e.charCode
	if (unicode == 13) {
		validateLogin(url);
	}

}

function login(random) {
	var data = jQuery('#login_form' + random).serialize(true);
	var url = siteurl + 'ajax/login.php';
	if (typeof processingShow == 'function') {
		processingShow('logging in');
	}
	jQuery.ajax( {
		type : "POST",
		url : url,
		data : data,
		success : function(response) {
			if (typeof processingHide == 'function') {
				processingHide();
			}

			// console.log(response);
		if (response.action) {
			eval(response.action);
		}
	},
	complete : function() {
		if (typeof processingHide == 'function') {
			processingHide();
		}
		jQuery('#store_ajax_status').hide();
	}
	});

}

function loginCrossDomain(random) {
	var data = jQuery('#login_form' + random).serialize(true);
	var url = siteurl + 'ajax/login.php';
	if (typeof processingShow == 'function') {
		processingShow('logging in');
	}
	jQuery.ajax( {
		url : url,
		data : data,
		dataType: 'jsonp',
        jsonp: 'callback',
		success : function(response) {
			if (typeof processingHide == 'function') {
				processingHide();
			}

			// console.log(response);
		if (response.action) {
			eval(response.action);
		}
		if (response.msg) {
			eval(response.action);
		}
	},
	complete : function() {
		if (typeof processingHide == 'function') {
			processingHide();
		}
		jQuery('#store_ajax_status').hide();
	}
	});

}

function validateLogin(random) {

	var data = jQuery('#login_form_' + random).serialize(true);
	// console.log(data);
	// return;

	var email_add = trim(jQuery('#login_username').val());
	jQuery('#login_username').val(email_add);
	if (jQuery('#login_username').val() != ""
			&& jQuery('#login_password').val() != ""
			&& isValidEmailAddress(jQuery('#login_username').val())) {
		doAjaxLogin(jQuery('#login_username').val(), jQuery('#login_password')
				.val(), 0, jQuery('#dest').val());
	} else {
		showNotification("Wrong Email and password combination.");
		return false;
	}
}

/*
 * function doAjaxLogin(username,pwd,desc,dest) { //
 * jQuery('#login_ajx_div').html('<img src="'+ siteurl + //
 * 'images/ajax-loader-facebook.gif" />'); processingShow('logging in');
 * if(jQuery('#login_remember').is(':checked')){ var remember = 'on'; } else {
 * var remember = 'off'; } // console.log(xml, 'xml');
 *
 * if(xml=='failure'){ processingHide(); showNotification("Invalid Email and
 * password combination."); // jQuery('#login_ajx_div').html('<a id="btn_login" //
 * onclick="return validateLogin(\'' + url + '\');" // href="#"></a>'); return
 * false; } else if(xml=='confirm account'){ processingHide();
 * showNotification("Your account has not yet been confirmed. Please check your
 * email."); // jQuery('#login_ajx_div').html('<a id="btn_login" //
 * onclick="return validateLogin(\'' + url + '\');" // href="#"></a>'); return
 * false; } else if(xml=='inactive') { processingHide(); showNotification("Your
 * account is not activated, Please contact administrator."); //
 * jQuery('#login_ajx_div').html('<a id="btn_login" // onclick="return
 * validateLogin(\'' + url + '\');" // href="#"></a>'); } else
 * if(xml=='confirm_error') { processingHide(); showNotification("Sorry,
 * Confirmation code or login details incorrect."); //
 * jQuery('#login_ajx_div').html('<a id="btn_login" // onclick="return
 * validateLogin(\'' + url + '\');" // href="#"></a>'); return false; } else {
 * window.location=xml; } }, error: function(data){ processingHide(); //
 * console.log('error', data); showNotification("Grrr! bompa is not available at
 * the moment. Please try again later."); }
 *
 * }); }
 */

function doAjaxLogout(username, dest) {
	jQuery
			.ajax( {
				type : 'POST',
				url : siteurl + 'ajax/submitLogout.php',
				data : {
					username : username,
					dest : dest
				},
				success : function(xml) {
					if (xml == 'failure') {
						showNotification("Invalid Email and password combination.");
						return false;
					} else {
						window.location = xml;
					}

				},
				error : function(data) {
					showNotification("Grrr! bompa is not available at the moment. Please try again later.");
					jQuery('#login_ajx_div').html('');
				}

			});
}

