function set_popup_title(str)
{
	$('page_popup_text').update(str);
}

function set_popup_form(script)
{
	var url = BASE_URL+script;
	$('page_popup').down('form').action = url;
	return url;
}

function page_warning(pop_disp, vals)
{
	err_popup();
	pop_disp(vals);
	delay_popup();
}

function err_popup()
{
	reskin_lightbox('page_popup', 'red_alert');
	$('page_popup_restart_btn').hide();
	hide_help();
}

function std_popup()
{
	reskin_lightbox('page_popup', 'fade_box');
	show_restart();
	show_help();
}

function set_reg()
{
	std_popup();
	set_popup_title('Get your Listingbook account');
	return set_popup_form('cgi-bin/ajax_register.php');
}

function set_login()
{
	std_popup();
	set_popup_title('Log Into Your Listingbook Account');
	return set_popup_form('cgi-bin/ajax_login.php');
}

function show_restart()
{
	$('page_popup_restart_btn').show();
}

function hide_restart()
{
	// HACK - commenting out so that the user always has the option to 'Start Over'
	//$('page_popup_restart_btn').hide();
}

function show_help()
{
	$('page_popup_help_btn').show();
}

function hide_help()
{
	$('page_popup_help_btn').hide();
}

function redirect_lightbox(url)
{
	parent.location.href = url;
}

function show_page_popup(f)
{
	if(typeof f == 'undefined')
		f = function(){};

	$$('select').each(function(e){
		if(e.up('.page_popup_form'))
			return;
		e.style.visibility='hidden';
	});
	$('page_popup').show();
	$('page_popup').scrollTo();
	$('overlay').show();
	f();
}

function delay_popup(tmr)
{
	if(typeof tmr == 'undefined')
		tmr = 300;
	setTimeout(function(){show_page_popup()}, tmr);
}

function hide_login_lightbox(obj){hide_page_popup(obj)}
function hide_page_popup(obj)
{
	obj=$(obj);
	obj.up('.lightbox_container').hide();
	$('overlay').hide();
	$$('select').each(function(e){e.style.visibility='';});
}

var page_popup_submitted = false;
function submit_page_popup(btn)
{
	if(page_popup_submitted)
		return false;

	page_popup_submitted = true;
	btn=$(btn);
	var frm = btn.up('form');
	popup_update(function(){return frm.action}, frm.serialize());
	show_restart();
}

function reg_action(params){popup_update(set_reg, params)}
function login_action(params){popup_update(set_login, params)}
function popup_update(frm_act_fnc, params)
{
	show_throbber();

	new Ajax.Updater({success: "page_popup_form"}, frm_act_fnc(), {
		'method' : 'post',
		'parameters' : params,
		'evalScripts' : true,
	  'onFailure' : function(){
				$('page_popup_form').update('We apologize, a temporary error has occurred. Please <a onclick="reset_page_form();" class="clickable">Try Again</a>.');
				return false;
			},
		'onComplete' : function(){
				hide_throbber();
				page_popup_submitted = false;
			}
	});
}

function show_throbber(){$('page_popup_throbber').show()}
function hide_throbber(){$('page_popup_throbber').hide()}

function reset_page_form(extra)
{
	if(typeof extra != 'undefined')
		extra = '&'+extra;
	else
		extra = '';

	if(typeof restart_action != 'undefined')
		extra = restart_action;

	hide_restart();

	var frm = $('page_popup_form').up('form');
	popup_update(function(){return frm.action}, 'form_action=restart_form'+extra);
}

function reg_setup(find_agent, coming_soon, set_type)
{
	if(typeof set_type == 'undefined')
		set_type = 0;

	var obj = {
		'find_agent_url' : find_agent,
		'coming_soon_url' : coming_soon
	};

	if(set_type)
	{
		obj.user_type = set_type;
		obj.first_step = 1;
	}
	else
		delete restart_action;

	return obj;
}

function reg_loc()
{
	$('popup_city').value = curr_loc.city;
	$('state').value = curr_loc.state;
}

function reg_state(find_agent, coming_soon, set_type, restart)
{
	var obj = reg_setup(find_agent, coming_soon, set_type);
	if(restart)
		obj.form_action = 'restart_form';
	reg_action(obj);
	delay_popup();
}

function curr_reg_state(find_agent, coming_soon, set_type)
{
	reg_state(find_agent, coming_soon, set_type, 0);
}

