/* Author: 

*/

 Cufon.replace('#motto,h1,h2,h3,#menu_headline',{hover: true}); 
 
$(document).ready(function() {
    
    $("#first_name").focus();
 
jQuery('.placeholder').each(function(i) {
 
     var item = jQuery(this);
     var text = item.attr('rel');
     var form = item.parents('form:first');

     if (item.val() === '')
     {
     item.val(text);
     item.css('color', '#888');
     }
     
     item.bind('focus.placeholder', function(event) {
     if (item.val() === text)
     item.val('');
     item.css('color', '');
     });
     
     item.bind('blur.placeholder', function(event) {
     if (item.val() === '')
     {
     item.val(text);
     item.css('color', '#888');
     }
     });
     
     form.bind("submit.placeholder", function(event) {
         if (item.val() === text)
            item.val("");
     });
 
 });
/* 
$("input").focusin(function(){
   $(this).prev('label').children('p').show();
   
});

$("input").focusout(function(){
   $(this).prev('label').children('p').hide(); 
});
*/
$("input.required").focusout(function(){
   if($(this).val() != '' && $(this).val() != $(this).attr('rel'))
      $(this).css({'background-color' : '#fff', 'background-position' : '189px -61px', 'outline' : 'none'});
   else
   {
      $(this).css({'background-position' : '189px -29px', 'outline' : 'none'});
      $(this).addClass('error');
   }
});

$("input#price, input#property_price").focusout(function(){
   if($(this).val() >= 100000)
      $(this).css({'background-color' : '#fff', 'background-position' : '189px -61px', 'outline' : 'none'});
   else
   {
      $(this).css({'background-position' : '189px -29px', 'outline' : 'none'});
      $(this).addClass('error');
   }
});

$("input#years").focusout(function(){
   if($(this).val() >= 2 && $(this).val() < 31)
      $(this).css({'background-color' : '#fff', 'background-position' : '189px -61px', 'outline' : 'none'});
   else
   {
      $(this).css({'background-position' : '189px -29px', 'outline' : 'none'});
      $(this).addClass('error');
   }
});

$("input#postcode").focusout(function(){
   if($(this).val() >= 10000 && $(this).val() < 100000)
      $(this).css({'background-color' : '#fff', 'background-position' : '189px -61px', 'outline' : 'none'});
   else
   {
      $(this).css({'background-position' : '189px -29px', 'outline' : 'none'});
      $(this).addClass('error');
   }
});

$("input#phone1").focusout(function(){
    var telReg = /^(\+420)? ?\d{3} ?\d{3} ?\d{3}$/;
    var tel = $('#phone1').val();
    if(tel == "" || !telReg.test(tel) || tel.val() == tel.attr('rel'))
    {
        $(this).css({'background-position' : '189px -29px', 'outline' : 'none'});
        $(this).addClass('error');
    }
    else
        $(this).css({'background-color' : '#fff', 'background-position' : '189px -61px', 'outline' : 'none'});
});

$("input#email").focusout(function(){
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var email = $('#email').val();
    if(email == "" || !emailReg.test(email))
    {
        $(this).css({'background-position' : '189px -29px', 'outline' : 'none'});
        $(this).addClass('error');
    }
    else
        $(this).css({'background-color' : '#fff', 'background-position' : '189px -61px', 'outline' : 'none'});
});

       
        

$("#hypform").submit(function(){
    
        $('input[type=submit]', this).attr('disabled', 'disabled');
        $('input[type=submit]', this).attr('value', 'Odesílám ...');
        
        $('input').each(function(){
          $(this).removeClass('error');
        });
        
        var message = "";
        var telReg = /^(\+420)? ?\d{3} ?\d{3} ?\d{3}$/;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        if(!$('#first_name').val())
        {
            message += "Jméno musí být vyplňeno!\n\n";
            $('#first_name').css({'background-position' : '189px 3px', 'outline' : 'none'});
            $('#first_name').addClass('error');
        }
        if(!$('#last_name').val())
        {
            message += "Příjmení musí být vyplňeno!\n\n";
            $('#last_name').css({'background-position' : '189px 3px', 'outline' : 'none'});
            $('#last_name').addClass('error');
        }
        
        if($('#price').val() < 100000)
        {
            message += "Výše hypotéky musí být zadána (min. 100 000 Kč)!\n\n";
            $('#price').css({'background-position' : '189px 3px', 'outline' : 'none'});
            $('#price').addClass('error');
        }    
        if($('#years').val() != "" && ($('#years').val() > 30 || $('#years').val() < 2))
        {
            message += "Splatnost musí být v rozmezí 2-30 let!\n\n";
            $('#years').css({'background-position' : '189px 3px', 'outline' : 'none'});
            $('#years').addClass('error');
        }    
        if($('#property_price').val() < 100000)
        {
            message += "Naši nabídku je možné využít pouze pokud máte nemovitosti do zástavy. V případě koupi nemovitosti je možné jako zástavu použít tuto nemovitost!\n\n";
            $('#property_price').css({'background-position' : '189px 3px', 'outline' : 'none'});
            $('#property_price').addClass('error');
        }    
        if(!$('#street').val())
        {
            message += "Ulice a číslo popisné/orientační musí být vyplněno!\n\n";
            $('#street').css({'background-position' : '189px 3px', 'outline' : 'none'});
            $('#street').addClass('error');
        }
        if(!$('#town').val())
        {
            message += "Město musí být vyplněno!\n\n";
            $('#town').css({'background-position' : '189px 3px', 'outline' : 'none'});
            $('#town').addClass('error');
        }    
        if($('#postcode').val() < 10000 || $('#postcode').val() > 100000)
        {
            message += "PSČ musí být korektně vyplňeno! (5 čísel bez mezer)\n\n";
            $('#postcode').css({'background-position' : '189px 3px', 'outline' : 'none'});
            $('#postcode').addClass('error');
        }
        var tel = $('#phone1').val();
        if(tel == "" || !telReg.test(tel)){
            message += "Chybí telefon nebo má nesprávný formát!\n\n";
            $('#phone1').css({'background-position' : '189px 3px', 'outline' : 'none'});
            $('#phone1').addClass('error');
        }
        var email = $('#email').val();
        if(email == "" || !emailReg.test(email) ){
            message += "Chybí e-mail nebo má nesprávný formát!\n\n";
            $('#email').css({'background-position' : '189px 3px', 'outline' : 'none'});
            $('#email').addClass('error');
        }
        
        if(!$('#agree').attr('checked'))
            message += "Před odesláním žádosti musí být udělen souhlas ke zpracování osobních údajů!\n\n";
        
        
        
        
        
        if(message)
        {
            alert(message);
             $('input[type=submit]', this).attr('value', 'odeslat žádost');
             $('input[type=submit]', this).removeAttr('disabled');
             
             jQuery('.placeholder').each(function(i) {
 
                 var item = jQuery(this);
                 var text = item.attr('rel');
                 var form = item.parents('form:first');

                 if (item.val() === '')
                 {
                 item.val(text);
                 item.css('color', '#888');
                 }
                 
                 item.bind('focus.placeholder', function(event) {
                 if (item.val() === text)
                 item.val('');
                 item.css('color', '');
                 });
                 
                 item.bind('blur.placeholder', function(event) {
                 if (item.val() === '')
                 {
                 item.val(text);
                 item.css('color', '#888');
                 }
                 });
                 
                 form.bind("submit.placeholder", function(event) {
                     if (item.val() === text)
                        item.val("");
                 });
             
             });
             $(this).scrollTop();
             $('.error').first().prev('label').focus();
             return false;
        }
    });
    
});

$(".hypocalculator").submit(function(){
    
              
        var message = "";
    
           
        if($('#amount').val() < 100000)
        {
            message += "Výše hypotéky musí být min. 100 000 Kč!\n\n";
        }    
        
        if(message)
        {
              alert(message);
             return false;
        }
    
});




















