var form_submission = false;
var disable_exit_popup = false;

function form_validation_mod10(number) {
    var doubled = [];
    
    for (var i = number.length - 2; i >= 0; i = i - 2) {
        doubled.push(2 * number[i]);
    }
    
    var total = 0;
    
    for (var i = ((number.length % 2) == 0 ? 1 : 0); i < number.length; i = i + 2) {
        total += parseInt(number[i]);
    }
    
    for (var i = 0; i < doubled.length; i++) {
        var num = doubled[i];
        var digit;
        
        while (num != 0) {
            digit = num % 10;
            num = parseInt(num / 10);
            total += digit;
        }
    }
 
    if (total % 10 == 0) {
        return true;
    } 
}

function form_validation_card(number) {
    var validate;
    
    var card_type; 
    
    var card_split;
    var card_split_number;             
    var card_split_length;
   
    var card_check        = new Array();
    var card_check_number = new Array();
    var card_check_length = new Array();       

    // MOD10 Test
    if(!form_validation_mod10(number)) {
        return '';
    }

    // Card Number & Length Check
    for(i=0;i<card.length;i++) {
        card_check_number[i] = false;
        card_check_length[i] = false;
                                  
        card_split = card[i].split("|");
            
        card_split_number = card_split[1].split(",");
        card_split_length = card_split[2].split(",");
                 
        for(ii=0;ii<card_split_length.length;ii++) {
            if($('#card_number').val().length == card_split_length[ii]) {
                card_check_length[i] = true;
            }
        }            
                 
        for(ii=0;ii<card_split_number.length;ii++) {
            validate = new RegExp("^"+card_split_number[ii]+"[0-9]+$");
                     
            if(validate.test($('#card_number').val())) {
                card_check_number[i] = true;
            }
        }                             
    }              

    // Card Type
    for(i=0;i<card.length;i++) {
        if(card_check_length[i] && card_check_number[i]) card_type = i;
    }
           
    // Return    
    if(card_type >= 0 && card_type<=100) {     
        card_type = card[card_type].split("|");
        
        return card_type[0];
    } 
            
    return '';
}

$(function(){       
    $("#form").submit(function(e){   
        var error = new Array();                

        if($('#email').attr('id') && !$('#email').val()) {  
             error[error.length] = "E-Mail Required";
        }

        if($('#shipping_first_name').attr('id') && !$('#shipping_first_name').val()) {  
             error[error.length] = "Shipping - First Name Required";
        }

        if($('#shipping_last_name').attr('id') && !$('#shipping_last_name').val()) {     
             error[error.length] = "Shipping - Last Name Required";
        }

        if($('#shipping_address').attr('id') && !$('#shipping_address').val()) {     
             error[error.length] = "Shipping - Address Required";
        }

        if($('#shipping_zip').attr('id') && !$('#shipping_zip').val()) {     
             error[error.length] = "Shipping - Zip Required";
        }

        if($('#shipping_city').attr('id') && !$('#shipping_city').val()) {     
             error[error.length] = "Shipping - City Required";
        }        
             
        if($('#shipping_country').attr('id') && $('#shipping_state_'+$('#shipping_country').val().toLowerCase()+'_layer').attr('id') && !$('#shipping_state_'+$('#shipping_country').val().toLowerCase()).val()) {
             error[error.length] = "Shipping - State Required";
        }          

        if($('#shipping_country').attr('id') && !$('#shipping_country').val()) {     
             error[error.length] = "Shipping - Country Required";
        }          

        if($('#shipping_phone').attr('id') && !$('#shipping_phone').val()) {     
             error[error.length] = "Shipping - Phone Required";
        }
 
        if($('#billing_first_name').attr('id') && !$('#billing_first_name').val()) {  
             error[error.length] = "Billing - First Name Required";
        }

        if($('#billing_last_name').attr('id') && !$('#billing_last_name').val()) {     
             error[error.length] = "Billing - Last Name Required";
        }

        if($('#billing_address').attr('id') && !$('#billing_address').val()) {     
             error[error.length] = "Billing - Address Required";
        }

        if($('#billing_zip').attr('id') && !$('#billing_zip').val()) {     
             error[error.length] = "Billing - Zip Required";
        }

        if($('#billing_city').attr('id') && !$('#billing_city').val()) {     
             error[error.length] = "Billing - City Required";
        }        

        if($('#billing_country').attr('id') && !$('#billing_country').val()) {     
             error[error.length] = "Billing - Country Required";
        }

        if($('#billing_state_province').attr('id') && !$('#billing_state_province').val()) {     
             error[error.length] = "Billing - State/Province Required";
        }

        if($('#billing_phone').attr('id') && !$('#billing_phone').val()) {     
             error[error.length] = "Billing - Phone Required";
        }       
        
        if($('#card_number').attr('id') && !$('#card_number').val()) {  
             error[error.length] = "Card - Number Required";
        }

        if($('#card_cvv').attr('id') && !$('#card_cvv').val()) {     
             error[error.length] = "Card - CVV Required";
        }

        if($('#card_expiry_month').attr('id') && !$('#card_expiry_month').val()) {     
             error[error.length] = "Card - Expiry Month Required";
        }

        if($('#card_expiry_year').attr('id') && !$('#card_expiry_year').val()) {     
             error[error.length] = "Card - Expiry Year Required";
        }        

        if($('#card_number').attr('id') && $('#card_number').val()) {  
             var card_error = true;

             var card_split = new Array();
             
             var card_type_check;
             var card_type_possibility = new Array();

             // Get Card Type
             card_type_check = form_validation_card($('#card_number').val());     
             
             // Check Card Type & Card Type Possibility
             for(i=0;i<card.length;i++) {
                 card_split = card[i].split("|");
                 
                 if(card_split[3] == '1') {
                     card_type_possibility[card_type_possibility.length] = card_split[0];
                     
                     if(card_type_check == card_split[0]) card_error = false;
                 }
             }              

             // Card Type Possibility Implode
             card_type_possibility = card_type_possibility.join('/');
             
             // Card Error
             if(card_error) error[error.length] = "Card - Number Invalid (We only accept "+card_type_possibility+")";
        }        

        if(error.length) {
            alert('Missing or incorrect data:\n\n'+error.join('\n')+'\n\nPlease correct the data and click Join button again.');

            return false;
        }
        
        if(form_submission) {
            return false;
        }       
        
        form_submission = true;                 
        disable_exit_popup = true;                
        
        if($('#loadmask_overlay').attr('id')) {
            $("#loadmask_overlay").show();        
            $("#loadmask_overlay").mask("Processing might take up to 2 Minutes, Please wait.");
        }
    });

    $('#shipping_country').change(function() {
        $('.shipping_state_province_layer').hide();
        
        if($('#shipping_state_'+$('#shipping_country').val().toLowerCase()+'_layer').attr('id')) {
            $('#shipping_state_'+$('#shipping_country').val().toLowerCase()+' option:first').attr('selected',true);
            
            $('#shipping_state_'+$('#shipping_country').val().toLowerCase()+'_layer').show();
        } else {           
            $('#shipping_province').val('');
            
            $('#shipping_province_layer').show();
        }
    });       
});
