/**
 * 
 */
var searchTerm = "Wpisz szukany tekst";
var login = "Adres email";
var haslo = "Hasło";

$(document).ready(function(){
	
	$("input.searchBox").val(searchTerm);
	$("input.searchBox").focus(function () {
		if ($(this).val() === searchTerm) {
			$(this).val("");
		}
	});
	
	$("input.searchBox").blur(function () {
		if ($(this).val() === "") {
			$(this).val(searchTerm);
		}
	});
        
        $("input.login").val(login);
	$("input.login").focus(function () {
		if ($(this).val() === login) {
			$(this).val("");
		}
	});
	
	$("input.login").blur(function () {
		if ($(this).val() === "") {
			$(this).val(login);
		}
	});
        
        $("input.haslo").val(haslo);
	$("input.haslo").focus(function () {
		if ($(this).val() === haslo) {
			$(this).val("");
		}
	});
	
	$("input.haslo").blur(function () {
		if ($(this).val() === "") {
			$(this).val(haslo);
		}
	});
	
	var formZam = $("#form_zamowieniaV");
	var rejesZam = $("#originalForm");
	var ohas = $("#originalFormOH");
	
	var adresemail = $("#adresemail");
	var adresemail_info = $("#adresemail_info");
	var imie = $("#imie");
	var imie_info = $("#imie_info");
	var nazwisko = $("#nazwisko");
	var nazwisko_info = $("#nazwisko_info");
	var haslo1 = $("#haslo1");
	var haslo2 = $("#haslo2");
	var haslo_info = $("#haslo_info");
	var captchaform = $("#captcha-form");
	var captchaform_info = $("#captcha-form_info");
	
	var ohasloA = $("#ohasloA");
	var ohaslo1 = $("#ohaslo1");
	var ohaslo2 = $("#ohaslo2");
	var ohaslo_info = $("#ohaslo_info");
	
	//On blur
	adresemail.blur(validateEMAIL);
	imie.blur(validateIMIE);
	nazwisko.blur(validateNAZWISKO);
	haslo1.blur(validateHASLO);
	haslo2.blur(validateHASLO);
	ohaslo1.blur(validateHASLOo);
	ohaslo2.blur(validateHASLOo);
	//On key press
	adresemail.keyup(validateEMAIL);
	imie.keyup(validateIMIE);
	nazwisko.keyup(validateNAZWISKO);
	haslo1.keyup(validateHASLO);
	haslo2.keyup(validateHASLO);
	ohaslo1.blur(validateHASLOo);
	ohaslo2.blur(validateHASLOo);
	
	var platnosc = $("#platnosc");
	var platnosc_info = $("#platnosc_info");
	var dokzak = $("#dokzak");
	var a1 = 0;
	var a2 = 0;
	
	formZam.submit(function(){
		if(validatePLAT() & validateDOKZAK()){
			
			return true;
		}else{
			return false;
		}
	});
	
	ohas.submit(function(){
		if(validateHASLOo()){
			
			return true;
		}else{
			return false;
		}
	});
	
	rejesZam.submit(function(){
		if(validateEMAIL() & validateIMIE() & validateNAZWISKO() & validateHASLO() & validateCAPT()){
			
			return true;
		}else{
			return false;
		}
	});
	
	function validateDOKZAK(){
		var a1 = $("input[name='dokzak']:checked").val();

		if(a1 > 0){
			return true;
		}else{
			return false;
		}
		
	}

	function validatePLAT(){
		var a2 = $("input[name='platnosc']:checked").val();
		
		if(a2 > 0){
			return true;
		}else{
			$("#platnosc_info").html('Musisz wybrać formę płatności');
			return false;
		}
	
	}
	
	function validateEMAIL(){

		if(adresemail.val().length < 6){
			adresemail.addClass("errorF");
			adresemail_info.text("Wpisz swój login");
			adresemail_info.addClass("errorF");
			return false;
		}
		//if it's valid
		else{
			adresemail.removeClass("errorF");
			adresemail_info.text("OK");
			adresemail_info.removeClass("errorF");
			adresemail_info.addClass("infoF");
			return true;
		}
	
	}
	
	function validateIMIE(){

		if(imie.val().length < 3){
			imie.addClass("errorF");
			imie_info.text("Wpisz swoje imię!");
			imie_info.addClass("errorF");
			return false;
		}
		//if it's valid
		else{
			imie.removeClass("errorF");
			imie_info.text("OK");
			imie_info.removeClass("errorF");
			imie_info.addClass("infoF");
			return true;
		}
	
	}
	
	function validateHASLO(){

		/*if(haslo1.val().length < 3 || haslo2.val().length < 3){
			haslo1.addClass("errorF");
			haslo_info.text("Hasło jest za krótkie");
			haslo_info.addClass("errorF");
			return false;
		}
		//if it's valid
		else{*/
			if(haslo1.val() == haslo2.val()){
				haslo1.removeClass("errorF");
				haslo_info.text("OK");
				haslo_info.removeClass("errorF");
				haslo_info.addClass("infoF");
				return true;
			}else{
				haslo1.addClass("errorF");
				haslo_info.text("Hasła nie pasują do siebie");
				haslo_info.addClass("errorF");
				return false;
			
			}
		
	
	}
	
	function validateHASLOo(){

		/*if(ohaslo1.val().length < 3 || ohaslo2.val().length < 3){
			ohaslo1.addClass("errorF");
			ohaslo_info.text("Hasło jest za krótkie");
			ohaslo_info.addClass("errorF");
			return false;
		}
		if it's valid
		else{*/
			if(ohaslo1.val() == ohaslo2.val()){
				ohaslo1.removeClass("errorF");
				ohaslo_info.text("OK");
				ohaslo_info.removeClass("errorF");
				ohaslo_info.addClass("infoF");
				return true;
			}else{
				ohaslo1.addClass("errorF");
				ohaslo_info.text("Hasła nie pasują do siebie");
				ohaslo_info.addClass("errorF");
				return false;
			
			}
		
	
	}
	
	function validateNAZWISKO(){

		if(nazwisko.val().length < 3){
			nazwisko.addClass("errorF");
			nazwisko_info.text("Wpisz swoje nazwisko!");
			nazwisko_info.addClass("errorF");
			return false;
		}
		//if it's valid
		else{
			nazwisko.removeClass("errorF");
			nazwisko_info.text("OK");
			nazwisko_info.removeClass("errorF");
			nazwisko_info.addClass("infoF");
			return true;
		}
	
	}
	
	function validateCAPT(){
		
		if(captchaform.val().length < 1){
			captchaform.addClass("errorF");
			captchaform_info.text("Wpisz kod z obrazka");
			captchaform_info.addClass("errorF");
			return false;
		}
		//if it's valid
		else{
			captchaform.removeClass("errorF");
			captchaform_info.text("OK");
			captchaform_info.removeClass("errorF");
			captchaform_info.addClass("infoF");
			return true;
		}
	
	}
	
	var form = $("#logFORM");
	
	/*form.submit(function(){
		var user = $("#uzytkownikM").val();
		var haslo = $("#haslo").val();
		
		$('#zawartoscPanelUzytkownika').html("loguje");
		
		var data = 'logon=1&uzytkownik=' + user + '&haslo=' + haslo;
		
		$.ajax({
		    type:           "post",
		    cache:          false,
		    url:            "exe/logon.ajax.php",
		    data:           data, //{myJson:  data}
		    success: function (html) {	
				
				//hide the progress bar
				//$('#loading').hide();	
				
				//add the content retrieved from ajax and put it in the #content div
				$('#zawartoscPanelUzytkownika').html(html);
				
				//display the body with fadeIn transition
				$('#zawartoscPanelUzytkownika').fadeIn('slow');		
				
				window.location.href = 'index.php?logb=1'
			}
	    });
		
		
	});*/
	
	$("a[name=DWyloguj]").click(function(e){
		
		e.preventDefault();
		
		$('#zawartoscPanelUzytkownika').html("wylogowywanie");
		var data = 'logoff=1';
		$.ajax({
		    type:           "post",
		    cache:          false,
		    url:            "exe/logon.ajax.php",
		    data:           data, //{myJson:  data}
		    success: function (html) {	
				
				//hide the progress bar
				//$('#loading').hide();	
				
				//add the content retrieved from ajax and put it in the #content div
				$('#zawartoscPanelUzytkownika').html(html);
				
				//display the body with fadeIn transition
				$('#zawartoscPanelUzytkownika').fadeIn('slow');		
			}
	    });
	});
	
$("#linkPK").click(function(){
		$("#more").toggle("normal");
	}).toggle(function(){ 
		$(this).text('schowaj'); 
	}, function() {
		$(this).text("więcej"); 
	});
	
$("a[name=wiecejC]").click(function(){
		var idWC = $(this).attr('id');
		$(idWC).toggle("normal");
	}).toggle(function(){ 
		$(this).text('schowaj'); 
	}, function() {
		$(this).text("pokaz więcej"); 
});

//test

    $('ul.ukryjczesc').each(function(){
      var liItems = $(this);
      if (liItems.children('li').length > 5) {
        liItems.children('li:gt(4)').hide();
        liItems.after('<a href="#" class="showhideul" id="hlShowMoreLess">Pokaż więcej...</a>');
        //$(this).addClass('hasshowhide'); // new line
      }
  });
  $('.showhideul').click(function(e){
      e.preventDefault();
      var liItemInWord = $(this);
      liItemInWord.prev().children('li:gt(4)').toggle();  
      if (liItemInWord.prev().children('li:hidden').length == 0) {
          liItemInWord.text('Pokaż mniej...');
          //liItemInWord.prev().attr('src','images/minus_sign.png');
      } else {
          liItemInWord.text('Pokaż więcej...');//.prev().attr('src','images/plus_sign.png');          

      }
       //also need to add different image "minus  sign(-)"
      $('ul.ukryjczesc').not(liItemInWord.prev()).each(function(){
         
        	var liClickedItem = $(this);           

        	liClickedItem.next().text('Pokaż więcej...');
        
        	liClickedItem.children('li:gt(4)').hide(); //hide();
    
        });
  });







//koniec_testu


$("a[name=DKoszyk]").click(function(e){
		
		e.preventDefault();
		
		$('#zawartoscKoszyka').html("czytam");
		
		var ile = $("#ile").val();
		//Get the A tag
		//var id = $(this).attr('href');
		
		var iddoc = $(this).attr('id');
		
		var data = 'kod=' + iddoc + '&ile=' + ile;
		
		if(ile > 0){
		
		$.ajax({
		    type:           "post",
		    cache:          false,
		    url:            "exe/koszyk.ajax.php",
		    data:           data, //{myJson:  data}
		    success: function (html) {	
				
				//hide the progress bar
				//$('#loading').hide();	
				
				//add the content retrieved from ajax and put it in the #content div
				$('#zawartoscKoszyka').html(html);
				
				//display the body with fadeIn transition
				$('#zawartoscKoszyka').fadeIn('slow');		
			}
	    });
		
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		//$('#mask').fadeIn(500);	
		$('#mask').fadeTo("slow",0.8);	
		
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$('#dialogPROK').css('top',  winH/2-$('#dialogPROK').height()/2);
		$('#dialogPROK').css('left', winW/2-$('#dialogPROK').width()/2);
	
		//transition effect
		$('#dialogPROK').fadeIn(100); 
		
		$.ajax({
		    type:           "post",
		    cache:          false,
		    url:            "exe/produkt.ajax.php",
		    data:           data, //{myJson:  data}
		    success: function (html) {	
				
				//hide the progress bar
				//$('#loading').hide();	
				
				//add the content retrieved from ajax and put it in the #content div
				$('#pole_do_nazwy_produktu').html(html);
				
				//display the body with fadeIn transition
				$('#pole_do_nazwy_produktu').fadeIn('slow');		
			}
	    });
		
		}else{
			alert("Ilość musi być większa od 0");
		}
	});

	//wybor platnosci

	$("input[name='platnosc']").change(function(){
		var kwota_koszyka = parseFloat($("#kwota_koszykaH").val());
		var kwota_platnosci3 = 0;
		//alert("platnosc "+$("input[name='platnosc']:checked").val() );
		if ($("input[name='platnosc']:checked").val() == '1'){
        // Code for handling value 'a'
			kwota_platnosci3 = 13;
			$("#kwota_platnosci").html("13");
		}
		else if ($("input[name='platnosc']:checked").val() == '2'){
        // Code for handling value 'b'
			kwota_platnosci3 = 18;
			$("#kwota_platnosci").html('18');
			
		}
		else{
			
		}
		
		if(kwota_koszyka > 200){
			kwota_platnosci3 =0;
			$("#kwota_platnosci").html("0");
			$("#platnosc_info").html('Przy zamówieniu powyżej 200 zł koszt przesyłki GRATIS');
		}
		
		var suma = kwota_koszyka+kwota_platnosci3;
        //alert("platnosc "+suma+" k"+ kwota_platnosci3+" k"+ kwota_koszyka);
		$("#kwota_razem").html(suma);
		
		// Code for handling 'c'
	});
	
	///$('#koszyk_main_ikona').click(function() {
		///$('#koszykMS').slideToggle('slow', function() {
			//$.print('Animation complete.');
			
			///$('#koszykMSz').html("ładowanie zawartości koszyka");
			
			///var data = 'koszyk_mini=1';
			
			///$.ajax({
			///    type:           "post",
			///    cache:          false,
			///    url:            "exe/koszyk_mini.ajax.php",
			///    data:           data, //{myJson:  data}
			///    success: function (html) {	
					
					//hide the progress bar
					//$('#loading').hide();	
					
					//add the content retrieved from ajax and put it in the #content div
			///		$('#koszykMSz').html(html);
					
					//display the body with fadeIn transition	
			///	}
		///    });
			
		///});
	///});
	
	$('#zamknijkoszykMS').click(function() {
		$('#koszykMS').slideToggle('slow', function() {
			//$.print('Animation complete.');
		});
	});
	
	// Expand Panel login
	$("#open").click(function(){
		$("div#panel").slideDown("slow");
	
	});	
	
	// Collapse Panel login
	$("#close").click(function(){
		$("div#panel").slideUp("slow");	
	});		
	
	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});	
	
	
	
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		//$('#mask').fadeIn(500);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(100); 
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		//$(this).hide();
		//$('.window').hide();
	});			
	
	

//$("input[name=DLogowanie]").click(function(e){
//		
//		e.preventDefault();
//		
//		$('#zawartoscPanelUzytkownika').html("loguje");
//		
//		var user = $("#uzytkownik").val();
//		var haslo = $("#haslo").val();
//		//Get the A tag
//		//var id = $(this).attr('href');
//		
//		var iddoc = $(this).attr('id');
//		
//		var data = 'uzytkownik=' + user + '&haslo=' + haslo;
//		
//		$.ajax({
//		    type:           "post",
//		    cache:          false,
//		    url:            "exe/logon.ajax.php",
//		    data:           data, //{myJson:  data}
//		    success: function (html) {	
//				
//				//hide the progress bar
//				//$('#loading').hide();	
//				
//				//add the content retrieved from ajax and put it in the #content div
//				$('#zawartoscPanelUzytkownika').html(html);
//				
//				//display the body with fadeIn transition
//				//$('#contentOkna').fadeIn('slow');		
//			}
//	    });
//	});

	
});

