﻿$(document).ready(function(){  

	//Forms        
    InitInputDefaultValue(".defaultvalue_input");    
    InitSubmitDefaultValue(".defaultvalue_submit", ".defaultvalue_input");
    
    $('select.select_redirect').show();        
    selectRedirect('select.select_redirect');    

    apply_tab_highlight('.downloadtable');
});

function selectRedirect(select_id)
{
    $(select_id).change(function(){
        if ($(this).val() != '')
        {
            if ($(this).val().match('http://')) 
                window.open($(this).val());
            else            
                document.location.href = $(this).val();
        }
        return false;
    });    
}

function apply_tab_highlight(table_id)
{
    if ($(table_id))
    {
        $(table_id + " tr").hover(function(){
            $(this).addClass("hover");
        },function(){
            $(this).removeClass("hover");
        });
    }
}

//--------------------------------------------- Forms
/* initialize the inputs which requires a 'default value' system */
function InitInputDefaultValue(input_class){
      
    $(input_class).each(function(i){   
        var current_input_id =  $(this).attr("id");
        var default_value = GetInputDefaultValue(current_input_id);        
        if($(this).val() == ""){
            $(this).val(default_value);
        }
        InitInputDefaultValueEvents("#" + current_input_id, default_value)
    });            
}

/* set the focus and blur events for the 'default value' inputs */
function InitInputDefaultValueEvents(input_id, default_value){

    $(input_id).click(function(){
        if($(this).val() == default_value)
            $(this).val("");
    });
    
    $(input_id).blur(function(){
        if($(this).val() == "")
            $(this).val(default_value);
    });
}

/* return the value of the label associated to the input */
function GetInputDefaultValue(input_id){
         
    var input_default_value = "";    
    $("label").each(function(j){
        if($(this).attr("for") == input_id){
            input_default_value = $(this).text();
        }
    });
    return input_default_value;
}

/* clear the value of the input still using their 'default value' when the form is submitted */
function InitSubmitDefaultValue(submit_class, defaultvalue_class){
    
    $(submit_class).click(function(){                                        
        $(this).parent().find(defaultvalue_class).each(function(i){
            var default_value = GetInputDefaultValue($(this).attr("id"));
            if($(this).val() == default_value){
                $(this).val("");
            }
        });        
    });
}
