/* ICSM.js © Copyright 2007 ICSM.org.
Written by Colin Ramsden.
LotechSolutions.com
Modification history:
	01Feb07	Created Contact Section
	11Feb07 Added copy section
	13Feb07 Added menu section
	15Feb07	Added menu update section
	16Feb07	Added common section and restructured
	24Feb07 Moved common section to Globals.js
*/


// ***START COPY SECTION***

/* 
This is the script section called by the onload event for the ICSM site home page.
This script requires the 'fnInitialiseFrameWork' function be listed in the display page onload 
event to trigger the source file loading and subsequent copy process:
 <body onload="fnInitialiseFrameWork();">

And of course, a link to this script file is also required on the display page:
 <script type="text/javascript" language="javascript" src="ICSM.js"></script>

This script also expects that the both the 'FrameWork' iframe object and 
the 'ContentDiv' div preexists in the display topic, and 
that each file contains the 'BodyDiv' div for copying.

For use with IE, this function associates the 'onreadystatechange' event of the iframe 
object 'FrameWork' with the 'fnTestStatus' handler. This event is triggered and called 
whenever the onready state of the 'FrameWork' is fired as a file is loaded into the iframe in IE.  
There are actually several states which can fire this event, however, only the final 'completed' 
state is used to further trigger the 'fnCopyContents' function. 

For use with Mozilla based browsers, this function associates the 'onload' event of the iframe
object 'FrameWork' directly with the 'fnCopyContents' function.
*/ 


function fnInitialiseFrameWork()
{
	//called from page onload event
	//alert("Initialising FrameWork");
	
	//determine browser type
	fnDetermineBrowserType();
	
	//initialise and associate object status change notification
	fnAssociateFrameObjStatusChange(strSourceDivID);

	//load next document
	fnLoadNextDoc();
}


function fnAfterInitialisation()
{
	//update custom browser based styles
	fnUpdateCustomBrowserBasedStyles();
	
	//reset default body div destination for menu doc swapping
	strCurDestDivID = strDefaultDestDivID;
		 
	//update document destination
	fnSetDestination(strCurDestDivID);
		
	//validate startDocument name
	fnValidateStartDocName();
	
	//set the default menu item
	fnSetMenuSelection(curMenu);
		
	//load default content file
	fnLoadFile(curDocument);
} 


function fnSetSource(WhichOne)
{
	//set the source iframe dependant upon browser DOM 
	if (isIE == true)
	{
  	//IE cannot access the iframe page directly. It must go through the frames collection
  	objSource = document.frames(WhichOne).BodyDiv; //returns '[object]'; works with IE
  	//alert("IE Source Document = " + objSource);
	}
	else if(isMozilla == true)
	{
  	//Mozilla
  	objSource = document.getElementById(WhichOne).contentDocument.getElementById("BodyDiv"); //returns '[object HTMLDivElement]' 
  	//alert("Mozilla Source Document = " + objSource);
	} 	 
}


function fnSetDestination(objDestID)
{
	//set the destination variable
	objDestination = document.getElementById(objDestID); //works for both IE and Mozilla
	//alert("Destination = " + objDestination); //IE returns '[object]'; Mozilla returns '[object HTMLDivElement]'
}


function fnAssociateFrameObjStatusChange(WhichOne)
{
	//alert("Associating iFrame status change for '" + WhichOne + "'.");
	if(isIE == true)
	{
  	//document.getElementById(WhichOne).onload=fnCopyContents; //does not work with IE
  	document.getElementById(WhichOne).onreadystatechange = fnTestStatus; //works with IE
	}
	else
	{
  	//Mozilla and others
  	document.getElementById(WhichOne).onload = fnCheckState; // works with Mozilla
	}
}


function fnTestStatus()
{
	//called whenever the iframe status is ready
	//this fires for any change in status
	//alert("FrameWork readyState = " + document.getElementById(strSourceDivID).readyState);
	//test whether iframe status is complete (file is fully loaded)
	if(document.getElementById(strSourceDivID).readyState == "complete")
	{
  	//copy source sections from currently loaded source file to destinations
  	fnCheckState();
	}
}


function fnCheckState()
{
	//reset the source object document for content transfer
	fnSetSource(strSourceDivID);

	//copy contents
	fnCopyContents();
		
	//alert("Checking initialisation state");
	if (isInitialised == true)
	{
		//assign sourcefile name to document tracker
		fnUpdateDocTracker();
		
		//update menus
		fnUpdateMenus();
	}
	else
	{
		//load next document
		fnLoadNextDoc();
	}
		
}


