
// This is called when the webpage is loaded.
function loadPage(){
  
// We're now displaying the large picture right away.  19Nov09 gp 
//   EnlargedPicVisibility('hide')

   // Instead, we display the 'first' image within the specified element.
   showFirstPic()

   // Update the web page's text.   
   updateWebpageText()
   
}



// Display the first picture listed on a the web page.
function showFirstPic(){

   elemnt = document.getElementById("tblGallery")
   
   if ( elemnt == null ) return

   var imagesArr = new Array();
   
   // Retrieve all the image tags within this specified element
   imagesArr = elemnt.getElementsByTagName("img");
   
   if (imagesArr.length == 0) return
   
   zoomPic(imagesArr[0])
}



function updateWebpageText(){

   var elemnt = document.getElementById('webpageText')
   
   // If that element exists.
   if ( elemnt != null ){
   
      var webPageName = getFileName()
   
      webPageName = webPageName.split('.')[0]
   
      if ( webPageName != 'index') elemnt.innerHTML = webPageName.toUpperCase()
   }
}



function EnlargedPicVisibility(hide){

   var elemnt = document.getElementById('largePic')

   // If the element is not found
   if ( elemnt == null ) return
   
   if ( hide != null && hide == 'hide') {
   
      elemnt.style.visibility = "hidden"

   } else if ( elemnt.style.visibility == "hidden" ){  

      elemnt.style.visibility = "visible" 
   }
}



// Returns the file name from the specified file path.
function getFileName(){

   var path = document.URL
   
   var slash = '\\'

   if ( path.lastIndexOf(slash) < 0 ) slash = "/"
   
   var parts = path.split(slash)
   
   var fileName = parts[parts.length-1]

   return fileName
}



// The form validation routine
function validateForm(){

   var email = document.getElementById('emailAddress')

   if ( email != null && !echeck(email.value) ) return false
   
   return true
}



function zoomPic(imgObj){

   if ( imgObj == null) return
   
   // Restore the border of the last selected image by processing all of the image objects.
   thinBorderImages(imgObj)
   
   // Highlight the image selected by modifying its border.
   imgObj.style.borderWidth = "2";

   imgObj.style.borderColor = "#FFFFFF";

   var elemnt = document.getElementById('largePic')
   
   if ( elemnt == null ) return

   var fileSrc = imgObj.src

   fileSrc = fileSrc.replace("\Thumbs", "")

   fileSrc = fileSrc.replace("/Thumbs", "")

   fileSrc = fileSrc.replace("%20Thumb", "")

   fileSrc = fileSrc.replace("Thumb", "")

   fileSrc = fileSrc.replace("_Thumb", "")

   elemnt.src = fileSrc

   EnlargedPicVisibility()
  
   showImgCaption(imgObj)
}


function showImgCaption(imgObj){

   if ( imgObj == null) return

   if ( imgObj.alt == "" ) return
   
   var elemnt = document.getElementById('description')
  
   if ( elemnt == null ) return
   
   elemnt.innerHTML = imgObj.alt
}



// Go through all the image object: Thin and change the colour of their borders
function thinBorderImages(imgObj){

   if ( imgObj == null) return
   
   var elemnt = getElementsByClass('Gallery', imgObj.parentNode.parentNode.parentNode, 'img');
         
   for(var cntr = 0; cntr < elemnt.length; cntr++){

     elemnt[cntr].style.borderWidth = "1";
	 
	 elemnt[cntr].style.borderColor = "#000000";
   }

}



/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		var validEmail = false
		
		if (str.indexOf(at)==-1){
		}
    	else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		}
		else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		}
        else if (str.indexOf(at,(lat+1))!=-1){
		}
        else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		}
        else if (str.indexOf(dot,(lat+2))==-1){
		}
		else if (str.indexOf(" ")!=-1){
		}
		else {
		
		   validEmail = true
		}

		if ( !validEmail ) alert("Invalid E-mail Address")

        return validEmail					
}



     //           Supply a class name as a string. 
     //(optional) Supply a node. 
     //           This can be obtained by getElementById. (it will be document if don’t supply a node) 
     //           If you know your parent and you don’t want to loop through the entire D.O.M. 
     //(optional) Limit your results by adding a tagName. 
     //           Very useful when you’re toggling checkboxes and etcetera. You could just supply “input“. 
     //           You can use the “*” asterisk as a catch-all (meaning ‘any element). 
     function getElementsByClass(searchClass, node, tag) {
        
        var classElements = new Array();
        
	    if ( node == null ) node = document;
	    
	    if ( tag == null ) tag = '*';
	    
	    var els = node.getElementsByTagName(tag);
	    
	    var elsLen = els.length;
	    
	    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	     
	    for (i = 0, j = 0; i < elsLen; i++) {
	    
	       if ( pattern.test(els[i].className) ) {
		
			   classElements[j] = els[i];
			
			   j++;
		   }
	   }
	   return classElements;
    }
