var currentAction = "";

function toggleHideShow(id) {
    var divToChange = document.getElementById(id);
    if (divToChange.className == 'hide'){
        divToChange.className = '';
    }else{
        divToChange.className = 'hide';
    }
}

function toggleDisplay(id) {
    var divToChange = document.getElementById(id);
    if (divToChange.style.display == 'block'){
        divToChange.style.display = 'none';
    }else{
        divToChange.style.display = 'block';
    }
}

function getCurrentUrl(currentUrl, event) {
	return currentUrl + document.getElementById("team-id").value + event;
}

function traverseChildrenElements(currentUrl, team) {
	var element = document.getElementById("events");
	for (var i=0; i<element.childNodes.length; i++) {
		var childObj = element.childNodes[i];
		if(childObj.className == 'event_item') {
			var link = childObj.childNodes[0];
			link.href=currentUrl + team + "/" + link.className;
		}
	}
}


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() {
        toggleRoBidPanel();
		fade_region('update_table_region', 1);
		setClassStyle("wait_indicator", 'hide');
		attachOnClickTabEvents();
}

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 drill_down_submit(update_region, action) {
	if (action == null)
		action = 'xhr';
	var url = getRequestedUrlWithParams(orderType, action, csiOrderType);
    url = url + "?random="+(new Date()).getTime();
    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 clearPriceBidField(field) {
	field.value = '';
	field.onclick = function() {
	};
}

function attachOnClickTabEvents(csi) {
	var prefix = 'tab-';
	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 confirmQtyAndLinkToOrder(eventName, orderType, marketId, orderId, actionType, params, selectQtyId){
	if($(selectQtyId).value == "") {
		alert("Please, pick quantity first.");
	}else {
		linkToOrder(contextPath, 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;
}

/* Bidding */

function checkBidValues(submitPrice) {
    var bidPrice = $("bid_ro")
    if (bidPrice.value >= '5') {
        bidPrice.style.cssText = ''
        if (submitPrice) {
            $(submitPrice).submit();
        }
    }
    else {
        bidPrice.style.cssText = 'border: 1px solid #990000; background-color: #FFF4F5;'
        return false;
    }
}

function submitBid() {

}

/* Marketplace Js */

function hideElement( eleId )
{
   var obj = document.getElementById(eleId);
   if( obj )
   {
	 	obj.style.visibility="hidden";
		obj.style.display="none";
  }
}

function showElement( eleId )
{
   var obj = document.getElementById(eleId);
   if( obj )
   {
	   obj.style.visibility="visible";
   	   obj.style.display="block";
  }
}

