/***********************************************
* Universal Countdown script- © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

function cdLocalTime(container, servermode, offsetMinutes, targetdate, localtime, TippingClosed, RoundNumber, debugmode){


if (!document.getElementById || !document.getElementById(container)) return
this.container=document.getElementById(container)
var servertimestring=(servermode=="server-php")? localtime : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'

<!--var servertimestring=(servermode=="server-php")? '<?php echo date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'-->
this.TippingClosed = TippingClosed
this.RoundNumber = RoundNumber
this.localtime=this.serverdate=new Date(servertimestring)
this.targetdate=new Date(targetdate)
this.debugmode=(typeof debugmode!="undefined")? 1 : 0
this.timesup=false
this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
this.updateTime()
}

cdLocalTime.prototype.updateTime=function(){
var thisobj=this
this.localtime.setSeconds(this.localtime.getSeconds()+1)
setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}

cdLocalTime.prototype.displaycountdown=function(baseunit, functionref){
this.baseunit=baseunit
this.formatresults=functionref
this.showresults()
}

cdLocalTime.prototype.showresults=function(){
var thisobj=this
var debugstring=(this.debugmode)? "<p style=\"background-color: #FCD6D6; color: black; padding: 5px\"><big>Debug Mode on!</big><br /><b>Current Local time:</b> "+this.localtime.toLocaleString()+"<br />Verify this is the correct current local time, in other words, time zone of count down date.<br /><br /><b>Target Time:</b> "+this.targetdate.toLocaleString()+"<br />Verify this is the date/time you wish to count down to (should be a future date).</p>" : ""

var timediff=(this.targetdate-this.localtime)/1000 //difference btw target date and current date, in seconds
if (timediff<0){ //if time is up
this.timesup=true
this.container.innerHTML=debugstring+this.formatresults()
return
}
var oneMinute=60 //minute unit in seconds
var oneHour=60*60 //hour unit in seconds
var oneDay=60*60*24 //day unit in seconds
var dayfield=Math.floor(timediff/oneDay)
var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
hourfield=dayfield*24+hourfield
dayfield="n/a"
}
else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
minutefield=dayfield*24*60+hourfield*60+minutefield
dayfield=hourfield="n/a"
}
else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
var secondfield=timediff
dayfield=hourfield=minutefield="n/a"
}
this.container.innerHTML=debugstring+this.formatresults(dayfield, hourfield, minutefield, secondfield)
setTimeout(function(){thisobj.showresults()}, 1000) //update results every second
}

/////CUSTOM FORMAT OUTPUT FUNCTIONS BELOW//////////////////////////////

//Create your own custom format function to pass into cdLocalTime.displaycountdown()
//Use arguments[0] to access "Days" left
//Use arguments[1] to access "Hours" left
//Use arguments[2] to access "Minutes" left
//Use arguments[3] to access "Seconds" left

//The values of these arguments may change depending on the "baseunit" parameter of cdLocalTime.displaycountdown()
//For example, if "baseunit" is set to "hours", arguments[0] becomes meaningless and contains "n/a"
//For example, if "baseunit" is set to "minutes", arguments[0] and arguments[1] become meaningless etc

//1) Display countdown using plain text
function formatresults(){
if (this.timesup==false){//if target date/time not yet met
var displaystring="<span>"+arguments[0]+" days "+arguments[1]+" hours "+arguments[2]+" minutes "+arguments[3]+" seconds</span> left until <b>Round "+ this.RoundNumber + "</b> lockout"
//#CFEAFE
}
else{ //else if target date/time met

	if (this.TippingClosed == "false") {
	window.location.reload()
	return ""
	}
	else {
	var displaystring= "Tipping for <b>Round " + this.RoundNumber + "</b> Closed"
	}
}
return displaystring
}

//2) Display countdown with a stylish LCD look, and display an alert on target date/time
function formatresults2(){
if (this.timesup==false){ //if target date/time not yet met
var displaystring="<span class='lcdstyle'>"+arguments[0]+" <sup>days</sup> "+arguments[1]+" <sup>hours</sup> "+arguments[2]+" <sup>minutes</sup> "+arguments[3]+" <sup>seconds</sup></span> left until round 1"
}
else{ //else if target date/time met
var displaystring="" //Don't display any text

window.location.reload()
//alert("Launch time!") //Instead, perform a custom alert
}
return displaystring
}


function validate(theform, $CheckEmail) {
 
	//Every form has first, last name, email and phone 	

		if (Trim(theform.username.value)=="") {

		alert("Please enter a login name");

		theform.username.focus()

		return false; }



		if (Trim(theform.password.value)=="") {

		alert("Please enter a password");

		theform.password.focus()

		return false; }

	
		if($CheckEmail)
		{
			if (Trim(theform.email.value)=="") {
	
			alert("Please enter an email address");
	
			theform.email.focus()
	
			return false; }
		}
				

return true;
}

function validateEmail(theform) {
 
			if (Trim(theform.email.value)=="") {
	
			alert("Please enter an email address");
	
			theform.email.focus()
	
			return false; }			

return true;
}
// I renamed the function TrimUsingWhileLoop to just Trim for short

function Trim(str)

{  while(str.charAt(0) == (" ") )

  {  str = str.substring(1);

  }

  while(str.charAt(str.length-1) == " " )

  {  str = str.substring(0,str.length-1);

  }

  return str;

}



function radio_button_checker(theform)
{
// set var radio_choice to false
var radio_choice = false;

// Loop from zero to the one minus the number of radio button selections
for (counter = 0; counter < theform.tip.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (theform.tip[counter].checked)
radio_choice = true; 
}

if (!radio_choice)
{
// If there were no selections made display an alert box 
alert("Please select your tip")
return (false);
}
return (true);
}


function score_checker(theform, submitbutton) {

	//if we're importing we don't need to validate
	if(submitbutton == "Import Results") {
	return true;
	}

var allResults = true;
var result;
// Loop from zero to the one minus the number of radio button selections
	for (counter = 1; counter <9; counter++)
	{
		result=""; //reset
		switch(counter)
		{
		case 1:
		  result = theform.result1.value;
			break;
		case 2:
		 result = theform.result2.value;
		  break;
				
		case 3:
		 result = theform.result3.value;
		  break;
				
		case 4:
		 result = theform.result4.value;
		  break;
		
		case 5:
		 result = theform.result5.value;
		  break;
		
		case 6:
		 result = theform.result6.value;
		  break;
		
		case 7:
		 result = theform.result7.value;
		  break;
				
		case 8:
		 result = theform.result8.value;
		  break;
		}
	
		if (result =="") {
		allResults =false;
		}
	
	}
	
	if (allResults==false) {
	alert("Please enter a result for each game");

	return false; }

return true;
}
	
	
	function validateShoutBox(theform) {
 
	//Every form has first, last name, email and phone 	

		if (Trim(theform.message.value)=="") {

		alert("Please enter a message");

		theform.message.focus()

		return false; }
		
		return true;
		
	}
	
	
	function sendValue (GolferName, Points, GolferID, CategoryID){

//window.opener.document.getElementById('details').value = selvalue;
//window.document.getElementById('player'+CategoryID).value = GolferName;
window.document.getElementById('hiddenplayer'+CategoryID).value = GolferName;
window.document.getElementById('hiddenpoints'+CategoryID).value = Points.toFixed(2);
//window.document.getElementById('hiddenid'+CategoryID).value = GolferID;

document.getElementById('player'+CategoryID).innerHTML = GolferName;
document.getElementById('points'+CategoryID).innerHTML = Points.toFixed(2);

var TotalPoints;
TotalPoints = GetTotalPoints();


//alert(TotalPoints);
document.getElementById('divtotalpoints').innerHTML = 'Total Points: '+TotalPoints.toFixed(2);
window.document.getElementById('totalpoints').value = TotalPoints.toFixed(2);

ajaxwin.close();
}

function  GetTotalPoints() {

var points1, points2, points3, points4;
points1 = document.getElementById('points1').innerHTML;
points2 = document.getElementById('points2').innerHTML;
points3 = document.getElementById('points3').innerHTML;
points4 = document.getElementById('points4').innerHTML;

	if(points1== "") {
		points1=0;
	}
	
		if(points2== "") {
		points2=0;
	}
	
		if(points3== "") {
		points3=0;
	}
	
		if(points4== "") {
		points4=0;
	}
	

	//alert(points1);
	//alert(points2);
	//alert(points3);
	//alert(points4);
	
return parseFloat(points1) + parseFloat(points2) + parseFloat(points3) + parseFloat(points4);
}





	function validateTips() {
		
		if(document.getElementById('player1').innerHTML == "") {
			alert("Please select an Australian golfer");
				  return false;
		}
		
		if(document.getElementById('player2').innerHTML == "") {
			alert("Please select a USA golfer");
				  return false;
		}
		
		if(document.getElementById('player3').innerHTML == "") {
			alert("Please select a Rest Of The World golfer");
				  return false;
		}
		
		if(document.getElementById('player4').innerHTML == "") {
			alert("Please select a Bonus golfer");
				  return false;
		}
		
		
		//Now check the total points doesn't exceed 10 points
		var points_array, points;
		points = document.getElementById('divtotalpoints').innerHTML;
		//alert(points);
		
		points_array = points.split(" ");

		//alert(points_array[2]);
		
		if(parseFloat(points_array[2]) > 10) {
			alert("Your squads total points exceeds the maximum of 10.00 points");
			return false;
		}
		
	return true;
	}
		
		
		
		
//The below function shows/hides the weekly results for the selected player
/*function toggle(showHideDiv, TournamentID) {//switchTextDiv) {
	
	//alert(TournamentID);
	var ele = document.getElementById(showHideDiv);
	var text = document.getElementById(showHideDiv);
	if(ele.style.display == "block") {
    		ele.style.display = "none";
		//text.innerHTML = "show";
  	}
	else {

		ele.style.display = "block";
		text.innerHTML = TournamentID;
		
	}
} 
*/



