//	Copyright: XT3 Internet Integrator	http://www.xt3.com.au	
//	Date Created:		30 March 2001
//	Initial Author:		Todd Baker - todd.baker@xt3.com.au	
//	Client :			Securities Institute of Australia
//	Purpose:			This file is included in every page and contains all common Javascript functions including browser check									//
//-----------------------------------------------------------------------------------------------------------------------------------------
// Ultimate client-side JavaScript client sniff. Version 3.02 (C) Netscape Communications 1999.  Permission granted to reuse and distribute.
// Please see master file @ XT3 for documentation
function Is (){
    var agt=navigator.userAgent.toLowerCase();
    this.major		= parseInt(navigator.appVersion);
    this.minor		= parseFloat(navigator.appVersion);
    this.nav		= ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
    this.nav4		= (this.nav && (this.major == 4));
    this.nav4up		= (this.nav && (this.major >= 4));    
    this.nav6		= (this.nav && (this.major == 5));
    this.nav6up		= (this.nav && (this.major >= 5));
    this.ie			= ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    this.ie4		= (this.ie && (this.major == 4) && (agt.indexOf("msie 5")==-1) );
    this.ie4up		= (this.ie  && (this.major >= 4));
    this.ie5		= (this.ie && (this.major == 4) && (agt.indexOf("msie 5.0")!=-1) );
    this.ie5_5		= (this.ie && (this.major == 4) && (agt.indexOf("msie 5.5") !=-1));
    this.ie5up		= (this.ie  && !this.ie3 && !this.ie4);
    this.ie5_5up	= (this.ie && !this.ie3 && !this.ie4 && !this.ie5);
    this.ie6		= (this.ie && (this.major == 4) && (agt.indexOf("msie 6.0") !=-1));
    this.ie6up		= (this.ie && !this.ie3 && !this.ie4 && !this.ie5 && !this.ie6);
    this.mac		= (agt.indexOf("mac")!=-1);
}
var is;
is = new Is(); 
// END BROWSER CHECK

//--------- ONLOAD FUNCTIONS ----//
// These functions handle the Gunshot and box's on the right hand side!
// It was too hard to put them in with HTML so we lay them over the top with DHTML 
// ResetBox() just moves the box's when the browser resizes!
var isHomepage = false
var isLoaded = false;
function StartUp(){
	isLoaded = true;
	var GunTop = 1;
	var BoxTop = getBoxHeight();
	if (is.nav4up){
		var BoxLeft = 1;
		var GunLeft = 1;
	}else{
		var BoxLeft = 0;
		var GunLeft = 0;	
	}
	if (is.mac && is.ie) GunTop = 1;
	if (is.mac && is.ie) BoxTop = 600; // Mac/IE seems is very buggy with this so we guess!
	ShowLayer("GunShot",GunLeft,GunTop);
	ShowLayer("Boxs",BoxLeft,BoxTop);
	if (isHomepage){
		SIA_preloadImages('/images/home_main_students_on.gif', '/images/home_main_members_on.gif', '/images/home_main_practitioners_on.gif', '/images/home_main_investors.gif','/images/home_main_study_on.gif','/images/home_main_careers_on.gif','/images/home_main_courses_on.gif','/images/home_main_training_on.gif')
	}
}


function doResize(){
	if (is.ns4up){
		window.location.reload();
	}else{
		HideLayer("Boxs");
		var BoxTop = 0
		BoxTop = getBoxHeight();
		if (is.nav4up){
			var BoxLeft = 1;
		}else{
			var BoxLeft = 0;
		}	
		if (is.mac && is.ie) GunTop = 1;
		if (is.mac && is.ie) BoxTop = 600; // Mac/IE seems is very buggy with this so we guess!
		ShowLayer("Boxs",BoxLeft,BoxTop);
	}
}
function getBoxHeight(){
		var ScreenHeight = 0;
		if (is.nav4up){
			ScreenHeight = document.height - 140;
		}else{
			ScreenHeight = document.body.scrollHeight - 120;
		}
		return ScreenHeight;
}

//**** END BOX AND GUNSHOT CODE  *********//
//****************************************//
// Form Validation
defaultEmptyOK = false;
// Checks to see if a field is empty
function isEmpty(s){
   return ((s == null) || (s.length == 0))
}

// Checks for digits using regular expressions
var reDigit = /^\d/
function isDigit (c){
   return reDigit.test(c)
}

// Checks for valid name@domain.tld style email address using regular expressions
var reEmail = /^.+\@.+\..+$/
function isEmail (s){
   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    else {
       return reEmail.test(s)
    }
} 

// Trims input strings of white space....
function strTrim(strText) {
	for (i=0; i<strText.length; ++i) {
		if (strText.charAt(i) != ' ') break;
	}
	if (i >= strText.length) return ('');
	if (i > 0) strText = strText.substring(i, strText.length);
	for (j=strText.length-1; j > i; --j) {
		if (strText.charAt(j) != ' ') break;
	}
	if (j < strText.length -1)
		strText = strText.substring(0, j+1);
	return strText;
}

