facebook_connect_refresh_in_progress = false;
ajax_loader = '';
ajax_loader_big = '';

var sso_enabled = (typeof sso_login == "function") 
		  && ((typeof sso_domain != "undefined") && (sso_domain != ""));
var sso_data = "";

// for test
if ( sso_enabled ) {
	sso_enabled = (Cookie.get('sso_enabled') == "1");
}

registration_script_url = '/bin/registration2/';

window.addEvent('domready', function()
{
	if ( sso_enabled ) {
		sso_start();
	} else {
		registration_popups();
	}
});


function registration_popups()
{
	step_3();
	password_renewal_show();
	notice_window_show();

	var username = Cookie.get('user');

	// open login pop-up from external links, but only if user is not already loggedin
	if( !username && String( window.location ).contains( '#login' ))
		login_show();

	if ( !username && String( window.location ).contains( '#register' ))
		register_show();
}

/**
 * Function displays login pop-up
 *
 */
function login_show( params )
{
    // list of errors
    error_step_1_email_forgot_password = '<br /><a href="#" onclick="password_renewal_display(); return false;">%1</a>?';
    error_24urcard_1 = "<br />Gesli se ne ujemata.";
    error_24urcard_2 = "<br />Napačno uporabniško ime.";
    error_24urcard_3 = "<br />Napačna kartica.";
    error_24urcard_4 = "<br />Napačno uporabniško ime ali geslo.";
    error_24urcard_5 = "<br />Napaka pri aktivaciji kartice.";
    error_24urcard_6 = "<br />Novo geslo mora imeti vsaj 4 znake.";
    error_24urcard_7 = "<br />Kartica je že porabljena.";
    error_24urcard_8 = "<br />Napaka pri aktivaciji kartice.";
    error_24urcard_9 = "<br />Nepravilen email naslov.";

    close_banner();

    mbox_open(450, 500, true);
    pre_fullscreen();

    step_1_help = new Array("main_help");
    active_step = 1;
    help_displayed = 0;

    args = params ? 'payable=' + '1' : '';
    payable = params ? 1 : null;

    new Ajax('/bin/registration2/login.php?' + args, {
	     method: 'get',
	     encoding: 'utf-8',
	     update: $('MBOX_window'),
             evalScripts: true,
	     onComplete: login_show_complete
	}).request();
}


function login_show_complete( return_status )
{
   if($('ajax_loader_big_div'))
       ajax_loader_big = $('ajax_loader_big_div').innerHTML;

    if( 1 == payable )
    {
	if(( 'undefined' !== typeof bb_login_profile ) && (( 'registered' == bb_login_profile )))
	{
	    step_1p_display(1, 2);
	    bb_login_profile = null;
	}

	step_1p_help = new Array("main_help");
	step_1r_help = step_1p_help;
	step_2p_help = step_1p_help;
	step_3p_help = step_1p_help;
	step_4p_help = step_1p_help;
	step_5p_help = step_1p_help;

	build_indicators( active_step );
    }
}


/**
 * Function displays step 1 of registration pop-up
 *
 */
function forgot_pw_show()
{
    close_banner();

    mbox_open(450, 500, true);
    pre_fullscreen();

    // help boxes in step 1
    step_1_help = new Array("username_help", "password_help", "email_help", "main_help");

    // how many steps are there
    all_steps = 3;
    active_step = 1;
	
    // help box visibility status
    help_displayed = false;

    password_renewal = false;

    url = '/bin/registration2/' + '?action=display' + '&what=1' + '&return=' + location.href;
    new Ajax(url, {
	     method: 'get',
	     encoding: 'utf-8',
	     update: $('MBOX_window'),
	     onComplete: forgot_pw_show_complete,
	     evalScripts: true
	    }).request();
}

function forgot_pw_show_complete()
{
	active_step = $('active_step').value;

    	// get HTML code of ajax loader
    	if ($('ajax_loader_div')) {
		ajax_loader = $('ajax_loader_div').innerHTML;
	}
   
	if ($('ajax_loader_big_div')) {
		ajax_loader_big = $('ajax_loader_big_div').innerHTML;
	}

	build_indicators( $('active_step').value );
	$('user_name').focus();

    password_renewal_display();

}

/**
 * Function displays step 1 of registration pop-up
 *
 */
function register_show()
{
    close_banner();

    mbox_open(450, 500, true);
    pre_fullscreen();

    // help boxes in step 1
    step_1_help = new Array("username_help", "password_help", "email_help", "main_help");

    // how many steps are there
    all_steps = 3;
    active_step = 1;
	
    // help box visibility status
    help_displayed = false;

    password_renewal = false;

    url = '/bin/registration2/' + '?action=display' + '&what=1' + '&return=' + location.href;
    new Ajax(url, {
	     method: 'get',
	     encoding: 'utf-8',
	     update: $('MBOX_window'),
	     onComplete: register_show_complete,
	     evalScripts: true
	    }).request();
}


