/**
*	@Name:	fantasygamescontroller.ajax.js
*	@Created by: Byron Bricegno
*	@DateCreated:	19/05/2009
*	@LastModification: 19/05/2009
*	@Category: Javascript
*	@Parameters: none
*	@Description: Fantasy Games Page Controller: AJAX - JS actions 
*	@Dependencies: Ajaxer.js
*	@Version: 1.0	
*	@Copyright: Fantazzle Fantasy Sports Inc
*/
var ObjAjax = new Ajaxer(); //ObjAxer - Ajaxer Module
var InitCounter = 1;
var useFanStock = false;
function ProcessFantasyGame(formid,fee,players,prize,currentElementId,poolid,fantastock,seriesoptionid,loginstatus,PrivateURL,requestdata,hastoshowcashier,isPassRequired)
{
	var FormInMess = document.getElementById(formid);
	var InMessElement = document.getElementById(currentElementId);
	var SubmitButton = document.getElementById('SubmitButton_'+poolid);
	var UseFreeBalance = document.getElementById('fantastockvalue_'+poolid);

	var InAmount = document.getElementById('Fee_'+poolid);
	var InPlayers = document.getElementById('Players_'+poolid);
	var InPrize = document.getElementById('Prize_'+poolid);	
	var InSeriesOptionId = document.getElementById('SeriesOptionId_'+poolid);	
	
	var amountvalue = (fee*1)/100;
	if(amountvalue!=0)
	{
		if(((fantastock*1)<amountvalue)||((fantastock*1)==0))
		{
			UseFreeBalance.disabled = true;
		  	UseFreeBalance.checked = false;
		} 
		else
		{
			UseFreeBalance.disabled = false;
			//UseFreeBalance.checked = false;
		}
	}
	else
	{
		UseFreeBalance.disabled = true;
		UseFreeBalance.checked = false;	
	}
	if(FormInMess !=null)
	{
		FormInMess.OptionClicked.value = "false";
		FormInMess.usefreebalancevalue.value = useFanStock;
		if(FormInMess.OptionClicked.value!="true")
		{
			if((InAmount!=null)&&(InPlayers!=null)&&(InPrize!=null)&&(InSeriesOptionId!=null))
			{
				FormInMess.removeChild(InAmount); //delete inputs
				FormInMess.removeChild(InPlayers);
				FormInMess.removeChild(InPrize);
				FormInMess.removeChild(InSeriesOptionId);
			}	
		
			var Inputfee = document.createElement('input');
			var Inputplayers = document.createElement('input');
			var Inputprize = document.createElement('input');
			var InputSeriesOptionid = document.createElement('input');
			
			Inputfee.setAttribute('name','amount'); 
			Inputfee.setAttribute('id','Fee_'+poolid); 			
			Inputfee.setAttribute('value',fee);
			Inputfee.setAttribute('type','hidden'); 

			Inputplayers.setAttribute('name','numplayers'); 
			Inputplayers.setAttribute('id','Players_'+poolid); 				
			Inputplayers.setAttribute('value',players);
			Inputplayers.setAttribute('type','hidden'); 
			
			Inputprize.setAttribute('name','prize'); 
			Inputprize.setAttribute('id','Prize_'+poolid); 			
			Inputprize.setAttribute('value',prize);
			Inputprize.setAttribute('type','hidden'); 
			
			InputSeriesOptionid.setAttribute('name','seriesoptionid'); 
			InputSeriesOptionid.setAttribute('id','SeriesOptionId_'+poolid); 			
			InputSeriesOptionid.setAttribute('value',seriesoptionid);
			InputSeriesOptionid.setAttribute('type','hidden'); 			
			
			FormInMess.appendChild(Inputfee);
			FormInMess.appendChild(Inputplayers);
			FormInMess.appendChild(Inputprize);	
			FormInMess.appendChild(InputSeriesOptionid);				
			
			FormInMess.OptionClicked.value = "true";
			FormInMess.ClickedBy.value = currentElementId; //it which element is already set
			
			//InMessElement.className="GamesOptionsSelected";		

			//SubmitButton.src="../images/fantasygames_images/play-button-yellow.png";
			//SubmitButton.disabled=false;
			SubmitAction(formid,loginstatus,PrivateURL,amountvalue,requestdata,hastoshowcashier,isPassRequired,poolid);	
		}
		else //it means that we have to take 2 decisions: 1 delete the current inputs or 2 delete current inputs and set new ones
		{
			if(InMessElement !=null)
			{
				var ElementtoUnset;
				if(FormInMess.ClickedBy.value==currentElementId) //it means that the exact option was clicked so it will be necessary to unset
				{
					UseFreeBalance.disabled = true;
					UseFreeBalance.checked = false;
							
					//InMessElement.className="GamesOptions"; //return the style
					FormInMess.removeChild(InAmount); //delete inputs
					FormInMess.removeChild(InPlayers);
					FormInMess.removeChild(InPrize);
					FormInMess.removeChild(InSeriesOptionId);					
					
					FormInMess.OptionClicked.value = "false"; //returnt to default parameter
					FormInMess.ClickedBy.value =""; //return to default parameter
					
					SubmitButton.src="../images/fantasygames_images/play-button-gray.png";
					SubmitButton.disabled=true;						
				}
				else
				{
					if(document.getElementById(FormInMess.ClickedBy.value))
					{
						ElementtoUnset = document.getElementById(FormInMess.ClickedBy.value);
						ElementtoUnset.className="GamesOptions"; //return the style
	
						//delete old inputs
						FormInMess.removeChild(InAmount); 
						FormInMess.removeChild(InPlayers);
						FormInMess.removeChild(InPrize);
						FormInMess.removeChild(InSeriesOptionId);							
						//Set the new inputs
						var newInputfee = document.createElement('input');
						var newInputplayers = document.createElement('input');
						var newInputprize = document.createElement('input');
						var newSeriesOptionId = document.createElement('input');
						
						newInputfee.setAttribute('name','amount'); 
						newInputfee.setAttribute('id','Fee_'+poolid); 			
						newInputfee.setAttribute('value',fee);
						newInputfee.setAttribute('type','hidden'); 

						newInputplayers.setAttribute('name','numplayers'); 
						newInputplayers.setAttribute('id','Players_'+poolid); 				
						newInputplayers.setAttribute('value',players);
						newInputplayers.setAttribute('type','hidden'); 
						
						newInputprize.setAttribute('name','prize'); 
						newInputprize.setAttribute('id','Prize_'+poolid); 			
						newInputprize.setAttribute('value',prize);
						newInputprize.setAttribute('type','hidden'); 
						
						newSeriesOptionId.setAttribute('name','seriesoptionid'); 
						newSeriesOptionId.setAttribute('id','SeriesOptionId_'+poolid); 			
						newSeriesOptionId.setAttribute('value',seriesoptionid);
						newSeriesOptionId.setAttribute('type','hidden'); 						
						
						FormInMess.appendChild(newInputfee);
						FormInMess.appendChild(newInputplayers);
						FormInMess.appendChild(newInputprize);
						FormInMess.appendChild(newSeriesOptionId);						
						
						FormInMess.OptionClicked.value = "true";
						FormInMess.ClickedBy.value = currentElementId; //it which element is already set	

						//InMessElement.className="GamesOptionsSelected"; //set the style on the new options		

						SubmitButton.src="../images/fantasygames_images/play-button-yellow.png";
						SubmitButton.disabled=false;	
						SubmitAction(formid,loginstatus,PrivateURL,amountvalue,requestdata,hastoshowcashier,isPassRequired,poolid);
					}
				}
			}
		}
		
	}
}
function SubmitAction(formname,logstatus,PrivateURL,amountvalue,requestdata,hastoshowcashier,isPassRequired,poolid)
{
	var MainForm = document.getElementById(formname);
	if(MainForm != null)
	{
		if(logstatus==1)
		{
			requestdata = 0;//bypass
			/*if((requestdata==1)&&(amountvalue==0))
				openrequest(formname,MainForm.action,amountvalue);*/
			if(isPassRequired==1)
				openpass(formname,MainForm.action,amountvalue,poolid);		
			else if(formname=="Form_3054")
				openprecashier(formname,MainForm.action,amountvalue);
			else if((requestdata==0)&&(amountvalue==0)&&(hastoshowcashier==1))	
			{
				MainForm.submit();
				//openprecashier(formname,MainForm.action,amountvalue);
			}
			else
				MainForm.submit();	
		}
		else
		{
			openlogin(formname,MainForm.action,amountvalue,poolid,PrivateURL);
		}
	}
}
function sendSubmit(formname)
{
	var MainForm = document.getElementById(formname);
	MainForm.submit();	
}
function LoadALLFantasyGames(categoryid)
{
	//alert(categoryid);
	//showSubMenu(categoryid);
	setBigLoadingImage();
	var foldersOut = fixAjaxURL();		
	ObjAjax.setURL(foldersOut+'phpajax/fantasygamesloader.ajax.php');
	ObjAjax.setParameter('catid',categoryid);	
	ObjAjax.setComplete(LoadALLFantasyGamesResponse);
	ObjAjax.execute();
}
function FiltersFantasyGames(categoryid,gametype,entryfee,leaguesize)
{
	setBigLoadingImage();
	var foldersOut = fixAjaxURL();		
	ObjAjax.setURL(foldersOut+'phpajax/fantasygamesloader.ajax.php');
	ObjAjax.setParameter('catid',categoryid);
	ObjAjax.setParameter('gametype',gametype);
	ObjAjax.setParameter('entryfee',entryfee);
	ObjAjax.setParameter('leaguesize',leaguesize);
	ObjAjax.setComplete( LoadALLFantasyGamesResponse);
	ObjAjax.execute();
}
function LoadSpecificFantasyGame(sport,type,categoryid)
{
	setBigLoadingImage();
	var foldersOut = fixAjaxURL();		
	ObjAjax.setURL(foldersOut+'phpajax/fantasygamesloader.ajax.php');
	ObjAjax.setParameter('sport',sport);
	ObjAjax.setParameter('type',type);
	ObjAjax.setParameter('categoryid',categoryid);
	ObjAjax.setComplete( LoadALLFantasyGamesResponse);
	ObjAjax.execute();
}
function LoadPrivateFantasyGames(sport,privateurl)
{
	setBigLoadingImage();
	var foldersOut = fixAjaxURL();	
	var categoryid=0;
	//showSubMenu(categoryid);
	ObjAjax.setURL(foldersOut+'phpajax/fantasygamesloader.ajax.php');
	ObjAjax.setParameter('sport',sport);
	ObjAjax.setParameter('privateurl',privateurl);	
	ObjAjax.setComplete( LoadALLFantasyGamesResponse);
	ObjAjax.execute();

}
function LoadPrivateFantasyGamesAccess(sport,privateurl,formname,action,game_fee,poolid)
{
	var foldersOut = fixAjaxURL();	
	var privatepassword = document.getElementById('privatepassword').value;
	ObjAjax.setURL(foldersOut+'phpajax/loadrequiredpass.ajax.php');
	ObjAjax.setParameter('sport',sport);
	ObjAjax.setParameter('privateurl',privateurl);	
	ObjAjax.setParameter('privatepassword',privatepassword);	
	ObjAjax.setParameter('formname',formname);
	ObjAjax.setParameter('action',action);	
	ObjAjax.setParameter('game_fee',game_fee);	
	ObjAjax.setParameter('poolid',poolid);	
	ObjAjax.setComplete( LoadALLFantasyGamesResponse);
	ObjAjax.execute();
}
function LoadALLFantasyGamesResponse()
{	
	var FantasyGamesContainer = document.getElementById('FantasyGamesAvailableContainer');
	if(FantasyGamesContainer!= null)
	{
		FantasyGamesContainer.innerHTML = ObjAjax.getResponseText();		
		ExecuteOnCallback('initcounters');
		ExecuteOnCallback('evaluableoncallback');		
		if(InitCounter==1)
		{		
			EventCountdown.Startup();
			InitCounter	= 0;	
		}
	}
	FantasyGamesContainer = null;	
}
function SetFreeGamesBypass(poolid,is_free,seriesoptionid,numplayers,prize)
{
	var FormInMess = document.getElementById('Form_'+poolid);
	var SubmitButton = document.getElementById('SubmitButton_'+poolid);
	if(is_free=="true")
	{
		if(FormInMess!=null)
		{
			if(!document.getElementById('Players_'+poolid))
				addByPassInput('Form_'+poolid,'Players_'+poolid,'numplayers',numplayers);
			if(!document.getElementById('Fee_'+poolid))				
				addByPassInput('Form_'+poolid,'Fee_'+poolid,'amount',0);
			if(!document.getElementById('Prize_'+poolid))			
				addByPassInput('Form_'+poolid,'Prize_'+poolid,'prize',prize);	
			if(!document.getElementById('SeriesOptionId_'+poolid))			
				addByPassInput('Form_'+poolid,'SeriesOptionId_'+poolid,'seriesoptionid',seriesoptionid);					
			SubmitButton.disabled=false;
			SubmitButton.src="../images/fantasygames_images/play-button-yellow.png";			
		}
	}
}
function UnsetFreeGamesBypass(poolid,is_free)
{
	var SubmitButton = document.getElementById('SubmitButton_'+poolid);
	if(is_free=="true")
	{
		SubmitButton.disabled=false;
		SubmitButton.src="../images/fantasygames_images/play-button-gray.png";	
	}
}
//Use the removeByPassInput to remove existing inputs on the final form
function removeByPassInput(parentDiv,childDiv)
{
    if (childDiv == parentDiv) 
	{
        alert("The parent div cannot be removed.");
    }
    else if (document.getElementById(childDiv)) 
	{     
		var child = document.getElementById(childDiv);
        var parent = document.getElementById(parentDiv);
		parent.removeChild(child);
    }
    else 
	{
        alert("Child input has already been removed or does not exist.");
        return false;
    }
}// removeElement()