// This validates the search box
function ValidateSearch(){
	var strSearchString = strTrim(document.frmSearch.Query.value)
	var numberOfErrors = 0;
	var strAlert = "";
	if ((strSearchString == "") || (strSearchString == "Search...")){
		strAlert = "-------------------------------------------------------\n--------------------- Finsia --------------------------\n\n     Please enter the words you\n     would like to search for.\n\n-------------------------------------------------------\n\n";
		alert(strAlert);
		return false;
	}else{
		if (strSearchString.length > 500){
		document.frmSearch.Query.value = strSearchString.substring(0,500);
		}
	return true;
	}
}
//****************************************//
// Open Pop-Up Window
function OpenChild(strURL,strName,strHeight,strWidth){
	NewChild = window.open(strURL,strName,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+strWidth+',height='+strHeight+',top=50,left=50')
	NewChild.resizeTo(strWidth,strHeight);
	NewChild.focus();
}
function OpenWindow(strURL,strName){
	window.open(strURL,strName);
}

// Print page Handler
function PrintPage(){
	if (window.print) {	
	agree = confirm('Would you like to print this page now?');
	if (agree) window.print(); 
	}
	else alert("Press ctrl/p for windows or command/p on a mac to print this page.")
}

// Rollover function
function ImageSwap(daImage, daSrc){
 var objStr,obj;
 if(document.images){
   if (typeof(daImage) == 'string') {
     objStr = 'document.' + daImage;
     obj = eval(objStr);
     obj.src = daSrc;
   } else if ((typeof(daImage) == 'object') && daImage && daImage.src) {	
     daImage.src = daSrc;
   }
 }
}
// Preload Images

function SIA_preloadImages() {
 if (document.images) {
   if (typeof(document.SIA) == 'undefined'){
     document.SIA = new Object();
   }
   document.SIA.loadedImages = new Array();
   var argLength = SIA_preloadImages.arguments.length;
   for(arg=0;arg<argLength;arg++) {
     document.SIA.loadedImages[arg] = new Image();
     document.SIA.loadedImages[arg].src = SIA_preloadImages.arguments[arg];
   }
 }
}



function GetObj(eleId){
	if (is.nav4){
		obj = eval("document."+eleId);
	}else if (is.ie4up){
		obj = eval("document.all."+eleId);
	}else if (is.nav6up){
	alert("NS6");
		obj = document.getElementById(eleId);
	}
 return obj;
}
// Writes the status bar message
function SetStatus(strStatus) {window.status=strStatus; return true;}
// Redirect to another page
function goTo(URL){
	window.location = URL;
}
function GoBack(){
	history.go(-1);
}

// Help pop up. loadHelp("URL",120,258,1/0,evnt)
function loadHelp(w_page,w_width,w_height,scroll,evnt){
	if (helpWindow) { // if there's already a helpWindow open, close it.
		helpWindow.close();
	}
	if (is.nav){ // this section tweaks both browsers' event coordinates so that they are the same.
		Xpos = evnt.screenX-20;
		Ypos = evnt.screenY-5;
	}else{ //IE.
		Xpos = window.event.screenX-10;
		Ypos = window.event.screenY-10;
	}
	Xpos -= w_width; // move Xpos to the left by the help window's width
	if ((Ypos + parseInt(w_height)) > screen.availHeight) {         // if the window would be off the bottom of the screen
		Ypos = screen.availHeight - w_height - 50;                       // move the window up so that its lowest position is 50 pixels above the bottom of the screen
	}
	helpWindow = window.open(w_page,'helpWindow','height='+w_height+',width='+w_width+',top='+Ypos+',left='+Xpos+',scrollbars='+scroll+',resizable') // open the help window
	if (is.nav){ // this section essentially refreshes the new window
		helpWindow.focus();
	}else{
		helpWindow.resizeTo(w_width,w_height);
		helpWindow.focus();
	}
}
function ShowLayer(LayerName,LeftPos,TopPos){
	var cLeftPos = 0;
	cLeftPos = LeftPos;
	var cTopPos = "0";
	cTopPos = TopPos;
	if (is.nav4){
		document.layers[LayerName].left = cLeftPos;
		document.layers[LayerName].top = cTopPos;
		document.layers[LayerName].visibility = "show";
	}else if(is.nav6up){
		document.getElementById(LayerName).style.left = cLeftPos;
		document.getElementById(LayerName).style.top = cTopPos;
		document.getElementById(LayerName).style.visibility = "visible";
	}else{
		document.all[LayerName].style.pixelLeft = cLeftPos;
		document.all[LayerName].style.pixelTop = cTopPos;
		document.all[LayerName].style.visibility = "visible";
	}
}
function HideLayer(LayerName){
	if (is.nav4){
		document.layers[LayerName].visibility = "hide";
	}else if(is.nav6up){
		document.getElementById(LayerName).style.visibility = "hidden";
	}else{
		document.all[LayerName].style.visibility = "hidden";
	}
}
function Check(){	
	if (124 == event.keyCode){
	//OpenChild("/includes/credits.asp","credits",500,500)
	}	
}