function register_show_complete( result )
{
	active_step = $('active_step').value;

    	// get HTML code of ajax loader
    	if ($('ajax_loader_div')) {
		ajax_loader = $('ajax_loader_div').innerHTML;
	}
   
	if ($('ajax_loader_big_div')) {
		ajax_loader_big = $('ajax_loader_big_div').innerHTML;
	}

	build_indicators( $('active_step').value );
	$('user_name').focus();
}


/**
 * Hide/Display help 
 *
 * @author Matej Balantič <matej.balantic@pop-tv.si>
 */
function help_toggle( which )
{
	if(which)
	{
		if ($(which).style.display == 'block')
			$(which).style.display='none';
		else
			$(which).style.display='block';

		return true;
	}

	for (i=0;i<eval('step_'+active_step+'_help').length;i++)
	{
		which = eval('step_'+active_step+'_help['+i+']');

		if( !$('smsnews') && 'sms_help' == which )
		    break;

		if( help_displayed )
			$(which).style.display = 'none';
		else
		        $(which).style.display = 'block';
	}
	
	if (help_displayed)
		help_displayed = false;
	else
		help_displayed = true;

}


/**
 * Navigate to the specific step & update current one
 * Before doing that, function checks if update was completed
 * 
 * @author Matej Balantič <matej.balantic@pop-tv.si>
 */
function navigate_to_step(step)
{
        previous_step = active_step;

        eval ('ret = step_' + active_step + '_error_check()');

	if ( !ret )
		return false;

	// navigating back
	if( step < active_step )
	{
	    step_display(step);
	    return;
	}

	// insert ajax loader into feedback field
	if($('step_feedback'))
	    $('step_feedback').innerHTML = ajax_loader_big;

	$('step_form').action = registration_script_url+'?action=update&what='+active_step+'&return='+location.href;
	$('step_form').send({
		onComplete: function( return_status )
		{
			level = return_status.split('_');
			if (level[0] != "success")
			{
			    if($('step_feedback'))
			    {
				$('step_feedback').innerHTML = eval( return_status );
				$('step_feedback').className = 'warning';
			    }
			    return false;
			}

			if($('step_feedback'))
			{
			    $('step_feedback').className = '';
			    $('step_feedback').innerHTML = '';
			}
			step_display(step);
		}
	});
	//	return false;
}


/**
 * End navigation
 */
function navigate_close()
{
        eval ('ret = step_' + active_step + '_error_check()');
	if ( !ret )
		return false;

	new Ajax('/bin/registration2/' + '?action=close' + '&what=registration', {
		method: 'get',
		encoding: 'utf-8',
		onComplete: navigate_close_complete
		}).request();
}


/**
 * Function closes navidation
 */
function navigate_close_complete( val )
{
	open_banner();
	mbox_close();
}


/**
 * Function checks if new password is valid and matches
 * 
 */
function new_password_check(check_empty)
{
    // check if field is empty
    if (check_empty && (!$('password').value.length || !$('password_repeat').value.length))
    {
	$('password_feedback').className = 'feedback_error';
	$('password_feedback').innerHTML = error_step_1_password_empty;
	return false;
    }

    // there is no password; don't react
    else if (!$('password').value && !$('password_repeat').value)
    {
	$('password_feedback').className='';
	$('password_feedback').innerHTML = '';
	return false;
    }

    // password and repeated password don't match
    else if ($('password').value != $('password_repeat').value)
    {
	$('password_feedback').innerHTML = error_step_1_passwords_different;
	$('password_feedback').className = 'feedback_error';
	return false;
    }
    else if ($('password').value.length < 5)
    {
	$('password_feedback').innerHTML = error_step_1_passwords_too_short;
	$('password_feedback').className = 'feedback_error';
	return false;
    }

    else if ($('password').value.contains($('user_name').value))
    {
	    $('password_feedback').className = 'feedback_error';
	    $('password_feedback').innerHTML = error_pwd_chg_unsafe;
	    return false;
    }

    else if ($('user_name').value.contains($('password').value))
    {
	    $('password_feedback').className = 'feedback_error';
	    $('password_feedback').innerHTML = error_pwd_chg_unsafe;
	    return false;
    }

    // password is valid and matches
    else if(( 'undefined' !== typeof renewing_password ) && true == renewing_password )
    {
	$('password_feedback').className = 'feedback_success';
	$('password_feedback').innerHTML = eval( 'success_step_1_passwords_ok' );
	return true;
    }

    return true;
}



/**
 * Function logs in user and optionally opens settings pop-up
 *
 */
