//general form functions

function changeAction(strFormName,strAction){
//changes the action of a form and submits it	
eval('document.' + strFormName + '.action=strAction');
eval('document.' + strFormName + '.submit()');
}
function checkEmailForm(){
//ensures that the comment field on fedback/comms panel form has content
if (document.forms['sendmail'].comment.value.length==0){
	alert('There is no text in this email');
	document.forms['sendmail'].comment.focus();
	return false;
}
return true;
}

function moveItem(strForm,objFrom,objTo,strTag){
//moves an item from one select box to another
var intToLength=eval('document.' + strForm + '.' + objTo + '.length');
//check if there is only a blank option
if((intToLength==1) && (eval('document.' + strForm + '.' + objTo + '.options[0].text')=='')){
	intToLength=0;
}
//check that something has been selected
var intPosition=eval('document.' + strForm + '.' + objFrom + '.options.selectedIndex'); 
if (intPosition.length<1){
	alert('Please choose a player to move');
	}else{
	//create a new option and add it to one list
	var strText=eval('document.' + strForm + '.' + objFrom + '.options[intPosition].text');
	var strValue=eval('document.' + strForm + '.' + objFrom + '.options[intPosition].value');
	//if strTag has been passed, add it to the end of the value
		if(strTag){
			if(strValue.indexOf('|')!=-1){
			var aryTemp=strValue.split('|');
			strValue=aryTemp[0];	
			}
		strValue+='|' + strTag;
		}
	objOption = new Option(strText,strValue, false, false);
	eval('document.' + strForm + '.' + objTo + '.options[intToLength]=objOption');
	//remove from the other list
	eval('document.' + strForm + '.' + objFrom + '.options[intPosition]=null');
	}
}

//string functions

function capitaliseFirstLetter(strString){
var strFirst=strString.substr(0,1);
strFirst=strFirst.toUpperCase();
strString=strFirst + strString.substr(1);
return strString;
}

//validation functions
function rtnEmptyStringIfNaN(theInput){
if (parseInt(theInput)>0){
	return theInput;
}
return '';
}

function showRounds(strFormName,strCompDropDown,strArrayName,strRoundDropDown){

//populates the rounds dropdown if necessary
	if(checkCompType(eval('document.' + strFormName + '.' + strCompDropDown + '[document.' + strFormName + '.' + strCompDropDown + '.selectedIndex].value'))){
		//get the rounds for this competition
		var aryCompTemp=eval('document.' + strFormName + '.' + strCompDropDown + '[document.' + strFormName + '.' + strCompDropDown + '.selectedIndex].value.split(\'|\')');
		var intCompid=aryCompTemp[0];
		var j=0;
		//go through the array and split each one
		for(i=0;i<eval(strArrayName +'.length');i++){
		aryRoundTemp=eval(strArrayName+ '[i].split(\'|\')');
		intRoundidTemp=aryRoundTemp[0];
		intCompidTemp=aryRoundTemp[1];
		strRoundNameTemp=aryRoundTemp[2];
			if(intCompid==intCompidTemp){
			objOption=new Option(strRoundNameTemp,intRoundidTemp, false, false);
			eval('document.' + strFormName + '.' + strRoundDropDown + '.options[j]=objOption');
			j+=1;
			}
		
		}
		eval('document.' + strFormName + '.' + strRoundDropDown + '.selectedIndex=0');
	}else{
	//empty the rounds dropdown
		eval('document.' + strFormName + '.' + strRoundDropDown + '.options.length=0');
	}

}
function checkCompType(strComp){
//returns true if it is a cup
var aryCompType=strComp.split('|');
if(aryCompType[1]=='CUP'){
	return true;
	}
return false;
}
//called from /admin/addreport.asp and /admin/editscore.asp
function checkPlayers(intPlayers,strFormName){
var aryPlayerCheck=new Array();
for(i=1;i<=intPlayers;i++){
	intFlag=0;
	objPlayer=eval('document.' + strFormName + '.player' + i);
	aryPlayerCheck[i]=objPlayer.options[objPlayer.selectedIndex].value;
	intTemp=aryPlayerCheck[i];
	for(j=0;j<aryPlayerCheck.length;j++){
		if ((intTemp==aryPlayerCheck[j]) && (aryPlayerCheck[j]!='')){
			intFlag++;
			if (intFlag>1){
				alert('You have the same player in two positions');
				return false;
				}
			}
		}
	}
return true;
}


