var validMail = /^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$/;
var typy = new Array();
function odnow_typ()
{
    var t = "";
    for(i=0;i<typy.length;i++)
    if(typy[i]!="")
    {
        if(t!="") t += ",";
        t += typy[i];
    }
    document.getElementById("typ").value = t;
}

$(document).ready(function(){

	$("a#up").click(function(){
		if(navigator.userAgent.indexOf("Opera")>-1) return true;
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});

    $("#t0").click(function(){
		var typ = document.getElementById("t0");
        if(typ.checked) { if(jQuery.inArray(typ.value, typy)==-1) typy.push(typ.value); }
        else { if(jQuery.inArray(typ.value, typy)>-1) typy[jQuery.inArray(typ.value, typy)] = ""; }
        odnow_typ();
	});
    $("#t1").click(function(){
		var typ = document.getElementById("t1");
        if(typ.checked) { if(jQuery.inArray(typ.value, typy)==-1) typy.push(typ.value); }
        else { if(jQuery.inArray(typ.value, typy)>-1) typy[jQuery.inArray(typ.value, typy)] = ""; }
        odnow_typ();
	});
    $("#t2").click(function(){
		var typ = document.getElementById("t2");
        if(typ.checked) { if(jQuery.inArray(typ.value, typy)==-1) typy.push(typ.value); }
        else { if(jQuery.inArray(typ.value, typy)>-1) typy[jQuery.inArray(typ.value, typy)] = ""; }
        odnow_typ();
	});
    $("#t3").click(function(){
		var typ = document.getElementById("t3");
        if(typ.checked) { if(jQuery.inArray(typ.value, typy)==-1) typy.push(typ.value); }
        else { if(jQuery.inArray(typ.value, typy)>-1) typy[jQuery.inArray(typ.value, typy)] = ""; }
        odnow_typ();
	});
    $("#t4").click(function(){
		var typ = document.getElementById("t4");
        if(typ.checked) { if(jQuery.inArray(typ.value, typy)==-1) typy.push(typ.value); }
        else { if(jQuery.inArray(typ.value, typy)>-1) typy[jQuery.inArray(typ.value, typy)] = ""; }
        odnow_typ();
	});
    $("#t5").click(function(){
		var typ = document.getElementById("t5");
        if(typ.checked) { if(jQuery.inArray(typ.value, typy)==-1) typy.push(typ.value); }
        else { if(jQuery.inArray(typ.value, typy)>-1) typy[jQuery.inArray(typ.value, typy)] = ""; }
        odnow_typ();
	});
    $("#t6").click(function(){
		var typ = document.getElementById("t6");
        if(typ.checked) { if(jQuery.inArray(typ.value, typy)==-1) typy.push(typ.value); }
        else { if(jQuery.inArray(typ.value, typy)>-1) typy[jQuery.inArray(typ.value, typy)] = ""; }
        odnow_typ();
	});

    if(document.getElementById("back")!=undefined)
        if(history.length>1)document.getElementById("back").innerHTML='<a href="javascript:history.go(-1)">powrót</a>';
        else document.getElementById("back").style.background = '';
    
    if(document.getElementById("powrot_do_listy")!=undefined)
        if(history.length>1)document.getElementById("powrot_do_listy").innerHTML='&lsaquo;  <a href="javascript:history.go(-1)" style="font-size:11px">powrót do listy ogłoszeń</a> &nbsp; &nbsp;';
        else document.getElementById("powrot_do_listy").innerHTML = '&lsaquo;  <a href="/ogloszenia.html" style="font-size:11px">powrót do ogłoszeń</a> &nbsp; &nbsp;';

    if(document.getElementById("powrot_do_listy_firm")!=undefined)
        if(history.length>1)document.getElementById("powrot_do_listy_firm").innerHTML='&lsaquo;  <a href="javascript:history.go(-1)" style="font-size:11px">powrót do listy</a> &nbsp; &nbsp;';
        else document.getElementById("powrot_do_listy_firm").innerHTML = '&lsaquo;  <a href="/firmy-i-instytucje.html" style="font-size:11px">powrót do listy</a> &nbsp; &nbsp;';



    $('.disSel').disableSelection();


    $("#photo_next").click(function(){
        if(photo_index<photos.length-1) show_photo(++photo_index);
    });
    $("#photo_prev").click(function(){
        if(photo_index>0) show_photo(--photo_index);
    });
    

    


    $("#show_contact_form").click(function(){
        $.get("/action/contact_form.php", {},
                   function(returned_data)
                   {
                       if(document.getElementById("contact_form").innerHTML=='')
                       {
                            document.getElementById("contact_form").innerHTML = returned_data;
                            document.getElementById("show_contact_form").innerHTML = 'Schowaj formularz';
                       }
                       else
                       {
                            document.getElementById("contact_form").innerHTML = '';
                            document.getElementById("show_contact_form").innerHTML = 'Pokaż formularz';
                       }
                       return false;
                   });
        return false;
    });
    

});