function login()
{
    settings = '&settings=0';

    if(( typeof settings_link != 'undefined' ) && ( settings_link == true ))
	settings = '&settings=1';

    new Ajax('/bin/registration2/login.php?action=login' + settings, {
	        method: 'post',
		encoding: 'utf-8',
		postBody: 'username=' + encodeURIComponent($('user_name').value) + '&password=' + encodeURIComponent($('password').value),
		onComplete: login_complete,
		evalScripts: true
	}).request();
}


function start24urcard() {
    settings = '&settings=0';

    if(( typeof settings_link != 'undefined' ) && ( settings_link == true ))
	settings = '&settings=1';

    new Ajax('/bin/registration2/login.php?action=testcard' + settings, {
	        method: 'post',
		encoding: 'utf-8',
		postBody: 'username=' + encodeURIComponent($('user_name').value) + '&password=' + encodeURIComponent($('password').value) + '&email=' + encodeURIComponent($('email').value),
		onComplete: login_complete,
		evalScripts: true
	}).request();
}


function activate24urcard()
{
    settings = '&settings=0';
if(( typeof settings_link != 'undefined' ) && ( settings_link == true ))
	settings = '&settings=1';

    new Ajax('/bin/registration2/login.php?action=logincard' + settings, {
	        method: 'post',
		encoding: 'utf-8',
		postBody: 'username=' + encodeURIComponent($('user_name').value) + '&password=' + encodeURIComponent($('password').value) + '&pass1=' + encodeURIComponent($('pass1').value) + '&pass2=' + encodeURIComponent($('pass2').value) + '&email=' + encodeURIComponent($('email').value),
		onComplete: login_complete,
		evalScripts: true
	}).request();
}


/**
 * Function displays settings pop-up if show_settings is set
 * or displays login status otherwise
 *
 */
function login_complete( return_status )
{
        if(( typeof show_settings != 'undefined' ) && ( true == show_settings ))
        {
	    $('MBOX_window').innerHTML = return_status;
	    settings_link = false;

	    if( $('gsmnum_save') )
		$('gsmnum_save').style.display = 'none';

	    // hide gsm form if SMS option unchecked
	    if( $('smsnews') && ( false == $('smsnews').checked ))
		$('gsmnum_verify').style.display = 'none';

	    $('button_close').className = 'close_button';

	    return false;
	}

	if( return_status )
	{
		level = return_status.split('_');

		$('login_feedback').className = 'feedback_' + level[0];

		if( 'success' == level[0] )
		{
			$('login_feedback').innerHTML = success_logged_in;
			$('login_feedback').style.display = '';

			// display payable step 2 if payed access is in process
			if ('activate' == level[1]) {
				step_5p_success();
				return;
			}
			if( 1 == payable )
			{
				step_2p();
				return;
			}

			$('div_login_button').style.display = 'none';
			$('div_settings_button').style.display = '';
			$('div_login_close_button').style.display = '';

			if (sso_enabled)
				sso_login(level[4]);

			if( 'undefined' !== typeof reload_user_info )
				reload_user_info();

			if ( 'undefined' !== typeof redirect_on_login_logout )
				window.location.href = redirect_on_login_logout;

			if ( 'undefined' !== typeof reload_on_login_logout )
				window.location.reload();

			if('undefined' !== typeof set_hasVoted)
				set_hasVoted();
		}
		else if ('valid' == level[0]) {
			$('div_24urcard').style.display = '';
			$('login_feedback').style.display = 'none';
			if ('4p' != active_step) {
				$('button_login').removeEvents();
				$('button_login').addEvent('click', activate24urcard);
			}
		} else {
			if ('1' == level[1]) $('login_feedback').innerHTML = error_24urcard_1;
			else if ('2' == level[1]) $('login_feedback').innerHTML = error_24urcard_2;
			else if ('3' == level[1]) $('login_feedback').innerHTML = error_24urcard_3;
			else if ('4' == level[1]) $('login_feedback').innerHTML = error_24urcard_4;
			else if ('5' == level[1]) $('login_feedback').innerHTML = error_24urcard_5;
			else if ('6' == level[1]) $('login_feedback').innerHTML = error_24urcard_6;
			else if ('7' == level[1]) $('login_feedback').innerHTML = error_24urcard_7;
			else if ('8' == level[1]) $('login_feedback').innerHTML = error_24urcard_8;
			else if ('9' == level[1]) $('login_feedback').innerHTML = error_24urcard_9;
			else $('login_feedback').innerHTML = error_step_1_email_forgot_password;

		    if($('div_login_button'))
			$('div_login_button').style.display = '';

			$('login_feedback').style.display = '';

		    if($('div_settings_button'))
			$('div_settings_button').style.display = 'none';
		}
	}
}


/**
 * Process step 1 
 * 
 * @author Matej Balantic <matej.balantic@pop-tv.si>
 */
function step_1()
{
	url = registration_script_url+'?action=display&what=1';
	new Ajax(url, {
		method: 'get',
	        encoding: 'utf-8',
		onComplete: step_on_update
	}).request();
}


