// function addGoogleStats() 
// {
    // var GAID = 'twój-GAID';
    // var head = document.getElementsByTagName('head')[0];
    // var scr = document.createElement('script');
    // scr.type = 'text/javascript';
    // scr.src = (location.protocol == 'http:' ? 'http://www.' : 'https://ssl.') + 'google-analytics.com/'+'ga.js';
    // var once = 0;

         // function runMe() { 
            // if (scr.readyState == 'loading' || _gat == undefined || once) { return; }
                // clearInterval(timer);
                // once++;

                // var pageTracker = _gat._getTracker(GAID);
                // pageTracker._initData();
                // pageTracker._trackPageview();
        // }

    // var timer = setInterval(runMe, 2000);
    // scr['onload'] = runMe;
    // scr['onreadystatechange'] = runMe;
    // head.appendChild(scr); 
// }

//window.onload = function init() { addGoogleStats(); };

$(document).ready(function()
{  
    // hide forms elements
    if ($("#army_delayed").attr("value") != "" && $("#army option:selected").attr("value") == 0)
    {
        $(".army_status").hide();
    }
    
    if ($("#civil_status option:selected").attr("value") != 2) 
    {
        $(".assets").hide();
    }
    
    if ( ! $("#contract").attr("checked") ) 
    {
        $(".contract").hide();
    }
    
    if ($("#contract_type option:selected").attr("value") != 2) {
      	$(".contract_to").hide();
    }
    
    if ( ! $("#business").attr("checked") ) 
    {
        $(".business").hide();
    }

    if ( ! $("#pension").attr("checked") ) 
    {
        $(".pension").hide();
    }    
    
    if ( ! $("#pension_oldt").attr("checked") ) 
    {
        $(".pension_old").hide();
    }
    
    if ( ! $("#other_income").attr("checked") ) 
    {
        $(".other_income").hide();
    }
    
    var product = $("#product option:selected").attr("value");
    
    if (product == 0) 
    {
        $(".cash").hide();
        $(".car").hide();
    }
    else if (product == 4)
    {
        $(".car").hide();
    }
    else
    {
        $(".cash").hide();
    }
    
    //let's do some magick
    $("#id_nr").click(function() 
    {
    	var pesel = $("#pesel").attr('value');
        
        if (! pesel) {
        	return false;
        }
        
        var year       = 1900 + parseInt(pesel.substr(0,2));
        var today      = new Date();
        var today_year = today.getFullYear();
        var age        = today_year - year;
        
        if (age < 18) 
        {
            alert("Powinieneś miec ukończone 18 lat");
            $(".army_status").hide();
        }
        else if (age >= 18 && age <= 28)
        {
            $(".army_status").show();
        }
        else
        {
            $(".army_status").hide();
        }
    });
    
    $("#civil_status").change(function() {
    	$("#civil_status option:selected").each(function() {

        if (parseInt($(this).attr("value")) == 2) {
    		$(".assets").show();
    	}
        else
        {
            $(".assets").hide();
        }
        });
    });
    
    $("#contract_type").change(function() {
    	$("#contract_type option:selected").each(function() {

        if (parseInt($(this).attr("value")) == 2) {
    		$(".contract_to").show();
    	}
        else
        {
            $(".contract_to").hide();
        }
        });
    });
    
    $(".select_income").click(function() {
    	var income = $(this).attr('name');
        $("."+income).toggle();
    });
    
    $("#product").change(function() {
    	$("#product option:selected").each(function() {
        var index = parseInt($(this).attr("value"));
        if (index >= 1 && index < 4) {
            $(".cash").hide();
    		$(".car").show();
    	}
        else
        {
            $(".car").hide();
            $(".cash").show();
        }
        });
    });
    
    $("#loan").submit(function() {
        var show_error  = false;
        var to_validate = new Array();
        to_validate[0]  = 'borrower_name';
        to_validate[1]  = 'pesel';
        to_validate[2]  = 'id_nr';
        to_validate[3]  = 'borrower_address';
        to_validate[4]  = 'phone';
        to_validate[5]  = 'email';
        to_validate[6]  = 'depending_people';
        to_validate[7]  = 'other_debts';
        to_validate[8]  = 'net_income';
        
        for(var i = 0; i < to_validate.length; i++)
        {
            if (! $("#"+to_validate[i]).attr("value")) 
            {
            	$("#"+to_validate[i]).attr("class", "form_error");
                
                if (show_error == false) 
                {
                	$("#show_js_error").show();
                    show_error = true;
                }
            }
        }
        
        if ($("#civil_status option:selected").attr("value") == 0)
        {
        	$("#civil_status").attr("class", "form_error");
            
            if (show_error == false) 
            {
            	$("#show_js_error").show();
                show_error = true;
            }
        }
        
        if ($("#product option:selected").attr("value") == 0) 
        {
        	$("#product").attr("class", "form_error");
            
            if (show_error == false) 
            {
            	$("#show_js_error").show();
                show_error = true;
            }
        }
        
        if ($("#sex option:selected").attr("value") == 0) 
        {
        	$("#sex").attr("class", "form_error");
            
            if (show_error == false) 
            {
            	$("#show_js_error").show();
                show_error = true;
            }
        }
        
        if (! $("#contract").attr("checked") && 
            ! $("#business").attr("checked") && 
            ! $("#pension").attr("checked") &&
            ! $("#pension_old").attr("checked") &&
            ! $("#other_income").attr("checked")
        ) {
        	$("#contract").attr("class", "form_error");
            $("#business").attr("class", "form_error");
            $("#pension").attr("class", "form_error");
            $("#pension_old").attr("class", "form_error");
            $("#other_income").attr("class", "form_error");
            
            if (show_error == false) 
            {
            	$("#show_js_error").show();
                show_error = true;
            }
        }
        
        if (show_error) 
        {
            return false;
        }
    });
    
    $("#contact").submit(function() {
        var show_error = false;
        
        if ($("#product option:selected").attr("value") == 0)
        {
        	$("#product").attr("class", "form_error");
            
            if (show_error == false) 
            {
            	$("#show_js_error").show();
                show_error = true;
            }
        }
        
        if (! $("#name").attr("value"))
        {
        	$("#name").attr("class", "form_error");
            
            if (show_error == false) 
            {
            	$("#show_js_error").show();
                show_error = true;
            }
        }
        
        if (! $("#money").attr("value"))
        {
        	$("#money").attr("class", "form_error");
            
            if (show_error == false) 
            {
            	$("#show_js_error").show();
                show_error = true;
            }
        }
        
        if (! $("#email").attr("value"))
        {
        	$("#email").attr("class", "form_error");
            
            if (show_error == false) 
            {
            	$("#show_js_error").show();
                show_error = true;
            }
        }
        
        if (! $("#phone").attr("value"))
        {
        	$("#phone").attr("class", "form_error");
            
            if (show_error == false) 
            {
            	$("#show_js_error").show();
                show_error = true;
            }
        }
        
        if (show_error) 
        {
            return false;
        }
    });
});
