$(function()
{
   $("a.confirmAction").live('click', function(event)
   {
      if (!confirmAction()) event.preventDefault(); 
   });
   //toggle admin banners
   var banners=$("div.bannersList").length;
   
   if (banners>0)
   {
//       $("div.bannersList div.itemList").hide();
      $("div.bannersList").find('.title span a').click(function(event)
      {
	 event.preventDefault();
	 $(this).parents('.bannersList').children('.itemList').toggle();
      } );
   }
   
   
   //highilght users admin table
   var rights=$("form.rights table tr").length;
   if (rights>0)
   {
      var colorDefault=$("form.rights table tr").css('background-color');
      $("form.rights table tr").hover(
	 function(){$(this).css('background-color','#f2f6f7')},
	 function(){$(this).css('background-color',colorDefault)}
      );
      $('form.rights table tr td[class^="field"]').hover(
	 
	 function()
	 {
	    var targetClass=$(this).attr('class');
	 
	    $("form.rights table").find('.'+targetClass).css('background-color','#f2f6f7');
	 },
	 function()
	 {
	    var targetClass=$(this).attr('class');
	 
	    $("form.rights table").find('.'+targetClass).css('background-color',colorDefault);
	 }
      );
   }
   
   $('.formsQuestionForm').submit(function(event)
   {
      event.preventDefault();
      var formIp=$(this).find('input[name=ip]').val();
      var formToken=$(this).find('input[name=token]').val();
      var formId=$(this).find('input[name=formId]').val();
      var formVote=$(this).find('input[name=form'+formId+']:checked').val();
      var formInfo="ERROR";

      var nowForm=$(this);

      $(this).parent('div').find('.info').text('');
      $(this).parent('div').find('.info').attr('class', 'info');
      
      if (!isNaN(formVote))
      {
	 $.ajax(
	 {
	    type: "POST", url: "ajax/question.php", data: "formIp="+formIp+"&formToken="+formToken+"&formVote="+formVote+"&formId="+formId,
	    complete: function(data)
	    {
	       var respond=trim(data.responseText);
	       
	       if (respond=='0')
	       {//vote added
		  formInfo='Głos oddany';
		  updateFormView(formId, true);
	       }
	       else if(respond=='-1')
	       {//user voted today already
		  formInfo='Już głosowałeś na tą ankietę.';
	       }
	       else if(resond=='-2')
	       {//user not verified
		  formInfo='Błąd weryfikacji użytkownika';
	       }
	       else if(respond=='-3')
	       {//wrong data
		  formInfo='Niekompletne dane';
	       }
	       else
	       {//other error
		  formInfo='Wystąpił nieznany błąd';
	       }
	       nowForm.parent('div').find('.info').text(formInfo);
	    }
	 });
      }
      else
      {
	 formInfo='Zaznacz jedną opcję';
	 $(this).parent('div').find('.info').text(formInfo);

      }
   });

   $("a.showResultsForm").click(function(event)
   {
      event.preventDefault();
      var formId=$(this).attr('title');
      updateFormView(formId, false);
   });

   $("#forms p.formAnswer")
   .each(function()
   {
      var tmpFormId=$(this).attr('title');
      updateFormView(tmpFormId, false);
   });
});

function updateFormView(formId, newVote)
{
   $('div.forms'+formId+'').children('.formAnswer').remove();
   $('div.forms'+formId+'').children('form').remove();

   $('div.forms'+formId+'').prepend('<img src="gfx/loader.gif" alt="Ładuję ..." />');
   	 $.ajax(
	 {
	    type: "POST", url: "ajax/question.php", data: "formId="+formId+"&updateQuestionData=true",
	    dataType : 'json',
	    success : function(data)
	    {
	       var result='';

	       if(data.length)
	       {
		  $.each(data, function (key, values)
		  {
		     result+='<p class="formAnswer" ><span class="title">'+values['title']+'&nbsp;('+values['width']+' %)</span><span class="value" style="width:'+values['width']+'%"></span></p>';
		  });
	       }
	       else
	       {
		  result='<p class="formAnswer">Nie oddano głosów</p>';
	       }
	       $('div.forms'+formId+'').children('img').remove();
	       $('div.forms'+formId+'').prepend(result);

	       if (newVote)
	       {
		  var oldVotes=$('div.forms'+formId+'').children('.votes').children('span').text();
		  oldVotes=parseInt(oldVotes);
		  oldVotes=oldVotes+1
		  $('div.forms'+formId+'').children('.votes').children(' span').text(oldVotes);
	       }
	    }
	 });
}

function updateData(div)
{
   var string="izba";
   for(var i=1;i<=20;++i)
   {
      var actualDiv=document.getElementById(string+i);
      actualDiv.style.display="none";
   }
   var changeDiv=document.getElementById(div);
   changeDiv.style.display="block";
}