/**
 * STEP 1: Check all field for errors and continue to step 2 if all is OK
 * 
 * @author Matej Balantic <matej.balantic@pop-tv.si>
 */
function step_1_error_check()
{
	if( $('general_terms') ) {
	    if ( !step_1_terms_check() )
	    	return false;
    }
	
	if ( typeof( window[ 'step_1_gender_selection_mandatory' ] ) != "undefined" ) {
	    if(step_1_gender_selection_mandatory == true) {
	    	if ( !step_1_gender_check() )
		    	return false;
	    }
	}
	
    if ( !step_1_user_name_check(true) )
    	return false;
    if ( !step_1_password_check(true) )
    	return false;
    if ( !step_1_sex_check() )
    	return false;
    if ( !step_1_bday_check() )
    	return false;
    if ( !step_1_email_check(true) )
    	return false;

    return true;
}

/**
 * STEP 1: Check Terms of use confirmation
 * 
 * @author Kristijan Tkalčec
 */
function step_1_terms_check()
{
	if( !$('general_terms').checked ) {
		$( 'general_terms_feedback' ).innerHTML = error_step_1_terms_unconfirmed;
        $( 'general_terms_feedback' ).className = 'feedback_error';
		return false;
	}
	else {
		$( 'general_terms_feedback' ).innerHTML = '';
        $( 'general_terms_feedback' ).className = '';
		return true;
	}
}

/**
 * STEP 1: Check gender selection
 * 
 * @author Kristijan Tkalčec
 */
function step_1_gender_check()
{
	if( $('male').checked || $('female').checked ) {
		$( 'gender_select_feedback' ).innerHTML = '';
        $( 'gender_select_feedback' ).className = '';
		return true;
	}
	else {
		$( 'gender_select_feedback' ).innerHTML = error_step_1_gender_unselected;		
        $( 'gender_select_feedback' ).className = 'feedback_error';
		return false;

	}
}

/**
 * STEP 1: Check user name availability 
 * 
 * @author Matej Balantic <matej.balantic@pop-tv.si>
 */
function step_1_user_name_check(check_empty)
{
	// check if field is empty
	if (check_empty && !$('user_name').value.length)
	{
	        $('user_name_feedback').innerHTML = error_step_1_user_name_empty;
	        $('user_name_feedback').className = 'feedback_error';
		return false;
	}

	// there is no user name; don't react
	else if (!$('user_name').value)
	{
		$('user_name_feedback').className = '';
		$('user_name_feedback').innerHTML = '';
		return false;
	}

	// reset all error indicators
	$('user_name_feedback').innerHTML = ajax_loader;
	$('user_name_feedback').className = '';

	url = registration_script_url+'?action=check-username';
	new Ajax(url, {
		method: 'post',
		encoding: 'utf-8',
		postBody: 'what=' + encodeURIComponent($('user_name').value),
		onComplete: step_1_user_name_check_complete
	}).request();

	return true;
}

/**
 * STEP 1: AJAX feedback for user nama avaliability check
 * 
 * @author Matej Balantic <matej.balantic@pop-tv.si>
 */
function step_1_user_name_check_complete(return_status)
{
	if( return_status )
	{
		level = return_status.split('_');
		$('user_name_feedback').className = 'feedback_' + level[0];
		$('user_name_feedback').innerHTML = eval(return_status);
	}
	else
	{
		 $('user_name_feedback').className = '';
		 $('user_name_feedback').innerHTML = '';
	}
}

/**
 * STEP 1: Check e-mail avaliability & if it is valid 
 * 
 * @author Matej Balantic <matej.balantic@pop-tv.si>
 */
function step_1_email_check(check_empty)
{
    // check if field is empty
    if (check_empty && !$('email_r').value.length)
    {
	$('email_feedback').className = 'feedback_error';
	$('email_feedback').innerHTML = error_step_1_email_empty;
	return false;
    }

    // there is no email; don't react
    else if (!$('email_r').value)
    {
	$('email_feedback').innerHTML = '';
	$('email_feedback').className='';
	return false;
    }

    // reset all error indicators
    $('email_feedback').innerHTML = ajax_loader;
    $('email_feedback').className = '';
		 
    url = registration_script_url+'?action=check-email';
	
    new Ajax(url, {
	     method: 'post',
	     encoding: 'utf-8',
	     postBody: 'what='+encodeURIComponent($('email_r').value),
	     onComplete: step_1_email_check_complete
	    }).request();
    return true;
}

/**
 * STEP 1: AJAX feedback from e-mail avaliability check 
 * 
 * @author Matej Balantic <matej.balantic@pop-tv.si>
 */
