$( document ).ready( function( ) {

    // Sets up the form monitor for forms that request this.
    setup_form_monitor( );
    
    // If we have clear fields, initiate.
	$('.clearfield').clearField( );
    
    // If we have a side image, make sure we have it rotating.
    $('div.side_image ul').innerfade({
        speed: 800,
        timeout: 3000,
        containerheight: '383px' 
    });    
    
    // If we have an inprogress call form, then set this to update every xx seconds.
    $("#inprogress_calls").everyTime( 5000, "inprogress_calls_timer", function( ) {
            update_inprogress_calls( );
    } );

    // If we have a call form, then initiate it's functions.
    if( $('#call_form').length != 0 ) {                        
        // Sets up all the rate lookups, country completion etc.
        initiate_call_form( );
    }
     
    $('.tooltip, .help_question').tooltip({
       track: true,
       delay: 0,
       showURL: false,
       showBody: " - ",
       fixPNG: true
    });         
        
    // Initiate cufon to replace all the headers with the font
    Cufon.replace('div.content_box h2, .custom_font')
    Cufon.now( );        
} );

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}


function setup_form_monitor( )
{
    $('form.monitor input, form.monitor select, form.monitor checkbox, form.monitor radio, form.monitor textarea').change( function( ) {
            if( changemade == false )
            {
                $('a').click( function( ) {
                    return confirm('There are unsaved changes to the form on this page! Click Ok to leave anyway, or cancel to stay.');
                });
                changemade = true;
            }            
    });     
}


function handleEnter(field, event) 
{
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
        var i;
        for (i = 0; i < field.form.elements.length; i++)
            if (field == field.form.elements[i])
                break;
        i = (i + 1) % field.form.elements.length;
        field.form.elements[i].focus();
        return false;
    } 
    else
    return true;
}      


