var contact_wyczyszczone = new Array();

$(document).ready(function() {
	// history back
	$('a.back').bind('click', function(){
		history.back();
		return false;
	});
	
	// print function
	$('a.print').bind('click', function(){
		window.print();
		return false;
	});
	
	// external links open in a new window
	$('a[@rel=external]')
	  .attr({
	    target: "_blank"
	});
	  
	//naglowek sif w zakladkach
	$('#main_offer h3.sifr, p.sifr').sifr({
	font: '/theme/js/myriadProCondensed',
		color: '#b50000'
	});

	/*/redirecty do https
	if($('input').length>0 && window.location.protocol=='http:')
	{
		location.href = "https://"+window.location.hostname + window.location.pathname
	}
	if($('input').length==0 && window.location.protocol=='https:')
	{
		location.href = "http://"+window.location.hostname + window.location.pathname
	}
	*/
	//naglowki Sifr
	$('#main_offer h4.sifr').sifr({
	font: '/theme/js/myriadProCondensed',
		color: '#656565'
	});
	
	// [ FORMULARZ KONTAKTOWY ]
	$("#contact_form :text,#contact_form textarea").click(function(){
		var czyscic = true;
		for(var i=0;i<contact_wyczyszczone.length;i++)
		{
			if(contact_wyczyszczone[i]==this.name) czyscic = false;
		}
		if(czyscic)
		{
			$(this).val("");
			contact_wyczyszczone[contact_wyczyszczone.length] = this.name;
		}
	});
	
	// [ TOOLTIP ]
	$(".banner_shop ul li a").mouseover(function(e){
	  var title = $(this).attr("title");
	  $(this).attr("title","");
	  $('body').prepend("<div id='tooltip'></div>");
	  $('#tooltip').html(title);
	  $('#tooltip').css("left",e.pageX);
	  $('#tooltip').css("top",e.pageY);
	}).mousemove(function(e){
	  $('#tooltip').css("left",e.pageX);
	  $('#tooltip').css("top",e.pageY);
	}).mouseout(function(){
	  var title2 = $('#tooltip').html();
	  $('#tooltip').remove();
	  $(this).attr("title",title2);
	});
	
	// [ MENU NA STRONIE GLOWNEJ ]
	$("#main_offer_menu a").click(function()
	{
		var classname = $(this).attr("href");
		var pozycja = classname.indexOf("#");
		classname = classname.substring(pozycja+1,classname.length);
		pokazTabOnClick(classname);
		return false;
	});
	
	/*
	$("#domains_search_main button").attr("class","submit_button").click(function(){
	   var domain;
	   
	   domain = $("#domains_search_main input").attr("name","domain_name").val();
       domainCheck (domain, 0);
       
       return false;
    });
    */
    $("#domains_search_main_input").click(function(){
	   if ($("#domains_search_main_input").val() == '(np.: domena.pl bez www)')
            $("#domains_search_main_input").val('');
    });
    
    // [ FORMULARZ REJESTRACJI DOMENY - STEP 4 ]
    if($("#registration_indyvidual_f") && $("#registration_corporation_f")) { 
        $("#registration_indyvidual_f").click(function() {registrationChangeType();});
        $("#registration_corporation_f").click(function() {registrationChangeType();});
        registrationChangeType();
    }
    if($("#corespond_other_f")) { 
        $("#corespond_other_f").click(function() {registrationChangeCorespAdr();});
        registrationChangeCorespAdr();
    }
    
    // [ FORMULARZ REJESTRACJI HOSTINGU ]
    /*
    if($("#registration_indyvidual_f") && $("#registration_corporation_f")) { 
        $("#registration_indyvidual_f").click(function() {registrationChangeType();});
        $("#registration_corporation_f").click(function() {registrationChangeType();});
        registrationChangeType();
    }
    if($("#corespond_other_f")) { 
        $("#corespond_other_f").click(function() {registrationChangeCorespAdr();});
        registrationChangeCorespAdr();
    }
    */
});