function step_1_email_check_complete( return_status )
{
	if( return_status )
	{
	    level = return_status.split('_');
	    $('email_feedback').className = 'feedback_' + level[0];
	    $('email_feedback').innerHTML = eval(return_status);
	}

	else
	{
	    $('email_feedback').className = '';
	    $('email_feedback').innerHTML = '';
	}
}

/**
 * STEP 1: check birthday
 */
function step_1_bday_check()
{
	if ( !$("bday_div") )
		return true;
		
	// no birthday given?
	if ( ($("bday_day").value == "") || ($("bday_month").value == "") || ($("bday_year").value == "") ) {
		$('bday_feedback').className = 'feedback_error';
		$('bday_feedback').innerHTML = error_step_1_bday_empty;
		return false;
	}
		
	// wrong day (like 31.4. ??)
	if (($("bday_month").value == "4") || ($("bday_month").value == "6") || 
	    ($("bday_month").value == "9") || ($("bday_month").value == "11")) {
		if ($("bday_day").value == "31") {
			$('bday_feedback').className = 'feedback_error';
			$('bday_feedback').innerHTML = error_step_1_bday_wrong_day;
			return false;
		}
	}
		
	// wrong day (like 30.2. ??)
	if ($("bday_month").value == "2") {
		if (($("bday_day").value == "30") || ($("bday_day").value == "31")) {
			$('bday_feedback').className = 'feedback_error';
			$('bday_feedback').innerHTML = error_step_1_bday_wrong_day;
			return false;
		}
	}

	$('bday_feedback').innerHTML = "";
		
	return true;
}

/**
 * STEP 1: check sex
 */
function step_1_sex_check()
{
	if ( !$("sex_div") )
		return true;
		
	if ( !$("sex_M").checked && !$("sex_F").checked ) {
		$('sex_feedback').className = 'feedback_error';
		$('sex_feedback').innerHTML = error_step_1_sex_empty;
		return false;
	}
		
	$('sex_feedback').innerHTML = "";

	return true;
}

/**
 * STEP 1: Check if password is valid and matches. 
 * 
 * @author Matej Balantic <matej.balantic@pop-tv.si>
 */
function step_1_password_check(check_empty)
{
	// check if field is empty
	if (check_empty && (!$('password').value.length || !$('password_repeat').value.length))
	{
		$('password_feedback').className = 'feedback_error';
		$('password_feedback').innerHTML = error_step_1_password_empty
		return false;
	}

	// there is no password; don't react
	else if (!$('password_repeat').value)
	{
		$('password_feedback').className='';
		$('password_feedback').innerHTML = '';
		return false;
	}

	// password and repeated password don't match
	else if ($('password').value != $('password_repeat').value)
	{
		$('password_feedback').className = 'feedback_error';
		$('password_feedback').innerHTML = error_step_1_passwords_different;
	}

	else if ($('password').value.length < 5)
	{
		$('password_feedback').className = 'feedback_error';
		$('password_feedback').innerHTML = error_step_1_passwords_too_short;
		return false;
	}

	else if ($('password').value.contains($('user_name').value))
	{
		$('password_feedback').className = 'feedback_error';
		$('password_feedback').innerHTML = error_pwd_chg_unsafe;
		return false;
	}

	else if ($('user_name').value.contains($('password').value))
	{
		$('password_feedback').className = 'feedback_error';
		$('password_feedback').innerHTML = error_pwd_chg_unsafe;
		return false;
	}

	// password is valid and matches
	else
	{
		$('password_feedback').className = 'feedback_success';
		$('password_feedback').innerHTML = success_step_1_passwords_ok;
	} 
	
	return true;
}

/**
 * Function displays STEP 2
 *
 */
function step_2()
{
	url = registration_script_url+'?action=display&what=2';
	new Ajax(url, {
		method: 'get',
		update: $('step'),
		onComplete: step_2_complete
	}).request();

}

function step_2_error_check()
{
	return true;
}

function step_3_error_check()
{
	return true;
}

function step_2_complete()
{
    $('email_sent').innerHTML = email_sent;
    $('email_sent2').innerHTML = email_sent2;
}


/**
 * Function updates process steps in footer
 * depending on update status
 *
 * @param string update status
 */
function step_on_update(update)
{
	update_status = update.split("_");
	if (update_status[0] == "error")
	{
		$('step').innerHTML = eval(update);
		build_indicators(-1, false);
	}

	else
	{
	    $('registration_box').innerHTML = update;
	    build_indicators(active_step);
	}

	if('undefined' != typeof payable && ( 1 == payable ) && ( '1r' == active_step ))
	    $('div_back_button').style.display = 'block';


	if( '4p' == active_step )
	{
 	    $("MBOX_window").setStyles({"width": '800px'});
 	    $("MBOX_dragger").setStyles({"width": '800px'});
	}

	reload_user_info();
}


/**
 * Activates a step
 * 
 * @author Matej Balantic <matej.balantic@pop-tv.si>
 */