// The following functions are to display cross-platform tooltips of an <a> tag
// Refer to inc_common.asp for the PrintHyperLink Function
//
if (!document.layers&&!document.all&&!document.getElementById){
   event="test"
}
function showtip(e,text,maxwidth){

   var objTooltip
   var arrText, tiptext
   var bgcolor, fontfamily, border
   var layerText

   bgcolor    = '#FFFFE7';
   fontfamily = 'Arial, Helvetica, sans-serif';
   border     = '1px solid black';

   // Replacing <p> to <br>, so that the tooltip can be displayed correctly in Netscape 4
   arrText = text.split('<p>');
   if (arrText.length > 1){
      tiptext = '';
      for (var i=0; i < arrText.length; i++){
         tiptext += arrText[i];
         if (i != arrText.length - 1 && i!= 0){
            tiptext += '<br>';
         }
      }
   }
   else{
      tiptext = text;
   }

   if (document.all||document.getElementById){
      objTooltip = document.getElementById("tooltip");
      objTooltip.innerHTML = tiptext;
      objTooltip.style.border = border;
      objTooltip.style.backgroundColor = bgcolor;
      objTooltip.style.fontFamily = fontfamily;
      objTooltip.style.fontSize = '11px';
      if (navigator.userAgent.indexOf('Netscape6') != -1){
         // Browser is Netscape 6.
         // Since Netscape 6 doesn't support the scrollLeft & scrollTop property,
         // So use pageX and pageY property instead.
         objTooltip.style.left = e.pageX + 5;
         objTooltip.style.top  = e.pageY + 5;
      }
      else{
		objTooltip.style.left = e.clientX + 5 + document.body.scrollLeft;
		objTooltip.style.top  = e.clientY + 5 + document.body.scrollTop;
	  }
      if (maxwidth > 0){
		objTooltip.style.width = maxwidth;
	  }
      objTooltip.style.visibility = "visible";
   }
   else{
      if (document.layers){
         layerText  = '<layer bgColor="' + bgcolor + '" ';
         layerText += 'style="border:' + border + ';font-family:' + fontfamily + ';font-size:12px;">';
         layerText += tiptext + '</layer>'
         document.tooltip.document.write(layerText);
         document.tooltip.document.close();
         document.tooltip.left = e.pageX + 10;
         document.tooltip.top  = e.pageY + 10;
         document.tooltip.visibility = "show";
      }
   }
}
function hidetip(){
   var objTooltip

   if (document.all||document.getElementById){
      objTooltip = document.getElementById("tooltip")
      objTooltip.style.visibility = "hidden"
   }
   else{
      if (document.layers){
         document.tooltip.visibility = "hidden"
      }
   }
}

function foldtreerow(objImg, sRowId){
	var objRow = document.getElementById(sRowId)
	
	if (typeof(objRow) != 'undefined'){
		if (objRow.style.display == 'none'){
			objImg.src = '/images/icontreeunfold.jpg';
			objRow.style.display = 'block';
		}
		else{
			objImg.src = '/images/icontreefold.jpg';
			objRow.style.display = 'none';
		}
	}
}

function foldtreeAllrow(objFoldAllImg){
	var arrTreeFoldImgs = document.getElementsByName("imgTreeFold");
	var objImg, objRow
	var sRowId
			
	// No Rows to expand/collapse.
	if (arrTreeFoldImgs.length == 0){
		return true;
	}
	
	if (typeof(arrTreeFoldImgs.length) == 'undefined'){
		// 1 Row to expand/collapse.
		objImg = arrTreeFoldImgs;
		sRowId = objImg.id.substring(3);
		objRow = document.getElementById(sRowId);
				
		// If Row is not shown and AllTreeItems are all folded, or
		//    Row is shown and AllTreeItems are all not folded
		if ((objRow.style.display == 'none' && document.getElementById("hidTableTreeAllFolded").value == 'true') ||
		    (objRow.style.display == 'block' && document.getElementById("hidTableTreeAllFolded").value == 'false')){	    
			foldtreerow(objImg, sRowId)
		}
	}
	else{
		// More than 1 Row to expand/collapse.
		for (var i=0; i < arrTreeFoldImgs.length; i++){
			objImg = arrTreeFoldImgs[i];
			sRowId = objImg.id.substring(3);
			objRow = document.getElementById(sRowId);
			
			// If Row is not shown and AllTreeItems are all folded, or
			//    Row is shown and AllTreeItems are all not folded
			if ((objRow.style.display == 'none' && document.getElementById("hidTableTreeAllFolded").value == 'true') ||
				(objRow.style.display == 'block' && document.getElementById("hidTableTreeAllFolded").value == 'false')){	    
				foldtreerow(objImg, sRowId)
			}
		}
	}
	
	// Change the Image of the FoldAllTreeRows Icon.
	if (document.getElementById("hidTableTreeAllFolded").value == 'true'){
		objFoldAllImg.src = '/images/icontreeunfold.jpg';
		document.getElementById("hidTableTreeAllFolded").value = 'false';
	}
	else{
		objFoldAllImg.src = '/images/icontreefold.jpg';
		document.getElementById("hidTableTreeAllFolded").value = 'true';
	}
}
