$(document).ready(function(){
    $('#checkoutSteps h3').click(function() {
	if ($(this).parent().attr('class').search('allow') != -1)
	{
	    openSection($(this).parent().attr('id'));
	}
    });
	
    $('#billing_shipping_yes').bind('change',function(){
        if($(this).val()=='on')
        {
	    $(this).val('');
            emptyForm([ 'first_name', 'last_name', 'address', 'zip_code', 'city', 'province', 'phone', 'country', 'zone', 'dni' ]);
        }
        else
        {
            $(this).val('on');
            
            /*$.getJSON(baseUrl + '/order/getShipping', function(data){*/
	    $.ajax ({
		url: baseUrl + '/order/getShipping',
		dataType: 'json',
		success: function(data) {
		    $.each(eval(data), function(i, n) {
			if (i != 'comments')
			{
			    $('#' + i).val(n);
			}
		    });
		    showAjax('#zoneAjax', baseUrl + '/several/zoneAjax', 'POST', 'country_ID=' + $('#country').val() + '&select=' + $('#zone').val());
		}
	    });
	} 
        showAjax('#zoneAjax', baseUrl + '/several/zoneAjax', 'POST', 'country_ID=' + $('#country').val() + '&select=' + $('#zone').val());
    });
});

function openSection(obj)
{
	$('#checkout-step-shipping').css('display', 'none');
	$('#checkout-step-shipping_method').css('display', 'none');
	$('#checkout-step-payment_method').css('display', 'none');
	$('#checkout-step-review').css('display', 'none');
	$('#opc-shipping .bottom').css('display', 'none');
	$('#opc-shipping_method .bottom').css('display', 'none');
	$('#opc-payment_method .bottom').css('display', 'none');
	$('#opc-review .bottom').css('display', 'none');
	
	switch(obj)
	{
		case 'opc-shipping':
			$('#opc-shipping_method').attr('class', 'section');
			$('#opc-payment_method').attr('class', 'section');
			$('#opc-review').attr('class', 'section');
			closeMiniSection('opc-shipping-progress');
			closeMiniSection('opc-shipping_method-progress');
			closeMiniSection('opc-payment_method-progress');
			break;
		case 'opc-shipping_method':
			$('#opc-shipping').attr('class', 'section allow');
			$('#opc-payment_method').attr('class', 'section');
			$('#opc-review').attr('class', 'section');
			closeMiniSection('opc-shipping_method-progress');
			closeMiniSection('opc-payment_method-progress');
			break;
		case 'opc-payment_method':
			$('#opc-shipping').attr('class', 'section allow');
			$('#opc-shipping_method').attr('class', 'section allow');
			$('#opc-payment_method').attr('class', 'section');
			$('#opc-review').attr('class', 'section');
			closeMiniSection('opc-payment_method-progress');
			break;
		case 'opc-review':
			$('#opc-shipping').attr('class', 'section allow');
			$('#opc-shipping_method').attr('class', 'section allow');
			$('#opc-payment_method').attr('class', 'section allow');
			break;
	}
	
	$('#' + obj + ' .box').css('display', 'block');
	$('#' + obj + ' .bottom').css('display', 'block');
	$('#' + obj).attr('class', 'section allow active');
}

function openMiniSection(obj)
{
	$('#' + obj + ' .content').remove();
	$('#' + obj + ' h4').after('<div style="display: none;" class="content"></div>');
	
	switch(obj)
	{
		case 'opc-shipping-progress': 
			$('#' + obj + ' .content').load(baseUrl + '/order/miniShipping', '', function() { $(this).slideDown('normal'); });
			$('#opc-shipping-progress span').css('display', 'inline');
			$('#opc-shipping-progress a').css('display', 'inline');
			closeMiniSection('opc-shipping_method-progress');
			closeMiniSection('opc-payment_method-progress');
			break;
		case 'opc-shipping_method-progress': 
			$('#' + obj + ' .content').load(baseUrl + '/order/miniShippingMethod', '', function() { $(this).slideDown('normal'); });
			$('#opc-shipping_method-progress span').css('display', 'inline');
			$('#opc-shipping_method-progress a').css('display', 'inline');
			closeMiniSection('opc-payment_method-progress');
			break;
		case 'opc-payment_method-progress': 
			$('#' + obj + ' .content').load(baseUrl + '/order/miniPaymentMethod', '', function() { $(this).slideDown('normal'); });
			$('#opc-payment_method-progress span').css('display', 'inline');
			$('#opc-payment_method-progress a').css('display', 'inline');
			break;
	}
}

