﻿var _countryID;
var _returnURL;
var countIncorectLogIn = 0;
$(document).ready(function() {	

	$(".dialog").dialog({
		modal: false,
		draggable: false,
		autoOpen: false,
		resizable: false,
		dialogClass: 'ui-noTitle',
		width: 'auto',
		minWidth: 0,
		minHeight: 0
	});

	$('#form1').click(function() {
		HideMessageDialog();
		$(".dialog").dialog('close');
	});

	$('#btnForgotPassword').click(function() {
		$('#txtEmail').attr('value', '');
		$("#FogotPassword.dialog").dialog('open');
		$('#txtEmail').focus();
		return false;
	});

	$('#btnLogin').click(function() {
		Submit();
		return false;
	});

	$('#btnSubmit').click(function() {
		var message = '';
		var params = new Object();
		params.userEmail = $('#txtEmail').val();

		$.ajaxTools({
			url: 'Login.aspx/SendForgotPassword',
			data: params,
			success: function(data) {
				message = data.Message;
				return true;
			},
			complete: function() {
				//HideWaitDialog();
				$("#FogotPassword.dialog").dialog('close');
				if (message != '')
					ShowMessageDialog(message);
			},
			beforeSend: function() {
				//ShowWaitDialog();
				return true;
			},
			error: function(req, status) {
				ShowMessageDialog("is currently experiencing technical difficulties with login. Please contact us if problems persist.<br />");
				return false;
			}
		});
		return false;
	});

	$("#form1").submit(function() {
		Submit();
		return false;
	});

	$("#form1").keyup(function(e) {
		if (e.which == 13 && ($.browser.mozilla)) {
			Submit();
			return false;
		}
		return true;
	});
});

function DataBindLogin(countryID, returnURL) {
	_countryID = countryID;
	_returnURL = returnURL;
}

function Submit() {
	var redirect = '';
	var params = new Object();
	params.username = $('#txtUserName').val();
	params.password = $('#txtPassword').val();
	params.rememberMe = $('#chkRemember').attr('checked');
	params.countIncorectLogIn = countIncorectLogIn;
	params.currentCountryID = _countryID;

	$.ajaxTools({
		url: 'Login.aspx/Submit',
		data: params,
		success: function(data) {
			if (!data.Result) {
				ShowMessageDialog(data.Message);
				redirect = '';
			}
			else {
				var returnUrl = _returnURL;
				if (returnUrl != '')
					redirect = returnUrl;
				else
					redirect = data.Message;
			}
			return true;
		},
		complete: function() {
			if (redirect != '')
				window.location = redirect;
			else {
				countIncorectLogIn++;
				//HideWaitDialog();
			}
		},
		beforeSend: function() {
			//ShowWaitDialog();
			return true;
		},
		error: function(req, status) {
			ShowMessageDialog("is currently experiencing technical difficulties login. Please contact us if problems persist.<br />");
			return false;
		}
	});
	return false;
}