$(document).ready(function(){




      $('#keyword').click(function(){
      	if($('#keyword').val() == 'wpisz szukany przedmiot lub kod odpadu')
      		$('#keyword').val('');
      }).blur(function(){
      	if($('#keyword').val() == '')
      		$('#keyword').val('wpisz szukany przedmiot lub kod odpadu');
      });


   if($('#gMap').length > 0)
   {
       {
           var map = new GMap2(document.getElementById("gMap"));
           map.setCenter(new GLatLng(52.31892,16.128359),11);
           map.addControl(new GSmallMapControl());
      	   map.addControl(new GMapTypeControl());
           var pointMarker = new GMarker(new GLatLng(52.31892,16.128359),{clickable: false});
           map.addOverlay(pointMarker);
       }
   }

   $("#offerForm input.code").mask("99 99 99");
   $("#offerForm input[name=zip]").mask("99-999");
   $("#registerForm input[name=phone]").mask("+99 99 99 99 999");
   $("#registerForm input[name=phone2]").mask("+99 999 999 999");


});

function rate(id)
{
	$('#frm_r'+id).parents('div.fl').find('td.tc').html('');
	if( $('#frm_r'+id+' input[name=jakosc]').val() != 0 && $('#frm_r'+id+' input[name=platnosc]').val() != 0 )
	{
		$('#frm_r'+id).submit();
	}else{
		$('#frm_r'+id).parents('div.fl').find('td.tc').html('Wybierz ocenę!');
		return false;
	}

}

function formSend()
{

  $('#contactFormSend .formError').hide();
  name = $('#contactFormSend input[name=name]').val();
  mail = $('#contactFormSend input[name=mail]').val();
  phone = $('#contactFormSend input[name=phone]').val();
  question = $('#contactFormSend textarea[name=question]').val();

  mail = validateEmail2(mail);

  if(name && mail && phone && question)
  {
    $('#contactFormSend').ajaxSubmit(function(msg) {

    if(msg.indexOf('true') != -1)
    {
      $('#contactFormSend .formOk').show();
       $('#contactFormSend input[name=name]').val('');
  		$('#contactFormSend input[name=mail]').val('');
  		$('#contactFormSend input[name=phone]').val('');
  		$('#contactFormSend textarea[name=question]').val('');
    }
  });
  }
  else
  {
    $('#contactFormSend .formError').show();
    return false;
  }
}

function formSend2()
{

  $('#contact2FormSend .formError').hide();
  name = $('#contact2FormSend input[name=name]').val();
  mail = $('#contact2FormSend input[name=mail]').val();
  phone = $('#contact2FormSend input[name=phone]').val();
  question = $('#contact2FormSend textarea[name=question]').val();

  mail = validateEmail2(mail);

  if(name && mail && phone && question)
  {
    $('#contact2FormSend').ajaxSubmit(function(msg) {

    if(msg.indexOf('true') != -1)
    {
      $('#contact2FormSend .formOk').show();
       $('#contact2FormSend input[name=name]').val('');
  		$('#contact2FormSend input[name=mail]').val('');
  		$('#contact2FormSend input[name=phone]').val('');
  		$('#contact2FormSend textarea[name=question]').val('');
    }
  });
  }
  else
  {
    $('#contact2FormSend .formError').show();
    return false;
  }
}

function validateEmail2(x) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x))
		return true;
	else
	{
		//alert('Wpisz poprawny adres email!');
		return false;
	}
}

function sprawdzNIP( inputVal )
{
  var argh = inputVal.replace(/-/g,"");
  var arg = argh.replace(/ /g,"");
  var coefficients = "657234567";
  var sum=0;
  var index=8;
  for (index=8; index>=0 ;index--)
  {
    sum += (parseInt(coefficients.charAt(index)) * parseInt(arg.charAt(index)));
  }

   if((sum % 11) == parseInt(arg.charAt(9)))
    	return true;
   if ( (sum % 11) == 10 )
   {
    if (parseInt(arg.charAt(9)) == 0)
    		return true;
   }
  return false;
}

