/*  This code copyright iWeb Solutions Pty Ltd 
	
	National Collection Services Version 1.0
	Created: 23rd January 2004
	By: iWeb Solutions Pty Ltd (Email: info@iwebsolutions.com.au)
	Name: common.js
	Modified:
*/

/*	BROWSER DETECTION OBJECT  */

function oBrowser() {
	var ua, s, i;
	this.isIE = false;
	this.isNS = false;
	this.ver = null;
	this.isWin = false;
	this.isMac = false;
 	ua = navigator.userAgent;
	this.isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false; // true if we're on windows
	this.isMac = (navigator.appVersion.indexOf("Macintosh") != -1) ? true : false; // true if we're on windows
  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.ver = parseFloat(ua.substr(i + s.length));
    return;
  }
  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.ver = parseFloat(ua.substr(i + s.length));
    return;
  }
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
	s = "Netscape/";
    if ((i = ua.indexOf(s)) >= 0) {
		this.ver = parseFloat(ua.substr(i + s.length));
	}else{
	    this.ver = 6.1;
	}
    return;
  }
  s = "Mozilla/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.ver = parseFloat(ua.substr(i + s.length));
    return;
  }
}
var browser = new oBrowser(); //Instantiate Browser object

/* Retrieves the base url from the current page */
function oBaseUrl(){
	var DocURL, protocolIndex, ServerIndex, BeginURL
	this.baseURL = null;
	DocURL = document.URL;
	protocolIndex=DocURL.indexOf("://",4);
	serverIndex=DocURL.indexOf("/",protocolIndex + 3);
	BeginURL=DocURL.indexOf("#",1) + 1;
	this.baseURL=DocURL.substring(BeginURL,serverIndex) + '/';
}
/* MODAL DIALOGS */

function oMWin(intW, intH, strURL, vArgs){
	var strWinOpts, vResult;
	strWinOpts = "dialogHeight: " + intH + "px; dialogWidth: " + intW + "px; center: yes; help: no; resizable: no; scroll: no; status: no;"
	vResult = window.showModalDialog(strURL, vArgs, strWinOpts);
	return vResult;
}

/*	WINDOW FUNCTIONS
	Usage:
		1. <a href="JavaScript:openWindowAtCentre('http://www.iwebsolutions.com.au','iWeb Solutions', 400, 200);">iWeb Solutions</a>
	->	2. <a href="http://www.heightdynamics.com.au" onclick="openScrollWindowAtCentre(this.href, 'Height Dynamics', 670, 700);return false;" onfocus="this.blur()">Height Dynamics</a><br>
*/
var oWin = null;
function openWindowAtCentre( location, name, sizeX, sizeY ) {
  var posx = (screen.availWidth-sizeX)/2;
  var posy = (screen.availHeight-sizeY)/2;
  var windowString = 'toolbar=no,location=no,menubar=no,status=no,resizable=no,directories=no,scrollbars=no,copyhistory=no,width='+sizeX+',height='+sizeY+',left='+posx+',top='+posy+',screenX='+posx+',screenY='+posy;
  oWin = window.open(location, name, windowString);
  oWin.focus();
}
function openScrollWindowAtCentre( location, name, sizeX, sizeY ) {
  var posx = (screen.availWidth-sizeX)/2;
  var posy = (screen.availHeight-sizeY)/2;
  var windowString = 'toolbar=no,location=no,menubar=no,status=no,resizable=no,directories=no,scrollbars=yes,copyhistory=no,width='+sizeX+',height='+sizeY+',left='+posx+',top='+posy+',screenX='+posx+',screenY='+posy;
  oWin = window.open(location, name, windowString);
  oWin.focus();
}
/* Used to load a page into the opener window form a popup. */
function fnLoadWin(strPage) {
	var oWin
	var base = new oBaseUrl();
	oWin = window.opener;
	oWin.location = base.baseURL + strPage;
	window.close();
}
/* Custom Functions */
function activateLogin(){
	oFrm = document.getElementById('NCSLogin')
	oFrm.username.focus();
	oFrm.username.select();
}

function fbClose() {  //Hides the Feedback Window.
	oFeedback = document.getElementById('Feedback');
	oFeedback.style.visibility = "hidden";
	oFeedback.style.position = "absolute";
}

function doLogin() {  //Used to Search the Store.
	oLogin = document.getElementById('NCSLogin');
	oLogin.submit();
	// if (oSearch.value != "" ) location='/search/default.aspx?S=' + oSearch.value;
}
function doLoginButton(oImg) {  //Used to Search the Store.
//	oImg.src = ((i = oImg.src.indexOf("/img/btn_go.gif")) >= 0) ? "/img/btn_go_ovr.gif" : "/img/btn_go.gif";
	oImg.style.cursor = "hand";
}

function fnKeyPress() {
	if (browser.isIE) {
		if (event.type == "keypress" && event.keyCode == 13) doLogin();	
	}
}

/* Validate length of Text Area 
	Usage: onKeyUp="fnLength(this, 255);" */
function fnLength(oTextArea, len){ if (oTextArea.value.length > len) oTextArea.value = oTextArea.value.slice(0,len); }

/* Validate numbers */
function fnValidNum(oCtrl, strField){
	if ((isNaN(oCtrl.value)) || (oCtrl.value == "")){ 
		alert(strField + " must contain a valid number"); 
		fnCtrlActivate(oCtrl);
		return false;}
	return true;			
}

