var currentAction = "";

function indicate_loading(csi_region) {
	if (csi_region == null) {
		csi_region = '';
	}
	fade_region('update_' + csi_region + 'table_region', 0);
	setClassStyle('wait_' + csi_region + 'indicator', 'markets-loading');
}

function failure_alert() {
	show_table();
	// alert('Error on the page.  Please reload.');
}

function show_table() {
	if (currentAction == "all") {
		fade_region('update_all_table_region', 1);
	}

	if (currentAction == "ro" || currentAction == "all") {
		toggleRoBidPanel();
		fade_region('update_table_region', 1);
		setClassStyle("wait_indicator", 'hide');
		attachOnClickTabEvents();
	}

	if (currentAction == "csi" || currentAction == "all") {
		toggleCsiBidPanel();
		fade_region('update_csi_table_region', 1);
		setClassStyle("wait_csi_indicator", 'hide');
		attachOnClickTabEvents('csi');
	}
}

function setClassStyle(region_id, className){
	var region = $(region_id);
	if(region){
		region.className =className;
	}
}

function fade_region(region_id, fade_num) {
	var region = $(region_id);
	if(region){
		region.fade(fade_num);
	}
}

function toggleRoBidPanel() {
	if (orderType != 'bid'){
		setClassStyle('bid-ro-container', 'hide');
	}
	if (orderType == 'bid'){
		setClassStyle('bid-ro-container', '');
	}
}

function toggleCsiBidPanel() {
	if (csiOrderType != 'bid')
		setClassStyle('bid-csi-container', 'hide');
	if (csiOrderType == 'bid')
		setClassStyle('bid-csi-container', '');
}

function drill_down_submit(update_region, action) {
	if (action == null)
		action = 'xhr';
	var url = getRequestedUrlWithParams(orderType, action, csiOrderType);
	url = url + "&random="+(new Date()).toUTCString();
	new Request.HTML( {
		url :url,
		update :update_region,
		evalResponse : true,
		evalScripts : true, 
		onSuccess :show_table,
		onFailure :failure_alert
	}).get($('drill-down-form'));
}

function drill_down_update() {
	currentAction == "all"
	indicate_loading();
	indicate_loading('csi_');
	drill_down_submit('update_all_table_region', 'xhr');
}

function tab_request(tab_name) {
	currentAction = "ro";
	orderType = tab_name;
	indicate_loading();
	drill_down_submit('update_table_region', 'ro');
}

function tab_request_csi(csi_tab_name) {
	currentAction = "csi";
	csiOrderType = csi_tab_name;
	indicate_loading('csi_');
	drill_down_submit('update_csi_table_region', 'csi');
}

function clearPriceBidField(field) {
	field.value = '';
	field.onclick = function() {
	};
}

function attachOnClickTabEvents(csi) {
	var prefix = 'tab-';
	if (csi) {
		prefix = prefix + 'csi-';
	}

	var tab_values = new Array('trade', 'buy', 'bid');
	for ( var i = 0; i < tab_values.length; i++) {
		var tab_value = tab_values[i];
		var tab = $(prefix + tab_value);
		if (tab) {
			( function(tab_value) {
				if (csi) {
					tab.onclick = function() {
						tab_request_csi(tab_value);
						return false;
					};
				} else {
					tab.onclick = function() {
						tab_request(tab_value);
						return false;
					};
				}
			})(tab_value);
		}
	}
}

function linkToOrder(eventName, orderType, marketId, orderId, actionType, params){
	var url = contextPath + "/order/" + eventName+ "/" + orderType + "/" + marketId;
	if(orderId){
		url += "/" + orderId;
	}
	url += "?actionType=" + actionType;
	for ( var param in params) {
		url += "&" + param + "=" + params[param];
	}
	location.href = url;
}

function confirmQtyAndLinkToOrder(eventName, orderType, marketId, orderId, actionType, params, selectQtyId){
	if($(selectQtyId).value == "") {
		alert("Please, pick quantity first.");
	}else {
		linkToOrder(eventName, orderType, marketId, orderId, actionType, params);
	}
}

function confirmQtyPriceAndLinkToOrder(eventName, orderType, marketId, orderId, actionType, params, selectQtyId, priceInputId){
	if(!isNumeric($(priceInputId).value)) {
		alert("Please, enter valid price greater than $5.00 .");
	}else {
		confirmQtyAndLinkToOrder(eventName, orderType, marketId, orderId, actionType, params, selectQtyId);
	}
}

var VALID_CHARS = "0123456789.";
function isNumeric(sText){
   if(!sText || sText == "")
	   return false;
   for (i = 0; i < sText.length; i++) { 
      if (VALID_CHARS.indexOf(sText.charAt(i)) == -1) {
         return false;
      }
   }
   return true;  
}
