// JavaScript Document


// input validation functions 

function submitEmailForm(theForm) {
	/*
	if (!validRequired(theForm.realname,"Your Name")) {
				return false;
			}
			
			if (!validEmail(theForm.email,"A valid email address")) {
				return false;
			}
			*/

		theForm.submit();
		return true
	
}

function isEmailAddr(email)
	{
	  var result = false;
	  var theStr = new String(email);
	  var index = theStr.indexOf("@");
	  if (index > 0)
	  {
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	  }
	  return result;
	}
	
	function isDate(newDate) {
		
		var arr = newDate.split("/");
		
		if (arr.length != 3) {
			return false;
		}
		
		var d = parseInt(arr[0],10);
		var m = parseInt(arr[1],10);
		var y = parseInt(arr[2],10);	
		
		if (isNaN(d) || isNaN(m) || isNaN(y) ) {
			return false;									 
		}	
		
		if (d > 31 || m > 12 || y <1900 || y > 3000) {
			return false;
		}
		
		return true;
		
		
	}

	function validRequired(formField,fieldLabel)
	{
		var result = true;
		
		if (formField.value == "")
		{
			alert('Please enter a value for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
		
		return result;
	}
	
	
	function validEmail(formField,fieldLabel,required)
	{
		var result = true;
		
		if (required && !validRequired(formField,fieldLabel))
			result = false;
	
		if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
		{
			alert("Please enter a complete email address in the form: yourname@yourdomain.com");
			formField.focus();
			result = false;
		}
	   
	  return result;
	
	}
	
	function validDate(formField,fieldLabel,required)  
	{
		
		
		if (required && !validRequired(formField,fieldLabel))
			return false;
		
		if (!isDate(formField.value) )
		{
			alert("Please enter adate in the form: dd/mm/yyyy");
			formField.focus();
			return false;
			}
	   
	  return true;
		
	}
	
	function getConfirmMsg (action) {
		return "Are you sure you want to "+action+" this record?";
		
	}
	
	function getDelMsg () {
		 return getConfirmMsg("delete") +  " Associated records may also be deleted from other tables.";
	}
	
	
	
// layout functions
	

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}


function setupNavButtons () {
	// iterate through the navigation buttons and replace the plain text
	// with an image.  note that the anchor tag'd id attribute
	// is used to determine the file name of the replacement image
	
	if (	!document.getElementById ||
				!document.getElementsByTagName
			) 
	{
					return;
	}
	
	var currentURL = location.href;
	var nav = document.getElementById("nav") 
	var navItems = nav.getElementsByTagName("a")
	var len = navItems.length;
	var theSection = null;

	for (var i=0; i<len; i++) {
		var theItem = navItems[i];
		var theLinkTarget = theItem.attributes.href.value;
		
		// determine the file name of the image we want to replace
		var imgName = "gfx/" + theItem.id
		if (currentURL.indexOf(theLinkTarget) != -1) {
			// if the current nav item is for the section we are currently in
			// use the _over image to highlight it visually
			imgName += "_over"
		}	
		imgName +=".gif"

		var imgTag = document.createElement('img');
		imgTag.setAttribute('src',imgName);
		theItem.replaceChild(imgTag,theItem.firstChild)
		
	}
	
}

function setup_page_h2 () {
		
	if (	!document.getElementById ||
				!document.getElementsByTagName
			) 
	{
					return;
	}
	
	var h2List = document.getElementsByTagName("h2")
	var len = h2List.length
	for (var i=0; i<len; i++) {
		var theItem = h2List[i]
		if (theItem.className == "replace") {
			var theValue = theItem.firstChild.nodeValue.toLowerCase();
			var file = "gfx/h2_" + theValue.split(" ").join("_") + ".gif";	
			
			var imgTag = document.createElement('img');
			imgTag.setAttribute('src',file);
			theItem.replaceChild(imgTag,theItem.firstChild)
			
		}
	}
		
}

function preloadImages (){
	if (document.images)
    {
      preload_image_object = new Image();
      // set image url
      image_url = new Array();
      image_url[0] = "gfx/nav_artist.gif";
			image_url[0] = "gfx/nav_artist_over.gif";
			image_url[0] = "gfx/nav_earth.gif";
			image_url[0] = "gfx/nav_earth_over.gif";
			image_url[0] = "gfx/nav_home.gif";
			image_url[0] = "gfx/nav_home_over.gif";
			image_url[0] = "gfx/nav_sea.gif";
			image_url[0] = "gfx/nav_sea_over.gif";

			//alert("preload: " + image_url);

       var i = 0;
       for(i=0; i<=image_url.length; i++) 
         preload_image_object.src = image_url[i];
    }
}

addLoadEvent(preloadImages)
addLoadEvent(setupNavButtons)
addLoadEvent(setup_page_h2)