jQuery(document).ready(function(){

    //if there are some scroll-panes, let`s add some functionality. used in Step 2
    jQuery('.scroll-pane').jScrollPane({
        showArrows:true,
        scrollbarWidth: 20,
        scrollbarMargin: 20,
        arrowSize: 22,
        dragMinHeight: 35,
        dragMaxHeight: 35
    });

    jQuery(document).pngFix(); //for IE`s, png transparency

    //if there are some datepickers.. used in Step 3
    jQuery('#date').datepick({
        useThemeRoller: true,
        mandatory: true,
        clearText: '',
        closeText: '',
        minDate: '-30Y',
        maxDate: '-1D',
        yearRange: '-31:+31',
        dateFormat: 'd.m.yy'
    });

    //custom checkbox styling function - Step 2
    var $this = jQuery(".customcheckbox");
    jQuery(".customcheckbox").toggle(function(){
        $this.addClass("checked");
        jQuery(".checkbox").attr("checked","checked");
        validate_agree();
    },function(){
        $this.removeClass("checked");
        jQuery(".checkbox").removeAttr("checked");
        validate_agree();
    });

    //hover button functionality
    jQuery("#img_home").hover(function(){
        jQuery(this).attr("src","/img/menu_buttons/home_hover.gif");
    }, function(){
        jQuery(this).attr("src","/img/menu_buttons/home.gif");
    });

    jQuery("#img_terms").hover(function(){
        jQuery(this).attr("src","/img/menu_buttons/terms_hover.gif");
    }, function(){
        jQuery(this).attr("src","/img/menu_buttons/terms.gif");
    });

    jQuery("#img_faq").hover(function(){
        jQuery(this).attr("src","/img/menu_buttons/faq_hover.gif");
    }, function(){
        jQuery(this).attr("src","/img/menu_buttons/faq.gif");
    });

    jQuery("#img_contact").hover(function(){
        jQuery(this).attr("src","/img/menu_buttons/contact_hover.gif");
    }, function(){
        jQuery(this).attr("src","/img/menu_buttons/contact.gif");
    });

    jQuery("#back input").hover(function(){
        jQuery(this).attr("src","/img/hover_back_color.png");
    }, function(){
        jQuery(this).attr("src","/img/back_color.png");
    });

    jQuery("#next input").hover(function(){
        jQuery(this).attr("src","/img/hover_next_color.png");
    }, function(){
        jQuery(this).attr("src","/img/next_color.png");
    });
    
    jQuery("#members tr:even").css("color", "#FAD");
    
    /*jQuery("#nextfirst").click(function(){
		if(validate_residence())	{
			new Ajax.Updater('content', '/second-step/'+jQuery("#residence").val(),{
				onComplete: function(f)	{
						jQuery('.scroll-pane').jScrollPane({
						        showArrows:true,
						        scrollbarWidth: 20,
						        scrollbarMargin: 20,
						        arrowSize: 22,
						        dragMinHeight: 35,
						        dragMaxHeight: 35
					    });
					    
					    jQuery(".customcheckbox").toggle(function(){
					        jQuery(this).addClass("checked");
					        jQuery(".checkbox").attr("checked","checked");
					        validate_agree();
					    },function(){
					        jQuery(this).removeClass("checked");
					        jQuery(".checkbox").removeAttr("checked");
					        validate_agree();
					    });
					    
					    jQuery("#back input").hover(function(){
					        jQuery(this).attr("src","/img/hover_back_color.png");
					    }, function(){
					        jQuery(this).attr("src","/img/back_color.png");
					    });
					
					    jQuery("#next input").hover(function(){
					        jQuery(this).attr("src","/img/hover_next_color.png");
					    }, function(){
					        jQuery(this).attr("src","/img/next_color.png");
					    });
					    
					    jQuery("#nextsecond").click(function(){
							if(validate_agree())	{
								new Ajax.Updater('content', '/third-step',{
									onComplete: function(f)	{
										
									}
								});
							}
						});
					    
					    //jQuery.preloadImages("/img/next_color.png","/img/next.png","/img/back_color.png","/img/back.png","/img/hover_back_color.png","/img/hover_next_color.png");
				}
			});
		}
	});*/
	
	

});


