function secondPage()
{
	$('.benchmark_form2').fadeIn('normal');
}

function firstPage()
{
	$('.benchmark_form').fadeIn('normal');
}

function thirdPage(session)
{
	$('#js_error p').html(lang['saved']).parent().fadeIn('normal');

	document.location.href='/rejestracja';
}

function statut_hide()
{
	$('#td_statut p').remove();
	$('#td_statut .select').hide().after('<p>'+lang['not_applicable']+'</p>');
	$('#td_statut .select input').val('');
}

function statut_show()
{
	$('#td_statut .select').show().next().remove();
}

function executeMe()
{

	$('.benchmark_form').fadeIn();
	
	//select
	$('#select_turnover').DeseoSelect();
	$('#select_age').DeseoSelect();
	$('#select_gender').DeseoSelect();
	$('#select_experience').DeseoSelect();
	$('#select_holiday').DeseoSelect();
	$('#select_statut').DeseoSelect();
	$('#select_part_time').DeseoSelect();
	$('#select_from_region').DeseoSelect();
	$('#select_sector_type').DeseoSelect();


	$value = $('#select_sector_type input').val();
	if($value>'0')
	$('.sector .btn').removeClass('hidden');
	else
	$('.sector .btn').addClass('hidden');
	
	
	$('#select_sector_type .DeseoSelectOption').click(function()
	{
		$value = $('#select_sector_type input').val();
		if($value>'0')
		$('.sector .btn').removeClass('hidden');
		else
		$('.sector .btn').addClass('hidden');

		if($value=='3')
		statut_show();
		else
		statut_hide();

		$('.sector .btn input').val('');
		$('.sector .blue').html('');
		
	});


	//alert($('#select_sector_type').parent().html());

	//rating
	$('#satisfy').DeseoVote(10, 31);

	//autocomplete

	function ac_item(row, i, max) {myclass=''; if(i==1) myclass='ac_first'; if(i==max) {myclass='ac_last'}; return '<span class="'+myclass+'">'+row[0]+'</span>';}
	
	function ac_match(row, i, max) {return row[0]}
	
	function acm_result(event, row, formatted) {
		$(this).hideError(); 

		$(this).val(row[1]).attr('loading', 'false').attr('disabled', true);

		$(this).parent().parent().parent().children().eq(0).htmlAddChange('blue', '<b>'+lang['choice']+':</b> '+row[0]); $(this).parent().find("input").eq(1).val(row[2]);

		$(this).parent().parent().find(".small_loader").remove();
		$(this).parent().before('<a href="#" class="btn_clear">'+lang['clear']+'</a>');
	}

	/*function ac_sector_result(event, row, formatted) {
		$(this).hideError(); 
		$(this).val(row[1]); 
		$(this).parent().parent().parent().children().eq(0).htmlAddChange('blue', '<b>'+lang['choice']+':</b> '+row[0]); 
		$(this).parent().find("input").eq(1).val(row[2]);$('#td_statut .select').show().next().remove();
	
		$(this).parent().parent().find(".small_loader").remove();
		$(this).parent().before('<a href="#" class="btn_clear">'+lang['clear']+'</a>');
		
	}*/

	$('.btn_clear').livequery('click', function() {
		$(this).parent().find('input').val('').attr('disabled', false);
		$(this).parent().find('input').eq(0).val(lang['3chars']);
		$(this).remove();
		return false;
	});

	$("#input_job").autocomplete("/extras/search.php", {
		width: 510, selectFirst: false, delay: 10, minChars: 3, max: 14, autoFill: false, extraParams: {type: "jobs"},
		formatItem: ac_item, formatMatch: ac_match
	});
	$("#input_job").result(acm_result);
	
		
	$("#input_company_name").autocomplete("/extras/search.php", {
		width: 510, selectFirst: false, delay: 10, minChars: 1, max: 14, autoFill: false, extraParams: {type: "companies"},
		formatItem: ac_item, formatMatch: ac_match
	});
	$("#input_company_name").result(function ac_result(event, row, formatted) {$(this).hideError(); $(this).val(row[0]);});
	
		
	$("#input_region").autocomplete("/extras/search.php", {
		width: 510, selectFirst: false, delay: 10, minChars: 3, max: 14, autoFill: false, extraParams: {type: "regions"},
		formatItem: ac_item
	});
	$("#input_region").result(acm_result);

		/*$("#input_sector").autocomplete("/extras/search.php", {
			width: 510, selectFirst: false, delay: 10, minChars: 3, max: 14, autoFill: false, extraParams: {type: "sectors", value: function() { return  $('#select_sector_type input').val(); }},
			formatItem: ac_item, formatMatch: ac_match
		});
		$("#input_sector").result(ac_sector_result);*/
	

	$('.btn_parttime').click(function() {
		$(this).backgroundToggle(); 
		res = $(this).next().htmlToggle('<b>'+lang['no']+'</b>', lang['no']);
			
		object = $(this).parent().parent().next();
		

		if(res==2) 
		object.show();
		else
		{
			object.children().children().children('input').attr('value', '');
			object.children().children().children('div').children('span').html(lang['choose']);
			object.hide();
		}
		return false;
	});

	if($('.btn_parttime').attr('value')!=='')
	$('.btn_parttime').click();

	$(".only_digits").keypress(function (e)  
		{ 
		  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
		  {
			$(this).showError(lang['only_digits'], false, false);
			return false;
		  }	
	});



	//FORM2 rating
	$('#rating_1').DeseoVote(6, 41);
	$('#rating_2').DeseoVote(6, 41);
	$('#rating_3').DeseoVote(6, 41);
	$('#rating_4').DeseoVote(6, 41);
	$('#rating_5').DeseoVote(6, 41);
	$('#rating_6').DeseoVote(6, 41);
	$('#rating_7').DeseoVote(6, 41);
	$('#rating_8').DeseoVote(6, 41);
	$('#rating_9').DeseoVote(6, 41);
	
	//FORM2 radio
	$('#important').DeseoRadio(important);
	
	if($('input[name=sector_type]').val()!=='3') statut_hide();
	
	$('.overlay').css({'height':$(document).height(), 'opacity':'0.7'})

	if($('input[name=job_id]').val()>'0')
	{
		$('input[name=job]').attr('loading', 'false').attr('disabled', true);
		$('input[name=job]').parent().before('<a href="#" class="btn_clear">'+lang['clear']+'</a>');
	}


}