function step_display(current_step)
{
	// activate indicator for this step
        build_indicators(current_step);

	// insert ajax loader into container
	$('step').innerHTML = ajax_loader_big;

	// execute step scripts
	eval ( 'step_' + current_step + '();' );

	active_step = current_step;

	if( 1 == current_step && $('username') )
	    $('user_name').focus();
}


/**
 * Function renders process indicators
 *
 * @param integers current step
 * @param boolean should steps be clickable or not
 */
function build_indicators(step, clickable_steps)
{
    if( 'undefined' == typeof clickable_steps )
	clickable_steps = true;

        html_indicators = '';

	for (i=0; i<steps.length; i++)
	{
	    num_step = parseInt( step );

	    nb = i + 1;

		// past steps
		if (nb<num_step)
		{
		    if( clickable_steps && steps[i][0] )
			html_indicators = html_indicators + '<div class="quad_pass"><a title="'+steps[i][1]+'" href="#" onclick="navigate_to_step(\''+ steps[i][2] +'\'); return false;">'+ nb +'</a></div>';
		    else
			html_indicators = html_indicators + '<div class="quad_pass">' + nb + '</div>';
		}

		// current step
		else if (nb==num_step)
		{
			html_indicators = html_indicators + '<div class="quad_active">'+ nb +'</div>';
			html_indicators = html_indicators + '<div class="text">' + steps[i][1] + '</div>';
		}		
	
		// next step
		else if (nb == num_step+1)
		{
		    if( clickable_steps && steps[i][0] )
			html_indicators = html_indicators + '<div class="quad_next"><a title="'+steps[i][1]+'" href="#" onclick="navigate_to_step(\''+ steps[i][2] +'\'); return false;">'+ nb +'</a></div>';
		    else
			html_indicators = html_indicators + '<div class="quad_next">'+ nb +'</div>';
		}

		// future steps
		else 
		    html_indicators = html_indicators + '<div class="quad_next">'+ nb +'</div>';
	}

	$('indicators').innerHTML = html_indicators;
}




/**
 * Function displays password renewal form
 * 
 */
function password_renewal_display()
{
    step_1_help   = new Array("main_help", "email_help");
    step_set_help = new Array("main_help", "email_help");

    if($("reg_email"))
	$("reg_email").innerHTML = $("renewal_email").innerHTML;

    password_renewal = true;

    $('help').innerHTML = header_help2;

    if($('email_help'))
	$('email_help').style.display = 'none';

    if($('back_button'))
	$('back_button').className = 'button_email';

    if($('button_close'))
	$('button_close').className = 'hidden';

    new Ajax(script_url+'?action=password_renewal_form', {
	        method: 'get',
		encoding: 'utf-8',
		update: $('step_1')
	    }).request();

    $('indicators').style.display = 'none';
}


/**
 * STEP 1: Check if email is valid 
 * 
 * @author Matej Balantic <matej.balantic@pop-tv.si>
 */
function password_renewal_email_check(check_empty)
{
    // check if field is empty
    if (check_empty && !$('email_r').value.length)
    {
	$('email_feedback').className = 'feedback_error';
	$('email_feedback').innerHTML = error_step_1_email_empty;
	return false;
    }

    if (!$('email_r').value)
    {
	$('email_feedback').innerHTML = error_step_1_email_empty;
	$('email_feedback').className='feedback_error';
	return false;
    }

    url = registration_script_url+'?action=check-email-renewal';

    new Ajax(url, {
	     method: 'post',
	     encoding: 'utf-8',
	     postBody: 'what='+encodeURIComponent($('email_r').value),
	     onComplete: password_renewal_email_check_complete
	    }).request();
    return true;
}

/**
 * STEP 1: AJAX feedback from e-mail avaliability check 
 */
function password_renewal_email_check_complete(return_status)
{
	if (return_status)
	{
	    level = return_status.split('_');
	    //$('email_r').className = 'field_' + level[0];
	    $('email_feedback').className = 'feedback_' + level[0];
	    $('email_feedback').innerHTML = eval(return_status);
	}

	else
	{
	    //$('email_r').className = '';
	    $('email_feedback').className = '';
	    $('email_feedback').innerHTML = '';
	}
}

/**
 * Processes supplied email for password renewal
 * 
 */
function password_renew()
{
	url = registration_script_url+'?action=password-renewal-email' + '&section_id=' + section_id + '&return=' + location.href;
	new Ajax(url, {
		method: 'post',
		encoding: 'utf-8',
		postBody: 'what='+encodeURIComponent($('email_r').value),
		onComplete: password_renewal_complete
	}).request();

	$('email_feedback').style.display = 'block';
}


/**
 * Function displays result message after email entered
 * 
 */