// END of jQuery ready function

//validate function for Step 1 - returns true if residence is selected, otherwise false
function validate_residence()
{
    if((jQuery("#residence").val()=="") || (jQuery("#residence").val()=="null"))
    {
        jQuery("#residenceok").attr("src","/img/error.png");
        return false;
    }
    else
    {
        jQuery("#residenceok").attr("src","/img/ok.png");
    }
    return true;
}

//validate Step 2 - checked I agree checkbox
function validate_agree()
{
    if(jQuery('#agree').is(':checked'))
    {
        jQuery("#agreeok").attr("src","/img/ok.png");
        return true;
    }
    else
    {
        jQuery("#agreeok").attr("src","/img/error.png");
    }
    return false;
}

//validate Step 3 - checks all forms component
function validate_form()
{
    var ret = true;
    //elements` names
    // familyname, firstname, date, idnumber, street, city, mail, zip
    ret = validate_empty("#familyname") && ret;
    ret = validate_empty("#firstname") && ret;
    ret = validate_empty("#date") && ret;
    ret = validate_empty("#idnumber") && ret;
    ret = validate_empty("#street") && ret;
    ret = validate_empty("#city") && ret;
    ret = validate_empty("#mail") && ret;
    ret = validate_empty("#zip") && ret;
    ret = validate_empty("#captcha-form") && ret;
    return ret;
}

//validates Contact Us form
function validate_message()
{
    var ret = true;
    ret = validate_empty("#contact_name") && ret;
    ret = validate_empty("#contact_mail") && ret;

    if((jQuery("#contact_message").val()=="")||(jQuery("#contact_message").val()=="Please, type your message here"))
    {
        jQuery("#contact_message").val("Please, type your message here");
        ret = false;
    }
    return ret;
}

//checks if input is empty
function validate_empty(component)
{
    var name = component + "ok";
    if(jQuery(component).val()=="")
    {
        jQuery(name).attr("src","/img/error.png");
        return false;
    }
    else
    {
        jQuery(name).attr("src","/img/ok.png");
    }
    return true;
}

//validates email address something@sth.st
function validate_mail(component)
{  
    var name = component + "ok";
    var re = new RegExp("^[0-9a-zA-Z._-]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");

    if(re.test(jQuery(component).val()))
    {
        jQuery(name).attr("src","/img/ok.png");
        return true;
    }
    else
    {
        jQuery(name).attr("src","/img/error.png");
    }
    return false;
}

//checks 5 digit of zip code
function validate_zip(component)
{
    var name = component + "ok";
    var re = new RegExp("^[0-9]{5}$");

    if(re.test(jQuery(component).val()))
    {
        jQuery(name).attr("src","/img/ok.png");
        return true;
    }
    else
    {
        jQuery(name).attr("src","/img/error.png");
    }
    return false;
}

//checks if country has a link to external page
function check_country(id)	{
	new Ajax.Request('/check_country/'+id, {
		onComplete: function(f)	{
			jQuery('#step1').attr('action', f.responseText);
		}
	});
}

//checks if country is usa, if so then create input for state
function check_usa(id)	{
	if($('id_country').options[$('id_country').selectedIndex].value == id)	{
		jQuery('.state_input').css('visibility', 'visible');
		jQuery('.state_input').fadeIn(500);
	} else {
		jQuery('.state_input').fadeOut(500);
		jQuery('.state_input').css('visibility', 'hidden');
	}
}

//preloading images on the page
jQuery.preloadImages = function()
{
    for(var i = 0; i<arguments.length; i++)
    {
        jQuery("<img>").attr("src", arguments[i]);
    }
}