//*****************************************************************************
// Code modified form Mike Hall
// See http://www.brainjar.com/
//*****************************************************************************

var activeMenu = null;

function doMenu(event, menuType) {
	var oMenu;   // Holds all the objects associated to the current menu item.
	if (browser.isIE)  // Get the current menu item.
		oMenu = window.event.srcElement;
	else
		oMenu = event.target;
	
	oMenu.blur();  //Comment out if sour is an image and not an a Href.
	if (oMenu == activeMenu) return false;  // Exit if this button is the currently active one.
	if (oMenu.subMenu == null) oMenu.subMenu = document.getElementById('sm' + oMenu.getAttribute("ID")); // Associate the sub menu to this menu item.
	initMenu(oMenu);  // initialise the sub menu.
	if (activeMenu != null) hideSubMenu(activeMenu); // Hide the current menu
	//  Show this menu items sub menu
	if (oMenu != activeMenu) {
		showSubMenu(oMenu, menuType);
		activeMenu = oMenu;
	} else activeMenu = null;
	return false;
}


function initPageMenus(Type){
/* Fix for netscape browsers to preposition the menus in the right position */
	var menuItems = null;
	var oMenu = null;
	var oSubMenu = null;
	var x, y;
	if (browser.isNS){
		oMenu = document.getElementById('navMenu');
		menuItems = oMenu.getElementsByTagName("IMG");
		for (i = 0; i < menuItems.length; i++){
			if (Type == 1) {
				x = getPageOffsetLeft(oMenu) + oMenu.offsetWidth;
				y = getPageOffsetTop(oMenu);
			} else {
				x = getPageOffsetLeft(oMenu);
				y = getPageOffsetTop(oMenu) + oMenu.offsetHeight;
			}
			oSubMenu = document.getElementById('sm' + menuItems[i].getAttribute("ID"));
			if (oSubMenu != null){
				oSubMenu.style.left = x + "px";
				oSubMenu.style.top  = y + "px";
			}
		}
	}
}

function initMenu(oMenu) {
	var menuItems;
	var width;
	// Set mouseout events for the menu item and sub menu.
	if (oMenu != null){
		if (oMenu.onmouseout == null) oMenu.onmouseout = itemMouseout;
	if (oMenu.subMenu != null){
		if (oMenu.subMenu.onmouseout == null) oMenu.subMenu.onmouseout = itemMouseout;
		// Set the width of the first sub menu item fix for IE
		if (browser.isIE) {
			menuItems = oMenu.subMenu.getElementsByTagName("A");
			width = menuItems[0].offsetWidth;
			menuItems[0].style.width = width +"px";
		}
	}
	}
}

function itemMouseout(event) {
	var oElement;
	if (activeMenu == null) return;
	if (browser.isIE)
		oElement = window.event.toElement;
	else if (event.relatedTarget != null)
		oElement = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
	if (getContainerWith(oElement, "DIV", "Menu") == null) {
		hideSubMenu(activeMenu);
		activeMenu = null;
	}
}

function showSubMenu(oMenu, Type) {
	var x, y;
	// Find the menu position.
	if (Type == 1) {
		x = getPageOffsetLeft(oMenu) + oMenu.offsetWidth;
		y = getPageOffsetTop(oMenu);
	} else {
		x = getPageOffsetLeft(oMenu);
		y = getPageOffsetTop(oMenu) + oMenu.offsetHeight;
	}
	// Adjust the position for IE and Mac
	if (browser.isIE && browser.isWin) {
		x += oMenu.offsetParent.clientLeft;
		y += oMenu.offsetParent.clientTop;
	}
	if (browser.isIE && browser.isMac) {
		x = Type == 1 ?	x - 20 : x - 3;
		y = y - 3;
	}
	if (oMenu.subMenu != null){
		// position the sub menu and then display it.
		oMenu.subMenu.style.left = x + "px";
		oMenu.subMenu.style.top  = y + "px";
		oMenu.subMenu.style.visibility = "visible";
	}
}

function hideSubMenu(menu) {
	if (menu == null || menu.subMenu == null) return;
	menu.subMenu.style.visibility = "hidden";
	menu.subMenu = null;
 }

function getContainerWith(node, tagName, className) {
	// Starting with the given node, find the nearest containing element
	// with the specified tag name and style class.
	while (node != null) {
		if (node.tagName != null && node.tagName == tagName && hasClassName(node, className)) return node;
		node = node.parentNode;
	}
	return node;
}

function hasClassName(el, name) {
	// Return true if the given element currently has the given class name.
	var i, list;
	list = el.className.split(" ");
	for (i = 0; i < list.length; i++)
		if (list[i] == name) return true;
	return false;
}
	
function getPageOffsetLeft(el) {
	// Return the x coordinate of an element relative to the page.
	var x;
	x = el.offsetLeft;
	if (el.offsetParent != null) x += getPageOffsetLeft(el.offsetParent);
	return x;
}

function getPageOffsetTop(el) {
  // Return the y coordinate of an element relative to the page.
	var y;
	y = el.offsetTop;
	if (el.offsetParent != null) y += getPageOffsetTop(el.offsetParent);
	return y;
}
