var ipath = 1;
var Now = new Date();
var NowDay = Now.getDate();
var NowMonth = Now.getMonth();
var NowYear = Now.getYear();
if (NowYear < 2000) NowYear += 1900; //for Netscape

function init(){
	var x,y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
	}
	else // Explorer Mac;
		 //would also work in Explorer 6 Strict, Mozilla and Safari
	{
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}
	document.images['sizer'].height = y-201;
	document.getElementById('submenutable').height = y-201;
}

function checkForm(frm){ /* test if element will work here */
	if(!validEmail(frm)){
		return false;
	}
	switch(frm.selPreference.value){
		case "telephone" :
			if(!validTelephone(frm)){
				alert("Please supply a valid telephone number if you would like to be contacted by this method");
				return false;
			}
			break;
		case "email" :
			return true;
			break;
		default:
			return false;
			break;
	}
	return true;
}

function validEmail(frm){
	var regpat = /^(\w+)(\.\w+)?(@)((\w+)(-)?(\w+))+(\.)((\w{2,6})|(\w{2,6}(\.)(\w{2,6})))$/;
	var b = regpat.test(frm.txtEmail.value);
	if(!b){
		alert("please enter a valid email address");
	}
	return b;
}

function validTelephone(frm){
	var str = frm.txtTelephone.value;
	str = str.replace(/\s/g, '');
	var regpat = /^(\+)?[\d\(\)]{6,}$/;
	var b = regpat.test(str);
	return b;
}

function confirmDelete(){
	var agree=confirm("Are you sure you want to delete this item?");
	if (agree)
		return true ;
	else
		return false ;
}

function buildBreadcrumb() {
	var aFolders = new Array();
	var href = document.location.toString();
	aFolders = href.split('/');
	
	var strout = '';
	for(var i=2; i<aFolders.length; i++){
		if(i==2){aFolders[i]='home'}
		strout += '<a href="';
		if(i==aFolders.length-1){
			strout += buildDepth((aFolders.length-i)+1) + aFolders[i];
		}
		else {
			strout += buildDepth((aFolders.length-i)+1) + 'default.php';
		}
		strout += '">' + tidyLabel(aFolders[i]) + '</a>&nbsp;&gt;&nbsp;';
	}
	document.write(strout);
}

function buildDepth(iterations){
	var iterations=iterations-3;
    var depthStr='';
	
    for (i=0;i<iterations;i++)
    {
        depthStr=depthStr + '../';
    }
    return depthStr;
}

function tidyLabel(str){
	var strout = '';
	var a = str.split(".")
	strout = a[0];
	strout = strout.replace(/(_)/g, " ");
	strout = strout.replace(/(-)/g, " ");
	return strout;
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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 fetchRandomImage(){
	var aImages = new Array ("/images/topbar-01a.jpg", "/images/topbar-02.jpg", "/images/topbar-03.jpg", "/images/topbar-04.jpg", "/images/topbar-05.jpg");
	var randomnumber = Math.floor(Math.random() * aImages.length);
	document.write("<img alt='' src='/common/script/resize-image.php?image=" + aImages[randomnumber] + "&max_width=770'>");
}

function DaysInMonth(WhichMonth, WhichYear)
{
  var DaysInMonth = 31;
  if (WhichMonth == "April" || WhichMonth == "June" || WhichMonth == "September" || WhichMonth == "November") DaysInMonth = 30;
  if (WhichMonth == "February" && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;
  if (WhichMonth == "February" && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;
  return DaysInMonth;
}

function ChangeOptionDays(Which)
{
  DaysObject = eval("document.frmContact." + Which + "Day");
  MonthObject = eval("document.frmContact." + Which + "Month");
  YearObject = eval("document.frmContact." + Which + "Year");

  Month = MonthObject[MonthObject.selectedIndex].text;
  Year = YearObject[YearObject.selectedIndex].text;

  DaysForThisSelection = DaysInMonth(Month, Year);
  CurrentDaysInSelection = DaysObject.length;
  if (CurrentDaysInSelection > DaysForThisSelection)
  {
    for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
    {
      DaysObject.options[DaysObject.options.length - 1] = null
    }
  }
  if (DaysForThisSelection > CurrentDaysInSelection)
  {
    for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
    {
      NewOption = new Option(DaysObject.options.length + 1);
      DaysObject.add(NewOption);
    }
  }
    if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}

function SetToToday(Which)
{
  DaysObject = eval("document.frmContact." + Which + "Day");
  MonthObject = eval("document.frmContact." + Which + "Month");
  YearObject = eval("document.frmContact." + Which + "Year");

  YearObject[0].selected = true;
  MonthObject[NowMonth].selected = true;

  ChangeOptionDays(Which);

  DaysObject[NowDay-1].selected = true;
}

function WriteYearOptions(YearsAhead)
{
  line = "";
  for (i=0; i<YearsAhead; i++)
  {
    line += "<OPTION>";
    line += NowYear + i;
    line += "</OPTION>";
  }
  return line;
}

function swapImage(){
	switch(ipath){
		case 1 :
			document.map.src = "/images/skye.gif";
			document.map.setAttribute('title', '[Click for a map of Scotland]');
			document.map.setAttribute('alt', '[Click for a map of Scotland]');
			ipath = 2;
			return false;
		case 2 :
			document.map.src = "/images/scotland.gif";
			document.map.setAttribute('title', '[Click for a map of Skye]');
			document.map.setAttribute('alt', '[Click for a map of Skye]');
			ipath = 1;
			return false;
	}
}