// JavaScript Document
window.addEvent("unload", function(event){
	//unload classes
	//if (App.searchSuggest != undefined) { App.searchSuggest.unload(); App.searchSuggest = null; }
	
	//unload local variable objects
	App.collectGarbage(App.userAuth); App.userAuth = null;
	//App.collectGarbage(profileLinks);
	
});

/*========= FOR LOGIN UI =========*/
function toggleLoginForm(show){
	var tempEl = null;
	
	if (!show){
		tempEl = $("login_overlay"); tempEl.show(); tempEl = null;
		tempEl = $("reg_message"); tempEl.hide(); tempEl = null;
		tempEl = $("reg_container"); tempEl.show();  tempEl = null;
	} else {
		tempEl = $("login_overlay"); tempEl.hide(); tempEl = null;
		tempEl = $("login_container"); tempEl.show(); tempEl = null;
		tempEl = $("forgot_container"); tempEl.hide(); tempEl = null;
		tempEl = $("reg_message"); tempEl.show(); tempEl = null;
		tempEl = $("reg_container"); tempEl.hide();	 tempEl = null;
	}
	App.val.clearAll();
}



/*========= FOR DASHBOARD UI =========*/
function toggleProfilePanel(UI){
	var tempEl = null;
	App.val.clearAll();
	if (UI == "profile"){
		tempEl = $("dash_view_profile"); tempEl.show(); tempEl = null;	//close button
		tempEl = $("dash_profile_details"); tempEl.hide(); tempEl = null;	//summary description
		tempEl = $("dash_account_form"); tempEl.hide(); tempEl = null;	
		tempEl = $("dash_notify_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_upload_photo"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_profile_form"); tempEl.show(); tempEl = null;
		tempEl = $("dash_sms_form"); tempEl.hide(); tempEl = null;
		tempEl = $("floating_form_container"); tempEl.show(); tempEl = null;
	} else if (UI == "notify") {
		tempEl = $("dash_view_profile"); tempEl.show(); tempEl = null;	//close button
		tempEl = $("dash_profile_details"); tempEl.hide(); tempEl = null;	//summary description
		tempEl = $("dash_account_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_notify_form"); tempEl.show(); tempEl = null;
		tempEl = $("dash_upload_photo"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_profile_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_sms_form"); tempEl.hide(); tempEl = null;
		tempEl = $("floating_form_container"); tempEl.show(); tempEl = null;
	} else if (UI == "account") {
		tempEl = $("dash_view_profile"); tempEl.show(); tempEl = null;	//close button
		tempEl = $("dash_profile_details"); tempEl.hide(); tempEl = null;	//summary description
		tempEl = $("dash_account_form"); tempEl.show(); tempEl = null;
		tempEl = $("dash_notify_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_upload_photo"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_profile_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_sms_form"); tempEl.hide(); tempEl = null;
		tempEl = $("floating_form_container"); tempEl.show(); tempEl = null;
	} else if (UI == "photo") {
		tempEl = $("dash_view_profile"); tempEl.show(); tempEl = null;	//close button
		tempEl = $("dash_profile_details"); tempEl.hide(); tempEl = null;	//summary description
		tempEl = $("dash_account_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_notify_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_upload_photo"); tempEl.show(); tempEl = null;
		tempEl = $("dash_profile_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_sms_form"); tempEl.hide(); tempEl = null;
		tempEl = $("floating_form_container"); tempEl.show(); tempEl = null;
	} else if (UI == "sms") {
		tempEl = $("dash_view_profile"); tempEl.show(); tempEl = null;	//close button
		tempEl = $("dash_profile_details"); tempEl.hide(); tempEl = null;	//summary description
		tempEl = $("dash_account_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_notify_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_upload_photo"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_profile_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_sms_form"); tempEl.show(); tempEl = null;
		tempEl = $("floating_form_container"); tempEl.show(); tempEl = null;
	} else {
		tempEl = $("dash_view_profile"); tempEl.hide(); tempEl = null;	//close button
		tempEl = $("dash_profile_details"); tempEl.show(); tempEl = null;	//summary description
		tempEl = $("dash_account_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_notify_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_upload_photo"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_profile_form"); tempEl.hide(); tempEl = null;
		tempEl = $("dash_sms_form"); tempEl.hide(); tempEl = null;
		tempEl = $("floating_form_container"); tempEl.hide(); tempEl = null;
	}
}

function validateRegistration(mode){
	var matched = true;
	
	//email validation
	matched = App.val.validate($("reg_email"), {
		required: true, checkFormat: "email", title: "Email Address", 
		message: "Please supply a valid email address."
	}, matched);
	
	if (mode == "add"){
		//password validation
		matched = App.val.validate($("reg_password"), {
			required: true, checkFormat: "password", title: "Password", 
			message: "Please supply a password. Password must be at least 6 characters and alphanumeric."
		}, matched);
		
		//passsword repeat validation
		matched = App.val.validate($("reg_password2"), {
			required: true, checkFormat: "password", checkAgainst: $("reg_password").value, title: "Repeat Password", 
			message: "Repeat Password does not match."
		}, matched);
	} else if (mode == "edit_account"){
		if ($("reg_password").value.trim().length > 0){
			//password validation
			matched = App.val.validate($("reg_password"), {
				required: true, checkFormat: "password", title: "Password", 
				message: "Please supply a password. Password must be at <br />least 6 characters and alphanumeric."
			}, matched);
			
			//passsword repeat validation
			matched = App.val.validate($("reg_password2"), {
				required: true, checkFormat: "password", checkAgainst: $("reg_password").value, title: "Repeat Password", 
				message: "Repeat Password does not match."
			}, matched);
		} else {
			if ($("reg_password2").getParent()){ $("reg_password2").detachError(); }
		}
	}
	
	if (mode != "edit_account"){
		//first name validation
		matched = App.val.validate($("reg_fname"), {
			required: true, minLength: 2, title: "First Name", 
			message: "Please supply your first name."
		}, matched);
		
		//last name validation
		matched = App.val.validate($("reg_lname"), {
			required: true, minLength: 2, title: "Last Name", 
			message: "Please supply your last name."
		}, matched);
		
			
		//address 1 validation
		if ($("reg_phone")){
			if ($("reg_phone").value.trim().length > 0){
				matched = App.val.validate($("reg_phone"), {
					required: true, minLength: 10, checkFormat: "phone", title: "Phone", 
					message: "It seems to be an invalid number or format. <br />Please supply a valid phone number."
				}, matched);
			}
		}
		
			
		//address 1 validation
		matched = App.val.validate($("reg_address_1"), {
			required: true, minLength: 5, checkFormat: "alphanumeric", title: "Street Address", 
			message: "Please supply your home address. A valid <br />address should contain both numbers and letters."
		}, matched);
		
		//city validation
		matched = App.val.validate($("reg_city"), {
			required: true, minLength: 3, title: "City Name", 
			message: "Please supply your home city."
		}, matched);
			
		//state validation
		if ($("reg_country").value == "United States"){
			matched = App.val.validate($("reg_state"), {
				required: true,  minLength: 2, title: "State", 
				message: "US residents must supply a state name."
			}, matched);
		} else {
			App.val.clear($("reg_state"));
		}
			
		//zip validation
		matched = App.val.validate($("reg_zip"), {
			required: true, minLength: 4, title: "ZIP or Postal Code", 
			message: "Please supply your ZIP/postal code."
		}, matched);
			
		
		//country validation
		matched = App.val.validate($("reg_country"), {
			required: true, title: "Country", 
			message: "Please select your country."
		}, matched);
	}
	
	if (mode == "add"){
		if ($("reg_broker_yes").checked){
			//company name
			matched = App.val.validate($("reg_company"), {
				required: true, minLength: 5, title: "Company Name", 
				message: "Real estate agents must supply a company name."
			}, matched);
			
			//license no.
			matched = App.val.validate($("reg_license"), {
				required: true, minLength: 5, title: "License No.", 
				message: "Real estate agents must supply a license number."
			}, matched);
			
		}
		
		matched = App.val.validate($("captcha_input"), {
			required: true, title: "Human Verification", 
			message: "Please supply the text displayed in the image above."
		}, matched);
	} else {
		//company name
		if ($("reg_company")){
			matched = App.val.validate($("reg_company"), {
				required: true, minLength: 5, title: "Company Name", 
				message: "Real estate agents must supply a company name."
			}, matched);
		}
		
		//license no.
		if ($("reg_license")){
			matched = App.val.validate($("reg_license"), {
				required: true, minLength: 5, title: "License No.", 
				message: "Real estate agents must supply a license number."
			}, matched);
		}
	}
	
	if (mode == "add"){
		matched = App.val.validate($("reg_terms"), {
			required: true, checked: true, title: "Terms and Privacy", 
			message: "You must review and agree to our Terms and Privacy policy to register."
		}, matched);
	}
	
	return matched;
}