﻿
function Validate() {
	//alert($find('#' + btnLogin.id));
	//$find('#'+btnLogin.id).attr("disabled", "disabled");
	ClearAllErrors();
	var message = ClientValidation();
	if (message != '') {
		ShowError(message);
		ClearPassword();
		SetFocus();
		return false;
	}
	else {
		return true;
	}
}

function ClientValidation() {
	var message = '';
	var linebreak = '</br>';
	var clientValidation = true;

	$(":text").each(function (index, textInput) {
		clientValidation = ValidateEmptyField(textInput.id);
		if (clientValidation == true) {
			clientValidation = ValidateFieldForSpaces(textInput.id);
			if (clientValidation == false) {
			    message += textInput.getAttribute("fieldname") + GlobalResources.Login_NoSpaces + linebreak;
				SetErrorStyle($(this).parents()[0].id);
			}
		}
		else {
		    message += textInput.getAttribute("fieldname") + GlobalResources.Login_NotBlank + linebreak;
			SetErrorStyle($(this).parents()[0].id);
		}
		if (textInput.getAttribute("fieldname") == "Email" && (clientValidation == true)) {
			clientValidation = ValidateEmail(textInput.id);
			if (clientValidation == false) {
				message += GlobalResources.Registration_InvalidEmail + linebreak;
				SetErrorStyle($(this).parents()[0].id);
			}
		}
	});
	$(":password").each(function (index, textInput) {
		if (textInput.getAttribute("fieldname") == "Password2" && textInput.getAttribute("canbeblank") != null && textInput.getAttribute("canbeblank") == "True")
			return true;
		clientValidation = ValidateEmptyField(textInput.id);
		if (clientValidation == false) {
		    message += textInput.getAttribute("fieldname") + GlobalResources.Login_NotBlank + linebreak;
			SetErrorStyle($(this).parents()[0].id);
		}
	});
	return message;
}

function ShowError(message) {
	//$find('#' + '<%=uxBtnLogOn.ClientID%>').removeAttr("disabled");
	$('#' + divErrorClientId).show();
	$('#' + divErrorClientId).html(message);
}
function SetErrorStyle(spanId) {
	$('#' + spanId).addClass('error');
}
function ClearAllErrors() {
	var staticSpanElements = $('#loginInformation')
	for (i = 0; i < staticSpanElements.length; i++) {
		var spanId = staticSpanElements[i].id;
		$('#' + spanId).removeClass("error");
	}
	$('#' + divErrorClientId).hide();
	$('#' + divErrorClientId).html("");

}

function ClearPassword() {
	$(":password").each(function (index, password) { password.value = ""; });
	SetFocus();
}

// initialize by hiding all messaging features
//$('#chkAndLink').show();       // the checkbox and popup message link
//$('.onscreenMsg').show();      // the onscreen messaging textbox
//$('.popupMsg').hide();         // the popup messaging textbox
//$('#loginHelpContent').hide(); // the login help tooltip

// popup window for terms & conditions copy
var isPopped = false;

function showPopup() {
    if (!isPopped) {        
		$("#backgroundTint").css({
			"opacity": "0.7"
		});
		$("#backgroundTint").fadeIn("slow");
		$(".popupMsg").fadeIn("slow", function () {
		    if (mustSeeTC == true)
		        hookScrollForTandC($('.tandcPopup'), true); 
		    else
		        onFinishReadingTAndC();		    
        });
		isPopped = true;
	}
}
function hidePopup() {
	if (isPopped) {
		$("#backgroundTint").fadeOut("slow");
		$(".popupMsg").fadeOut("slow"); 
		isPopped = false;
	}
}
function centerPopup() {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(".popupMsg").height();
	var popupWidth = $(".popupMsg").width();

	//centering
	$(".popupMsg").css({
		"position": "absolute",
		"top": windowHeight / 2 - popupHeight / 2,
		"left": windowWidth / 2 - popupWidth / 2
	});
}

$(document).ready(function () {
	$(".termsLink").click(function () {
		centerPopup();
		showPopup();
	});
	$("#closeWindow").click(function () {
		hidePopup();
	});
	$("#backgroundTint").click(function () {
		hidePopup();
	});
	$(document).keyup(function (e) {
		if (e.keyCode == 27 && isPopped) {
			hidePopup();
		}
	});

	// show help tooltip with these settings
	$('#loginHelp').bt({
		trigger: 'click',
		clickAnywhereToClose: true,
		contentSelector: "$('#loginHelpContent').html()",
		width: 240,
		fill: '#ffffff',
		strokeStyle: '#666666',
		spikeLength: 10,
		spikeGirth: 10,
		padding: 8,
		cornerRadius: 0,
		shadow: true,
		shadowOffsetX: 3,
		shadowOffsetY: 3,
		shadowBlur: 8,
		shadowColor: 'rgba(0,0,0,.9)',
		shadowOverlap: false,
		noShadowOpts: { strokeStyle: '#999', strokeWidth: 2 },
		cssStyles: { color: '#333333' },
		positions: ['right', 'top']
	});
});