function checkFixtureForm(){
	//called from /admin/addfixture.asp and /admin/editindividualfixture.asp
if (document.addfixture.opponent.selectedIndex==0){
	alert('Please select an Opponent');	
	document.addfixture.opponent.focus();
	return false;
	}
if (document.addfixture.venue.selectedIndex==0){
	alert('Please select a venue');
	document.addfixture.venue.focus();
	return false;
	}
if (document.addfixture.competition.selectedIndex==0){	
	alert('Please select a competition');
	document.addfixture.competition.focus();
	return false;
	}
//alert(document.addfixture.year.options[document.addfixture.year.selectedIndex].value + 'year');
var aryKO=document.addfixture.kickoff.options[document.addfixture.kickoff.selectedIndex].value.split(':');
var intHour=parseInt(aryKO[0]);
var intMin=parseInt(aryKO[1]);

var objDate=new Date(document.addfixture.year.options[document.addfixture.year.selectedIndex].value,document.addfixture.month.options[document.addfixture.month.selectedIndex].value-1,document.addfixture.day.options[document.addfixture.day.selectedIndex].value,intHour,intMin);
var strDate=objDate.toString();
var intDay=objDate.getDay();
intDay=parseInt(intDay);
//alert(intDay + 'day' + intHour + 'hour');
if (intDay==6 && intHour>15){
alert (strDate + ' is a Saturday but the kick-off time is after 3 p.m. Please input the correct kick-off time.');
document.addfixture.kickoff.focus();
return false;
}
if (intDay<6 && intDay>1 && intHour<19){
	var blnDateOK=confirm(strDate + ' is not a Saturday/Sunday/Monday but the kick-off time is 3 p.m. Are you sure this is the correct kick-off time?');
	if(!blnDateOK){
		document.addfixture.kickoff.focus();
		return false;
	}	
}
return true;
}

//Display functions
function openPollWindow(intPollID,intWidth,intHeight){
	var strView='questions';
	//alert(checkCookie(intPollID) + ' * ' + intPollID);
	if (checkCookie(intPollID)){
		strView='results';
		}
	window.open("/polls/popup.asp?view=" +strView + "&questionID="+ intPollID,"Poll" + intPollID,"toolbar=no, leftmargin=0, topmargin=0, location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=" +intWidth+",height=" +intHeight); 
}
function closeWindow(){
	window.opener.focus();
	window.close();

}

//Cookie and poll functions
function checkCookie(intPollID){
	//if cookie exists, check pollid and return true or false
	//alert('ok' + document.cookie);
	if (document.cookie.indexOf('poll')!=-1){
		var myArray=document.cookie.split('|');
		var poll=myArray[1];
		if (poll==intPollID){
			return true;
		}
	}
	return false;
}

function writeCookie(intPollID){
	document.cookie='poll|' + intPollID + '|=true;expires=' + getExpiryDate() + ';path=/';
	//alert(document.cookie)
}


function processVote(intPollID){
	//alert(checkCookie(intPollID) + 'checkcookie * ' + intPollID + 'pollid | ' + document.cookie + 'docu.cook');
	if (checkCookie(intPollID)){
		alert('Sorry, you have already voted in this poll');
		location.href='/polls/popup.asp?view=results&questionid=' + intPollID;
	}
	else{
		//alert('Write');
		if(checkVote()){
		writeCookie(intPollID);
		document.vote.submit();
		}
	}
}