function changeState(docID)
{
   if (document.getElementById(docID).disabled==true)
   {
      document.getElementById(docID).disabled=false;
   }
   else
   {
      document.getElementById(docID).disabled=true;
   }
}

function changeGame(number)
{
   if (document.getElementById("home"+number).disabled==true)
   {
      document.getElementById("home"+number).disabled=false;
      document.getElementById("guest"+number).disabled=false;
   }
   else
   {
      document.getElementById("home"+number).disabled=true;
      document.getElementById("guest"+number).disabled=true;
   }
}

function makeEnabled(number)
{
//    if (number==1)
//    {
      document.getElementById('addName'+number).disabled=false;
      document.getElementById('addTeam'+number).checked=true;
//    }
}

function confirmAction()
{
   if (confirm("Jesteś pewien ?"))
   {
      return true;
   }
   else return false;
}

function adjustSize()
{
   var myWidth = 0, myHeight = 0;
   if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
   } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
   }
//    alert("Site height="+siteHeight);
//    var test1=document.body.offsetHeight;
//    var test2=window.innerHeight;
//    var test3=document.body.clientHeight;
//    alert ("offset="+test1+" , inner="+test2+" , client="+test3);
   var marginTop=myHeight-324;
   marginTop=marginTop/2;

   document.getElementById('intro').style.height="139px";
   document.getElementById('intro').style.padding="185px 0 0 0";
   document.getElementById('intro').style.top=marginTop+"px";
   document.getElementById('introLink').style.height="67px";
}

function downFooter()
{
   var myWidth = 0, myHeight = 0;
   if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
   } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
   }

   var minHeight=myHeight-369;

   document.getElementById('content').style.minHeight=minHeight+"px";
}

function alertSize() {
   var myWidth = 0, myHeight = 0;
   if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
   } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
   }
   window.alert( 'Width = ' + myWidth );
   window.alert( 'Height = ' + myHeight );
}


function addFav()
{
   if(window.external)
   {
      window.external.AddFavorite("http://www.twojaapteka.info/", "Twoja apteka - Serwis informacyjny dla farmaceutów");
   }
   else
   {
      window.sidebar.addPanel("Twoja apteka - Serwis informacyjny dla farmaceutów", "http://www.twojaapteka.info/", "");
   }
}


function image(wwidth, wheight, src)
{
	wwidth = wwidth + 10;
	wheight = wheight + 10;
	wleft = (screen.availWidth / 2) - (wwidth / 2);
	wtop = (screen.availHeight / 2) - (wheight / 2);
	window.open("image.php?img="+src, "epimg", "width="+wwidth+", height="+wheight+", innerWidth="+wwidth+", innerHeight="+wheight+", left="+wleft+", top="+wtop);
	return false;
}

function startTimer()
{

	Tm = Math.floor(bum/60);
	Ts = (bum%60);
	document.getElementById('timer-time').innerHTML = "" + Math.floor(Tm/10) + (Tm%10) + ":" + Math.floor(Ts/10) + (Ts%10);
	if (bum>0)
		{ bum = bum - 1 ; setTimeout("startTimer()", 1000); }
	else
		{ document.test.submit(); }
}

function frekr()
{
	var rek = document.getElementById('drekr');

	if(rek.style.display == 'block') {
		rek.style.display = 'none';
	}
	else {
		rek.style.display = 'block';
	}
}

function dodaj(obj, title, url){
  if(window.sidebar){
    // Mozilla Firefox Bookmark
    window.sidebar.addPanel(title, url,"");
    return false;
  } else if(window.external){
    // IE Favorite
    window.external.AddFavorite( url, title);
    return false;
  } else if(window.opera && window.print){
    //Opera Hotlist
    obj.setAttribute('href',url);
    obj.setAttribute('title',title);
    obj.setAttribute('rel','sidebar');
    obj.click();
    return false;
  }
}

function showKursy(nazwa)
{
     document.getElementById(nazwa).style.visibility="visible";
}

function hideKursy(nazwa)
{
     document.getElementById(nazwa).style.visibility="hidden";
}


function checkForm_newsletter()
{
     submitState="true";
     var email=document.getElementById("emailNewsletter").value;
     var licznik=document.getElementById("emailNewsletter").value.indexOf("@");
     if (email.length==0 || licznik<2)
     {
	  alert("Podano nieprawidłowy adres email!");
	  submitState="false";
     }
     if (submitState=="false")
     {
	  return false;
     }
     else
     {
	  return true;
     }

}


