﻿// JScript File
// Version 1.1
// Written by Robie Boisvert

// ******************************************************************************************
// Configuration section
// ******************************************************************************************

// <summary>
// This array is used to store the images that requires preloading
// </summary>
// <param name="UniqueID">This is the ClientID of the control passed by parameter to the HyperlinkOver() HyperlinkOut() methods</param>
// <param name="DefaultImageName">Name of the default image state (including the file extension)</param>
// <param name="HoverImageName">Name of the hover image (including the file extension).
//                              If not defined, the system will automatically load the following image format: [DefaultImageName]_hover[extension]
// </param>
// <param name="Width">Image's width</param>
// <param name="Height">Image's height</param>

var _imagesInfoArray = new Array(
    new Array("ctl00_hlHome", "/home.jpg", "", "227", "50"),
    new Array("ctl00_hlSearch", "/search.jpg", "", "227", "50"),
    new Array("ctl00_hlAdvice", "/advice.jpg", "", "227", "50"),
    new Array("ctl00_hlAnnounce", "/announce.jpg", "", "227", "50")
);

// ******************************************************************************************
// Global data
// ******************************************************************************************

//Const Array
var IMAGE_UNIQUE_ID = 0;
var IMAGE_DEFAULT_NAME = 1;
var IMAGE_HOVER_NAME = 2;
var IMAGE_WIDTH = 3;
var IMAGE_HEIGHT = 4;

//Const State
var IMAGE_DEFAULT_STATE = 0;
var IMAGE_HOVER_STATE = 1;

//Images Array Holder
var _imageArrayObj = new Array();

// ******************************************************************************************
// Function : PreLoadImage()
// Description : Preload images on page load
// ******************************************************************************************
function PreLoadImage(path) {
      
   try
   {
        //Make sure the browser support preloading
        if (document.images) {
        
            //Initialize and apply business rules
            if (InitializeImageArray() == true) {
    
                //Loop into the array of images
                for (var i = 0; i < _imagesInfoArray.length; i++) {
    
                    //Build the normal image path (src)
                    var defaultPath = path + _imagesInfoArray[i][IMAGE_DEFAULT_NAME];

                    //Build the hover image path (src)
                    var hoverPath = path + _imagesInfoArray[i][IMAGE_HOVER_NAME];
                    
                    //Create another array that will holds the normal and hover images informations
                    var imgInfo = new Array();

                    //Create a new array that will contains the default and hover image information
                    imgInfo[IMAGE_DEFAULT_STATE] = new Image(_imagesInfoArray[i][IMAGE_WIDTH], _imagesInfoArray[i][IMAGE_HEIGHT]);
                    imgInfo[IMAGE_DEFAULT_STATE].src = defaultPath;
                    imgInfo[IMAGE_HOVER_STATE] = new Image(_imagesInfoArray[i][IMAGE_WIDTH], _imagesInfoArray[i][IMAGE_HEIGHT]);
                    imgInfo[IMAGE_HOVER_STATE].src = hoverPath;

                    //Add info to the global array
                    _imageArrayObj[i] = imgInfo;
                
                }
                
            }
            
        }
        
	}
	catch (e) { }	
			
}

// ******************************************************************************************
// Function : HyperlinkOver()
// Description : This function will change the image when the mouse is over this object.
// ******************************************************************************************
function HyperlinkOver(obj) {

   	try {
   	
   	    var element = null;

   	    if (document.all) {
   	        element = document.all[obj.id].firstChild;
   	    }
   	    else {
   	        element = document.getElementById(obj.id).firstChild;
   	    }

   	    if (element != null) {

            //Get current image src
   	        var currentSrc = element.src;

   	        if ((currentSrc.indexOf("_hover.jpg") == -1) && (currentSrc.indexOf("_selected.jpg") == -1)) {

   	            //Get the UniqueID index
   	            var index = GetUniqueIdIndex(obj.id);

   	            if (index != -1) {
   	                //Replace the current image
   	                element.src = _imageArrayObj[index][IMAGE_HOVER_STATE].src;
   	            }

   	        }

   	    }

    }
    catch (e) {}								
}

// ******************************************************************************************
// Function : HyperlinkOut()
// Description : This function will change the image when the mouse is over this object.
// ******************************************************************************************
function HyperlinkOut(obj) {
   
   try {

       var element = null;

       if (document.all) {
           element = document.all[obj.id].firstChild;
       }
       else {
           element = document.getElementById(obj.id).firstChild;
       }

       if (element != null) {

           //Get current image src
           var currentSrc = element.src;

           if (currentSrc.indexOf("_hover.jpg") > -1) {

               //Get the UniqueID index
               var index = GetUniqueIdIndex(obj.id);

               if (index != -1) {
                   //Replace the current image
                   element.src = _imageArrayObj[index][IMAGE_DEFAULT_STATE].src;
               }
                
           }

       }
   
	}
	catch (e) {}
}

