$(document).ready(function(){
   
    $('#menu ul li a img').fadeTo(1, 0);
    $('#menu ul li a img')  	
    	.mouseover(function(){
    		$(this).stop().fadeTo("normal", 1)
    		})
    	.mouseout(function(){
    		$(this).stop().fadeTo("normal", 0)
    		})
 

     $('#leftColumn a img, img.btn')
    	.mouseover(function(){
    		$(this).stop().fadeTo("fast", 0.6)
    		})
    	.mouseout(function(){
    		$(this).stop().fadeTo("fast", 1)
    		})

   if($('#gMap').length > 0) 
   {
       if(GBrowserIsCompatible()) 
       { 
           var map = new GMap2(document.getElementById("gMap")); 
           map.setCenter(new GLatLng(52.305159,16.415935),15); 
           map.addControl(new GSmallMapControl());
      	   map.addControl(new GMapTypeControl());
           var pointMarker = new GMarker(new GLatLng(52.305000,16.415939),{clickable: false}); 
           map.addOverlay(pointMarker); 
       } 
   }
   
}); 

function formSend()
{

  $('#formError').hide();
  name = $('input[name=name]').val();  
  mail = $('input[name=mail]').val();
  phone = $('input[name=phone]').val();
  question = $('textarea[name=question]').val();
  
  mail = validateEmail2(mail);  
  rx = /http:\/\/|url=|link=/i; 
  if(rx.test(question))
  	question = false; 

  if(name && mail && phone && question)
  {
    $('#contactFormSend').ajaxSubmit(function(msg) {

    if(msg.indexOf('true') != -1)
    {
      $('#formOk').show();
       $('input[name=name]').val('');  
  		$('input[name=mail]').val('');
  		$('input[name=phone]').val('');
  		$('input[name=company]').val('');
  		$('textarea[name=question]').val('');
    }
  });  
  }
  else
  {
    $('#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 #C2C2C2';
    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);	
	      }    	
	    }
    }


     
	imie = $('#registerForm input[name=name]').val(); 
	company = $('#registerForm input[name=company]').val();	
	if(imie.length < 3 && company.length < 3)
	{
      ok = false;
	  	inputOutline('#registerForm input[name=name]', 'Wpisz dane osobowe!', false);		
	}else{
	  	inputOutline('#registerForm input[name=name]', '', 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( !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);	
	}
	
  	if(!ok)
    {      
      //alert('nie wysylam');
      return false;
    }
    else 
    {
      $('#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();
    
    
}
