// JavaScript Document
function setCurrentDate() {
  // changes the date selector menus to the current date
  var currentDate = new Date();

  document.searchForm.year2.selectedIndex = document.searchForm.year2.options.length-1;
  document.searchForm.year.selectedIndex = 0;
  
  document.searchForm.month2.selectedIndex = document.searchForm.month2.options.length-1;
  document.searchForm.month.selectedIndex = currentDate.getMonth();
if(document.searchForm.year.value != currentDate.getYear())
{
	document.searchForm.month.selectedIndex=0;
}
  setDays();  
  document.searchForm.day.selectedIndex = 0;
  document.searchForm.day2.selectedIndex = document.searchForm.day2.options.length-1;
}

function setDays() {

  var y = document.searchForm.year.options[document.searchForm.year.selectedIndex].value;
  var m = document.searchForm.month.selectedIndex;
  var d;

  // find number of days in current month
  if ( (m == 3) || (m == 5) || (m == 8) || (m == 10) ) {
    days = 30;
  }
  else if (m == 1) {
    // check for leapyear - Any year divisible by 4, except those divisible by 100 (but NOT 400)
    if ( (Math.floor(y/4) == (y/4)) && ((Math.floor(y/100) != (y/100)) || (Math.floor(y/400) == (y/400))) )
      days = 29;
    else
      days = 28;
  }
  else {
    days = 31;
  }


  // if (days in new month > current days) then we must add the extra days
  if (days > document.searchForm.day.length) {
    for (i = document.searchForm.day.length; i < days; i++) {
      document.searchForm.day.length = days;
      document.searchForm.day.options[i].text = i + 1;
      document.searchForm.day.options[i].value = i + 1;
    }
  }

  
  // if (days in new month < current days) then we must delete the extra days
  if (days < document.searchForm.day.length) {
    document.searchForm.day.length = days;
    if (document.searchForm.day.selectedIndex == -1) 
      document.searchForm.day.selectedIndex = days - 1;
  }

}


function setDays2() {

  var y = document.searchForm.year2.options[document.searchForm.year2.selectedIndex].value;
  var m = document.searchForm.month2.selectedIndex;
  var d;

  // find number of days in current month
  if ( (m == 3) || (m == 5) || (m == 8) || (m == 10) ) {
    days = 30;
  }
  else if (m == 1) {
    // check for leapyear - Any year divisible by 4, except those divisible by 100 (but NOT 400)
    if ( (Math.floor(y/4) == (y/4)) && ((Math.floor(y/100) != (y/100)) || (Math.floor(y/400) == (y/400))) )
      days = 29;
    else
      days = 28;
  }
  else {
    days = 31;
  }


  // if (days in new month > current days) then we must add the extra days
  if (days > document.searchForm.day2.length) {
    for (i = document.searchForm.day2.length; i < days; i++) {
      document.searchForm.day2.length = days;
      document.searchForm.day2.options[i].text = i + 1;
      document.searchForm.day2.options[i].value = i + 1;
    }
  }

  
  // if (days in new month < current days) then we must delete the extra days
  if (days < document.searchForm.day2.length) {
    document.searchForm.day2.length = days;
    if (document.searchForm.day2.selectedIndex == -1) 
      document.searchForm.day2.selectedIndex = days - 1;
  }

}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function getrequest(compid)
	{
		
		varForm=document.getElementById("frmgetletter");
		varForm.submit();
	}
function compwarning(id,compid)
	{
		
				varoutcome=confirm("Players who are not members of  a recognised golf club may not be eligible to play in all competitions\n If this could affect you, please contact the club before sending in your entry form. \n Press OK to continue");
				
					return varoutcome;
				
			
	}

function getacknow(compid)
	{
		
					theURL="../../competitions/acknowledgment_form.php?compid="+compid;
					features="width=700px,scrollbars=1";
					winName="acknowledge";
					window.open(theURL,winName,features);
	}
	
	function formprint(compid)
		{
				
			alert ("An acknowledgement slip will now be displayed on screen \n You should also print and return this with your form"); 
			theURL="../../competitions/acknowledgment_form.php?compid="+compid;
					features="width=700px,scrollbars=1";
					winName="acknowledge";
					window.print();
					window.location.href=theURL;
		}
function formSubmit(formname)
{
document.getElementById(formname).submit()
}

function UserValidate()
	{
			var pwrd=document.getElementById("pwrd");
			var name=document.getElementById("Name");
			var pswderror=document.getElementById("validpswd");
			var usererror=document.getElementById("validuser");
			noerr=true;
		
			if(name.value.indexOf('@') == -1)
				
				{
					usererror.innerHTML="<strong>Invalid User id</strong>";
					name.focus();
					noerr=false;
				}
			if (pwrd.value=="")
				{
					pswderror.innerHTML="<strong>empty password</strong>";
					pwrd.focus();
					noerr=false;	
				}
			return noerr; 
			
	}
function bookmark(title,url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url,"");
    } else if( document.all ) {
        window.external.AddFavorite( url, title);
    } else if( window.opera && window.print ) {
        return true;
    }
}