// ******************************************************************************************
// Function : GetImageInfoIndex()
// Description : This function will returns the index of the located UniqueID
// ******************************************************************************************
function GetUniqueIdIndex(UniqueID) {

    var index = -1;

    try {
    
        //Loop into the array of images
        for (var i = 0; i < _imagesInfoArray.length; i++) {

            //Search for the UniqueID
            if (_imagesInfoArray[i][IMAGE_UNIQUE_ID] == UniqueID) {
                index = i;
                break;            
            }            
            
        }
    
    }
    catch (e) { }

    return index; 

}

// ******************************************************************************************
// Function : InitializeImageArray()
// Description : This function will initialize the image array and apply business rules
// Comments: This will raise an alert if one of the parameters are not properly entered
// ******************************************************************************************
function InitializeImageArray() {
    
    var output = true;

    try {

        //Loop into the array of images
        for (var i = 0; i < _imagesInfoArray.length; i++) {

            //Check for the Unique ID
            if (_imagesInfoArray[i][IMAGE_UNIQUE_ID].length == 0) {
                alert('Warning: Missing Unique Id');
                output = false;
                break;
            }

            //Check the default image name
            if (_imagesInfoArray[i][IMAGE_DEFAULT_NAME].length == 0) {
                alert('Warning: Missing Default Image Name');
                output = false;
                break;
            }

            //Check the hover image name
            if (_imagesInfoArray[i][IMAGE_HOVER_NAME].length == 0) {

                //Get the file extension
                var extension = GetFileExtension(_imagesInfoArray[i][IMAGE_DEFAULT_NAME]);

                //Business rules: If not specified, use the following format: [DefaultImageName]_hover[extension]
                _imagesInfoArray[i][IMAGE_HOVER_NAME] = _imagesInfoArray[i][IMAGE_DEFAULT_NAME].replace(extension, ("_hover" + extension));

            }

        }

    }
    catch (e) { }

    return output;

}

// ******************************************************************************************
// Function : GetFileExtension()
// Description : This function will returns a file extension
// ******************************************************************************************
function GetFileExtension(fileName)
{
    try {
    
        //Get the index of the last dot (.)
        var index = fileName.lastIndexOf(".");
        
        if (index != -1) {
            //Return the file extension
            return fileName.substring(index);     
        }
        else {
            //Return an empty string
            return "";
        }
         
    }
    catch (e) {}	

}

// ******************************************************************************************
// Function : DisplayConfirmation()
// Description : Display a confirmation message
// ******************************************************************************************
function DisplayConfirmation(Message)
{
    return confirm(Message);
}

// ******************************************************************************************
// Function : DisplayWarning()
// Description : Display a warning message
// ******************************************************************************************
function DisplayWarning(Message)
{
    alert(Message);
}

// ******************************************************************************************
// Function : Print()
// Description : Print current page
// ******************************************************************************************
function PrintPage() {
    window.print();
}

// ******************************************************************************************
// Function : BookMark()
// Description : Print current page
// ******************************************************************************************
function BookMark()
{ 
    var bookmarkUrl = window.document.location;
    var bookmarkTitle = window.document.title;

    if (window.sidebar) 
    {
        // opens firefox bookmark dialogue 
        window.sidebar.addPanel(unescape(bookmarkTitle), unescape(bookmarkUrl), ""); 
    }
    else
    { 
        if (window.opera && window.print) // opens opera bookmark dialogue 
        { 
            var lvll = document.createElement('a'); 
            lvll.setAttribute('href',bookmarkUrl); 
            lvll.setAttribute('title',bookmarkTitle); 
            lvll.setAttribute('rel','nosidebar'); 
            lvll.click();
        }
        else 
        {
            if (document.all) 
            {
                // opens ie bookmark dialogue 
                window.external.AddFavorite(bookmarkUrl, bookmarkTitle);
            }
        }
    }
}

// ******************************************************************************************
// Function : ClearTextbox()
// Description : Clear a textbox object
// ******************************************************************************************
function ClearTextboxIfDefault(obj, defaultvalue) {
    if (obj.value == defaultvalue)
        obj.value = "";
}

// ******************************************************************************************
// Function : SetTextboxValue()
// Description : Set the textbox value
// ******************************************************************************************
function SetTextboxValueIfEmpty(obj, msg) {
    if (obj.value.length <= 0)
        obj.value = msg;
}