var refreshCount = 0;

function ajaxGet(targetUrl, targetDivId){
	ajaxConnect(targetUrl, targetDivId, 'get');
}

function ajaxPost(targetUrl, targetDivId){
	ajaxConnect(targetUrl, targetDivId, 'post');
}

/*
 * Method for making ajax requests.  Response (JSP content) placed in div.
 */
function ajaxConnect(targetUrl, targetDivId, type){
	var targetDiv = document.getElementById(targetDivId);
	if (targetDiv!=null){
		var height = targetDiv.offsetHeight;
		var width = targetDiv.offsetHeight;	
		
		targetDiv.innerHTML = "<div class='loading' style='height: " + height + "; width: " + width + ";'></div>";
		targetUrl = addAjaxParam(targetUrl);
	}
	
	var ajax = new Ajax.Request(targetUrl,   
		{     
			method: type,     
			onSuccess: function(transport){       
				var response = transport.responseText;		
				if (response.indexOf('sessionTimedOut')!=-1)
					window.location='member.ctrm';
				else{
					if (targetDiv!=null)
						targetDiv.innerHTML = response;
				}	
			},     
			onFailure: function(){ 
				alert('An unexpected error occured.')
			}   
		});	
}

function addAjaxParam(targetUrl){
	var ajaxParam = "ajaxReq=true";
	if (targetUrl.indexOf('?')==-1)
		return targetUrl + "?" + ajaxParam;
	else
		return targetUrl + "&" + ajaxParam;
}

/**
 * Function to enable auto-refreshing on any target div.
 * 
 * @param targetUrl
 * @param targetDivId
 * @return
 */
function autoRefresh(){
	/* autoRefresh('wager.ctrm?cmd=refreshHorseOdds','horse1odds');	 */
	new Ajax.PeriodicalUpdater('', 'wager.ctrm?cmd=refreshHorseOdds', {
		method: 'get', frequency: 10, decay: 5, 
		onSuccess: function(transport){       
			var response = transport.responseText;		
			if (response.indexOf('sessionTimedOut')!=-1)
				window.location='member.ctrm';
			else{				
				parseHorseOddsContent(response);
				
				if (refreshHorses)
					autoRefresh();
			}	
		},     
		onFailure: function(){ 
			alert('An unexpected error occured.');
		}   
	});
}

function autoRefreshRaces(){
	if ($('races')!=null){
		var updater = new Ajax.PeriodicalUpdater($('races'), 'home.ctrl?cmd=refreshRaces', {
			method: 'get', frequency: 30, decay: 1, 
			onSuccess: function(transport){       			
				refreshCount = refreshCount + 1;
				
				if (refreshCount>20)
					updater.stop();
			},     
			onFailure: function(){ 
				alert('An unexpected error occured.');
			}   
		});
	}
}

/**
 * Method for parsing the response for 'response div's containing horse odds.  This is needed since we only
 * want to refresh the horse odds and not the entire section where the odds are contained.
 * 
 * @param response
 * @return
 */
function parseHorseOddsContent(response){
	$('horseUpdateDiv').innerHTML = response;
	
	for (var x=1; x<30; x++){
		var respDiv = $('h' + x + 'OddsRespDiv');
		var targetDiv = $('horse' + x + 'odds');
		
		if (respDiv!=null && targetDiv!=null){
			targetDiv.innerHTML = respDiv.innerHTML;
		}
	}
}
 
 
/* ----------------------------- YUI methods ------------------------------- */
 
function ajaxPanelGet(url, yuiPanel){
	ajaxPanelConnect(url, yuiPanel, 'GET');
}

function ajaxPanelPost(url, yuiPanel){
	ajaxPanelConnect(url, yuiPanel, 'POST');
}

function ajaxPanelConnect(url, yuiPanel, type){
	url = addAjaxParam(url);
	
	var height = yuiPanel.height;
	var width = yuiPanel.width;	
	
	yuiPanel.setBody("<div class='loading' style='height: " + height + "; width: " + width + ";'></div>");
	
	var callback = {
		success: function(resp){
			if (resp.responseText.indexOf('sessionTimedOut')!=-1)
				window.location='member.ctrm';
			else{
	 			yuiPanel.setBody(resp.responseText);
			}
 		},
 		failure: function(resp){
 			alert('An unexpected error occured!  Please try again!');
 		}
	};
 	
	YAHOO.util.Connect.asyncRequest(type, url, callback, null); 
}
