function ltrim(sent_str)
// Function that removes the leading spaces.
{
var spaces=new String(" \t\r\n");
var str=new String(sent_str);

// This checks that there really leading blanks before before the string.
if(spaces.indexOf(str.charAt(0))!=-1)
{
    // Keep iterating until you reach at a character that is not a space.
    var j=0, i=str.length;
    while(j<i && spaces.indexOf(str.charAt(j))!=-1) j++;

    // Copy the rest of the string.
    str=str.substring(j,i);
}
return str;
}

function rtrim(sent_str)
// Function that removes the trailing spaces.
// The explanation for this function is same as the
// above function, except for it moves backward.
{
var spaces=new String(" \t\r\n");
var str=new String(sent_str);
if(spaces.indexOf(str.charAt(0))!=-1)
{
var i=str.length-1;
while(i>=0 && spaces.indexOf(str.charAt(i))!=-1)
i--;
str=str.substring(0, i+1);

}
return str;
}


function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function isDate(dtStr){
	var pos1=dtStr.indexOf(' ')
	var pos2=dtStr.indexOf(' ',pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	strMonth = strMonth.toUpperCase()
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)

	// Check date format
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd mmm yyyy (e.g. 11 jan 2004)")
		return false
	}

	// Check day basic range
    if (strDay.length<1 || day<1 || day>31){
		alert("Please enter a valid day")
		return false
	}

	// Check months and days in month
    switch (strMonth)
    {
        // 31 days
	    case 'JAN':
	    case 'MAR':
	    case 'MAY':
	    case 'JUL':
	    case 'AUG':
	    case 'OCT':
	    case 'DEC':
            if (day>31) {
        		alert("Invalid day for this month")
        		return false
        	}
	        break

	    // February !!!
	    case 'FEB':
            if (day>daysInFebruary(year)) {
        		alert("Invalid day for this month")
        		return false
        	}
	        break

        // 30 days
	    case 'APR':
	    case 'JUN':
	    case 'SEP':
	    case 'NOV':
            if (day>30) {
        		alert("Invalid day for this month")
        		return false
        	}
	        break

      	default:
		    alert ('Invalid month (JAN -> DEC are valid) - must be 3 characters')
	        break
    }

	if (strYear.length != 4 || year==0 || year<1950 || year>2050){
		alert("Please enter a valid 4 digit year between 1950 and 2050")
		return false
	}

return true
}

function ValidateMaintMembers1(){

	var dt=document.maintmembers1.date_of_birth
	if (dt.value != '') {
    	if (isDate(dt.value)==false){
    		dt.focus()
    		return false
	    }
    }

	var dt=document.maintmembers1.date_joined
	if (dt.value != '') {
    	if (isDate(dt.value)==false){
    		dt.focus()
    		return false
	    }
    }

	var dt=document.maintmembers1.date_left
	if (dt.value != '') {
    	if (isDate(dt.value)==false){
    		dt.focus()
    		return false
	    }
    }

	if (rtrim(document.maintmembers1.first_name.value) == '') {
		alert("Please enter a first name")
  		return false
    }

	if (rtrim(document.maintmembers1.last_name.value) == '') {
		alert("Please enter a last name")
  		return false
    }

	if (rtrim(document.maintmembers1.password.value) == '') {
		alert("Please enter a password")
  		return false
    }

    return true
}

function ValidateMaintCalendarStandard(){

	if (rtrim(document.maintcalendarstandard.date.value) == '') {
		alert("Please enter a date")
  		return false
    }

	var dt=document.maintcalendarstandard.date
	if (dt.value != '') {
    	if (isDate(dt.value)==false){
    		dt.focus()
    		return false
	    }
    }
}

function ValidateMaintCalendarOther(){

	if (rtrim(document.maintcalendarother.date.value) == '') {
		alert("Please enter a date")
  		return false
    }

	if (rtrim(document.maintcalendarother.otherdesc.value) == '') {
		alert("Please enter a description")
  		return false
    }

	var dt=document.maintcalendarother.date
	if (dt.value != '') {
    	if (isDate(dt.value)==false){
    		dt.focus()
    		return false
	    }
    }
}

function ValidateMaintLinks2(){

	if (rtrim(document.maintlinks2.description.value) == '') {
		alert("Please enter a description")
  		return false
    }


    return true
}

function ValidateMaintOutdoors1(){

	if (rtrim(document.frmmaintoutdoors1.competitiondate.value) == '') {
		alert("Please enter a date")
  		return false
    }

    var dt=document.frmmaintoutdoors1.competitiondate
	if (dt.value != '') {
    	if (isDate(dt.value)==false){
    		dt.focus()
    		return false
	    }
    }


    return true
}



