var formValidaSignIn;
function signInGotta() {
	formValidaSignIn = $("#formSignInGotta").validate({
		onkeyup : false,
		errorPlacement : function(error, element) {
			showErrorValidationForm(element, error);
		},
		unhighlight : function(element, errorClass, validClass) {
			hideErrorValidationForm(element);
		},
		rules : {
			signInEmail : {
				required : true,
				email : true
			},
			signInPassword : {
				required : true
			}
		},
		messages : {
			signInEmail : {
				required : 'Please enter en email',
				email : 'Please enter a valid email address'
			},
			signInPassword : {
				required : 'Please enter a password'
			}
		}
	});
	if ($("#cbxRememberMe").is(':checked')) {
		newCookie('gottaPublicEmail', $("#txtSignInEmail").val());
		newCookie('gottaPublicPassword', $("#txtSignInPassword").val());
		newCookie('gottaPublicCheckbox', 'true');
	} else {
		eraseCookie('gottaPublicEmail');
		eraseCookie('gottaPublicPassword');
		eraseCookie('gottaPublicCheckbox');
	}

	if (formValidaSignIn.form()) {
		showLoadingGotta();
		var data  = "action=signInDeals";
			data += "&email=" + $("#txtSignInEmail").val();
			data += "&password=" + $("#txtSignInPassword").val();

		$.ajax({
			url : urlAppHttpsMode+"/signIn/manage.do",
			data : data,
			error : function(object, errorMsg, guid) {
				hideLoadingGotta();
			},
			success : onSignInGotta,
			dataType : 'json',
			timeout : 20000,
			type : "POST"
		});
	}
}

function onSignInGotta(data) {
	if (data.result == 1) {
		var dealId = $("#hdIdDeal").val();
		var optionId = $("#hdIdOption").val();
		$('#dvHiddenSignIn').hide();
		var redirectTo = $("#hdRedirectTo").val();
		if ($("#hdRedirectMyAccount").val() == 'true' || dealId == undefined) {
			if (redirectTo != '' && redirectTo != undefined) {
				window.location.href = urlAppHttpMode+redirectTo;
			}else{
				window.location.href = urlAppHttpMode+'/account';	
			}
		} else {
			var url = urlAppHttpsMode+'/checkout';
			var dataArray = new Array();
			dataArray.push({
				name : 'dealId',
				value : dealId
			},{
				name : 'optionId',
				value : optionId
			}, {
				name : 'action',
				value : 'selectDealToPay'
			});
			sendDataNoAjax(url, dataArray);
		}
	} else {
		if ($("#hdRedirectMyAccount").val() == 'true') {
			$("#dvErrorSignIn").show();
			$("#lblSignInMessage").html(data.message);
		} else {
			$("#lblFailedSignin").html(data.message);
		}
		hideLoadingGotta();
	}
}

function openSignInGotta(redirectMyAccount) {
	$("#hdRedirectMyAccount").val(redirectMyAccount);
	$("#txtSignInEmail").val('');
	$("#txtSignInPassword").val('');

	$('#dvHiddenSignIn').css('position', 'fixed');
	$('#dvHiddenSignIn').css('top', '50%');
	$('#dvHiddenSignIn').css('left', '50%');
	$('#dvHiddenSignIn').css('margin-left', '-210px');
	$('#dvHiddenSignIn').css('margin-top', '-131px');

	var checkboxCookie = readCookie("gottaPublicCheckbox");
	if (checkboxCookie == 'true') {
		$("#txtSignInEmail").val(readCookie("gottaPublicEmail"));
		$("#txtSignInPassword").val(readCookie("gottaPublicPassword"));
		$("#cbxRememberMe").attr('checked', true);
	} else {
		eraseCookie('gottaPublicEmail');
		eraseCookie('gottaPublicPassword');
		eraseCookie('gottaPublicCheckbox');
		$("#cbxRememberMe").attr('checked', false);
	}

	showModal();
	$('#dvHiddenSignIn').show();
	$('#txtSignInEmail').focus();
}

function closeSignInGotta() {
	$('#dvHiddenSignIn').hide();
	closeModal();
}

function selectCityToSubscribe() {
	var idCity = $("#ddlSelectCityDiv").val();
	if (idCity == "") {
		showErrorValidationForm($("#ddlSelectCityDiv"), 'Please select a city');
		return false;
	} else {
		hideErrorValidationForm($("#ddlSelectCityDiv"));
	}
	var data  = 'action=registerUser';
		data += '&email=session'; // / user in session
		data += '&idCity=' + idCity;

	showLoadingGotta();
	$.ajax({
		url : urlAppHttpsMode+"/register/registerUser.do",
		data : data,
		error : function(object, errorMsg, guid) {
			hideLoadingGotta();
		},
		success : onSelectCityToSubscribeCallBack, // onSelectCityToSubscribeCallBack
													// should be in each page
													// from was called the
													// ssoFacebook function
		dataType : 'json',
		timeout : 200000,
		type : "POST"
	});
}