function button_on(id)
{
    document.getElementById(id).style.opacity ='1';
    document.getElementById(id).style.cursor = 'pointer';
}
function button_off(id)
{
    document.getElementById(id).style.opacity ='0.2';
    document.getElementById(id).style.cursor = 'default';
}

function border_thin()
{
    for(i=0;i<photos.length;i++)
    {
       document.getElementById('photo_min_'+i).style.borderWidth = '1px';
       document.getElementById('photo_min_'+i).style.borderColor = '#b3b3b3'
    }
}

var photos = new Array();
var photo_index = 0;
function show_photo(i)
{
    if(!isNaN(i)) photo_index = i; else photo_index = 0;

    if(photo_index>0) button_on('photo_prev');
    else button_off('photo_prev');

    if(photo_index==photos.length-1) button_off('photo_next');
    else button_on('photo_next');

    $("#photo_preloader").fadeIn("fast");
    document.getElementById('photo_preloader').style.width = $('#photo_big').width()+'px';
    document.getElementById('photo_preloader').style.height = $('#photo_big').height()+'px';
    document.getElementById('photo_preloader').style.marginLeft = ((468-$('#photo_big').width())/2)+'px';
    document.getElementById('photo_preloader').style.marginTop = '2px';
    document.getElementById('photo_preloader').style.display ='';

    $('#photo_big').attr("src", '/images/ogloszenia/'+photos[photo_index]).load(function()
    {
        document.getElementById('photo_preloader').style.display ='none';
        border_thin();
        document.getElementById('photo_min_'+i).style.borderWidth = '4px';
        document.getElementById('photo_min_'+i).style.borderColor = '#ffb66d'
       // $("#photo_preloader").fadeOut("fast");
    });


}
jQuery.fn.extend({
        disableSelection : function() {
                this.each(function() {
                        this.onselectstart = function() { return false; };
                       // this.unselectable = "on";
                        jQuery(this).css('-moz-user-select', 'none');
                });
        }
});

function valid_log()
{
    var f = document.forms.reg;

    var l = f.reg_login.value;
    var p = f.reg_pass.value;

    var blad = "";
    if(l == "") blad += " - Podaj login\n";
    if(p == "") blad += " - Podaj hasło\n";

    if(blad)
    {
        alert("Popraw dane:\n\n"+blad);
        return false;
    }
    return true;
}

function send_contact_form()
{
    var blad = '';
    if(document.getElementById('adres_email').value=="" && document.getElementById('nr_telefonu').value=="") blad += "- Podaj swój e-mail lub nr telefonu\n";
    if(document.getElementById('adres_email').value!="" && document.getElementById('adres_email').value.search(validMail) == -1) blad += "- Podany adres e-mail jest niepoprawny\n";
    if(document.getElementById('wiadomosc').value=="") blad += "- Podaj treść wiadomości\n";

    if(blad) { alert('Popraw dane:\n\n'+blad); return false; }

    if(navigator.userAgent.indexOf("Opera")>-1) return true;
    document.getElementById('ld').style.display = '';
    document.getElementById('wiadomosc').disabled = 'disabled';
    document.getElementById('adres_email').disabled = 'disabled';
    document.getElementById('nr_telefonu').disabled = 'disabled';
    document.getElementById('btn_wyslij').disabled = 'disabled';

        $.post("/action/contact_form_send.php", {
            id:document.getElementById('ad_id').value,
            typ:document.getElementById('ad_typ').value,
            adres_email:document.getElementById('adres_email').value,
            nr_telefonu:document.getElementById('nr_telefonu').value,
            wiadomosc:document.getElementById('wiadomosc').value
        },
                   function(returned_data)
                   {
                       document.getElementById("contact_form").innerHTML = returned_data;
                       document.getElementById("show_contact_form").innerHTML = '';
                       document.getElementById("grot").innerHTML = '';
                       return false;
                   });
        return false;
}