// [ AJAX ]
function domainCheck (domain, alternative)
{
    var message;
    var status;
    var data;
    var domain_name;
    var domain_available;
    
    showBoxAjaxLoading();
    
    $.ajax({
		type: "POST",
		url: "/ajax/domain-check",
		dataType: "json",
        timeout: 10000,
		data: "domain="+domain+"&alternative="+alternative,
		success: function(response)
        {      
            status = response.status;
            data = response.resdata;
            
            if (status == 1000)
            {
                domain_name = data[0]['domain:name'];
                domain_available = data[0]['domain:avail'];
    
                if (domain_available == 'true')
                    message = 'Domena <b>'+domain_name+'</b> jest dostępna. <a href="/domeny/wyswietl/267/rejestracja_domeny.html?domain='+domain_name+'">Zarejestruj</a>.';
                else
                    message = 'Domena <b>'+domain_name+'</b> jest zajęta.';
            }
            else if (status == 1002)
                message = 'Wpisano nieprawidłową domenę!';
            else if (status == 1003)
                message = 'Nie wpisano domeny!';
            else
                message = 'Wystąpił nieoczekiwany błąd. Spróbuj ponownie za jakiś czas.';
            
            hideBoxAjaxLoading();
            $("#domain_message").append(message);
		}
	});
    
}

	
function pokazTab (id) {
	id = "offer_serwery";
	$("#main_offer_menu .active").removeClass("active");
	$("#main_offer_menu a[href*='#"+id+"']").addClass("active");
	$("#main_offer li:visible").hide();
	$("#main_offer li#"+id).show();
	return false;
}
function pokazTabOnClick (id) {
	$("#main_offer_menu .active").removeClass("active");
	$("#main_offer_menu a[href*='#"+id+"']").addClass("active");
	$("#main_offer li:visible").hide();
	$("#main_offer li#"+id).show();
	return false;
}
	  /**/
function showBoxAjaxLoading ()
{
    $("#box_ajax_loader").fadeIn("slow");
    $("#domain_message").html('');
    $("#domain_message").append('Sprawdzam dostępność domeny...');
}

function hideBoxAjaxLoading ()
{
    $("#box_ajax_loader").fadeOut("slow");
    $("#domain_message").html('');
}


function registrationChangeType() 
{
    if($("#registration_indyvidual_f:checked").val()) 
    {
        //$("#corporation_data").slideUp("slow",function(){$("#indywidual_data").slideDown("slow");});
        
        $("#corporation_data").hide();
        //$("#corporation_data2").hide();
        $("#corporation_data_info").hide();
        $("#indywidual_data").show();
        $("#indywidual_data2").show();
    }
    else if($("#registration_corporation_f:checked").val())
    {
        //$("#indywidual_data").slideUp("slow",function(){$("#corporation_data").slideDown("slow");});
        
        $("#indywidual_data").hide();
        $("#indywidual_data2").hide();
        $("#corporation_data").show();
        //$("#corporation_data2").show();
        $("#corporation_data_info").show();
    }
} 

function domianRegister2Submit() {
    $("#domain_register2_form").submit();
}

function domianRegister2Refresh() {
    $('#domain_register2_form').attr('action','/domeny/wyswietl/280/rejestracja_domeny_krok_2.html');
    $('#domain_registration2_step').val(1);
    $('#domain_register2_form').submit();
}

function domianRegister3Ureg()
{
    $("#login_f").val('');
    $("#domain_register3_form").submit();
}

function domianRegister5Refresh() {
    $("#domain_registration5_step").val(3);
    $("#domain_register5_form").submit();
}

function domianRegister5Submit() {
    $("#domain_register5_form").submit();
}

function registrationChangeCorespAdr() 
{
    if($("#corespond_other_f:checked").val()) 
    {
        $("#corespond_data").show();
    }
    else
    {
        $("#corespond_data").hide();
    }
} 

function registrationChangeReason(select_id,item_id)
{
    $("#"+item_id).val($("#"+select_id).children("[@selected]").text());
}

function serverRegister1Ureg()
{
    $("#login_f").val('');
    $("#server_register1_form").submit();
}

function servicesRegulaminSubmit()
{
    var to_domains = false
    if($('input[name="domains_f[]"]').length>0 && $('#domain_registration2_step').length>0)
    {
        $('input[name="domains_f[]"]').each(function(){
           if(this.checked) {to_domains = true; }
        });
    }
    
    if(to_domains == true) 
    {
        $('#regulamin_checkb:checked').attr("checked", ""); // niech sie jeszcze raz wyswietli strona z podsumowaniem
        $('#regulamin_form').attr('action','/register/wyswietl/282/rejestracja.html');
        $("#regulamin_form").submit();
    }
    else
        if($('#regulamin_checkb:checked').val())
            $("#regulamin_form").submit();
        else
            alert('Musisz zaakceptować regulamin, by zarejestrować usługi.');
}

function regulaminDomainCheck()
{
    $('#regulamin_form').attr('action','/domeny/wyswietl/280/rejestracja_domeny_krok_2.html');
    $('#form_type_input').attr('name','domain_registration_step');
    $('#form_type_input').val('1');
    if($('#domain_registration2_step').length>0) $('#domain_registration2_step').val('1');
    $("#regulamin_form").submit();
}
//oblicza cene względnie od ilosci pakietow resellera
function ResellerCount(price)
{
	if(!isNaN(price))	$("#reseller_num_count").html("<p>Wybrałeś <strong>"+ price + "</strong> pakietów o łącznej wartości <strong>" + price * 300 + "</strong> zł</p>");
	else 	$("#reseller_num_count").html("Błąd");
	$("#reseller_num_count").show();
}