function WriteRandomLink()
{
	var urlarray= new Array
	(
		"http://www.fartypants.com/index.html",
	      	"http://www.bored.com",
	      	"http://www.heptune.com/farts.html",
	      	"http://www.heptune.com/poop.html",
		"http://www.prawnography.net",
		"http://www.heptune.com/StarTrkM.html",
		"http://www.jokes.com",
		"http://www.rotten.com",
		"http://www.travelmateinfo.com",
		"http://www.restrooms.org/standing.html",
		"http://www.femaleurinal.com/factsandfables.html",
		"http://www.hogwatch.org/",
		"http://www.hogwatch.org/html/java/counter.html",
		"http://www.catenema.com/cat1.html",
		"http://shtick.org/Misc/ryans.htm",
		"http://www.darwinawards.com",
		"http://www.mannotincluded.com",
		"http://www.howstuffworks.com",
		"http://www.turdtwister.com"
	);

	var textarray= new Array
	(
		"Fart Retaining Pants",
		"Are you Bored ?",
	        "Facts about Farts",
		"The low down on Pooh",
		"Luscious Prawnography",
		"Star Trek spoof scripts",
		"Jokes Galore",
		"Rotten Stuff !",
		"Stand-up urinal device (for women)",
		"How to pee standing up (for women)",
		"Female Urinal !",
		"Hog farming in North Carolina",
		"Hog Pooh Counter in North Carolina",
		"I gave my cat an enema!",
		"I shat my pants !",
		"The Darwin Awards",
		"Sperm donation for lesbian couples",
		"How stuff works (All kinds of stuff!)",
		"Turd Twister !! (don't ask !)"
	);

	randomno=Math.floor((Math.random()*urlarray.length));

	urlstr="<a target=new class=randomlink href=\""+urlarray[randomno]+"\">" ;
	urlstr+=textarray[randomno];
	urlstr+="</a>"
	document.write (urlstr);
}

function Popup(URL, NAME)
{
	var popWin = window.open("", NAME,"") ;
	popWin.location = URL ;
}


function PopupWeather(TownID)
   {
   var URL
	if (TownID=='READING') {
		URL = 'http://www.bbc.co.uk/weather/5day.shtml?id=3190&links'
	}
	if (TownID=='POOLE') {
		URL = 'http://www.bbc.co.uk/weather/5day.shtml?id=1187&links'
	}
	if (TownID=='WESTON') {
		URL = 'http://www.bbc.co.uk/weather/5day.shtml?id=1273&links'
	}
	if (TownID=='BATH') {
		URL = 'http://www.bbc.co.uk/weather/5day.shtml?id=1123&links'
	}
	if (TownID=='ASHCOMBE') {
		URL = 'http://www.bbc.co.uk/weather/5day.shtml?id=2040&links'
	}
	if (TownID=='GUILDFORD') {
		URL = 'http://www.bbc.co.uk/weather/5day.shtml?id=2040&links'
	}
	if (TownID=='GLOUCESTER') {
		URL = 'http://www.bbc.co.uk/weather/5day.shtml?id=2003&links'
	}
	if (TownID=='CAMBRIDGE') {
		URL = 'http://www.bbc.co.uk/weather/5day.shtml?id=1413&links'
	}
	if (TownID=='CROYDE') {
		URL = 'http://www.bbc.co.uk/weather/5day.shtml?id=1898&links'
	}


   popWin = window.open(URL , 'weather', "toolbar=0,scrollbars=1, location=0,menubar=0,resizable=1,width=600,height=425");
   }

function ShowCal(calID)
  {
    if (!document.getElementById) {
      return null;
    }
    if (document.getElementById(calID).style.display == 'block') {
        document.getElementById(calID).style.display = 'none';
    } else {
        document.getElementById(calID).style.display = 'block';
    }
  }

 window.onload = function()
  {
/*    hideall();*/
  }

  function gogirl(e) {
    if (document.getElementById(e).style.display == 'block') {
        document.getElementById(e).style.display = 'none';
    } else {
        document.getElementById(e).style.display = 'block';
    }
}

function hideall() {
        var Nodes = document.getElementsByTagName('ul')
        var max = Nodes.length
        for(var i = 0;i < max;i++) {
            var nodeObj = Nodes.item(i)
            nodeObj.style.display = 'block';
            if (nodeObj.className == 'menu1' ) {
                nodeObj.style.display = 'none';
            }
            if (nodeObj.className == 'menu2' ) {
                nodeObj.style.display = 'none';
            }
            if (nodeObj.className == 'menu3' ) {
                nodeObj.style.display = 'none';
            }
        }
}