function switchDisplay(id){
	var li = $('#'+id);

//wylacza pozostale
	$('#lmenu li.sub').each(function(){
		if(id == $(this).attr('id'))
		{
			if(li.css('display') == 'none')
				li.css('display', 'inline');
			else
				li.css('display', 'none');
		}else{
			$(this).css('display', 'none');
		}
	});

/*
 * tylko wlacza wylacza :
 * 			if(li.css('display') == 'none')
				li.css('display', 'inline');
			else
				li.css('display', 'none');
 *
 *
 if ($.browser.msie )
 	var disp = 'inline';
 else
 	var disp = 'table-row';

  	obj = document.getElementById(id);;
  	if (obj.style.display == 'none')
  		obj.style.display = disp;
  	else
  		obj.style.display = 'none';
  		*
  		*/
}

function podmiana(elem, img) {
		//alert(img);
		$(elem).find('img').attr('src', img);
	}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function inputOutline(jqstring, tekst, ok)
{

    var border_org = 'solid 1px #BFD779';
    var border_err = 'solid 1px red';

	if(!ok) {
	    $(jqstring).css('border', border_err);
		$(jqstring).parent().find('p.error').html(tekst);
		$(jqstring).parent().find('p.error').css('display', 'block');
	}else{
	    $(jqstring).css('border', border_org);
		$(jqstring).parent().find('p.error').css('display', 'none');
	}
}

function checkSearch() {
	keyword = $('#searchForm input[name=keyword]').val();
	if(keyword.length <= 4){
		alert('Wpisane słowo jest zbyt krótkie!');
		return false;
	}
	else if(keyword == 'wyszukaj...'){
		return false;
	}else{
		$('#searchForm').submit();
		return true;
	}

}

function checkCommentForm() {

    var ok = true;

    title = $('#commentForm input[name=title2]').val();
	  if(title.length < 6 || title.length > 80){
	    ok = false;
	  	inputOutline('#commentForm input[name=title2]', 'Tytuł musi zawierać od 6 do 80 znaków!', false);
	  }else{
	  	inputOutline('#commentForm input[name=title2]', 'Tytuł musi zawierać od 6 do 80 znaków!', true);
	  }

    txt = $('#commentForm textarea[name=txt]').val();
	  if(txt.length < 1 ) {
	    ok = false;
	  	inputOutline('#commentForm textarea[name=txt]', 'Wpisz treść wiadomości!', false);
	  }else{
	  	inputOutline('#commentForm textarea[name=txt]', 'Wpisz treść wiadomości!', true);
	  }

  	if(!ok)
    {
      //alert('nie wysylam');
      return false;
    }
    else
    {
      $('#commentForm').submit();
      return false;
    }
}

function checkOfferForm() {

    var ok = true;

	category = $('#offerForm select[name=category]').val();
	  if(category == 0){
	    ok = false;
	  	inputOutline('#offerForm select[name=category]', 'Wybierz kategorię!', false);
	  }else{
	  	inputOutline('#offerForm select[name=category]', 'Wybierz kategorię!', true);
	  }

    title = $('#offerForm input[name=title]').val();
	  if(title.length < 6 || title.length > 80){
	    ok = false;
	  	inputOutline('#offerForm input[name=title]', 'Tytuł musi zawierać od 6 do 80 znaków!', false);
	  }else{
	  	inputOutline('#offerForm input[name=title]', 'Tytuł musi zawierać od 6 do 80 znaków!', true);
	  }

    price = $('#offerForm input[name=price]').val();
	//  if(!(/^-?[0-9]+,?.?[0-9]*$/).test(price)){
	  if(price.length < 1 || price.length > 80){
	    ok = false;
	  	inputOutline('#offerForm input[name=price]', 'Wpisz poprawna cenę!', false);
	  }else{
	  	inputOutline('#offerForm input[name=price]', 'Wpisz poprawna cenę!', true);
	  }

    code = $('#offerForm input[name=code]').val();
	  if(code.length < 1 || code.length > 80){
	    ok = false;
	  	inputOutline('#offerForm input[name=code]', 'Kod musi zawierać od 1 do 80 znaków!', false);
	  }else{
	  	inputOutline('#offerForm input[name=code]', 'Kod musi zawierać od 1 do 80 znaków!', true);
	  }

    amount = $('#offerForm input[name=amount]').val();
	  if(amount.length < 1 || amount.length > 80){
	    ok = false;
	  	inputOutline('#offerForm input[name=amount]', 'Ilość musi zawierać od 1 do 80 znaków!', false);
	  }else{
	  	inputOutline('#offerForm input[name=amount]', 'Ilość musi zawierać od 1 do 80 znaków!', true);
	  }

    txt = $('#offerForm textarea[name=txt]').val();
	  if(txt.length < 1 ) {
	    ok = false;
	  	inputOutline('#offerForm textarea[name=txt]', 'Wpisz treść ogłoszenia!', false);
	  }else{
	  	inputOutline('#offerForm textarea[name=txt]', 'Wpisz treść ogłoszenia!', true);
	  }

	check = /^[0-9]{2}-[0-9]{3}|[0-9]{5}$/;
	zip = $('#offerForm input[name=zip]').val();
	if(zip.length > 0 && !check.test(zip) )
	{
      ok = false;
	  	  inputOutline('#offerForm input[name=zip]', 'Wpisz poprawny kod pocztowy!', false);
	}else{
	  	  inputOutline('#offerForm input[name=zip]', '', true);
	}

  	if(!ok)
    {
      //alert('nie wysylam');
      return false;
    }
    else
    {
      $('#offerForm').submit();
      return false;
    }
}

