
// Hack a delay method into jQuery object
//$.fn.delay = function(time, callback){
    // Empty function:
//  jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
//    return this.animate({delay:1}, time, callback);
//}



var popv_submitting = false;

function popv_loaded() {
    $('#i_firstname').focus();
    $('#bogform_container').slideDown( 'normal', popv_replace );
}

function popv_replace() {
}

function popv_no_thanks() {
    window.location = "default.asp?tr=2";
}

function popv_string_not_empty( s ) {
    return s.match(/\w+/);
}

function popv_email_valid( email ) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test( email ))
        return true;
    return false;
}

function popv_phonenum_valid( phonenum ) {
    var reg = /^[1-9][0-9]{7}$/;
    if(reg.test( phonenum ))
        return true;
    return false;
}

function popv_remove_whitespace( s ) {
    return s.replace( /\s+/g, '' );
}


function popv_form_error( fieldname, errormessage ) {
    $('div#formconsole').text( errormessage );
    var input_id = "#i_" + fieldname;
    var label_id = "#l_" + fieldname;
    var input = $( input_id );
    input.removeClass( 'textfield_normal' );
    input.addClass( 'textfield_error' );
    input.focus();
    popv_submitting = false;
    return false;
}


function popv_resetformerrors() {
    $('div#formconsole').text( "" );

    var fnames = ['firstname', 'lastname', 'company', 'address', 'mail', 'mobile'];

    $.each( fnames, function() {
            $('#i_' + this ).removeClass('textfield_error');
            $('#i_' + this ).addClass('textfield_normal');            
            $('#l_' + this ).removeClass('label_error');
            $('#l_' + this ).removeClass('label_normal');
        });

}


function popv_validate() {
    if( popv_submitting)
        return false;
    popv_submitting = true;
    popv_resetformerrors();
    var firstname =  $.trim( $('#i_firstname').val() );
    var lastname  =  $.trim( $('#i_lastname').val() );
    var company   =  $.trim( $('#i_company').val() );
    var address   =  $.trim( $('#i_address').val() );
    var mail      =  $.trim( $('#i_mail').val() );
    var mobile    =  $.trim( $('#i_mobile').val() );

    if(!popv_string_not_empty( firstname ))
        return popv_form_error("firstname", "Du skal indtaste fornavn");
    $('#i_firstname').val( firstname );

    if(!popv_string_not_empty( lastname ) )
        return popv_form_error("lastname", "Du skal indtaste dit efternavn");
    $('#i_lastname').val( lastname );

    if(!popv_string_not_empty( company ) )
        return popv_form_error("company", "Du skal indtaste dit firmas navn");
    $('#i_company').val( company );

    $('#i_address').val(  address );

    if(!popv_string_not_empty( mail ) )
        return popv_form_error("mail", "Du skal indtaste din mailadresse");
    if(!popv_email_valid(mail))
        return popv_form_error("mail", "Din mailadresse ser lidt sær ud");
    $('#i_mail').val( mail );

    if(popv_string_not_empty( mobile ) ) {
        mobile = popv_remove_whitespace( mobile );
        if( !popv_phonenum_valid( mobile ) )
            return popv_form_error("mobile", "Dit telefonnummer skal være et 8-cifret dansk nummer");
        $('#i_mobile' ).val( mobile );
    }
        else 
            $('#i_mobile' ).val( "" );
    
    $('#bogform_container').slideUp('normal', function() {
            $.post( $('#bogform').attr('action') , $('#bogform').serialize(), popv_senddata_callback );
        }
        );
    return false;
}

function popv_senddata_callback( data, text_status ) {
    popv_submitting = false;

    // Copy html from hidden div 
    var html = $('#text1').html();
    var text1 = $('#text1').clone();
    text1.css({display: "block"});
    $('#bogform_container').html( text1 );
    $('#bogform_container').slideDown('normal');
    // $('#bogform_container').delay(10000, function() { window.location("default.asp?tr=1") });
}


$(document).ready( popv_loaded );