//Use the addByPassInput method to add inputs type=hidden the final form
function addByPassInput(parentDiv,idname,inputname,inputvalue) 
{
	var FormContainer = document.getElementById(parentDiv);
	var newinput = document.createElement('input');
		
	newinput.setAttribute('id',idname);
	newinput.setAttribute('name',inputname); 
	newinput.setAttribute('value',inputvalue);
	newinput.setAttribute('type','hidden');   
	FormContainer.appendChild(newinput);
}
function GamesOptionsStyle(elementid,action)
{
 var ObjDiv = document.getElementById(elementid);
 
 if(ObjDiv != null)
 {
	if(action=="over")
		ObjDiv.className="GamesOptionsSelected";
	else
		ObjDiv.className="GamesOptions";
 }
 ObjDiv = null;
} 

function SetFantaStock()
{	
    var forminmess = document.getElementsByTagName('form');
	var fanstock = document.getElementById('fantastockvalue');
	for(var i=0; i<forminmess.length; i++)
	{
		usefreebalance = forminmess[i].usefreebalancevalue;
		if(usefreebalance != null)
		{
			if(fanstock.checked==true)
			{
				useFanStock = true;	
				usefreebalance.value = "true";
			}
			else
			{
				useFanStock = false;
				usefreebalance.value = "false";
			}
		}	
	}	
}
function setBigLoadingImage()
{
	var LoadingImageContainer = document.getElementById('FantasyGamesAvailableContainer');
	var content="<div id='LoadingImageContainer'><img id='BigLoadingImage' src='../images/fantasygames_images/LoadingImage.gif' alt='Loading Fantasy Games...'/></div>";
	
	if(LoadingImageContainer!= null)
	{
		LoadingImageContainer.innerHTML=content;
		return true;
	}
	else return false;
	LoadingImageContainer = null;
}
function CheckMyStatus(action,islogged,currentform)
{
	var FormInMess= document.getElementById(currentform);
	if(islogged!=1)
	{
		openlogin(currentform,action,0,'');
		return false;
	}
	else
	{
		return true;
	}
}
function openlogin(formname,action,fee,poolid,PrivateURL)
{
	var FixedURL = fixAjaxURL();
	if(PrivateURL == null)
		PrivateURL = '';
	var PopUpURL = FixedURL+'phpajax/loginpopup.ajax.php?action='+action+'&formname='+formname+'&game_fee='+fee+'&poolid='+poolid+'&privateurl='+PrivateURL;
	$.fn.colorbox({iframe:true, innerWidth:410, innerHeight:290, href:PopUpURL});
}
function openrequest(formname,action,fee)
{
	var FixedURL = fixAjaxURL();
	var PopUpURL = FixedURL+'phpajax/collectfreegamedata.ajax.php?action='+action+'&formname='+formname+'&game_fee='+fee;
	$.fn.colorbox({iframe:true, innerWidth:410, innerHeight:290, href:PopUpURL});
}
function openprecashier(formname,action,fee)
{
	var FixedURL = fixAjaxURL();
	var PopUpURL = FixedURL+'phpajax/loadprecashier.ajax.php?action='+action+'&formname='+formname+'&game_fee='+fee;
	$.fn.colorbox({iframe:true, innerWidth:410, innerHeight:290, href:PopUpURL});
}
function openpass(formname,action,fee,poolid)
{
	var FixedURL = fixAjaxURL();
	var PopUpURL = FixedURL+'phpajax/loadrequiredpass.ajax.php?action='+action+'&formname='+formname+'&game_fee='+fee+'&poolid='+poolid;
	$.fn.colorbox({iframe:true, innerWidth:410, innerHeight:290, href:PopUpURL});
}
var currentTime;
var Timezone = "EST";
function TickClock()
{
  currentTime.setSeconds(currentTime.getSeconds()+1)  
  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;
  var currentTimeString = currentHours+":"+currentMinutes+":" +currentSeconds+" "+timeOfDay+" "+Timezone;
  document.getElementById("serverclock").firstChild.nodeValue = currentTimeString;
}
function showSubMenu(categoryid)
{
	var football = document.getElementById("footballMenu");
	var baseball = document.getElementById("baseballMenu");
	var basketball = document.getElementById("basketballMenu");
	var racing = document.getElementById("racingMenu");
	var golf = document.getElementById("golfMenu");
	var hockey = document.getElementById("hockeyMenu");
	if(categoryid == 1)
	{
		football.className = "display";
		baseball.className = "hidden";
		basketball.className = "hidden";
		racing.className = "hidden";
		golf.className = "hidden";
		hockey.className = "hidden";
	}
	else if(categoryid == 6)
	{
		football.className = "hidden";
		baseball.className = "display";
		basketball.className = "hidden";
		racing.className = "hidden";
		golf.className = "hidden";
		hockey.className = "hidden";
	}
	else if(categoryid == 8)
	{
		football.className = "hidden";
		baseball.className = "hidden";
		basketball.className = "display";
		racing.className = "hidden";
		golf.className = "hidden";
		hockey.className = "hidden";
	}
	else if(categoryid == 7)
	{
		football.className = "hidden";
		baseball.className = "hidden";
		basketball.className = "hidden";
		racing.className = "display";
		golf.className = "hidden";
		hockey.className = "hidden";
	}
	else if(categoryid == 4)
	{
		football.className = "hidden";
		baseball.className = "hidden";
		basketball.className = "hidden";
		racing.className = "hidden";
		golf.className = "display";
		hockey.className = "hidden";
	}else if(categoryid == 3)
	{
		football.className = "hidden";
		baseball.className = "hidden";
		basketball.className = "hidden";
		racing.className = "hidden";
		golf.className = "hidden";
		hockey.className = "display";
	}
	else
	{
		football.className = "hidden";
		baseball.className = "hidden";
		basketball.className = "hidden";
		racing.className = "hidden";
		golf.className = "hidden";
		hockey.className = "hidden";
	}
}
function SetOpenFilters() 
{
	var FixedURL = fixAjaxURL();
	var action = "ShowMenu";
	var PopUpURL = FixedURL+'phpajax/LoadFantasyGamesMenu.ajax.php?action='+action;
	$.fn.colorbox({iframe:true, innerWidth:805, innerHeight:205, href:PopUpURL});
}
function ShowGameDetails(seriesid,stageid,optionid)
{
	var FixedURL = fixAjaxURL();
	var action = "GameDetails";
	var PopUpURL = FixedURL+'phpajax/LoadFantasyGamesMenu.ajax.php?action='+action+'&seriesid='+seriesid+'&stageid='+stageid+'&optionid='+optionid;
	$.fn.colorbox({iframe:true, innerWidth:580, innerHeight:235, href:PopUpURL});
}
function ShowPrizes(optionid)
{
	var FixedURL = fixAjaxURL();
	var action = "PrizeDetails";
	var PopUpURL = FixedURL+'phpajax/LoadFantasyGamesMenu.ajax.php?action='+action+'&optionid='+optionid;
	$.fn.colorbox({iframe:true, innerWidth:200, innerHeight:100, href:PopUpURL});
}
