$(document).ready(function(){
    $(".addToCart").click(function(){
    	var partId = $(this).attr('id').replace(/part/,'');
    	var partTitle = $('#title'+partId).text();
    	$.post(Registry.URL_ADD_TO_CART, { id: partId, title: partTitle }, function(cartMessage){
            if(partId == 0) toUrl(Registry.URL_SHOPPING_CART);
            else MessageBox(cartMessage);
        });
    });
    
    $("#checkout_button").click(function(){
        $('#do_checkout').val(1);
        $('#cartForm').submit();
    });
    
    $("#addressForm").submit(function(){
    	
    	var checkRequireds = $('#customer_first_name').val().length *
                             $('#customer_last_name').val().length *
                             $('#customer_address1').val().length *
                             $('#customer_city').val().length *
                             $('#customer_zip').val().length;
        if(!checkRequireds)
        {
            MessageBox('Please fill all the fields');
            return false;
        }
    	
    });
    
    $("#sameBillingAddress").click(function(){
        if(elem('sameBillingAddress').checked)
        {
        	//reset values
        	$('#customer_first_name').val($('#h_customer_first_name').val());
        	$('#customer_last_name').val($('#h_customer_last_name').val());
            $('#customer_address1').val($('#h_customer_address1').val());
            $('#customer_address2').val($('#h_customer_address2').val());
            $('#customer_city').val($('#h_customer_city').val());
            $('#customer_zip').val($('#h_customer_zip').val());
            $('#customer_state').val($('#h_customer_state').val());
            
            //hide
            $('.cc_address').each(function(){ $(this).addClass('hidden'); });
        }
        else
        {
        	//show
        	$('.cc_address').each(function(){ $(this).removeClass('hidden'); });
        }
    });
   
    $("#cartForm").submit(function(){ 
    	
    	/*
    	var checkRequireds = $('#night_phone_a').val().length *
                             $('#night_phone_b').val().length *
                             $('#night_phone_c').val().length *
                             $('#email').val().length;
    	if(!checkRequireds)
        {
            MessageBox('Please fill all the fields');
            return false;
        }
    	
    	// transfer address values from paypal form to continue merchant checkout
    	
    	$('#customer_phone').val($('#night_phone_a').val() + '-' + $('#night_phone_b').val() + '-' + $('#night_phone_c').val());        
        $('#customer_email').val($('#email').val());
        //*/
           	
        if(checkWarranty && elem('agree').checked == false)
        {
            MessageBox('Please read and accept all the Terms of Warranty');
            $('#do_checkout').val(0);
            return false;
        }
    });
    
    $("#paypalForm").submit(function(){
    	var checkRequireds = $('#night_phone_a').val().length *
                             $('#night_phone_b').val().length *
                             $('#night_phone_c').val().length *
                             $('#email').val().length;
        if(!checkRequireds)
        {
            MessageBox('Please fill all the fields');
            return false;
        }
        if(checkWarranty && elem('agree').checked == false)
        {
            MessageBox('Please read and accept all the Terms of Warranty');
            return false;
        }
    });
    
    $("#shippingForm").submit(function(){
    	var selected = false;
        $("input[name=shipping_service]").each(function(){
        	var id = $(this).attr('id');
        	if(elem(id).checked) selected = true;        	
        });
        if(!selected)
        {
            MessageBox('Please select shipping method');
            return false;
        }
    });
    
});