function openTutorialPopup()
{
  tutwindow=window.open("http://www.dailydonkey.com/tutorial.html",'tutwindow','height=600,width=580,resizable=NO,left=100,top=50,scrollbars=YES');
  if (window.focus) {
    newwindow.focus()
  }

}

function openOddsPopup()
{
  tutwindow=window.open("http://www.dailydonkey.com/odds2.php",'oddswindow','height=500,width=580,resizable=NO,left=100,top=50,scrollbars=YES');
  if (window.focus) {
    newwindow.focus()
  }

}
function openPopup(url)
{
  newwindow=window.open(url,'browseresults','height=400,width=770,resizable=YES,left=100,top=50,scrollbars=YES');
  if (window.focus) {
    newwindow.focus()
  }
}

function openFormPopup( url )
{
  newwindow=window.open(url,'formpopup','height=450,width=360,resizable=YES,left=100,top=50,scrollbars=YES');
  if (window.focus) {
    newwindow.focus()
  }
}

function isNumeric(inputText)
{
   var validChars = "0123456789.";
   var isNumber = true;
   var inp;

   for (i = 0; i < inputText.length && isNumber == true; i++)
   {
      inp = inputText.charAt(i);
      if( validChars.indexOf(inp) == -1 )
      {
        isNumber = false;
      }
   }

   return isNumber;
}

function validateOdds()
{
  if( managedonkeys.odds.value == null || managedonkeys.odds.value == "" )
  {
    alert("Please enter the Odds on this donkey")
  }
}

function processStake()
{
    var oddsNumeric = isNumeric(managedonkeys.odds.value)
    var stakeNumeric = isNumeric(managedonkeys.stake.value)

    if( !oddsNumeric || !stakeNumeric )
    {
        alert("The odds or stake field do not contain numeric data which is required.")

        if(!oddsNumeric)
        {
            managedonkeys.odds.focus();
        }
        else
        {
            managedonkeys.stake.focus();
        }

        return
    }

    managedonkeys.liability.value = ( managedonkeys.odds.value * managedonkeys.stake.value ) - managedonkeys.stake.value;
}

function processResult()
{
    if( !isNumeric(managedonkeys.result.value) && managedonkeys.result.value != "n/r" )
    {
      alert("The result box contains invalid data entry.")
      return
    }

    if( managedonkeys.result.value == "n/r")
    {
        managedonkeys.profit.value = 0
        managedonkeys.loss.value = 0
    }
    else if( managedonkeys.result.value == 1 )
    {
        managedonkeys.profit.value = 0
        managedonkeys.loss.value = managedonkeys.liability.value
    }
    else
    {
        managedonkeys.profit.value = managedonkeys.stake.value
        managedonkeys.loss.value = 0
    }

    //this is not complete - it will be accumulated to the total column in the database
    managedonkeys.total.value = ( managedonkeys.profit.value - managedonkeys.loss.value )
}

function findObj(n)
{
	for( i = 0; i < document.forms.length; i++ )
	{
		if(document.forms[i][n] )
		{
		  x = document.forms[i][n];
		  break;
		}
	}
  return x;
}

function validateFormFields()
{
	 var curr_token, flag;
	 var i, errors = '', args=validateFormFields.arguments;
	
	 for (i = 0; i < ( args.length - 1 ); i += 3)
	 {
	  flag = args[i+1];
	  curr_token = findObj( args[i] );

	  if( flag == 'E' )
	  {
	    if( checkMail( curr_token.value ) == false || curr_token.value == "" || curr_token.value == "-1" )
	    {
	      errors += '- Valid email is required.\n';
	    }
	  }
	
	  if( flag == 'R' )
	  {
	    if( curr_token.value == "" || curr_token.value == "-1" )
	    {
	      errors += '- ' + args[i+2] + ' is required.\n';
	    }
	  }
	
	  if( flag == 'RC' ) 
	  {
	    next_token = findObj( args[i+3] );
	    // A password is required so check both
	    if( curr_token.value != "" && next_token.value != "" )
	    {
	      if( curr_token.value != next_token.value )
	      {
	        errors += '- Passwords dont match.\n';
	      }
	    }
	    else
	    {
	      errors += '- Please fill-in both password fields.\n';
	    }
	  }
	  if( flag == 'C' )
	  {
	    // A password may not be required here
	    if( curr_token.value != "" )
	    {
	      next_token = findObj( args[i+3] );
	      if( curr_token.value != next_token.value )
	      {
	        errors += '- Passwords dont match.\n';
	      }
	    }
	  }
	
	  if( flag == 'C2' )
	  {
	    // A password may not be required here
	    if( curr_token.value != "" )
	    {
	      next_token = findObj( args[i-3] );
	      if( curr_token.value != next_token.value )
	      {
	        errors += '- Passwords dont match.\n';
	      }
	    }
	  }
	 }
	 if( errors )
	 {
	  alert( 'The following error(s) occurred:\n' + errors );
	 }
	 
	 document.returnValue = ( errors == '' );
}

function checkMail( email )
{
  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if ( filter.test( email ) )
    return true;
  else
    return false;
}

function writeDate() {
	var d=new Date()
	var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
	var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December")

	document.write(weekday[d.getDay()] + ", ");
	document.write(d.getDate() + " ");
	var hours = d.getHours();
	if( hours < 10 )
		hours = "0" + hours;
	
	var mins = d.getMinutes();
	
	if( mins < 10 )
		mins = "0" + mins;
		
	document.write(monthname[d.getMonth()] + " ( " + hours + ":" + mins + " GMT )" );
}

function clearField( fieldObj ) {
	fieldObj.value="";
}

var numbers = new Array(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1)
var total_sky_ads = 4;
var sky_ad_index =Math.ceil((Math.random()*99)%total_sky_ads);
var last_ad = 0;
function cycleAdsSky()
{
	if( document.getElementById('sa_'+sky_ad_index) ) {
		if(++sky_ad_index > total_sky_ads )
			sky_ad_index = 1;
	
		if( last_ad != 0 ) {
			last_ad.style.display = "none";
			last_ad = document.getElementById('sa_'+sky_ad_index);
		}
		else {
			last_ad = document.getElementById('sa_'+sky_ad_index);
		}
		last_ad.style.display = 'block';
			
	}			
	
	setTimeout("cycleAdsSky()", 15000);
}

window.onload = cycleAdsSky;