function fnLoadNextDoc()
{
	//alert("Loading next document: " + curTopicIdx + " of " + (aryDocs['aryDocSourceFiles'].length - 1));
	
	if (curTopicIdx <= aryDocs['aryDocSourceFiles'].length - 1)
	{
		if (isIE == true)
		{
			//check that destination exists
			if (typeof(window[aryDocs['aryDocDestinations'][curTopicIdx]]) != "undefined")
			{
				//set document destination
				//fnSetDestination(aryDocs['aryDocDestinations'][curTopicIdx]);
			}
			else
			{
				//destination not found
				var msg = "WARNING: LOAD DOCUMENT ERROR";
				msg += "\nPlease inform the site webmaster.\n\nWebmaster:";
				msg += "\nDocument destination '";
				msg += aryDocs['aryDocDestinations'][curTopicIdx];
				msg += "' not found.";
				msg += "\nPlease update \"destinations\" array.";
				alert(msg);
			}
	  }
		//set document destination
		fnSetDestination(aryDocs['aryDocDestinations'][curTopicIdx]);
		
		//load source document
		fnLoadFile(aryDocs['aryDocSourceFiles'][curTopicIdx] + ".htm");
		
		//increment current topic index value
		curTopicIdx++;
	}
	else
	{
		//set initialised tracking flag
		isInitialised = true;
		
		//process post inititialisation actions
		fnAfterInitialisation();
	}
}		


function fnCopyContents()
{
	//copies the file content from the hidden iframe to the document contents div
	//alert("Copying contents"); 
	objDestination.innerHTML = objSource.innerHTML; 
} 


function fnUpdateDocTracker()
{
	//assign sourcefile name to document tracker
	if (isIE == true)
	{
	 	//returns filename extracted from full path & filename!
	 	curDocument = fnExtractFileNameFromPath(document.frames(strSourceDivID).document.location.href); 
  }
  else if (isMozilla == true)
  {
	 	//returns filename extracted from full path & filename!
	 	curDocument = fnExtractFileNameFromPath(document.getElementById(strSourceDivID).contentDocument.location.href);
 	} 
	else
	{
  	//Not IE nor Mozilla
  	alert("Unknown browser detected!\n\nSorry. You're page may not display properly.\nPlease contact the site webmaster.");
	}
	//alert("Updating document tracker to '" + curDocument + "'.");
}


function fnLoadFile(FileName)
{
	//alert("Loading file " + FileName);
	//load source file
	document.getElementById(strSourceDivID).src = FileName; 
}


function fnUpdateMenus()
{
 //make current menu selection match current topic being displayed
 //iterate through all array filenames until match found
 for(var i=1; i<aryTopicFiles['aryFiles'].length; i++)
 {
	
 	if ((aryTopicFiles['aryFiles'][i] + ".htm") == curMenu)
 	{
		//var msg = "Current Document is " + curDocument;
		//msg += "\nCurrent Document found in TopicFiles array at index " + i; 
		//msg += "\nCurrent topic index = " + curTopicIdx; 
		//msg += "\nCurrent Menu is " + curMenu;
		//msg += "\nChanging to " + (aryTopicFiles['aryMenus'][i]);
		//alert(msg);
		
		//store current topic array index
		curTopicIdx = i;

		//update title image
		fnSetTopicTitle();		
		
		//update menus
		fnChangeMenu(aryTopicFiles['aryMenus'][i]);
		
		//exit iteration
		break;
	}
 } 
}

function fnExtractFileNameFromPath(FullPath)
{
 //trim string to extract filename
 //var strFileName = FullPath.substring(FullPath.lastIndexOf("/")+1);
 //alert("File name = " + strFileName);
 //return strFileName;
 return FullPath.substring(FullPath.lastIndexOf("/")+1);
}

function fnUpdateCustomBrowserBasedStyles()
{
 if (isMozilla == true)
  {
		document.getElementById('SupportForList').className = "ListInTableInMozilla";		
 	} 
}
  
// ***END COPY SECTION***


// ***START MENU SECTION***

function fnInitiateMenu()
{
	alert("Initiating Menu to " + curMenu);
	//isMozilla ? alert("Initiating Menu in Mozilla"): fnDoNothng() ;
	//set the default menu item
	fnSetMenuSelection(curMenu);
}
	

