/*  hmiscripts.js  ..*/

// +==============================================+
// |      +++   FUNCTIONS FOR HMI site   +++      |
// +==============================================+

/*  Global variables  */
var newWindow = null;
var ht0 = null;

	function fileopenedalert() {
		alert ("hmiscripts opened");
	}
//------------------------------------------------

function makeCaptions() {
	if (!document.getElementById) return false;
	var imgdiv = document.getElementById("imgdiv");
	var popimg = document.createElement("img");
	popimg.setAttribute("id","imgx");
	popimg.setAttribute("src",opener.imgsrc);
//	alert ("Opener Src is " + opener.imgsrc);
	popimg.setAttribute("alt",opener.desc);
	popimg.setAttribute("title","");
	var para = document.createElement("p");
	para.setAttribute("id","desc");
	para.style.top = opener.capht + "px";
	var txt = document.createTextNode(opener.desc);
	para.appendChild(txt);
	imgdiv.appendChild(popimg);
	imgdiv.appendChild(para);
	return null;
}
//------------------------------------------------


function closeWindow() {
	alert ("closeWindow called");
	if (newWindow != null) {
		newWindow.close();
		newWindow = null;
	}
	return;
}
//------------------------------------------------



function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// Prepares links
// Gets 'alt' from thumbnail as both caption and 'alt' for main image
// Gets title from direct links where there is no thumbnail.
function getLinks() {
	if (!document.getElementsByTagName) return false;
	var wd, ht0;
	var inner = document.getElementById("inner");
	var lnks = inner.getElementsByTagName("a");
	for (var i=0; i<lnks.length; i++) {
		lnks[i].onclick = function() {
			if (this.className == "ls" || this.className == "pt") {
//				alert (this.className);
				if (this.firstChild.nodeName.toLowerCase() == "img") {
					descr = this.firstChild.getAttribute("alt");
//					ht0 = this.firstChild.getAttribute("height")
				} else {
					descr = this.getAttribute("title");  // no valid 'alt' attribute for links
				}
				popUp(this.getAttribute("href"),this.className,descr, ht0);
				return false;
			}
		}
	}
	return null;
}

// function for ImageGallery
function popUp(imgname,clname,descr, ht0) {
//	alert ("function popUp called");
//	alert ("Height = " + ht0);
//	alert("Class Name = " + clname);
	if (newWindow != null) {
		newWindow.close();
		newWindow = null;
	}
//	alert ("Image is " + imgname);
//	var loc = new String(location);
	var loc = document.URL;
//	alert ("Location is " + loc);
//	alert ("Pathname is " + location.pathname);
	var lastChar = loc.slice(loc.length -1);
//	alert ("Last char is " + lastChar);
	var dir = unescape(loc.substring(0,(loc.lastIndexOf("/")) +1));
//	alert ("dir is " + dir);
	var popTgt = "../inc/popUp.htm";  //  path to popUp for folders
//	if (loc.indexOf("index.html") != -1 || lastChar == "/") {
//		popTgt = "../inc/popUp.htm";  //..path to popUp from root
//		if (imgname.indexOf("http://") == -1) {
//			imgname = "../" + imgname;  //  path to image from popUp (only applied if path is relative)
//			}
//	}
//	alert ("Image is now " + imgname);
//	alert ("popTarget is " + popTgt);
	imgsrc = imgname;
	desc = descr;
	var windowName = "gallery";
	if (clname == "ls") {
		wd = 640;
		ht = 480;
	}
	if (clname == "pt") {
		wd = 480;
		ht = 640;
	}
	if (ht0 > 0) ht = ht0;
//	alert("width = " + wd + ", height = " +ht);
	capht = ht - 32;
	newht = ht;
//	alert ("newht= " + newht +" \rcapht= " + capht);
	var widthHeight = "width="+wd + ",height="+ht;
//	alert ("You clicked on " + imgsrc);  // confirms image to be loaded into window
	newWindow = window.open(popTgt, windowName, widthHeight);
	newWindow.focus();
}




// Focus on subject when opening e-mail form
	function focussubj() {
		if (document.getElementById("form1")) {
//			alert ("Trying to focus");
			document.form1.subject.focus();
//			alert ("Focused");
		}
	}

// Error report
	function goBack(jp) {
		alert ("Sorry, there has been a code error. \nClick OK to return to the previous page");
		history.go(-jp);
	}

// Address not found
	function notFound() {
		alert ("Sorry, e-mail address not found.\nYou could try visiting their web site \nClick OK to return to the previous page");
		history.go(-1);
	}

// Check e-mail form content
	function checkForm() {
//	Check subject
		var subj =document.getElementById("subject");
		if (subj.value == "") {
			alert ("Please enter a subject");
			subj.focus();
			return false;
		}
//	Check name entry
		var fullName = document.getElementById("fullName");
		if (fullName.value == "") {
			alert ("Please enter your name");
			fullName.focus();
			return false;
		}
//	Check first email
		var email = document.getElementById("email");
		var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		if (!re.test(email.value)) {
			alert ("Your e-mail address appears to be invalid. \nPlease check and re-enter");
			email.focus();
			return false;
		}
//	Check emails match
		var email2 = document.getElementById("email2");
//		alert (email.value+" "+email2.value);
		if (email.value != email2.value) {
			alert ("Your e-mail address entries differ. \nPlease check and re-enter");
			email.focus();
			return false;
		}
//	Check message content
		var msg = document.getElementById("msg");
		if (msg.value == "") {
			alert ("You have not included any message.");
			msg.focus();
			return false;
		}
		return true;
	}


// function for pop-up (firstmap)
function makeNewWindow(URL,windowName,chrome) {
//	alert ("MakeNewWindow called");
	if (!newWindow || newWindow.closed) {
		newWindow = window.open(URL, windowName, chrome)
	} else {
//		alert ("newWindow already open");
		newWindow.focus()
	}
}


// function for creating bookmarks
function CreateBookmarkLink() {
	title = "HolidayMull: " + title;
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	}	else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title);
	}	else {
		return false;
	}
}

function bookmarkthis(title,url) {
  if (window.sidebar) { // firefox
     window.sidebar.addPanel(title, url, "");
  } else if (document.all) { // IE
     window.external.AddFavorite(url, title);
  } else if (window.opera && window.print) { // opera
     var elem = document.createElement('a');
     elem.setAttribute('href',url);
     elem.setAttribute('title',title);
     elem.setAttribute('rel','sidebar');
     elem.click();
  }
}


