//----------------------------------

function ready_order() {

	//------- init
	
	var products				= $("table.step1 input");
	var options				= $("table.step2 input");
	var infos					= $("table.step2 a");
	var country				= $("select[name='country']");
	var miscs					= $("table.step3 select");
	var total_amount		= $("table.step2 .total div");
	var order_now_btn	= $("td.order_now input");
	var total,tmp1,tmp2	= 0;

	//------- initialization

	country_changes();
	calculate_total_order_amount();
	if_js_enabled();

	//------- actions

	products.change(calculate_total_order_amount);
	options.change(calculate_total_order_amount);
	country.change(calculate_total_order_amount);
	
	//-------

	order_now_btn.click(check_outbound);

	//-------
	
	infos.click(function show_additional_info() {
		//-------
		var it = $(this);
		//-------
		var dlg = '<div id="dialog" title="Info"><div style="height:195px;border:1px solid white;overflow:hidden;">'+
		'<iframe src="'+it.attr('href')+'" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" style="height:370px;left:-30px;position:relative;top:-220px;width:770px;"><iframe>'+
		'</div></div>';
		$('body').append(dlg);
		dlg = $('#dialog');
		//-------
		dlg.dialog("destroy");
		//-------
		dlg.dialog({
			modal: true, resizable: false, width:760,
			buttons: {
				//-------
				Ok: function() {
					$(this).dialog('close').dialog("destroy");
					dlg.remove();
				}
				//-------
			}
		});
		//-------
		return false;
		//-------
	});

	//-------

	function if_js_enabled() {
		//-------
		$(".total td:first").html('Total Order Amount').removeClass('without_js').addClass('with_js');
		//-------
	}

	//-------
	
	function hide_shipping_td() {
		//-------
		if(country.data('hide_shipping') == 1) {
			$(".shipping_box").hide();
		} else {
			$(".shipping_box").show();
		}
		//-------
	}

	//-------
	
	function country_changes() {
		//-------
		var price = country.parent().find("input[type='hidden']:"+((country.val() == 'USA') ? 'first' : 'last')).val();
		$('.step2 .shipping_box label:last').html('$'+price);
		return price;
		//-------
	}

	//------- calculate total order amount as is...
	
	function calculate_total_order_amount() {
		//-------
		total = 0;
		country.data('hide_shipping',1);
		//-------
		$("table.step1 input:checked").each(function() {
			total = total + Math.round(parseFloat($(this).parent().find("input[type='hidden']").val())*100);
		});
		//-------
		$("table.step2 input:checked").each(function() {
			total = total + Math.round(parseFloat($(this).parent().find("input[type='hidden']").val())*100);
			if($(this).val() == 'cd' || $(this).val() == 2) {
				country.data('hide_shipping',0);
				total = total + Math.round(parseFloat(country_changes())*100);
			}
		});
		//-------
		hide_shipping_td();
		//------- normalize and show total amount
		tmp1 = Math.floor(parseInt(total) / 100);
		tmp2 = parseInt(parseInt(total) - (tmp1*100));
		if(tmp2 < 10) {
			tmp2 = tmp2+"0";
		}
		total_amount.html('$'+tmp1+'.'+tmp2);
		//-------
	}
	
	//-------

	function check_outbound() {
		//-------
		var flag, alert_text = '';
		//-------
		flag = false;
		$(".step1 input:checked").each(function() {
			flag = true;
		});
		if(flag == false) {
			alert_text = alert_text + "You must select at least one of the products in Order Product section."+"\n";
		}
		//-------
		$(".step2 input:checked").each(function() {
			var chkval = $(this).val();
			if(chkval == 'cd' || chkval == 2) {
				if(country.val() == '') {
					alert_text = alert_text + "You must select some country for shipping."+"\n";
				}
			}
		});
		//-------
		flag = true;
		$(".step3 select").each(function() {
			if($(this).val() == '') {
				flag = false;
			}
		});
		if(flag == false) {
			alert_text = alert_text + "You must select in Misc. Information section."+"\n";
		}
		//-------
		if(alert_text != '') {
			alert(alert_text);
			return false;
		}
		//-------
		return true;
		//-------
	}

	//-------
	
}

//----------------------------------



