function checkForm(op, formObj)
{
	if (!formObj)
	{ var sForm = document.forms['enquiry']; }
	else
	{ var sForm = formObj; }
	
	if( op == 'search') {
		sForm.action = '/searchresults.asp';
	} else {
		sForm.method = "get";
		sForm.action = 'http://reservations.ihotelier.com/crs/step1.cfm';
	}
	sForm.submit();
}

function showFavourites() {

	var sfav, afav, x, sHTML;
	
	sHTML = "";
	
	if( getCookie("fav")) {
		sfav = getCookie("fav");
		afav = sfav.split("|");		
				
		for( x = 1; x < afav.length - 1; x++) {
			sHTML += "<li><a href=\"\/default2.asp\?" + afav[x] + "\">" + afav[x+1] + "</a></li>";
			x++;
		}
		sHTML += "<li>&nbsp;</li><li><a href=\"manageFav.asp\"><strong>Manage CSE Favourites</strong></a></li>";
		
	}	else {
		sHTML += "<li>To retrieve previously stored pages of particular interest to you, please login here with your email address and a choice of password."
								+ "</li>"
	}
	if( document.getElementById('favlinks')) {
		document.getElementById('favlinks').innerHTML = sHTML;
	}
}	

function addToFavourites( sLink) {
	var sList;

	if( getCookie("fav")) {
		sList = getCookie("fav");
	} else {
		sList = "|";
	}
	if ( sList.indexOf("|" + sLink + "|") == -1) {
		sList += sLink + "|";
		
		setCookie( "fav", sList);

		showFavourites();
	} else {
		alert("Sorry this page already exists in your CSE favourites.");
	}
}

function removeFromFavourites( sLink) {
	var sList;
	
	if( getCookie("fav")) {
		sList = getCookie("fav");
		
		sList = sList.replace( "|" + sLink, ""); 
		
		setCookie( "fav", sList);
	}
	document.location.href = "/manageFav.asp";
}

function clearFavourites() {
	var msg;

	msg = "You are about to clear all pages in your CSE favourites.\n\n"
	msg += "This action is irreversible.\nAre you sure you want to continue?"
	
	if( confirm( msg) ) {
		if( getCookie("fav")) {
			setCookie( "fav", "");
			document.location.href = "/manageFav.asp";
		}
	}
}

function checkPassword() {
	var sForm  = document.forms['emailform'];
	var isOK = 0;
	var pass1 = sForm.password.value;
	var pass2 = sForm.password2.value;
		
	if( pass1 == pass2) {
		isOK = 1;
	} else {
		alert("Please check that the passwords you have entered in the fields below are identical for confirmation.");
	}
	
	if( isOK == 1) {
		checkrequired('emailform');
	}
}
	
// Original JavaScript code by Duncan Crombie: dcrombie at chirp.com.au
// Please acknowledge use of this code by including this header.
function getCookie(name) { // use: getCookie("name");
  var re = new RegExp(name + "=([^;]+)");
  var value = re.exec(document.cookie);
  return (value != null) ? unescape(value[1]) : null;
}

function setCookie(name, value)
{
	// use: setCookie("name", value);
  document.cookie=name + "=" + escape(value) + "; path=/";
}

function showLogin()
{   
  document.getElementById('logindiv').style.display="block"; 
  var sForm = document.forms["login"];
	sForm.username.focus();
}

function isDate(dateStr) {

  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
  var matchArray = dateStr.match(datePat); // is the format ok?
  var dateNow  	= new Date();
  var curDay  	= dateNow.getDate();
  var curMonth 	= dateNow.getMonth();
  var curYear  	= dateNow.getFullYear();
  
  if (matchArray == null) {
    alert("Please enter date as dd/mm/yyyy");
    return false;
  }

  month = matchArray[3]; // p@rse date into variables
  day = matchArray[1];
  year = matchArray[5];
  
  var dob = new Date( year, month, day)
  
  if (month < 1 || month > 12) { // check month range
    alert("Month must be between 1 and 12.");
    return false;
  }

  if (day < 1 || day > 31) {
    alert("Day must be between 1 and 31.");
    return false;
  }

  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
    alert("Month "+month+" doesn`t have 31 days!")
    return false;
  }

  if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day==29 && !isleap)) {
      alert("February " + year + " doesn`t have " + day + " days!");
      return false;
    }
  }
  if( day >= curDay && month >= curMonth && year >= curYear) {
    alert("Specified date is not vaild as DOB.");
    return false;
  }
  return true; // date is valid
}
function checkDOB() {
  var sForm = document.forms['theForm'];
  var dob   = sForm.elements['*dob'].value;
  
  if( isDate(dob)) {
		if(sForm.othermem1.selectedIndex==0){
      sForm.othermem1.focus();
      alert("Please answer 'Are you a member of any other clubs?'");
		}
    else if(sForm.othermem1.selectedIndex == 1 && sForm.othermem.value == "") {
      sForm.othermem.focus();
      alert("Please specify the club name");
    }
		else if(sForm.favdrink1.selectedIndex==0){
      sForm.favdrink1.focus();
      alert("Please answer 'Do you have a preferred drink?'");
		}
		else if ( sForm.favdrink1.selectedIndex == 1 && sForm.favdrink.value == "") {
      sForm.favdrink.focus();
      alert("Please specify your preferred drink");    
    } 
		else if(sForm.nominated1.selectedIndex==0){
      sForm.nominated1.focus();
      alert("Please answer 'Nominated by a committee member?'");
		}
		else if ( sForm.seconded1.selectedIndex == 1 && sForm.seconded.value == "") {
      sForm.seconded.focus();
      alert("Please specify Met Bar member");
    } else {    
      checkrequired('theForm');
    }
  } else {
    sForm.elements['*dob'].focus();
  }    
}
function showBigPic(filename)
{
	var winID = window.open("pic_viewer.asp?file=" + filename, 'bigpic', 'width=705,height=300,scrollbars=no,resizable=yes,left=80,top=80');
}