function registerGotta() {
	var formValidaRegister = $("#formRegisterGotta").validate({
		onkeyup : false,
		errorPlacement : function(error, element) {
			try {
				$(element).qtip("destroy");
			} catch (e) {
			}
			; // destroy tip if exists
			$(element).fadeOut(function() {
				$(element).fadeIn();
			}); // hide and show again the element
			$(element).qtip( // create tip
			{
				content : error,
				style : {
					name : 'red',
					tip : true
				},
				show : 'mouseover',
				hide : 'mouseout',
				position : {
					corner : {
						tooltip : 'topMiddle',
						target : 'bottomMiddle'
					}
				}
			});
		},
		unhighlight : function(element, errorClass, validClass) {
			try {
				$(element).qtip("destroy");
			} catch (e) {
			}
			; // destroy tip if exists
		},
		rules : {
			emailRegister : {
				required : true,
				email : true
			},
			selectCity : {
				required : true
			}
		},
		messages : {
			emailRegister : {
				required : 'Please enter en email',
				email : 'Please enter a valid email address'
			},
			selectCity : {
				required : 'Please select a city'
			}
		}
	});

	if (formValidaRegister.form()) {
		var data  = 'action=registerUser';
			data += '&email=' + $("#txtRegisterEmail").val();
		    data += '&idCity=' + $("#ddlSelectCity").val();

		showLoadingGotta();
		$.ajax({
			url : urlAppHttpMode+"/register/registerUser.do",
			data : data,
			error : function(object, errorMsg, guid) {
				hideLoadingGotta();
				// showErrorMessage("Oops!. There has been an unexpected
				// behavior on the system. We are looking into it right now.");
			},
			success : onRegisterGottaCallBack,
			dataType : 'json',
			timeout : 200000,
			type : "POST"
		});
	}
}

function onRegisterGottaCallBack(data) {
	hideLoadingGotta();
	if (data.result == 1) {
		goPage(urlAppHttpMode+"/thankyouregister");
		//showDivMessage('Confirmation', $('#divSuccessRegister').html(), 'A',false);
		//document.formRegisterGotta.reset();
		// $("#txtEmail").focus();
	} else if (data.result == 0) {
		showErrorMessage(data.message);
	} else {
		// showErrorMessage(data.message);
		var contentHtml = "<div style='text-align:justify;'><h2 style='font-size:16px;font-weight:bold;height:26px;'>This email is already registered with Gottahalfit!</h2>"
				+ "<p style='font-size:12px;'>To sign in to your account click on the <b><a class='link' href='"+urlAppHttpMode+"/signin'>Sign In</a></b>. If you forgot your password click on the <b><a class='link' href='"+urlAppHttpMode+"/signin'>Forgot Password</a></b>.</p><div>";
		showDivMessage('Message', contentHtml, 'B', true);
	}
}

function showModal() {
	// Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	// Set height and width to mask to fill up the whole screen
	$('#mask').css({
		'width' : maskWidth,
		'height' : maskHeight
	});

	// transition effect
	$('#mask').fadeIn(200);
	$('#mask').fadeTo("fast", 0.6);
}

function closeModal() {
	$('#mask').hide();
}

function showForgotPassword() {
	showModal();
	$('#dvHiddenSignIn').hide();
	$("#txtForgotEmail").val('');

	$('#dvHiddenForgotPassword').css('position', 'fixed');
	$('#dvHiddenForgotPassword').css('top', '50%');
	$('#dvHiddenForgotPassword').css('left', '50%');
	$('#dvHiddenForgotPassword').css('margin-left', '-205px');
	$('#dvHiddenForgotPassword').css('margin-top', '-126px');

	$('#dvHiddenForgotPassword').show();
	$('#dvSuccessForgotPassword').hide();
	$('#dvFormForgotPassword').show();
	$('#txtForgotEmail').focus();
}

function closeForgotPassword() {
	$('#dvHiddenForgotPassword').hide();
	closeModal();
}