function password_renewal_complete( return_status )
{
    $('email_feedback').className = '';
    $('email_feedback').innerHTML = '';

    if( return_status )
    {
	level = return_status.split('_');
	$('email_feedback').className = 'feedback_' + level[0];

	if( 'success' == level[0] )
	{
		$('password_renewal_button').style.display = 'none';
		$('div_go_to_24ur_button').style.display = 'block';
	}

	$('email_feedback').innerHTML = eval( return_status );
    }
}


/**
 * Function submits passwords
 * and updates result message
 * 
 */
function save_password()
{
    $('renewal_form').action = registration_script_url + '?action=renew-password';
    $('renewal_form').send( {
	    onComplete: function( return_status )
	    {
		level = return_status.split('_');
		$('password_feedback').className = 'feedback_' + level[0];
		$('password_feedback').innerHTML = eval( return_status );

		if( 'success' == level[0] )
		{
		    $('button_pwd_save').style.display = 'none';
		    $('div_settings_button').style.display = 'block';
		    $('div_go_to_24ur_button').style.display = 'block';
		}
	    }
     });
}


/**
 * Function displays password renewal pop-up
 * 
 */
function password_renewal_show()
{
    cookieVal = Cookie.get('pr_pending');

    if( cookieVal )
    {
            close_banner();
	    mbox_open(450, 700);

	    new Ajax('/bin/registration2/?action=renewal', {
	             method: 'get',
		     encoding: 'utf-8',
                     update: $('MBOX_window'),
		     onComplete: pr_cookie_check_complete,
		     evalScripts: true
	            }).request();
    }
}


function pr_cookie_check_complete( return_status )
{
        level = return_status.split('_');

        if( 'error' == level[0] )
	{
	    mbox_close();
	    open_banner();
	    return false;
	}

	step_pwdr_help = new Array("main_help");
	active_step = 'pwdr';
	help_displayed = false;

	renewing_password = true;

	if($('password'))
	    $('password').focus();

	if( 'undefined' !== typeof reload_user_info )
	    reload_user_info();
}


/**
 * Function displays notice window
 * 
 */
function notice_window_show()
{
    cookieVal = Cookie.get('reg2_notice');

    if( cookieVal )
    {
            close_banner();
	    mbox_open(450, 700);

	    new Ajax('/bin/registration2/?action=notice_window&what='+cookieVal, {
	             method: 'get',
		     encoding: 'utf-8',
		     update: $('MBOX_window')
	            }).request();
    }
}


/**
 * Function displays registration step 3
 * 
 */
function step_3()
{
    cookieVal = Cookie.get('reg2_complete');

    if( cookieVal )
    {
	close_banner();
	mbox_open(450, 500);
	pre_fullscreen();

	// how many steps are there
	all_steps = 3;

	url = '/bin/registration2/' + '?action=display' + '&what=3' + '&return=' + location.href;
	new Ajax(url, {
		method: 'get',
		    encoding: 'utf-8',
		    update: $('MBOX_window'),
		    onComplete: step_3_complete,
		    evalScripts: true
		    }).request();
    }
}


function step_3_complete()
{
    step_3_help = new Array("main_help");
    help_displayed = 0;
    active_step = $('active_step').value;
    build_indicators( active_step, false );
    $('help').innerHTML = head_links;
}


/**
 * Display Payable step 1
 */
function step_1p( registered )
{
    login_show(1);
}


/**
 * Unhide login form in payable step 1
 */
function step_1p_display( unhide, hide )
{
    $('question_buttons').innerHTML = '';
    $('payable_login_' + hide).innerHTML = '';
    $('payable_login_' + unhide).style.display = 'block';
    $('buttons_bottom').style.display = 'block';
    $('div_forgot_password').style.display = '';

    // user already registered
    if( 1 == unhide )
    {
	active_step = '1r';
	$('button_login').addEvent('click', login);
	$('instructions1').style.display = 'none';
	$('instructions2').style.display = '';
    }

    // new user
    if( 2 == unhide )
    {
	active_step = '1p';
	$('button_login').addEvent('click', function(){navigate_to_step('2p');});
	$('instructions1').style.display = 'none';
	$('instructions3').style.display = '';
    }
}


function step_1p_display_complete( return_status )
{
    $('user_name').value = return_status;
}


/**
 * Check for errors in payable step 1
 */
function step_1p_error_check()
{
    return new_password_check(true);
}


/**
 * Display payable step 2
 */
function step_2p()
{
    url = registration_script_url+'?action=display&what=2p&current=' + active_step;
    active_step = '2p';

    new Ajax(url, {
	    method: 'get',
	    encoding: 'utf-8',
	    evalScripts: true,
	    onComplete: step_on_update
	}).request();
}


/**
 * Check for errors in payable step 1
 */
function step_2p_error_check()
{
    return true;
}


/**
 * Display forward button in payable step 2
 */
