/*
 * Simple Email Sender - AJAX functions.
 *
 * By Artyom Gordiyevsky <kievan@gmail.com>
 * This code is licensed under a Creative
 * Commons Attribution-ShareAlike 3.0 License
 * http://creativecommons.org/licenses/by-sa/3.0/
 *
 * Thu Jun 19, 2008
 *
 *************************************************/

/*
 * GLOBALS
 *********/
 var ini_email_msg = "Your e-mail here";
 /*
  * GLOBALS END
  *************/ 

$(document).ready(
                  function()
                  {
                    $("#email_input").focus
                    (
                    function ()
                    {
                        var val = get_value( "email_input" )
                        if( val == ini_email_msg )
                            set_value( "email_input", "" );
                    }
                    );
                    
                    $("#email_input").blur
                    (
                    function ()
                    {
                        var val = get_value( "email_input" )
                        if( val == "" )
                            set_value( "email_input", ini_email_msg );
                    }
                    );                    
                  }
                 );
function set_value( id, val )
{
    document.getElementById( id ).value = val;
}
function get_value( id )
{
    return document.getElementById( id ).value;
}

/* Params: id     -> id of an elem into which data will be injected
 *         method -> which method of injection to use
 *         data   -> data to inject
 ******************************************************************/
function inject( id, method, data )
{
    switch( method )
    {
        // Insert inner html.
        case 1:
            $(id).html(data);
            break;
        
        default:
            document.write( "Func: inject() -> default case reached." );
            break;
    }
}

function email_submit()
{
    var email_val = get_value( "email_input" );
    $.post(
            "php/email.php",
            { email: email_val },
            function(data)
            {
                inject_resp( data, "#status" );
            }
          );
}

function inject_resp( data, id )
{
    var method = 1;
    var empty = "''";
    
    inject( id, method, data );
    setTimeout( "inject(" + "'" + id + "'" + "," + method + "," + empty + ")", 7000 );
}