var formValidaForgotPassword;
function sendForgotPassword() {
	formValidaForgotPassword = $("#formForgotPasswordGotta").validate({
		onkeyup : false,
		errorPlacement : function(error, element) {
			showErrorValidationForm(element, error);
		},
		unhighlight : function(element, errorClass, validClass) {
			hideErrorValidationForm(element);
		},
		rules : {
			forgotEmail : {
				required : true,
				email : true
			}
		},
		messages : {
			forgotEmail : {
				required : 'Please enter en email',
				email : 'Please enter a valid email address'
			}
		}
	});
	if (formValidaForgotPassword.form()) {
		var data = "action=sendNewPassword";
		data += "&email=" + $("#txtForgotEmail").val();

		$.ajax({
			url : urlAppHttpsMode+"/myaccount/manage.do",
			data : data,
			error : function(object, errorMsg, guid) {
				hideLoadingGotta();
				// showErrorMessage("Oops!. There has been an unexpected
				// behavior on the system. We are looking into it right now.");
			},
			success : onSendForgotPasswordGotta,
			dataType : 'json',
			timeout : 20000,
			type : "POST"
		});
	}
}

function onSendForgotPasswordGotta(data) {
	if (data.result == 1) {
		$('#dvFormForgotPassword').hide();
		$('#dvSuccessForgotPassword').show();
		setTimeout("closeForgotPassword()", 7000);
	} else {
		hideLoadingGotta();
		closeForgotPassword();
		var contentHtml = "The email you have entered does not exist yet on our database.<br/><br/>You can create a new account using <b><a class='link' href='"+urlAppHttpMode+"/signup'>this form</a></b>";
		showDivMessage('Message',contentHtml, "A", true);
	}
}

function showDivCities() {
	showModal();
	$('#w_cities').css('position', 'fixed');
	$('#w_cities').css('top', '50%');
	$('#w_cities').css('left', '50%');
	$('#w_cities').css('margin-left', '-255px');
	$('#w_cities').css('margin-top', '-120px');
	$('#w_cities').show();
}

function hideDivCities() {
	$('#w_cities').hide();
	closeModal();
}

function selectOtherCity(idCity) {
	// displayDropArea(false);
	var urls = window.location.pathname;
	var url = urls;
	//alert(url);
	var dataArray = new Array();
	dataArray.push({
		name : 'citySelected',
		value : idCity
	});
	sendDataNoAjax(url, dataArray);
}

function showDivMessage(title, content, type, warning) {
	showModal();
	if (warning) {
		$("#imgIconAlert" + type).show();
		$("#imgIconOk" + type).hide();
	} else {
		$("#imgIconAlert" + type).hide();
		$("#imgIconOk" + type).show();
	}
	$('#titleMessage' + type).html(title);
	$('#contentMessage' + type).html(content);

	$('#w_generic_' + type).css('position', 'fixed');
	$('#w_generic_' + type).css('top', '50%');
	$('#w_generic_' + type).css('left', '50%');
	$('#w_generic_' + type).css('margin-left', '-255px');
	if (type == "A")
		$('#w_generic_' + type).css('margin-top', '-157px');
	else
		$('#w_generic_' + type).css('margin-top', '-180px');

	$('#w_generic_' + type).show();
}

function hideDivMessage(type) {
	$('#w_generic_' + type).hide();
	closeModal();
}

function showMsgSessionExpired(site) {
	if (site == "admin") {
		showErrorMessage("Your session has expired.  Please sign in.");
		setTimeout("goPage('"+urlAppHttpsMode+"/signIn/signIn.do')", 3000);
	} else {
		showDivMessage('Session expired',
				"Your session has expired.  Please sign in.", "B", true);
		setTimeout("goPage('"+urlAppHttpsMode+"/signin')", 3000);
	}
}

function goPage(url) {
	window.location.href = url;
}

function logOut() {
	showLoadingGotta();
	logoutFacebook();
	setTimeout("goPage('"+urlAppHttpMode+"/signIn/signIn.do?action=logoutPublic')", 2000);
}

function rollOver(imagenName, imagenSrc) {
	document[imagenName].src = imagenSrc;
}

function displayDropArea(display) {
	var title = document.getElementById("dropAreasTitle");
	var drop = document.getElementById("dropAreasBody");

	if (!display) {
		title.style.display = 'block';
		drop.style.display = 'none';
	} else {
		drop.style.display = 'block';
		title.style.display = 'none';
	}
}

function downloadDone() {
	window.onfocus = fnEmpty;
	hideLoadingGotta();
	hideLoading();
}
function fnEmpty(){}

function showLoadingGotta(){
	$('#divLoadingGotta').css('position', 'fixed');
	$('#divLoadingGotta').css('top', '50%');
	$('#divLoadingGotta').css('left', '50%');
	$('#divLoadingGotta').css('margin-left', '-40px'); 
	$('#divLoadingGotta').css('margin-top', '-40px'); 
	showModal();
	$('#divLoadingGotta').show();	
}
function hideLoadingGotta(){
	$('#divLoadingGotta').hide();	
	closeModal();
}