//wyswietla odpowiednie elementy formularza 
function MainFormSelector(element,subelement)
{
	//ukryj tresc
	$(".tresc_opis1").hide();
	$(".tresc_opis2").hide();
	$(".tresc_opis3").hide();
	$(".regulamin_class").hide();
	$(".tresc_tytul").hide();
	//pokaż wybraną tresc
	$("#" + element + "_1").show();
	$("#" + element + "_2").show();
	$("#" + element + "_3").show();
	$("#" + element + "_4").show();
	$("#" + element + "_5").show();
	
	$("#statystyki_type").hide();
	$("#name").hide();
	$("#surname").hide();
	$("#email").hide();
	$("#phone").hide();
	$("#kod_captcha").hide();
	$("#adres_www").hide();
	$("#metakey").hide();
	$("#question").hide();
	$("#reseller_num").hide();
	$("#address").hide();
	$("#post_code").hide();
	$("#city").hide();
	$("#company").hide();
	$("#nip").hide();
	$("#label_adres").hide();
	$("#hosting_username").hide();
	$("#reseller_num_count").hide();
	
	if((element=='zamow_pozycjonowanie')||(element=='zamow_adwords')||(element=='zamow_adwords_kampania'))
	{
		document.getElementById('name').style.display 			= '';
		document.getElementById('surname').style.display 		= '';
		document.getElementById('email').style.display 			= '';
		document.getElementById('phone').style.display 			= '';
		document.getElementById('kod_captcha').style.display 	= '';
		document.getElementById('adres_www').style.display 		= '';
		document.getElementById('metakey').style.display 		= '';
		document.getElementById('question').style.display 		= '';
	}
	if((element=='zamow_strone')||(element=='zamow_sklep'))
	{
		document.getElementById('name').style.display 			= '';
		document.getElementById('surname').style.display 		= '';
		document.getElementById('email').style.display 			= '';
		document.getElementById('phone').style.display 			= '';
		document.getElementById('kod_captcha').style.display 	= '';
		document.getElementById('question').style.display 		= '';
	}
	if(element=='zamow_reseller')
	{
		document.getElementById('name').style.display 			= '';
		document.getElementById('surname').style.display 		= '';
		document.getElementById('email').style.display 			= '';
		document.getElementById('phone').style.display 			= '';
		document.getElementById('kod_captcha').style.display 	= '';
		document.getElementById('reseller_num').style.display 	= '';
		document.getElementById('address').style.display 		= '';
		document.getElementById('post_code').style.display 		= '';
		document.getElementById('city').style.display 			= '';
		document.getElementById('company').style.display 		= '';
		document.getElementById('label_adres').style.display 	= '';
		document.getElementById('nip').style.display 			= '';
	}
	if(element=='zamow_statystyki')
	{
		document.getElementById('statystyki_type').style.display = '';
		
		$(".tresc_opis2").hide();
		$(".tresc_opis3").hide();
		$(".regulamin_class").hide();
		$(".tresc_tytul").hide();
		if((subelement=='optimum')||(subelement=='optimumPlus')||(subelement=='biznes'))
		{
			$("#" + element + "_2").show();
			$("#" + element + "_3").show();
			$("#" + element + "_4").show();
			$("#" + subelement + "_5").show();
			document.getElementById('adres_www').style.display 		= '';
			document.getElementById('name').style.display 			= '';
			document.getElementById('surname').style.display 		= '';
			document.getElementById('email').style.display 			= '';
			document.getElementById('phone').style.display 			= '';
			document.getElementById('kod_captcha').style.display 	= '';
			document.getElementById('address').style.display 		= '';
			document.getElementById('post_code').style.display 		= '';
			document.getElementById('city').style.display 			= '';
			document.getElementById('company').style.display 		= '';
			document.getElementById('nip').style.display 			= '';
			document.getElementById('label_adres').style.display 	= '';
		}
		if(subelement=='analytics')
		{
			$("#" + subelement + "_2").show();
			$("#" + subelement + "_3").show();
			$("#zamow_" + subelement + "_4").show();
			$("#" + subelement + "_5").show();
			document.getElementById('adres_www').style.display 		= '';
			document.getElementById('name').style.display 			= '';
			document.getElementById('surname').style.display 		= '';
			document.getElementById('email').style.display 			= '';
			document.getElementById('phone').style.display 			= '';
			document.getElementById('kod_captcha').style.display 	= '';
			document.getElementById('hosting_username').style.display 	= '';
		}
		if(subelement=='stat24')
		{
			$("#" + subelement + "_2").show();
			$("#" + subelement + "_3").show();
			$("#" + element + "_4").show();
			$("#" + subelement + "_5").show();
			document.getElementById('adres_www').style.display 		= '';
			document.getElementById('name').style.display 			= '';
			document.getElementById('surname').style.display 		= '';
			document.getElementById('email').style.display 			= '';
			document.getElementById('phone').style.display 			= '';
			document.getElementById('kod_captcha').style.display 	= '';
		}
		
	}
}