function saveData(next)
{
	req = '';
	$('input, textarea').not('.btn, .send_btn').each(function() {
		value = this.value;
		value = value.replace('&', '$amp;');
		req += this.name+'='+value+'&';
	});
	if(next=='3')
	req += 'db=true';

	$.ajax({
		type: "POST",
		url: "extras/saveData.php",
		data: req,
		success: function(data){
			
			msg = data.split('|');
		
			if(msg[0]=='1')
			{
				if(next=='1')
				firstPage();
				
				if(next=='2')
				secondPage();
				
				if(next=='3')
				thirdPage();
			}
			else
			alert(msg);
		}
	});

	
	return false;
}

$(document).ready(function() {
	$('#js_error').fadeOut('normal', executeMe);

	$('.btn_sector').click(function() {
		go_top = Math.ceil($(window).scrollTop() - ($(window).height()/3));
		$('.list_container').show().css('marginTop', go_top);
		$('.sector_list').html('<img src="/media/images/loader.gif" class="loading">');
		$('.overlay').fadeIn();

		type = $('#select_sector_type input').val();

		$('.sector_list div').remove();

		$.ajax({
			type: "POST",
			url: "extras/sectors.php",
			data: 'type='+type,
			success: function(data){
				$('.sector_list .loading').remove();
				$('.sector_list').append(data);
			}
		});
		return false;
	});

	
	$('.list_container a').click(function() {
		$('.list_container').hide();
		$('.overlay').fadeOut();
	});

	$('.sector_list div').livequery('mouseover', function() {
		$(this).addClass('active');
	});
	
	$('.sector_list div').livequery('mouseout', function() {
		$(this).removeClass('active');
	});
	$('.tooltip').html('<p>'+lang['sector']+'</p>');
		$('.btn_zoom').mouseover(function() {
		$('.tooltip').show();
	});

	$('.btn_zoom').mouseleave(function() {
		$('.tooltip').hide();
	});

	$('.sector_list div').livequery('click', function() {
		val = $(this).text();
		val_id = $(this).attr('value');

		$('input[name=sector_id]').parent().parent().parent().children().eq(0).htmlAddChange('blue', '<b>'+lang['choice']+':</b> '+val);
		$('input[name=sector_id]').val(val_id);
		$('input[name=sector]').val(val);
		$('.list_container').hide();
		$('.overlay').fadeOut();
	});

	$('input').filter(function(){if($(this).attr('name')=='job' || $(this).attr('name')=='region') return true}).blur(function() {
	
		$(this).parent().parent().find(".small_loader").remove();

		$id = $(this).next();
		if($id.val()=='' && $(this).val()!=='')
		{
			$(this).showError(lang['not_found'], false, false);
			$(this).val(lang['3chars']).attr('loading', 'false');
			return false;		
		}
		else if($id.val()=='' && $(this).val()=='')
		{
			$id.val('');
			$(this).val(lang['3chars']);
			return false;
		}
		else
		$(this).hideError();
		
	});

	$('input[name=company_name], input[name=region]').blur(function() {
		$(this).parent().parent().find(".small_loader").remove();

		$id = $(this).next();

		if($(this).val()=='')
		{
			$id.val('');
			$(this).val(lang['3chars']);
			return false;	
		}
		else
		$(this).hideError();
	});


	$('input[name=job], input[name=company_name], input[name=region]').keyup(function()
	{
		if($(this).val().length>2 && ($(this).attr('loading')=='false' || $(this).attr('loading')==undefined))
		{
			$(this).parent().before('<div class="small_loader"><img src="/media/images/loader3.gif"></div>');
			$(this).attr('loading', 'true');
		}

	});

	$('input[name=job], input[name=company_name], input[name=region]').focus(function() {
		$id = $(this).next();
		if($(this).val()==lang['3chars'])
		{
			$(this).val('');
			$id.val('');
		}
	});

	$('input[name=sector], input[name=job], input[name=company_name], input[name=region]').each(function(){
		if($(this).val()=='') $(this).val(lang['3chars']);
	});

	$('textarea[name=job_description]').keypress(function(e) {
		if(e.which!=8 && $(this).val().length >= 200)
		{
			$(this).showError(lang['max200'], false, false);
			return false;
		}
		if($(this).val().length==200 && e.which==8)
		$(this).hideError();
	});

	list = ['job_id', 'job_new', 'basepay'];
	list2 = ['basepay', 'bonus_last', 'bonus_max'];

	$('.btn_more').click(function() {

		if($('input[name=bonus_max]').val()=='0') $('input[name=bonus_max]').val('');
		if($('input[name=bonus_last]').val()=='0') $('input[name=bonus_last]').val('');


		error = false;
		
		$('input[name=job], input[name=company_name], input[name=region]').each(function(){
			if($(this).val()==lang['3chars']) $(this).val('');
		});

		if($('input[name=sector_type]').val()>0 && $('input[name=sector_id]').val()=='')
		{
			$('.btn_sector').showError(lang['choose_sector'], false, error);
			error = true;
		}


		if($('#select_from_region').is(':visible') && $('input[name=from_region]').val()=='')
		{
			$('#select_from_region').showError(lang['cannot_be_empty'], false, error);
			error = true;
		}

		if($('.btn_parttime').next().html()!==lang['no'] &&  $('input[name=part_time]').val()=='')
		{
			$('#select_part_time').showError(lang['cannot_be_empty'], false, error);
			error = true;
		}
	
		for(ss=0; ss<list.length; ss++)
		{
			if(list[ss]=='job_id')
			{
				if($('input[name=job]').is(':visible') && $('input[name=job_id]').val()=='')
				{
					$('input[name=job]').showError(lang['cannot_be_empty'], false, error);
					error = true;
				}
			}
			else
			{
				if($('input[name='+list[ss]+']:visible').val()=='')
				{
					$('input[name='+list[ss]+']:visible').showError(lang['cannot_be_empty'], false, error);
					error = true;
				}
				else
				$('input[name='+list[ss]+']:visible').hideError();
			}
		}

		for(ss=0; ss<list2.length; ss++)
		{
			if(ss==0) limit = 600;
			if(ss>0) limit = 50;

			if($('input[name='+list2[ss]+']').val().length > 0)
			{	
				if($('input[name='+list2[ss]+']').val() < limit)
				{
					$('input[name='+list2[ss]+']').showError(lang['value_too_small'], false, error);
					error=true;
				}
				else if($('input[name='+list2[ss]+']').val() > 800000)
				{
					$('input[name='+list2[ss]+']').showError(lang['value_too_big'], false, error);
					error=true;
				}
				else
				$('input[name='+list[ss]+']:visible').hideError();
			}
		}


		if(error==true)
		$(this).showError(lang['check_errors'], true, false);
		else
		{
			$(this).hideError();
			$('.benchmark_form').fadeOut('normal', saveData('2'));
		}

		return false;
	});

	$('.btn_back').click(function() {
		$('.benchmark_form2').fadeOut('normal', saveData('1'));
		return false;
	});

	$('.btn_end').click(function() {
		$('.benchmark_form2').fadeOut('normal', saveData('3'));
		return false;
	});

	$('.btn_new_job').click(function() {
		$('.normal_job input').val('').attr('disabled', false);
		$('.normal_job .btn_clear').remove();
		$('.normal_job .blue').remove();

		$('.normal_job').hide();
		$('.extra_job').show();
		return false;
	});

	if($('input[name=job_new]').val()!=='')
	{
		$('.btn_new_job').click();
	}

	$('.btn_normal_job').click(function() {
		$('.extra_job textarea, .extra_job input').val('');
		$('.normal_job').show();
		$('.extra_job').hide();
		return false;
	});



/*
	list = new Array('basepay', 'basepay_region', 'basepay_age', 'basepay_turnover', 'total', 'total_age', 'total_region', 'bonus_max', 'bonus_max_age', 'bonus_max_region', 'holiday', 'holiday_age', 'holiday_region', 'rating_1', 'rating_1_age', 'rating_1_region', 'rating_2', 'rating_2_age', 'rating_2_region', 'rating_3', 'rating_3_age', 'rating_3_region', 'rating_4', 'rating_4_age', 'rating_4_region', 'rating_5', 'rating_5_age', 'rating_5_region', 'rating_6', 'rating_6_age', 'rating_6_region', 'rating_7', 'rating_7_age', 'rating_7_region', 'rating_8', 'rating_8_age', 'rating_8_region');

	//list = new Array('basepay', 'total', 'rating_1');
	for (zz=0; zz<list.length; zz++)
	{
		if($('.'+list[zz]))
		{
			data = $('.'+list[zz]+' .data').html();
			
			if(data==null) data='0;0;0;0;0';
			
			data = data.split(';');

			
			$('.'+list[zz]).flash(
				{ 
				  src: '/media/images/graph.swf',
				  width: 450,
				  height: 225,
				  flashvars: { language: 'pl', q1: data[0], mdn: data[1], avg: data[2] , q3: data[3], usr: data[4] }
				}
			);

			
			
		}
	}*/


	data = $('.chart .data').html();
	
	if(data)
	{
		var myvalues = data.split(',');
		$('.chart .x').sparkline(myvalues, {type:'pie',  height: 200, sliceColors: ['#099ec8', '#bc49a2', '#fe3312', '#ffd800']});
	}



});