/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var loadedobjects=""
var rootdomain="http://"+window.location.hostname

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
	
	page_request.open('GET', url, true)
	page_request.send(null)
		
}

function loadpage(page_request, containerid){
	
		
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
			
		var ele = document.getElementById(containerid);
	
			if(ele.style.display == "block") {
					ele.style.display = "none";
			}
			else {
		
				ele.style.display = "block";
				document.getElementById(containerid).innerHTML=page_request.responseText
				
			}
	
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}



	//This is the beginning of the code for Tabs ********************
	
    var tabLinks = new Array();
    var contentDivs = new Array();

   function init() {

	
      // Grab the tab links and content divs from the page
      var tabListItems = document.getElementById('tabs').childNodes;
      for ( var i = 0; i < tabListItems.length; i++ ) {
        if ( tabListItems[i].nodeName == "LI" ) {
          var tabLink = getFirstChildWithTagName( tabListItems[i], 'A' );
          var id = getHash( tabLink.getAttribute('href') );
          tabLinks[id] = tabLink;
          contentDivs[id] = document.getElementById( id );
        }
      }

      // Assign onclick events to the tab links, and
      // highlight the first tab
      var i = 0;

      for ( var id in tabLinks ) {
        tabLinks[id].onclick = showTab;
        tabLinks[id].onfocus = function() { this.blur() };
        if ( i == 0 ) {
			tabLinks[id].className = 'selected';
		    contentDivs[id].className = 'tabContent';
		  }
        i++;
      }

      // Hide all content divs except the first
      //var i = 0;

      //for ( var id in contentDivs ) {
      //  if ( i != 0 ) contentDivs[id].className = 'tabContent hide';
      //  i++;
      //}
    }



    function showTab($tab) {
     //alert($tab);
	 
	 var selectedId;
	 
	 if($tab != "#history") {
	 selectedId = getHash( this.getAttribute('href') );
	 }
	 else {
 	 selectedId = getHash( $tab );
	 }
	 
      // Highlight the selected tab, and dim all others.
      // Also show the selected content div, and hide all others.
      for ( var id in contentDivs ) {
        if ( id == selectedId ) {
          tabLinks[id].className = 'selected';
          contentDivs[id].className = 'tabContent';
        } else {
          tabLinks[id].className = '';
          contentDivs[id].className = 'tabContent hide';
        }
      }

      // Stop the browser following the link
      return false;
    }


    function getFirstChildWithTagName( element, tagName ) {
      for ( var i = 0; i < element.childNodes.length; i++ ) {
        if ( element.childNodes[i].nodeName == tagName ) return element.childNodes[i];
      }
    }

    function getHash( url ) {
//		alert(url);
		
      var hashPos = url.lastIndexOf ( '#' );
      return url.substring( hashPos + 1 );
    }

	//This is the END of the code for Tabs ********************
	
	
	function openmypage(country){ //Define arbitrary function to run desired DHTML Window widget codes
var URL = 'worldrankings.php?c='+country;
ajaxwin=dhtmlwindow.open("ajaxbox", "ajax", URL, "Select Golfer", "width=325px,height=300px,left=300px,top=100px,resize=0,scrolling=1")
}


	function validateReset(resetCheckBox)
	{
		if(resetCheckBox.checked == true)
		{
			if(!confirm("You can reset your squad at any time during the season but only once. Are you sure you want to reset your squad?"))
			{
				return false;
			}
			
			//reset players
			document.getElementById('player1').innerHTML = "";
			document.getElementById('player2').innerHTML = "";
			document.getElementById('player3').innerHTML = "";
			document.getElementById('player4').innerHTML = "";
			
			//reset points
			document.getElementById('points1').innerHTML ="";
			document.getElementById('points2').innerHTML ="";
			document.getElementById('points3').innerHTML ="";
			document.getElementById('points4').innerHTML ="";
			
			//total points 
			document.getElementById('divtotalpoints').innerHTML ="";			
		}
		
		else {
			window.location.reload();
		}
		
		return true;
	}