function step_2p_button_display()
{
    $('div_forward_button').style.display = 'block';
}


/**
 * Display payable step 3
 */
function step_3p()
{
   if($('ajax_loader_big_div'))
       ajax_loader_big = $('ajax_loader_big_div').innerHTML;

    url = registration_script_url+'?action=display&what=3p&current=' + active_step;
    active_step = '3p';

    new Ajax(url, {
	    method: 'get',
	    encoding: 'utf-8',
	    evalScripts: true,
	    onComplete: step_on_update
	}).request();
}


/**
 * Check for errors in payable step 1
 */
function step_3p_error_check()
{
    return true;
}


/**
 * Display payable step 4
 */
function step_4p()
{
    url = registration_script_url+'?action=display&what=4p&current=' + active_step;
    active_step = '4p';

    new Ajax(url, {
	    method: 'get',
	    encoding: 'utf-8',
	    evalScripts: true,
	    onComplete: step_on_update
	}).request();
}


/**
 * Check for errors in payable step 4
 */
function step_4p_error_check()
{
    return true;
}


/**
 * Display payable step 5 on success
 */
function step_5p_success()
{
    url = registration_script_url+'?action=display&what=5p_success&current=' + active_step;
    active_step = '5p';

    new Ajax(url, {
	    method: 'get',
	    encoding: 'utf-8',
	    evalScripts: true,
	    onComplete: step_on_update
	}).request();
}


/**
 * Display payable step 5 on failure
 */
function step_5p_failure()
{
    url = registration_script_url+'?action=display&what=5p_failure&current=' + active_step;
    active_step = '5p';

    new Ajax(url, {
	    method: 'get',
	    encoding: 'utf-8',
	    evalScripts: true,
	    onComplete: step_on_update
	}).request();
}


/**
 * Check for errors in payable step 5
 */
function step_5p_error_check()
{
    return true;
}


/**
 * Check for errors in payable step 4
 */
function step_1r_error_check()
{
    return true;
}


/**
 * Log out user
 */
function logout()
{
    new Ajax('/bin/logout.php', {
	    method: 'get',
	    encoding: 'utf-8',
	    onComplete: reload_user_info
	}).request();
}

/**
 * Reload user info after user is logged out
 */
function logout_complete()
{
    reload_user_info();
}

function logout_with_sso()
{
	if ( sso_enabled ) {
		sso_logout( true );
		return;
	}

	document.location.href = "/bin/logout.php";
}

/**
 * Initiates Facebook Connect
 * @author mb
 * @param el Pointer to the link
 * @param ref strign Reference from where connect was initiated
 * @returns false
 */
function facebook_connect(el,ref)
{
	if (!ref)
		ref='uknown';

	el.blur();

	fb_connect = window.open('/bin/facebook/connect/?action=connect&ret='+window.location.hostname+'&ref='+ref,'fb_connect','height=500,width=600,menubar=no,toolbar=no,location=no,status=no,resizable=no');
	if (window.focus) {fb_connect.focus()}

	return false;
}

/**
 * After finished connect/disconnect to Facebook, it refreshes username and other stuff
 * @author mb
 * @returns null
 */
function facebook_reload_userinfo()
{
	if( 'undefined' !== typeof reload_user_info )
		reload_user_info();

	if ( 'undefined' !== typeof redirect_on_login_logout )
		window.location = redirect_on_login_logout;

	if ( 'undefined' !== typeof reload_on_login_logout )
		window.location.reload();

	if('undefined' !== typeof set_hasVoted)
		set_hasVoted();
}

/**
 * Initiates disconnect from Facebook connect
 * @author mb
 * @param el Pointer to the link
 * @returns false
 */
function facebook_connect_disconnect(el)
{
	el.blur();

	fb_connect = window.open('/bin/facebook/connect/?action=disconnect','fb_connect','height=500,width=600,menubar=no,toolbar=no,location=no,status=no,resizable=no');
	if (window.focus) {fb_connect.focus()}

	mbox_close(); 
	open_banner(); 

	return false;
}


/**
 * Returns TRUE if user has granted that permission, or 
 * initiates permission request
 */
function facebook_permission_check(user, perms, clback, ref)
{
	var granted = 0;
	var requested = perms.length;

	$each(user.facebook_permissions, function(granted_p) 
	{
		$each(perms, function(requested_p) {
			if (granted_p == requested_p)
				granted=granted+1;
		});
	});

	if (granted>=requested)
			return true;

	// initiate request
	fb_connect = window.open('/bin/facebook/connect/?action=connect&additional_permissions='+perms.join(',')+'&ret='+window.location.hostname+'&callback='+clback+'&ref='+ref,'fb_connect','height=500,width=600,menubar=no,toolbar=no,location=no,status=no,resizable=no');
	
	if (window.focus) {fb_connect.focus()}
	return false;
}

