$(document).ready( function () {
    /***** ****** ****
     ***** global ****
     ***** ****** ****/
    do_post=0;
    $('a.progress').click(function() {if (do_post) { return process_order($(this).attr('href')); } else { return true; }});
    $('#order_form').submit(function () { return process_order('/order'); } );
    $('#stop').fadeIn(1500);
    $('#order_form').one('change',function() { do_post=1; return true; });
    $('#order_form').ajaxError(error.show);
	
});
var error = {
	errors: [],
	add:  function(msg) {
		this.errors.push(msg);
	},

	show: function() {
		$('.ui-infobox-warning').remove();
	    var error_div = $('<div class="ui-infobox ui-infobox-warning"><div class="ui-box"><h4>Errors were encountered during processing</h4><ol id="error_list"></ol></div></div>');
	    $('#step_name').parent().prepend(error_div);
	    var errors = this.errors;
	    if (!errors)
	    	errors = ['Unspecified submission error'];
	    for (var i = 0; i < errors.length; i++) {
	    	$('#error_list').append($('<li/>').text(errors[i]));
	    }
	    this.errors = [];
	    $('html, body').animate({scrollTop:0}, 'fast');
	}

}
var process_order = function (url) {
    $('div.ui-infobox-warning').remove();
    $('.error').removeClass('error');
    // pop in modal
    payload = $('#order_form').serialize();
    $.post('/pages/ajax_engine?engine=process_order&s='+session_id+'&rand=' + (+new Date), payload + '&session_id=' + session_id, function (resp) {
        if (resp.status != true) {
            var first, error_div = $('<div class="ui-infobox-warning ui-infobox"><div class="ui-box"><h4>Errors were encountered during processing</h4><dl id="error_list"></dl></div></div>');
            if (resp.errors) {
				$.each(resp.errors, function(err_cnt) {
					if (this.id) {
						if ($('#' + this.id).next().hasClass('sublet')) {
							$('#' + this.id).next().html(this.msg).show();
						}
						else {
							$('<div class="warning sublet"></div>').html(this.msg).insertAfter('#' + this.id);
						}
						$('#' + this.id).change(function(){
							$('#' + this.id).closest('div.sublet').fadeOut();
						});
					}
					else {
						$('#error_list').append($('<dd>' + this.msg + '</dd>'));
					}
				});
				first = $('div.warning:first'); 
			}
			
//            if (window.console && window.console.firebug && resp.debug)
//                $.each(resp.debug, function () { console.log(this) });
			if ($('#error_list').children().length > 0) {
				var $container = $('table .warning:eq(0)').parents('div:visible:eq(0)');
				if ($container.length < 1) {
					$container = $('#step_name').next();
				} 
	            $container.before(error_div);
				first = $container;
			}
			
			var offset = first.offset();
            $('html, body').animate({scrollTop:offset.top-first.outerHeight()}, 'fast');
            return false;
        } else {
            if (typeof url == 'function') {
                url();
            } else if (url) {
                window.location = url;
            }
            return false;
        }
    }, "json");
    return false;
}
