/*  This code copyright iWeb Solutions Pty Ltd 
	
	RED Common Functions Version 1.1
	Created: 23rd January 2004
	By: iWeb Solutions Pty Ltd (Email: info@iwebsolutions.com.au)
	Name: common.js
	Modified: 19th April 2004 - removed redundant functions :: Rob
	
*/

/*	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;
	this.url = null;
	this.url = document.location.pathname + document.location.search;
	DocURL = document.URL;
	protocolIndex=DocURL.indexOf("://",4);
	serverIndex=DocURL.indexOf("/",protocolIndex + 3);
	BeginURL=DocURL.indexOf("#",1) + 1;
	this.baseURL=DocURL.substring(BeginURL,serverIndex) + '/';
}

/*	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;			
}
/*	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();
}

function doButton(oButton) {  // Image swapper
	var ext = oButton.src.substring(oButton.src.lastIndexOf("."), oButton.src.length)
	oButton.src = ((i = oButton.src.indexOf("_ovr")) >= 0) ? "/img/" + oButton.id + ext : "/img/" + oButton.id + "_ovr" + ext;
	oButton.style.cursor = 'hand';
}
function btnIS(oButton) {  // Image swapper
	var ext = oButton.src.substring(oButton.src.lastIndexOf("."), oButton.src.length)
	var path = oButton.src.substring(0, oButton.src.lastIndexOf("/")+1)
	var file = oButton.src.substring(oButton.src.lastIndexOf("/")+1,oButton.src.lastIndexOf("."))
	oButton.src = ((i = file.indexOf("_ovr")) >= 0) ? path + file.substring(0, file.lastIndexOf("_")) + ext : path + file + "_ovr" + ext;
}

function fbClose() {  //Hides the Feedback Window.
	oFeedback = document.getElementById('Feedback');
	oFeedback.style.visibility = "hidden";
	oFeedback.style.position = "absolute";
}

function doPage(Page) {  //Used for pageing.
	$('txtPage').value = Page; 
	$('btnPage').click();
}

//function doPage(eventArgument) {  //Used for pageing.
//	var oFrm = document.iWebForm;
//	oFrm.txtPage.value = eventArgument; 
//	oFrm.btnPage.click();
//}

function doLogon() {  // Used to submit the search form.
	var oFrm = document.frmLogon;
	oFrm.submit();
}
function fnKeyPress() {
	if (browser.isIE) {
		if (event.type == "keypress" && event.keyCode == 13) doLogon();	
	}
}

/* Email Validation */
function validEmail(emailStr) { // Complex eMail Address Validation
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	if (emailStr.length < 1 ){ //Email address is invalid 0 length string.
		return false;
	}

	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {	// Email address invalid - @ and .'s)
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat)==null) { // The username is invalid.
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) { // Destination IP address is invalid
				return false; }
		}
		return true;
	}
	var domainArray=domain.match(domainPat);
	if (domainArray==null) { // The domain name is invalid.
		return false; }
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) { // The domain must be three-letters
		return false;
	}
	if (len<2) { // This address is missing a hostname!
		return false; }
	// The Address is Valid	
	return true;
}


// Sets default buttons.
// Originally created by Janus Kamp Hansen - http://www.kamp-hansen.dk
// Extended by Darrell Norton - http://dotnetjunkies.com/weblog/darrell.norton/ 
//   -- added Mozilla support, fixed a few issues, improved performance
// Example HTML:
//  <input type=text name=Text1 onkeydown="KeyPress(submit2,event);" />
//  <input type=submit name=submit2 value="Submit Button 2 Clicked" class=button />
function KeyPress(btn, event) {
 //alert('click');
 if (document.all) { if (event.keyCode == 13) {event.returnValue=false;event.cancel = true;btn.click();} }               // IE
 else if (document.getElementById) { if (event.which == 13){event.returnValue=false;event.cancel = true;btn.click();} }  // Mozilla
 else if (document.layers) { if(event.which == 13){event.returnValue=false;event.cancel = true;btn.click();} }           // Netscape 4.x
}

// Nav Menu

slideMenu.Registry = [];
slideMenu.lineHeight = 21;
function slideMenu(id,numLines) {
	slideMenu.Registry[id] = [];
	slideMenu.Registry[id]['height']       = 1;
	slideMenu.Registry[id]['containerObj'] = document.getElementById(id+"Container");
	slideMenu.Registry[id]['contentObj']   = document.getElementById(id+"Content");
	slideMenu.Registry[id]['maxHeight']    = new Number((numLines * slideMenu.lineHeight) + 1);
	slideMenu.Registry[id]['interval']     = null;
	slideMenu.Registry[id]['timeout']      = null;
	slideMenu.Registry[id]['contentObj'].style.height  = '0px';
	// slide down events
	slideMenu.Registry[id]['containerObj'].onmouseover = function() { slideMenu.showMenu(id); }
	slideMenu.Registry[id]['containerObj'].onfocus     = function() { slideMenu.showMenu(id); }
	// slide up events
	slideMenu.Registry[id]['containerObj'].onmouseout  = function() { slideMenu.hideMenu(id); }
	slideMenu.Registry[id]['containerObj'].onblur      = function() { slideMenu.hideMenu(id); }
}

slideMenu.showMenu = function(id) {
	clearInterval(slideMenu.Registry[id]['interval']);
	slideMenu.Registry[id]['contentObj'].style.display = 'block';
	slideMenu.Registry[id]['contentObj'].style.zIndex += 10;
	slideMenu.Registry[id]['interval'] = window.setInterval("slideMenu.slideDown('"+id+"')",1);
}

slideMenu.hideMenu = function(id) {
	clearInterval(slideMenu.Registry[id]['interval']);
	slideMenu.Registry[id]['contentObj'].style.zIndex = 10;
	slideMenu.Registry[id]['interval'] = window.setInterval("slideMenu.slideUp('"+id+"')",1);
}

slideMenu.slideDown = function(id) {
	for(var i=0;i<15;i++) {
		if(slideMenu.Registry[id]['height'] >= slideMenu.Registry[id]['maxHeight']) {
			clearInterval(slideMenu.Registry[id]['interval']);
		} else {
			slideMenu.Registry[id]['contentObj'].style.height = slideMenu.Registry[id]['height']+'px';
			slideMenu.Registry[id]['height'] ++;
		}
	}
}

slideMenu.slideUp = function(id) {
	for(var i=0;i<15;i++) {
		if(slideMenu.Registry[id]['height'] <= 1) {
			clearInterval(slideMenu.Registry[id]['interval']);
			slideMenu.Registry[id]['contentObj'].style.display = 'none';
		} else {
			slideMenu.Registry[id]['contentObj'].style.height = slideMenu.Registry[id]['height']+'px';
			slideMenu.Registry[id]['height'] --;
		}
	}
}