function fnChangeMenu(WhichOne)
{
	//alert ("Changing Menu from " + curMenu + " to " + WhichOne);
	if(curMenu != WhichOne)
	{
		//fnUpdateMenus();
		//reset previous menu selection
		//fnResetPreviousMenuSelection();
		
		//set current menu selection
		fnSetMenuSelection(WhichOne);
	}
}


function fnMenuMouseOver(WhichOne)
{
	if(curMenu != WhichOne)
	{
		//set menu highlight
		ThisOne = "obj" + WhichOne; 
		ThatOne = "images/" + WhichOne + "Hover.jpg";
		document.getElementById(ThisOne).src = ThatOne;
	}
}


function fnMenuMouseOut(WhichOne)
{
	if(curMenu != WhichOne)
	{
		//reset menu highlight
		ThisOne = "obj" + WhichOne; 
		ThatOne = "images/" + WhichOne + "Unselected.jpg";
		document.getElementById(ThisOne).src = ThatOne;
	}
}


function fnResetPreviousMenuSelection()
{
	if (curMenu != "none")
	{
	//reset previous menu selection
	ThisOne = "obj" + curMenu ; 
	ThatOne = "images/" + curMenu + "Unselected.jpg";
	document.getElementById(ThisOne).src = ThatOne;
	}
}


function fnSetMenuSelection(WhichOne)
{
	//assign current menu selection
	curMenu = WhichOne;
	//alert("Setting Menu to " + curMenu);
	
		
	if (curMenu != "none")
	{
		//set selected menu selection
		ThisOne = "obj" + WhichOne; 
		ThatOne = "images/" + WhichOne + "Selected.jpg";
		document.getElementById(ThisOne).src = ThatOne;
	}
}		


function fnSetTopicTitle()
{
	//set selected topic title
	document.getElementById('HeaderTitle').src = "images/" + aryTopicFiles['aryTitles'][curTopicIdx] + ".jpg";
	//alert("fnSetTopicTitle: Current topic index = " + curTopicIdx);
	//document.getElementById(HeaderTitle).src = "images/" + aryTopicFiles['aryTitles'][curTopicIdx] + ".jpg";
	//ThisOne = "HeaderTitle"; 
	//ThatOne = "images/" + aryTopicFiles['aryTitles'][curTopicIdx] + ".jpg";
	//document.getElementById(ThisOne).src = ThatOne;
}


// ***END MENU SECTION***


// ***START CONTACT SECTION***

function fnDisplayRequestLink()
{
	var LinkOpening = "<a href=\"javascript:fnProcessRequest();\" ";
	var LinkMiddle = "title=\"" + LinkTooltip + "\"";
	var LinkClosing = ">" + LinkText + "</a>";
	document.write(LinkOpening + LinkMiddle + LinkClosing);
}
 	
function fnProcessRequest()
{
		//alert("Name: " + document.getElementById('objTxtName').value + newlines 
		//+ "Email: " + document.getElementById('objTxtEmail').value);
		
		//check to see if user altered name field
		if(document.getElementById('objTxtName').value != "Type your name here")
		{
			//create local copy of user name for sending update request
			var UserName = "Name = " + document.getElementById('objTxtName').value;
			
			//check to see if user altered email field
			if(document.getElementById('objTxtEmail').value != "Type your email address here")
			{
				//create local copy of user name for sending update request
				var UserEmail = "Email = " + document.getElementById('objTxtEmail').value;
			
      				//alert("Address computes to: " + address);
				
				//construct message request
				var instruction = mailopening + dblequote + mailleader + address 
				+ subjectleader + subject + bodyleader + bodytextIntro + URLnewline 
				+ UserName + URLnewline + UserEmail + URLnewline + URLnewline 
				+ bodytext1 + URLnewline + bodytext2 + dblequote; 

      				//alert("Instruction computes to: " + instruction);
      				eval(instruction);
			}
			else
			{
				alert("Please enter your email address in the email field" + newlines
				+ "The current email value is: " + document.getElementById('objTxtEmail').value); 
			}
		}
		else
		{
			alert("Please enter your name in the name field" + newlines
			+ "The current name value is: " + document.getElementById('objTxtName').value); 
		}
}

// ***END CONTACT SECTION***


// ***END OF FILE***


