$(document).ready(function() {
	function scrollTop(){
		$('html').scrollTo($('#messageBox'));
	}
	$('#shipping_billing_form').each(function(){
		form = $(this);
		$(this).validate({
			rules: {
				s_firstname: "required",
				s_lastname: "required",
				s_phone: "required",
				s_address: "required",
				s_city: "required",
				s_zip: "required",
				b_firstname: "required",
				b_lastname: "required",
				b_phone: "required",
				b_address: "required",
				b_city: "required",
				b_zip: "required",
				//s_email: "email",
				b_email: "email",
				b_state: "required",
				s_state: "required"
			},
			messages: {
				s_firstname: "Please enter the First Name on the Shipping Address",
				s_lastname: "Please enter the Last Name on the Shipping Address",
				s_phone: "Please enter the contact Phone number on the Shipping Address",
				s_address: "Please enter the street Address on the Shipping Address",
				s_city: "Please enter the City name on the Shipping Address",
				s_zip: "Please enter the Zip code on the Shipping Address",
				
				b_firstname: "Please enter the First Name on the Billing Address",
				b_lastname: "Please enter the Last Name on the Billing Address",
				b_phone: "Please enter the contact Phone number on the Billing Address",
				b_address: "Please enter the street Address on the Billing Address",
				b_city: "Please enter the City name on the Billing Address",
				b_zip: "Please enter the Zip code on the Billing Address",
				b_email: "Email format is invalid. Please enter a valid email address",
				s_email: "Email format is invalid. Please enter a valid email address",
				b_state: "Please select the State on the Billing Address",
				s_state: "Please select the State on the Shipping Address"
			},
			errorContainer: '#messageBox',
			errorLabelContainer: "#messageBox .messenger_ul_error",
			wrapper: "li",
			invalidHandler: function(){$('#messageBox ul').html('');window.setTimeout(scrollTop, 30);}
		});
		function copyFieldVal(){
			//alert(document.getElementById('equals_shipping').checked);
			//if($('#equals_shipping').attr('checked')){
			if(document.getElementById('equals_shipping').checked){
				form.find('input[id^=b_]').each(function(){
					form.find('input[id=s_' + $(this).attr('id').replace('b_', '') + ']').val($(this).val());
				});
				form.find('select[id^=b_]').each(function(){
					form.find('select[id=s_' + $(this).attr('id').replace('b_', '') + '] option').eq($(this).find('option').index($(this).find('option:selected'))).attr('selected', 'selected');
				});
			}
		}
		$('#equals_shipping').bind('change', function(){
			if($('#equals_shipping').attr('checked')){
				form.find('input').bind('change.copyFieldVal', copyFieldVal);
				form.find('select').bind('change.copyFieldVal', copyFieldVal);
				copyFieldVal()
			} else {
				form.find('input').unbind('change.copyFieldVal');
				form.find('select').unbind('change.copyFieldVal');
			}
		}).triggerHandler('change');
	});
	$('#pay_with').each(function(){
		var submitButton = $(this).parents('form').find('input[type=submit]'),
		submitButtonValue = submitButton.val();
		//submitButton.css({
		//	width:(submitButton.width()+38)+'px'
		//})
		function pay_with_paypal(event){
			event.preventDefault();
			$.ajax({
				cache: false,
				type: "POST",
				url: $('#save_online_order_url').val(),
				data: {},
				success: function(r){
					if(r != '0'){
						$('#invoice').val(r);
						$('.paypal_form').trigger('submit');
					}
                    else{
                        alert('ORDER ERROR');
                    }
				}
			});
		}
		$(this).bind('change', function(){
			if($(this).val()=='paypal'){
				$('#pay_with_card').fadeOut();
				submitButton.val('Pay with Paypal').bind('click.payPal', pay_with_paypal);// ' + String.fromCharCode(187) 
				submitButton.fadeIn();
			} else if($(this).val()==0) {
				$('#pay_with_card').fadeOut();
				submitButton.fadeOut();
			} else {
				$('#pay_with_card').fadeIn();
				submitButton.val(submitButtonValue).unbind('click.payPal');
				submitButton.fadeIn();
			}
		});
		$(this).triggerHandler('change');
	});
}); // Document Ready Function





