var bkColor = "yellow";

function changePage(newLoc)
 {
   nextPage = newLoc.options[newLoc.selectedIndex].value
   if (nextPage != "")
   {
      document.location.href = nextPage
   }
 }

function changeTournament(newLoc)
 {
   alert("The change of Tournament will only affect the information displayed on the 'Tournament' and 'Statistics' sections.");
   nextPage = newLoc.options[newLoc.selectedIndex].value
   if (nextPage != "")
   {
      document.location.href = nextPage
   }
 }

function setBKColor(objControl)
{
  objControl.style.bkColor = objControl.style.backgroundColor;
  objControl.style.backgroundColor = bkColor;
  if (!(objControl.type == 'select-one'))
    objControl.select();
}
function reSetBKColor(objControl)
{
  objControl.style.backgroundColor = objControl.style.bkColor;
}

function CheckDataForm(frm)
{
  if (verify(frm))
    { return CheckIfGameBetweenSameTeam(frm); }
  else
    { return false; }
}

function checkRegistrationForm(frm)
{
  if (verify(frm))
    {
      if (!validateEmail(frm.EmailAddress.value,1,1))
      {
        frm.EmailAddress.focus(); 
        return false;
      }
    }
  else
    { return false; }
}

function CheckIfGameBetweenSameTeam(frm)
{
  if (frm.HostTeam.options[frm.HostTeam.options.selectedIndex].value == frm.VisitorTeam.options[frm.VisitorTeam.options.selectedIndex].value)
  {
    alert('Game can not be scheduled between same team!  Select different teams');
    return false;
  }
}

function checkPasswords(frm)
{
  if (verify(frm))
    { 
      if (frm.NewPassword.value != frm.PasswordConfirmation.value)
      {
        alert('You have typed two different passwords.  Passwords must match!');
        return false;
      }
    }
  else
    { return false; }
}

// A utility function that returns true if a string contains only 
// whitespace characters.
function isblank(s) {
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

// This is the function that performs form verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
function verify(f) {
    var msg;
    var empty_fields = "";
    var errors = "";

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // verify that they are numbers and in the right range.
    // If the element has a "numeric" property defined, verify that
    // it is a number, but don't check its range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) 
    {
        var e = f.elements[i];
        if ((e.type == "text") || (e.type == "textarea") || (e.type == "select-one") || (e.type == "password"))
        {
          if (!e.optional)
          {
            if ((e.value == null) || (e.value == "") || isblank(e.value)) 
            {
                empty_fields += "\n          " + e.name;
                continue;
            }

            // Now check for fields that are supposed to be numeric.
            if (e.numeric || (e.min != null) || (e.max != null)) 
            { 
                var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) 
                {
                    errors += "- The field " + e.name + " must be a number";
                    if (e.min != null) 
                        errors += " that is greater than " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " and less than " + e.max;
                    else if (e.max != null)
                        errors += " that is less than " + e.max;
                    errors += ".\n";
                }
            }
          }
          //alert(e.name + " length: " + e.value.length + " and Maxlength: " + e.maxlength );
          //errors += "- The field " + e.name + " can not be more than " + e.maxlength + " characters.";
          if (e.value.length > e.maxlength)
          { 
            errors += "- The field " + e.name + " can not be more than " + e.maxlength + " characters.";
          }
        }
    }

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) return true;

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- The following required field(s) are empty:" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
}