// Funkcja sprawdzająca formualrz rejestracyjny
  function checkRegisterForm( info ) {

  var ok = true;

/*
    if(info != 'edit')
    {
      $.ajax({
        type: 'GET',
        url: 'ajax/responses.php?what=checkEmail&email='+email,
        async: false,
        success: function(msg){
          if(msg.indexOf('true') == -1)
          {
            emailErr = 'Podany email już jest zarejestrowany!';
          }
        }
      });
    }
    if(emailErr != '')
    {
      $('#registerForm input[name=email]').css('border', 'solid 1px red');
      $('#registerForm input[name=email]').parent().parent().find('p.error').text(emailErr);
    }
*/
/*
    login = $('#registerForm input[name=login]').val();
    if(info != 'edit')
    {
	   var check = /^([A-Za-z0-9\-_\.])+$/;
	   if(login.length < 5 || login.length > 16) {
	      ok = false;
	  	  inputOutline('#registerForm input[name=login]', 'Login musi zawierać od 5 do 25 znaków!', false);
	   }else {
	      //alert('sprawdzam');
	      if(!check.test(login) )
			{
		      ok = false;
			  inputOutline('#registerForm input[name=login]', 'Twój login zawiera niedozwolone znaki! <br />Dozwolone są : duże i małe litery, cyfry oraz znaki: - , _ , .', false);
		   }else{
			inputOutline('#registerForm input[name=login]', '', true);
		   }
	   }
    }

    // Sprawdzanie hasła
    pass = $('#registerForm input[name=pass]').val();
    if(info != 'edit' || pass.length > 0)
    {
      if(pass.length < 6 || pass.length > 25) {
	      ok = false;
	  	  inputOutline('#registerForm input[name=pass]', 'Hasło musi zawierać od 6 do 25 znaków', false);
	   }else{
	  	  inputOutline('#registerForm input[name=pass]', '', true);
	   }
    }

    pass2 = $('#registerForm input[name=pass2]').val();
    if(pass != pass2)
    {
      ok = false;
	    inputOutline('#registerForm input[name=pass2]', 'Podane hasła są różne!', false);
   }else{
	  	inputOutline('#registerForm input[name=pass2]', '', true);
   }
*/
    // Sprawdzanie adresu email
    email = $('#registerForm input[name=email]').val();
    check = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!check.test(email))
    {
      ok = false;
	  	  inputOutline('#registerForm input[name=email]', 'Wpisz poprawny adres email!', false);
    }else{
	  	  inputOutline('#registerForm input[name=email]', '', true);
    }

    if(info != 'edit')
    {
	    zgoda = $('#registerForm input[name=zgoda]:checked');
	    if(zgoda.length == 0)
	    {
	      ok = false;
	  	  inputOutline('#registerForm input[name=zgoda]', 'Musisz wyrazić zgodę na przetwarzanie danych osobowych aby sie zarejestrować!', false);
	    }else{
	  	  inputOutline('#registerForm input[name=zgoda]', '', true);
	    }
	    regulamin = $('#registerForm input[name=regulamin]:checked');
	    if(regulamin.length == 0)
	    {
	      ok = false;
	  	  inputOutline('#registerForm input[name=regulamin]', 'Musisz zaakceptowac regulamin aby się zarejestrować!', false);
	    }else{
	      if(zgoda.length != 0)
	      {
	  	  inputOutline('#registerForm input[name=regulamin]', '', true);
	      }
	    }
    }

	check = /^([0-9]{5})$/;
	nr_karty = $('#registerForm input[name=nr_karty]').val();
	//check = /(^\d+$)|(^\d+\.\d+$)/;
	//check2 = [1-9];
	//company = $('#registerForm input[name=company]').val();
	//if(imie.length < 3 && company.length < 3)
	if(!check.test(nr_karty))
	{
      ok = false;
      //alert(nr_karty.length);
	  	inputOutline('#registerForm input[name=nr_karty]', 'Wpisz poprawny numer karty!', false);
	}else{
	  	inputOutline('#registerForm input[name=nr_karty]', '', true);
	}


	imie = $('#registerForm input[name=name]').val();
	//company = $('#registerForm input[name=company]').val();
	//if(imie.length < 3 && company.length < 3)
	if(imie.length < 3)
	{
      ok = false;
	  	inputOutline('#registerForm input[name=name]', 'Wpisz imię!', false);
	}else{
	  	inputOutline('#registerForm input[name=name]', '', true);
	}


	company = $('#registerForm input[name=company]').val();
	if(company.length < 3)
	{
      ok = false;
	  	inputOutline('#registerForm input[name=company]', 'Wpisz nazwę firmy!', false);
	}else{
	  	inputOutline('#registerForm input[name=company]', '', true);
	}

	surname = $('#registerForm input[name=surname]').val();
	if(surname.length < 3)
	{
      ok = false;
	  	inputOutline('#registerForm input[name=surname]', 'Wpisz nazwisko!', false);
	}else{
	  	inputOutline('#registerForm input[name=surname]', '', true);
	}


	street = $('#registerForm input[name=street]').val();
	if(street.length < 3)
	{
      ok = false;
	  	inputOutline('#registerForm input[name=street]', 'Wpisz adres!', false);
	}else{
	  	inputOutline('#registerForm input[name=street]', '', true);
	}


	check = /^([0-9]{2}-[0-9]{3}|[0-9]{5})$/;
	zip = $('#registerForm input[name=zipCode]').val();
	if(!check.test(zip) )
	{
      ok = false;
	  	  inputOutline('#registerForm input[name=zipCode]', 'Wpisz poprawny kod pocztowy!', false);
	}else{
	  	  inputOutline('#registerForm input[name=zipCode]', '', true);
	}

	miasto = $('#registerForm input[name=town]').val();
	if(miasto.length < 3)
	{
      ok = false;
	  	  inputOutline('#registerForm input[name=town]', 'Wpisz nazwę miejscowosci', false);
	}else{
	  	  inputOutline('#registerForm input[name=town]', '', true);
	}

	nip = $('#registerForm input[name=nip]').val();
	if( nip.length > 0 && !sprawdzNIP(nip) )
	{
      ok = false;
	  	  inputOutline('#registerForm input[name=nip]', 'Wpisz poprawny NIP', false);
	}else{
	  	  inputOutline('#registerForm input[name=nip]', '', true);
	}

	check = /^([0-9\+\-\s])+$/;
	phone = $('#registerForm input[name=phone]').val();
	if(!check.test(phone) )
	{
      ok = false;
	  	  inputOutline('#registerForm input[name=phone]', 'Wpisz poprawny numer telefonu!', false);
	}else{
	  	  inputOutline('#registerForm input[name=phone]', '', true);
	}

/*
	phone2 = $('#registerForm input[name=phone2]').val();
	if(!check.test(phone2) )
	{
      ok = false;
	  	  inputOutline('#registerForm input[name=phone2]', 'Wpisz poprawny numer telefonu!', false);
	}else{
	  	  inputOutline('#registerForm input[name=phone2]', '', true);
	}
*/
  	if(!ok)
    {
      //alert('nie wysylam');
      return false;
    }
    else
    {
      //alert('wysyłam');
      $('#registerForm').submit();
      return false;
    }

}
// Sprawdzanie ankiet
function pollVote(id) {
    var type = '';
    var error = 0;
    if($('#'+id).find('input[type=radio]').length > 0)
    {
        if($('#'+id).find('input[type=radio]:checked').length == 0)
          error = 1;
    }
    else if($('#'+id).find('input[type=checkbox]').length > 0)
    {
        if($('#'+id).find('input[type=checkbox]:checked').length == 0)
          error = 1;
    }
    else if($('#'+id).find('select').length > 0) {
        if($('#'+id).find('select').val() == -1)
          error = 1;
    }
    if(error)
      $('#'+id).find('p.pollError').show();
    else
      $('#'+id).submit();


}
