﻿/*******************************\
| Nazwa: Formularz AJAX         |
| Autor: Seba0p                 |
| Strona: http://seba0p.pl      |
\*******************************/
jQuery(document).ready(function(){ 
   getcaptcha();
   var frmoptions = { 
       target: null,
       beforeSubmit: frmverify,         
       success: frmsuccess,
       url: 'includes/contact/check.php',
       type: 'post',
       dataType: null
   }; 
   jQuery('#e_contactfrm').submit(function(){    
      jQuery(this).ajaxSubmit(frmoptions);
      return false;        
   }); 
   jQuery('#e_back_link').click(function(){
      getcaptcha();
      jQuery('#e_formalert').fadeOut(); 
      jQuery('#e_formback').fadeOut(function(){
         jQuery('#e_contactfrm').resetForm();
         jQuery('#e_formdata').fadeIn();
      });    
   });
   jQuery('#e_show_captcha').click(function(){
      getcaptcha();
   });
}); 
 
var frmfname = new Array('Imię', 'Nazwisko', 'Adres e-mail', 'Kod pocztowy', 'Miejscowość', 'tel.', 'Treść wiadomości', 'Przepisz kod');
var frmfmust = new Array('0', '1', '2', '5', '6', '7', '9', '11');

function getcaptcha(){
   jQuery('#e_show_captcha').html("Ładowanie kodu!");
   jQuery.post("e_captcha.php", { create: "true" }, showcaptcha, "html");
};

function showcaptcha(ctext, cstatus){ 
   if(cstatus == "success" && ctext != "no"){
      jQuery('#e_show_captcha').html(ctext);
   }else{
      jQuery('#e_show_captcha').html("Nie można pobrać kodu!");
   }
};

function trim(str){
   return str.replace(/^\s+|\s+$/g,"");
};
    
function frmverify(frmdata){ 
	var frmferror = "";
	for(var a = '0'; a < frmfmust.length; a++){
	   if(trim(frmdata[frmfmust[a]].value) == ""){
	      frmferror += "<b>" + frmfname[a] + "</b>, ";    
	   }
	}
	if(frmferror.length > '0'){
	   jQuery('#e_formalert').fadeOut(function(){   
	      jQuery('#e_formalert').attr('className', 'e_alerterr');
	   	jQuery('#e_formalert').html("Pola: " + frmferror.substring(0, frmferror.length-2) + " nie mogą pozostać puste!");
	   	jQuery('#e_formalert').fadeIn();
	   });	   
	   return false;
	}else{
      return true; 
   }
}; 

function frmsuccess(frmtext, frmstatus){
   var frmview = "";
   var frmclass = "e_alertok";
   var frmhide = false;
   if(frmstatus == "success" && frmtext == "ok"){
      frmview = "Wiadomość została wysłana!";
      frmclass = "e_alertok";
      frmhide = true;
   }else{
      if(frmtext == "no"){
         frmview = "Wiadomość nie została wysłana! Spróbuj ponownie później!";
         frmhide = true;
      }else{
         var frmtext2 = frmtext.substring(3);
         frmtext2 = frmtext2.split(",");
         if(frmtext2.length > '0'){
            frmview += "Pola: ";
			   for(var a = '0'; a < frmtext2.length; a++){
	      	   if(Number(frmtext2[a])){
	      	      frmview += "<b>" + frmfname[frmtext2[a]] + "</b>, ";  
	      	   }
	   		}
	   		frmview = frmview.substring(0, frmview.length-2);
	   		frmview += " nie zostały poprawnie uzupełnione!";
         }else{
            frmview = "Wiadomość nie została wysłana! Spróbuj ponownie później!";
            frmhide = true;
         }
      }
      frmclass = "e_alerterr";
   }
   jQuery('#e_formalert').fadeOut(function(){ 
	   jQuery('#e_formalert').attr('className', frmclass);
	  	jQuery('#e_formalert').html(frmview);
	   jQuery('#e_formalert').fadeIn();
	   if(frmhide == true){
	      jQuery('#e_formdata').fadeOut(function(){
            jQuery('#e_formback').fadeIn();
         });
	   }
   });
}; 