function checkForm_test()
{
     submitState="true";
     var filia=document.getElementById("filia").value;
     var imie=document.getElementById("name").value;
     var surname=document.getElementById("surname").value;
     var adres=document.getElementById("adres").value;
     var phone=document.getElementById("phone").value;
     var email=document.getElementById("email").value;
     var zgoda=document.getElementById("zgoda");
     if (filia.length==0 || imie.length==0 || surname.length==0 || adres.length==0 || phone.length==0 || email.length==0 )
     {
	  alert("Nie wypełniono wszystkich pól !");
	  submitState="false";
     }
     else if (!zgoda.checked)
     {
	  alert("Zgoda na przetwarzanie danych osobowych jest wymagana!");
	  submitState="false";
     }
     if (submitState=="false")
     {
	  return false;
     }
     else
     {
	  return true;
     }

}

function checkForm_feedback()
{
     submitState="true";
     var imie=document.getElementById("nameFeedback").value;
     var licznik=document.getElementById("emailFeedback").value.indexOf("@");
     var email=document.getElementById("emailFeedback").value;
     var question=document.getElementById("questionFeedback").value;
     if (imie.length==0)
     {
	  alert("Wpisz swoje imię!");
	  submitState="false";
     }
     else if (question.length==0)
     {
	  alert("Uzupełnij komentarz!");
	  submitState="false";
     }
     else if (licznik<2 || email.length==0)
     {
	  alert("Wpisz prawidłowy adres email");
	  submitState="false";
     }
     if (submitState=="false")
     {
	  return false;
     }
     else
     {
	  return true;
     }
}

function checkForm_kontakt()
{
     submitState="true";
     var imie=document.getElementById("nameKontakt").value;
     var licznik=document.getElementById("emailKontakt").value.indexOf("@");
     var email=document.getElementById("emailKontakt").value;
     var question=document.getElementById("questionKontakt").value;
     var subject=document.getElementById("subjectKontakt").value;
     if (imie.length==0)
     {
	  alert("Wpisz swoje imię!");
	  submitState="false";
     }
     else if (question.length==0)
     {
	  alert("Wpisz pytanie");
	  submitState="false";
     }
     else if (subject.length==0)
     {
	  alert("Wpisz temat");
	  submitState="false";
     }
     else if (licznik<2 || email.length==0)
     {
	  alert("Wpisz prawidłowy adres email");
	  submitState="false";
     }
     if (submitState=="false")
     {
	  return false;
     }
     else
     {
	  return true;
     }
}

function checkForm_zamowienie()
{
     submitState="true";
     var imie=document.getElementById("zamowienieName").value;
     var surname=document.getElementById("zamowienieSurname").value;
     var licznik=document.getElementById("zamowienieMail").value.indexOf("@");
     var email=document.getElementById("zamowienieMail").value;
     var street=document.getElementById("zamowienieStreet").value;
     var city=document.getElementById("zamowienieCity").value;
     if (imie.length==0 || surname.length==0 || email.length==0 || street.length==0 | city.length==0)
     {
	  alert("Uzupełnij wymagane dane");
	  submitState="false";
     }
     else if (licznik<2 || email.length==0)
     {
	  alert("Wpisz prawidłowy adres email");
	  submitState="false";
     }
     if (submitState=="false")
     {
	  return false;
     }
     else
     {
	  return true;
     }
}//zamowienie

function checkForm_opinie()
{
     submitState="true";
     var imie=document.getElementById("name").value;
     var surname=document.getElementById("surname").value;
     var content=document.getElementById("content").value;
     if (imie.length==0)
     {
	  alert("Wpisz imię!");
	  submitState="false";
     }
     else if (surname.length==0)
     {
	  alert("Wpisz Nazwisko!");
	  submitState="false";
     }
     else if (content.length==0)
     {
	  alert("Wpisz Komentarz!");
	  submitState="false";
     }
     if (submitState=="false")
     {
	  return false;
     }
     else
     {
	  return true;
     }
}

function checkForm_rekrutacja()
{
     submitState="true";
     var imie=document.getElementById("yourName").value;
     var adres=document.getElementById("yourAdres").value;
     var birth=document.getElementById("dateBirth").value;
     var email=document.getElementById("yourEmail").value;
     var licznik=email.indexOf("@");
     if (imie.length==0 || adres.length==0 || birth.length==0 || email.length==0)
     {
	  alert("Wymagane jest wypełnienie wszystkich pól");
	  submitState="false";
     }
     else if (licznik<2)
     {
	  alert("Wpisz rzeczywisty adres email!")
	  submitState="false";
     }
     if (submitState=="false")
     {
	  return false;
     }
     else
     {
	  return true;
     }
}

function toWhite(idLinku)
{
     //alert("test"+idLinku);
     document.getElementById(idLinku).style.color="white";
}

function toYellow(idLinku)
{
     //alert("test"+idLinku);
     document.getElementById(idLinku).style.color="yellow";
}
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
