

//	if (password !== confirm_password){o.addClass('ui-state-error');updateTips("Password and");return false;}else{return true;}



	$(function() {
		
	//	document.getElementById("loginame").onchange = function(){validateLoginName();};
		document.getElementById("loginame").onchange = validateLoginName;
		document.getElementById("password").onchange = validatePassword;
		
		validUsername = false;
		validPassword = false;
		passwordConfirmed = false;
		

		var loginame = $("#loginame"),
			password = $("#password"),
			confirm_password = $("#confirm_password"),
			tips = $("#validateTips"),
			allFields = $([]).add(loginame).add(password).add(confirm_password).add(tips);

		var validationNumber = $("#validationNumber"),
			UserName = $("#UserName"),
			tips2 = $("#validateTips2"),
			allFields2 = $([]).add(validationNumber).add(UserName).add(tips2);

		var findpassword = $("#findpassword"),
			tips3 = $("#validateTips3"),
			allFields3 = $([]).add(loginame).add(tips3);


//===========================================================================================
		$("#newuserdialog").dialog({
			bgiframe: true,
			autoOpen: false,
			width: 400,
			position: 'top',
			modal: true,
			open: function(){allFields.val('');allFields.removeClass('ui-state-error');validUsername = false;validPassword = false;document.getElementById("loginame").value = document.getElementById("HECM_Login").value;},
			
			buttons: {
				'Create an account': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					if (validUsername == false) {validateLoginName();}
					
					if ((validUsername == true) && (validPassword == false)) {validatePassword();}
					
					if ((validUsername == true) && (validPassword == true) && (passwordConfirmed == false)) {confirmPassword();}
					
				if ((validPassword && validUsername && passwordConfirmed)) {
					
			 		jQuery(this).parents('.ui-dialog').appendTo('#Register'); 
					document.getElementById("HECM_Login").value = document.getElementById("loginame").value;
					document.getElementById("HECM_pwd").value = document.getElementById("password").value;
					
						var remember = jQuery.cookie('remember2');
						if (jQuery('#remember').attr('checked')) {
							var HECM_Login = jQuery('#HECM_Login').attr("value");
							var HECM_pwd = jQuery('#HECM_pwd').attr("value");
				
							// set cookies to expire in 14 days
							jQuery.cookie('HECM_Login', HECM_Login, { expires: 14 });
							jQuery.cookie('HECM_pwd', HECM_pwd, { expires: 14 });
							jQuery.cookie('remember2', true, { expires: 14 });
						}
			
	

					document.getElementById("Register").submit();
					}
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		
		
		$('#create-homeowner').click(function() {	
			$('#newhomeownerdialog').dialog('open');
		})
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function(){
			$(this).addClass("ui-state-active"); 
		})
		.mouseup(function(){
				$(this).removeClass("ui-state-active");
		});
		



//===========================================================================================
		$("#validateuserdialog").dialog({
			bgiframe: true,
			autoOpen: false,
			width: 400,
			position: 'top',
			modal: true,
			
			buttons: {
				'Validate Account': function() {
					var bValid = true;
					allFields2.removeClass('ui-state-error');

				
					
			 		jQuery(this).parents('.ui-dialog').appendTo('#HECMLogin'); 
					document.getElementById("HECMLogin").submit();
					
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
	
	
//===========================================================================================
		$("#forgotpassworddialog").dialog({
			bgiframe: true,
			autoOpen: false,
			width: 400,
			position: 'top',
			modal: true,
			open: function(){document.getElementById("findpassword").value = document.getElementById("HECM_Login").value;},
			
			buttons: {
				
				
				'Email my login info': function() {var bValid = true; allFields3.removeClass('ui-state-error');
			 		jQuery(this).parents('.ui-dialog').appendTo('#ForgotPassword'); 
					document.getElementById("ForgotPassword").submit();	},
					
				'Show Hint': function() {getLoginHint();},
					
				
				Cancel: function() {$(this).dialog('close');}
				
				
				
				},
			
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		

	
	
	});

//===========================================================================================
function DisplayNewUserHandler(newUserType) {
	jQuery('#newuserdialog').dialog('option', 'title', 'New '+newUserType);
	jQuery('#newuserdialog').dialog('option', 'height', 350);
	jQuery('#newuserdialog').dialog('open');
	document.getElementById("UserType").value = newUserType;
}

//===========================================================================================
function getLoginHint(){
	AJUTIL.AJUTIL_GetPasswordHint(document.getElementById("findpassword").value,
								
	function(result,e){
	
		if(result.PasswordHint == ""){
			alert("You did not create a password hint!");
		}else if (result.PasswordHint == "-1"){
			alert("Email address not found!");
		} else {
			alert("Your Login hint is '"+result.PasswordHint+"'");
		};});
}



//===========================================================================================
function validatePassword(){
		validPassword = checkLength($("#password"), "Length of password must be 8 or more characters.",8,24);
		}

//===========================================================================================
function confirmPassword(){
		 if (document.getElementById("password").value === document.getElementById("confirm_password").value) {
			 passwordConfirmed = true;
			 displayDialogError(false, $("#confirm_password") ,"");
		 } else {
			 passwordConfirmed = false;
			 displayDialogError(true, $("#confirm_password") ,"Password and Confirm Password do not match!");
		 }
	}

//===========================================================================================
function validateLoginName() {
	
					bValidemail = checkRegexp($("#loginame"),/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com", $("#validateTips"));
					if (bValidemail) {
						AJUTIL.AJUTIL_ValidUserName(document.getElementById("loginame").value,function(result,e){isUsernameValid(result,e,$("#loginame"),$("#validateTips"));});
					} else {
						//document.getElementById("loginerror").innerHTML = '<B><font color="#ff0000">Please enter a valid email address</font><B>';
						validUsername = false;
					}
	}



//===========================================================================================

function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				displayDialogError(true, o ,n);
				return false;
			} else {
				displayDialogError(false, o ,"");
				return true;
			}
		}


//===========================================================================================

function checkRegexp(o,regexp,n,tipObject) {

			if ( !( regexp.test( o.val() ) ) ) {
				displayDialogError(true, o ,n);
				return false;
			} else {
				displayDialogError(false, o ,"");
				return true;
			}
		}

	
//===========================================================================================
function isUsernameValid(result ,e ,objUsername, tip){
	
	if(result.ValidUsername != "OK") {
		displayDialogError(true, objUsername ,result.ValidUsername)
		validUsername = false;
	} else {
		displayDialogError(false, objUsername ,"")
		validUsername = true;
	}
}

//===========================================================================================
function displayDialogError(isError, obj ,objText) {
	
	if (isError) {
		obj.addClass('ui-state-error');
		document.getElementById("validateTips").innerHTML = '<B><font color="#ff0000">'+objText+'</font><B>';
	} else {
		obj.removeClass('ui-state-error');
		document.getElementById("validateTips").innerHTML = objText;
	}
}





//===========================================================================================
function okToSubmit(){
	if (validUsername && validPassword) {
		alert("okToSubmit");	
	}
	
}





