/**
*	@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

function ProcessFantasyGame(formid,fee,players,prize,currentElementId,poolid,fantastock,seriesoptionid)
{
	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)
	{
		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;
			
		}
		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 necesary 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;						
					}
				}
			}
		}
		
	}
}

function LoadALLFantasyGames(sport)
{
	setBigLoadingImage();
	var foldersOut = fixAjaxURL();		
	objAjax.setURL(foldersOut+'phpajax/fantasygamesloader.ajax.php');
	objAjax.setParameter('sport',sport);	
	objAjax.setComplete( LoadALLFantasyGamesResponse);
	objAjax.execute();

}
function LoadPrivateFantasyGames(sport,privateurl)
{
	setBigLoadingImage();
	var foldersOut = fixAjaxURL();	
	
	objAjax.setURL(foldersOut+'phpajax/fantasygamesloader.ajax.php');
	objAjax.setParameter('sport',sport);
	objAjax.setParameter('privateurl',privateurl);	
	objAjax.setComplete( LoadALLFantasyGamesResponse);
	objAjax.execute();

}
function LoadALLFantasyGamesResponse()
{	
	var FantasyGamesContainer = document.getElementById('FantasyGamesAvailableContainer');
	if(FantasyGamesContainer!= null)
	{
		FantasyGamesContainer.innerHTML = objAjax.getResponseText();
		// var CounterScripts = document.getElementbyClass('CounterDowndScript');
		// alert('Number of elements to change: '+CounterScripts.length);
		// for(var i=0; i<CounterScripts.length;i++)
		// {
			// eval(CounterScripts[i].innerHTML);
		// }
	}
	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(poolid)
{
	var FormInMess = document.getElementById('Form_'+poolid);
	var FantaStockFlag = document.getElementById('fantastockvalue_'+poolid);
	
	if((FormInMess!=null)&&(FantaStockFlag!=null))
	{
		if(FantaStockFlag.checked==true)
		{
			FormInMess.usefreebalancevalue.value = "true";
		}
		else
		{
			FormInMess.usefreebalancevalue.value = "false";
		}
	}
	else
	{
		alert('Javascript Error: Cannot locate the form and the free balance');
	}
	
}
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);
		return false;
	}
	else
	{
		return true;
	}
	
}
function openlogin(formname, action)
{
	loginwindow=dhtmlmodal.open('LoginBox', 'iframe', '../phpajax/loginpopup.ajax.php?action='+action+'&formname='+formname, 'Log-in to Play Fantasy Games', 'width=280px,height=300px,center=1,resize=1,scrolling=0');
}