
  function valid(text,type){
    switch(type){
      case 'login': var regex = /^[a-zA-Z0-9_-]{6,32}$/; break;
      case 'password': var regex = /^[a-zA-Z0-9_-]{6,32}$/; break;
      case 'email': var regex = /^[0-9a-z]+([\.\-_][0-9a-z]+)*\@[0-9a-z]+([\.\-][0-9a-z]+)*\.([a-z]{2,4})$/; break;
      case 'numeric': var regex = /^[0-9]/; break;
      default: var regex = /^[a-zA-Z0-9_-]{6,32}$/;
    }
    return regex.test(text);
  }
  
  $(document).ready(function(){

    //powodowało błędy w adminie.
    /*$('.help').tooltip({
    	  showURL: false,
    	  track: true,
    	  fade: 250,
    	  showBody: "#"
    });*/
    
    $("#ajax_loader").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow' : true, 'hideOnContentClick' : false, 'callbackOnShow' : function(){ $('#fancy_close').hide();	$("#fancy_overlay, #fancy_close").unbind("click") } });

    $('#es_img_refresh').click(function(){
      $('#es_val_img').attr('src','../obrazek.php5?'+Math.random());
    });
        
    $('#annMenu a').click(function(){                  
      var catId = $(this).parent().attr('id');            
      var exCatId = catId.split('_');                  
      var mainEl = $('#annMenu');
      mainEl.children('[class]:not(.subcat_'+exCatId[1]+')').hide();
      mainEl.children('.subcat_'+exCatId[1]+'').toggle();
      
    });
    
		$('#mainmenu table td a').hover(
      function(){
        $(this).parents('td').css({'background-image':'url(\'../images/bg_activemenu.jpg\')','background-repeat':'repeat-x'});
        $(this).parent().css({'background-image':'url(\'../images/bg_activemenu_left.jpg\')','background-repeat':'no-repeat','background-position':'left'});
        $(this).parent().parent().css({'background-image':'url(\'../images/bg_activemenu_right.jpg\')','background-repeat':'no-repeat','background-position':'right'});
        $(this).css('color','#fafae2');				 			
      },
      function(){
        $(this).parents('td').css('background','');
        $(this).parent().css('background','');
        $(this).parent().parent().css('background','');
        $(this).css('color','');
      }				 		
    );
    $('#lg_mainmenu a').hover(
      function(){
        $(this).parents('.single').css({'background-image':'url(\'../images/bg_activemenu.jpg\')','background-repeat':'repeat-x'});
        $(this).parent().parent().parent().prev('div').css({'background-image':'url(\'../images/bg_activemenu_left.jpg\')','background-repeat':'no-repeat','background-position':'left'});
        $(this).parent().parent().parent().next('div').css({'background-image':'url(\'../images/bg_activemenu_right.jpg\')','background-repeat':'no-repeat','background-position':'right'});
        $(this).css('color','#fafae2');				 			
      },
      function(){
        $(this).parents('.single').css('background','');
        $(this).parent().parent().parent().prev('div').css('background','');
        $(this).parent().parent().parent().next('div').css('background','');
        $(this).css('color','');
      }				 		
    );
    
    $('.fschangesize span').click(function(){
      $('.fschangesize span').not(this).css('text-decoration','none');
      $(this).css('text-decoration','underline');
      var fid = $(this).attr('id');
      if(fid == 'fsnormal'){
        $('div#services_details_desc').css({'font-size':'100%','line-height':'16px'});
      } else if(fid == 'fsbigger') {
        $('div#services_details_desc').css({'font-size':'120%','line-height':'19px'});
      } else if(fid == 'fsbiggest') {
        $('div#services_details_desc').css({'font-size':'140%','line-height':'22px'});
      }
    });
    
    $('form#AnnRegisterForm input, form#AnnLoginForm input, form#PassReminderForm input').blur(function(){
      if($(this).val() !='' )
        $(this).next().next().text('');
    });
    
    /**************************************************************************/     
    
    $('form#AnnRegisterForm #annRegisterButton').click(function(){
       $('#main_msg').text('');

       if($('#cust_login').val() == undefined || 
          $('#cust_password').val() == undefined || 
          $('#cust_password_repeat').val() == undefined || 
          $('#cust_email').val() == undefined || 
          $('#cust_email_repeat').val() == undefined ||  
          $('#es_text_image').val() == undefined){
          alert('You buster!');
          return false;
       }
       
       var errorCounter = 0;       
       if( $('#cust_login').val() =='' ){
        $('#cust_login').next().next().text('#Wpisz login.');
        errorCounter++;
       }
       if( !valid($('#cust_password').val(),'password') ){
        $('#cust_password').next().next().text('#wymagane 6 do 32 znaków z zakresu a-zA-Z0-9_-');
        errorCounter++;
       }
       if( !valid($('#cust_password_repeat').val(),'password') ){
        $('#cust_password_repeat').next().next().text('#wymagane 6 do 32 znaków z zakresu a-zA-Z0-9_-');
        errorCounter++;
       }
       if( $('#cust_email').val() =='' ){
        $('#cust_email').next().next().text('#Wpisz e-mail.');
        errorCounter++;
       }
       if( $('#cust_email_repeat').val() =='' ){
        $('#cust_email_repeat').next().next().text('#Wpisz e-mail.');
        errorCounter++;
       }
       if( $('#es_text_image').val()==''){
        $('#es_text_image').next().next().text('#Wpisz tekst z obrazka.');
        errorCounter++;
       }  
       
      if(!errorCounter) {          
         
            $.ajax({ 
              type:"POST",
              url: mainurl+"?module=customers&action=register&token="+token,
              data: "cust_login="+$('#cust_login').val()+
                    "&cust_password="+MD5($('#cust_password').val())+
                    "&cust_password_repeat="+MD5($('#cust_password_repeat').val())+
                    "&cust_email="+$('#cust_email').val()+                  
                    "&cust_email_repeat="+$('#cust_email_repeat').val()+                  
                    "&es_text_image="+$('#es_text_image').val(),
              dataType: 'xml',
              beforeSend: function(){
                $('form#AnnRegisterForm input').attr("disabled", true);  
                $('#ajax_loader').click();
              }, 			
              success: function(xml){
                $('form#AnnRegisterForm input').attr("disabled", false);
                $('span.smsg').text('');                
                $(xml).find('item').each(function(){                
                    //main_msg/php
                    if($(this).attr('key') == 'main_msg'){                     
                      $('#main_msg').text($(this).attr('value'));                        
                      $('form#AnnRegisterForm input:not([type=button])').val('');
                     } else {
                      // show error
                      $('#'+$(this).attr('key')).next().next().text($(this).attr('value'));
                    }              
                }); // end find             
             $.fn.fancybox.close();
             } // end success           
           }); // end ajax
              
       } // end errorCounter  
       // refresh if ajax request
       
       $('#es_img_refresh').ajaxStop(function(){
          $(this).click();
       });  
            
    });
  
    /**************************************************************************/       
  
    $('form#AnnLoginForm #annLoginButton').click(function(){
      $('#main_msg').text('');

      if($('#cust_login').val() == undefined || $('#cust_password').val() == undefined){
          alert('You buster!');
          return false;
      }      

      var errorCounter = 0;       
      if( $('#cust_login').val() =='' ){
        $('#cust_login').next().next().text('#Wpisz login.');
        errorCounter++;
      }
      if( !valid($('#cust_password').val(),'password') ){
        $('#cust_password').next().next().text('#wymagane 6 do 32 znaków z zakresu a-zA-Z0-9_-');
        errorCounter++;
      }
      
      if(errorCounter) return false;  
    
      $.ajax({
        type:"POST",
        url: mainurl+"?module=customers&action=login&token="+token,
        data: "cust_login="+$('#cust_login').val()+
              "&cust_password="+MD5($('#cust_password').val()),
        dataType: 'xml',
        beforeSend: function(){
           $('form#AnnLoginForm input').attr("disabled", true);
           $('#ajax_loader').click();   
        }, 			
        success: function(xml){
          $('form#AnnLoginForm input').attr("disabled", false);
          $('span.smsg').text('');
          $(xml).find('item').each(function(){  
            if($(this).attr('key') == 'main_msg'){                     
                if($(this).attr('value') == 'ok'){
                  $('#main_msg').text('Zalogowałeś się poprawnie. Zaraz zostaniesz przeniesiony do panelu administracyjnego.');                        
                  location.assign(document.location);
                }
                else {
                  $('#main_msg').text($(this).attr('value'));                        
                  $('form#AnnLoginForm input:not([type=button])').val('');
                }
            } else {
              //show error
              $('#'+$(this).attr('key')).next().next().text($(this).attr('value'));
            }           
          }); //end find
          $.fn.fancybox.close(); 
        } // end succness
      }); //end ajax
    });
                   
                   
    /**************************************************************************/                          
                   
    $('form#PassReminderForm #PassReminderButton').click(function(){
      $('#main_msg').text('');

      if($('#email_or_login').val() == undefined){
          alert('You buster!');
          return false;
      }      

      var errorCounter = 0;       
       if( $('#email_or_login').val() == '' ){
        $('#cust_email').next().next().text('#Wpisz e-mail lub login.');
        errorCounter++;
      }
      
      if( $('#es_text_image').val()==''){
        $('#es_text_image').next().next().text('#Wpisz tekst z obrazka.');
        errorCounter++;
      } 
      
      if(errorCounter) return false;  
    
      $.ajax({
        type:"POST",
        url: mainurl+"?module=customers&action=passReminder&token="+token,
        data: "email_or_login="+$('#email_or_login').val()+
              "&es_text_image="+$('#es_text_image').val(),
        dataType: 'xml',
        beforeSend: function(){
           $('form#PassReminderForm input').attr("disabled", true);
           $('#ajax_loader').click();   
        }, 			
        success: function(xml){
            $('form#PassReminderForm input').attr("disabled", false);
            $('span.smsg').text('');            
            $(xml).find('item').each(function(){                
                //main_msg/php
                if($(this).attr('key') == 'main_msg'){                     
                  $('#main_msg').text($(this).attr('value'));                        
                  $('form#PassReminderForm input:not([type=button])').val('');
                 } else {
                  // show error
                  $('#'+$(this).attr('key')).next().next().text($(this).attr('value'));
                }              
            }); // end find             
           $.fn.fancybox.close();
        } // end success   
      }); //end ajax
    });
   
    
    $('#AnnLoginForm input:not([type=button])').keypress(function (e) {   
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {   
            $('#AnnLoginForm input#annLoginButton').click();   
        }   
    });

  });
  