function reg_start(find_agent, coming_soon, set_type)
{
	reg_state(find_agent, coming_soon, set_type, 1);
}

function reg_zip(zip, find_agent, coming_soon)
{
	reg_action({
		'user_type' : 'client',
		'zip_code' : zip,
		'find_agent_url' : find_agent,
		'coming_soon_url' : coming_soon,
		'first_step' : 1,
		'form_action' : 'restart_form'
	});
}

function reg_select_agent(uid, city, state)
{
	reg_action({
		'user_type' : 'client',
		'account_type' : 'agent',
		'agent_uid' : uid,
		'city' : city,
		'state' : state,
		'first_step' : 1
	});
	delay_popup();
}

function lost_pass(param)
{
	if(param == '' || param == 'mls=' || param == 'email=')
		return false;
	if(param == 'mls=unlisted_mls')
		mls_select_dropdown(arguments[1]);
	else
		new Ajax.Updater("page_popup_form", set_popup_form('cgi-bin/ajax_lost_password.php')+'?'+param, {evalScripts: true});
}

function lost_pass_process(email)
{
	var opts = {
		method : 'post',
		parameters : {
			'email' : email,
			'execute' : 1
		}
	};

	new Ajax.Request(set_popup_form('cgi-bin/ajax_lost_password.php'), opts);
}

function show_login_lightbox(f)
{
	if(typeof f == 'undefined')
		f = function(){
			login_action({
				'form_action' : 'startup'
			});
			delete restart_action;
		};

	f();
	delay_popup();
}

function submit_user_type(type, first_step)
{
	if(typeof first_step == 'undefined')
		first_step = 0;

	login_action({
		'form_action' : 'submit_user_type',
		'user_type' : type,
		'first_step' : first_step
	});
}

// login_id and password are the names of the input fields that we need
// extra can be PIN for safemls or login type in triad
function submit_login(login_id, password, pin)
{
	login_id = $(login_id).value;
	password = $(password).value;
	if($(pin))
		pin = $(pin).value;

	login_action({
		'form_action' : 'submit_login',
		'login_uid' : login_id,
		'password' : password,
		'pin' : pin
	});
}

// verification can be a password or an email
function submit_activation(login_id, verification)
{
	login_id = $(login_id).value;
	verification = $(verification).value;

	login_action({
		'form_action' : 'submit_activation',
		'login_uid' : login_id,
		'verification' : verification
	});
}

// currently assumes that only agents will be using this
function submit_state(state)
{
	if(typeof state == 'object')
		state = $(state).value;

	var rst = (typeof restart_action == 'undefined') ? 0 : 1;

	login_action({
		'form_action' : 'submit_state',
		'selected_state' : state,
		'restart_action' : rst
	});
}

// currently assumes that only agents will be using this
function submit_mls(mls)
{
	var rst = (typeof restart_action == 'undefined') ? 0 : 1;
	if(typeof mls == 'object')
		mls = $(mls).value;
	if(mls == 'unavaliable')
		alert('sorry');
	else
		login_action({
			'form_action' : 'submit_mls',
			'selected_mls' : mls,
			'restart_action' : rst
		});
}

function submit_login_type(login_type)
{
	login_type = $(login_type).value;
	login_action({
		'form_action' : 'submit_login_type',
		'selected_login_type' : login_type
	});
}

function show_activation_form()
{
	login_action({
		'form_action' : 'show_activation_form'
	});
}

function hide_activation_form()
{
	login_action({
		'form_action' : 'hide_activation_form'
	});
}

function show_forget_form()
{
	login_action({
		'form_action' : 'show_forget_form'
	});
}

function lb_debug(lbl, val)
{
	try
	{
		var d = $('debug');
		d.show();
		d.insert('<span class="debug_label">'+lbl+'</span>: '+val+'<br />');
	}
	catch(e){}
}

function reskin_lightbox(id, css)
{
	var tbl = $(id).down('table');
	var curr_css = tbl.down('td').classNames().toArray();
	curr_css = curr_css[0].slice(0, curr_css[0].length-3);

	if(curr_css == css)
		return;

	for(var i=0;i<tbl.rows.length;i++)
	{
		var row = tbl.rows[i];
		for(var j=0;j<row.cells.length;j++)
		{
			var cell = $(row.cells[j]);
			cell.classNames().each(function(n){
				cell.removeClassName(n);
				cell.addClassName(n.replace(curr_css, css));
			});
		}
	}
}