function closeMiniSection(obj)
{
	$('#' + obj + ' span').hide();
	$('#' + obj + ' a').hide();
	$('#' + obj + ' .content').slideUp('slow', function() {
		$(this).remove();
	});
}

function saveShipping()
{
	$('#checkout-step-shipping .message').removeClass('error');
	loading('#checkout-step-shipping .message', 'send', 'min');
	$.ajax({
        type: 'POST',
        url: baseUrl + '/order/saveShipping',
        data: 'first_name=' + $('#first_name').val() + '&last_name=' + $('#last_name').val() + '&address=' + $('#address').val() + '&zip_code=' + $('#zip_code').val() + '&city=' + $('#city').val() + '&province=' + $('#province').val() + '&country=' + $('#country').val() + '&phone=' + $('#phone').val() + '&zone=' + $('#country_zone').val() + '&comments=' + $('#comments').val() + '&dni=' + $('#dni').val(),
        success: function(msg) {
			if (msg == 'true')
			{
				$('#checkout-step-shipping_method').load(baseUrl + '/order/getShippingMethod', '', function() {
					openSection('opc-shipping_method');
					openMiniSection('opc-shipping-progress');
					$('#checkout-step-shipping .message').html('');
				});
			}
			else
			{
				if (msg == '')
				{
					setLocation(baseUrl + '?session=clear');
				}
				else
				{
					$('#checkout-step-shipping .message').attr('class', 'message error');
					$('#checkout-step-shipping .message').html(msg);
				}
			}
		}
	});
}

function saveShippingMethod()
{
	$('#checkout-step-shipping_method .message').removeClass('error');
	loading('#checkout-step-shipping_method .message', 'send', 'min');
	$.ajax({
        type: 'POST',
        url: baseUrl + '/order/saveShippingMethod',
        data: ({transport : $('#checkout-step-shipping_method input[name="transport"]:checked').val(), gift_yes : $('#gift_yes').val(), gift_from : $('input[name="gift[from]"]').val(), gift_to : $('input[name="gift[to]"]').val(), gift_message : $('#gift_message').val()}),
        success: function(msg) {
			if (msg == 'true')
			{
				$('#checkout-step-payment_method').load(baseUrl + '/order/getPaymentMethod', '', function() {
					openSection('opc-payment_method');
					openMiniSection('opc-shipping_method-progress');
					$('#checkout-step-shipping_method .message').html('');
				});
		   }
		   else
		   {
			   if (msg == '')
			   {
				   setLocation(baseUrl + '?session=clear');
			   }
			   else
			   {
	               $('#checkout-step-shipping_method .message').attr('class', 'message error');
				   $('#checkout-step-shipping_method .message').html(msg);
			   }
		   }
		}
	});
}

function savePaymentMethod()
{
	$('#checkout-step-payment_method .message').removeClass('error');
	loading('#checkout-step-payment_method .message', 'send', 'min');
	$.ajax({
		type: 'POST',
		url: baseUrl + '/order/savePaymentMethod',
		data: ({payment : $('#checkout-step-payment_method input[name="payment"]:checked').val()}),
		success: function(msg) {
			if (msg == 'true')
			{
				$('#checkout-step-review').load(baseUrl + '/order/getReview', '', function() {
					openSection('opc-review');
					openMiniSection('opc-payment_method-progress');
					$('#checkout-step-payment_method .message').html('');
				});
			}
			else
			{
				if (msg == '')
				{
					setLocation(baseUrl + '?session=clear');
				}
				else
				{
					$('#checkout-step-payment_method .message').attr('class', 'message error');
					$('#checkout-step-payment_method .message').html(msg);
				}
			}
		}
	});
}

function saveOrder(msg)
{
	$('#checkout-step-review .message').removeClass('error');
	loading('#checkout-step-review .message', 'send', 'min');
    if ($('#terms').is(':checked'))
	{
		postwith(baseUrl + '/order',{terms:'on'});
	}
	else
	{
		$('#checkout-step-review .message').attr('class', 'message error');
		$('#checkout-step-review .message').html(msg);
	}
}