function validateEmail(addr,man,db) 
{
  if (addr == '' && man) {
     if (db) alert('email address is mandatory');
     return false;
  }
  var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
  for (i=0; i<invalidChars.length; i++) {
     if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
        if (db) alert('email address contains invalid characters');
        return false;
     }
  }
  for (i=0; i<addr.length; i++) {
     if (addr.charCodeAt(i)>127) {
        if (db) alert("email address contains non ascii characters.");
        return false;
     }
  }
  var atPos = addr.indexOf('@',0);
  if (atPos == -1) {
     if (db) alert('email address must contain an @');
     return false;
  }
  if (atPos == 0) {
     if (db) alert('email address must not start with @');
     return false;
  }
  if (addr.indexOf('@', atPos + 1) > - 1) {
     if (db) alert('email address must contain only one @');
     return false;
  }
  if (addr.indexOf('.', atPos) == -1) {
     if (db) alert('email address must contain a period in the domain name');
     return false;
  }
  if (addr.indexOf('@.',0) != -1) {
     if (db) alert('period must not immediately follow @ in email address');
     return false;
  }
  if (addr.indexOf('.@',0) != -1){
     if (db) alert('period must not immediately precede @ in email address');
     return false;
  }
  if (addr.indexOf('..',0) != -1) {
     if (db) alert('two periods must not be adjacent in email address');
     return false;
  }
  var suffix = addr.substring(addr.lastIndexOf('.')+1);
  if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
     if (db) alert('invalid primary domain in email address');
     return false;
  }
  return true;
}

function scrollText(pText)
{
//Specify the marquee's width (in pixels)
var marqueewidth="300px"
//Specify the marquee's height
var marqueeheight="25px"
//Specify the marquee's marquee speed (larger is faster 1-10)
var marqueespeed=2
//configure background color:
var marqueebgcolor="#DEFDD9"
//Pause marquee onMousever (0=no. 1=yes)?
var pauseit=1

//Specify the marquee's content (don't delete <nobr> tag)
//Keep all content on ONE line, and backslash any single quotations (ie: that\'s great):

var marqueecontent='<nobr>'+pText+'</nobr>'


////NO NEED TO EDIT BELOW THIS LINE////////////
marqueespeed=(document.all)? marqueespeed : Math.max(1, marqueespeed-1) //slow speed down by 1 for NS
var copyspeed=marqueespeed
var pausespeed=(pauseit==0)? copyspeed: 0
var iedom=document.all||document.getElementById
if (iedom)
document.write('<span id="temp" style="visibility:hidden;position:absolute;top:-100px;left:-9000px">'+marqueecontent+'</span>')
var actualwidth=''
var cross_marquee, ns_marquee

function populate(){
if (iedom){
cross_marquee=document.getElementById? document.getElementById("iemarquee") : document.all.iemarquee
cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
cross_marquee.innerHTML=marqueecontent
actualwidth=document.all? temp.offsetWidth : document.getElementById("temp").offsetWidth
}
else if (document.layers){
ns_marquee=document.ns_marquee.document.ns_marquee2
ns_marquee.left=parseInt(marqueewidth)+8
ns_marquee.document.write(marqueecontent)
ns_marquee.document.close()
actualwidth=ns_marquee.document.width
}
lefttime=setInterval("scrollmarquee()",20)
}
window.onload=populate

function scrollmarquee(){
if (iedom){
if (parseInt(cross_marquee.style.left)>(actualwidth*(-1)+8))
cross_marquee.style.left=parseInt(cross_marquee.style.left)-copyspeed+"px"
else
cross_marquee.style.left=parseInt(marqueewidth)+8+"px"

}
else if (document.layers){
if (ns_marquee.left>(actualwidth*(-1)+8))
ns_marquee.left-=copyspeed
else
ns_marquee.left=parseInt(marqueewidth)+8
}
}

if (iedom||document.layers){
with (document){
document.write('<table border="0" cellspacing="0" cellpadding="0"><td>')
if (iedom){
write('<div style="position:relative;width:'+marqueewidth+';height:'+marqueeheight+';overflow:hidden">')
write('<div style="position:absolute;width:'+marqueewidth+';height:'+marqueeheight+';background-color:'+marqueebgcolor+'" onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=marqueespeed">')
write('<div id="iemarquee" style="position:absolute;left:0px;top:0px"></div>')
write('</div></div>')
}
else if (document.layers){
write('<ilayer width='+marqueewidth+' height='+marqueeheight+' name="ns_marquee" bgColor='+marqueebgcolor+'>')
write('<layer name="ns_marquee2" left=0 top=0 onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=marqueespeed"></layer>')
write('</ilayer>')
}
document.write('</td></table>')
}
}
}