// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//^$(document).ready(function() { 
//^    $("table") 
//^    .tablesorter({widthFixed: true, widgets: ['zebra'], sortList: [[0,0], [1,0]]}) 
//^    .tablesorterPager({container: $("#pager"), positionFixed: false}); 
//^/*	$("table").tablesorter( {sortList: [[0,0], [1,0]]} ); */
//^	$("a.button").click(function(){
//^	   this.blur();
//^	});
//^	$("a.button").wrapInner("<span></span>")
//^});


function copyAddressToBilling()  {
    if ($('billing_is_same').checked == false)  {
        // reset billing address form  
        $('billing_address').value = "";
        $('billing_city').value = "";
        $('billing_state').selectedIndex = 0;
        $('billing_zip').value = "";
        $('billing_country').selectedIndex = 0;
    }
    else  {
        $('billing_address').value = $('address1').value;
        $('billing_city').value = $('city').value;
        $('billing_state').selectedIndex = $('state').selectedIndex;
        $('billing_state_other').value = $('state_other').value;
        $('billing_zip').value = $('zip').value;
        $('billing_country').selectedIndex = $('country').selectedIndex;
    }
}

/**
 * Validate Payment information
 */
function validate_credit_card()  {
    if ($('payment_method').options[$('payment_method').selectedIndex].text == "Select Credit Card")  {
        errorMessage += "Credit card is required\n";
    }   
    
    // validate card number
    if ($('card_number').value == "")  {
        errorMessage += "Credit card number is required\n";
    }   
    else  {
        var cardnum = new String($('card_number').value);
        // get rid of whitespace
        cardnum = cardnum.replace(/^\s+/,'').replace(/\s+$/,'');

        // make sure there aren't any spaces or dashes
        if (cardnum.match(/(\s|-)/))  {
            errorMessage += "Credit card number cannot have dashes or spaces\n";
        }
        else  {
            if ($('payment_method').value == 'amex')  {
                // there should be 15 numbers
                if (cardnum.length != 15)  {
                    errorMessage += "Credit card number has the wrong number of digits\n";
                }
            }
            else  {
                // these should have 16 numbers
                if (cardnum.length != 16)  {
                    errorMessage += "Credit card number has the wrong number of digits\n";
                }
            }
        }
    }

    if ($('exp_month').options[$('exp_month').selectedIndex].text == "Month" ||
        $('exp_year').options[$('exp_year').selectedIndex].text == "Year")  {
        errorMessage += "Expiration date is required\n";
    }
    if ($('billing_name').value == "")  {
        errorMessage += "Exact name on card is required\n";
    }
    if ($('cvv_code').value == "")  {
        errorMessage += "Credit card code is required\n";
    }
}

function showSecurityCodeMessage()  {
    if ($F('payment_method') == 'amex')  {
        Element.show('amex_cvv_help');
        Element.hide('other_cvv_help');
    }
    else if ($F('payment_method') == 'Select Credit Card')  {
        alert('Please select a credit card first.');
    }
    else  {
        Element.hide('amex_cvv_help');
        Element.show('other_cvv_help');
    }
}

var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=450,height=350,resizable';

function raw_popup(url, target, features) {
    if (isUndefined(features)) {
        features = _POPUP_FEATURES;
    }
    
    if (isUndefined(target)) {
        target = '_blank';
    }
    
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    
    return theWindow;
}

function link_popup(src, features) {
    return raw_popup(src.getAttribute('href'),
        src.getAttribute('target') || '_blank', features);
}

function isUndefined(obj)  {
    if (obj == "" || obj == null)  {
        return 1;
    }
    else  {
        return 0;
    }
}

