/*////////////////////////////////////////////////////////////
JavaScript Code Library
Developed by GrafX Design Division Pty Ltd
http://www.grafx.com.au

Last Checked In By : Richard Czeiger
Public Revision No : 1.0

The software and related user documentation are
protected under copyright laws and remain the sole
property of GrafX Design Division. Full license is
available on the GrafX Design Division web site or
in the original download.

Technical support is available via the GrafX web site at
http://www.grafx.com.au
////////////////////////////////////////////////////////////*/



// SEO Tracking Script v1.0
var ns_data,ns_hp,ns_tz,ns_rf,ns_sr,ns_img,ns_pageName;
ns_pageName= this.location;
document.cookie='__support_check=1';ns_hp='http';
ns_rf=document.referrer;ns_sr=window.location.search;
ns_tz=new Date();if(location.href.substr(0,6).toLowerCase() == 'http:')
ns_hp='http';ns_data='&an='+escape(navigator.appName)+
'&sr='+escape(ns_sr)+'&ck='+document.cookie.length+
'&rf='+escape(ns_rf)+'&sl='+escape(navigator.systemLanguage)+
'&av='+escape(navigator.appVersion)+'&l='+escape(navigator.language)+
'&pf='+escape(navigator.platform)+'&pg='+escape(ns_pageName);
ns_data=ns_data+'&cd='+screen.colorDepth+'&rs='+escape(screen.width+ ' x '+screen.height)+
'&tz='+ns_tz.getTimezoneOffset()+'&je='+ navigator.javaEnabled();
ns_img=new Image();ns_img.src=ns_hp+'://tracker.statgauge.com/statistics.aspx'+
'?v=1&s=213&acct=226630'+ns_data+'&tks='+ns_tz.getTime();



// Get the hacked stylesheet
document.write('<style type="text/css">@import url("styles/hacks.css");</style>');



// Suppress JavaScript Errors
window.onerror=new Function("return true")



// Set the Status Bar Message
var statusMsg = 'Australasian Institute of Dangerous Goods Consultants';
window.defaultStatus=statusMsg;



// Fixes IE6 CSS Rendering Bug
if (document.all && window.attachEvent) window.attachEvent("onload", fixWinIE);
function fixWinIE() {
	if (document.body.scrollHeight < document.body.offsetHeight) { document.body.style.display = 'block'; }
}



// Spam Suppression for Emails
function stopSpam(name, domain, subject) {
	var symbol = '@';
	// Took out the Subject due to MS fault in Outlook Express (Source - http://support.microsoft.com/kb/q182985/ )
	// document.write('<a href="mailto:' + name + symbol + domain + '&subject=' + subject + '">' + name + symbol + domain + '</a>');
	document.write('<a href="mailto:' + name + symbol + domain + '">' + name + symbol + domain + '</a>');
}



// Roll Over Nav
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);



// Create Short Cuts
/*
document.write('<div id="shortCuts">');
document.write('<ul>');
document.write('<li><a href="index.html" title="AIDGC Home Page">Home<\/a><\/li>');
document.write('<li><a href="about.html" title="Organisation overview and Membership Details">About<\/a><\/li>');
document.write('<li><a href="contact.html" title="Contact Details and Feedback Form">Contact<\/a><\/li>');
document.write('<\/ul>');
document.write('<\/div>');
*/


// Create Content
function createContent() {
	var myBody = document.getElementsByTagName('body')[0];


	// Create Short Cuts
	if (document.getElementsByTagName) {
		getContainer = document.getElementById('container');

		shortDIV = document.createElement('div');
		shortDIV.setAttribute('id','shortCuts');
		myBody.insertBefore(shortDIV, getContainer);
		//masthead = document.getElementById('masthead');
		//getContainer.insertBefore(shortDIV, masthead);

		//newTest = document.createTextNode('Home');
		//getContainer.appendChild(newTest);

		newList = document.createElement('ul');
		shortDIV.appendChild(newList);

		//////////////////////////////////////////////////////////////

		newListItem01 = document.createElement('li');
		newList.appendChild(newListItem01);

		newLink01 = document.createElement('a');
		newLink01.setAttribute('href','index.html');
		newLink01.setAttribute('title','AIDGC Home Page');
		newListItem01.appendChild(newLink01);
			linkText01 = document.createTextNode('Home');
			newLink01.appendChild(linkText01);

		//////////////////////////////////////////////////////////////

		newListItem02 = document.createElement('li');
		newList.appendChild(newListItem02);

		newLink02 = document.createElement('a');
		newLink02.setAttribute('href','about.html');
		newLink02.setAttribute('title','Organisation overview and Membership Details');
		newListItem02.appendChild(newLink02);
			linkText02 = document.createTextNode('About');
			newLink02.appendChild(linkText02);

		//////////////////////////////////////////////////////////////

		newListItem03 = document.createElement('li');
		newList.appendChild(newListItem03);

		newLink03 = document.createElement('a');
		newLink03.setAttribute('href','contact.html');
		newLink03.setAttribute('title','Contact Details and Feedback Form');
		newListItem03.appendChild(newLink03);
			linkText03 = document.createTextNode('Contact');
			newLink03.appendChild(linkText03);

	}

	// Create Login Box
	if (myBody.className != 'member') {
		if (myBody.className != 'admin') {

			// Create DIV for Login
			loginDIV = document.createElement('div');
			myBody.appendChild(loginDIV);
			loginDIV.id='loginBox';

			// Create FORM for Login
			loginFORM = document.createElement('form');
			loginFORM.id='logon';
			loginFORM.setAttribute('action','http://www.aidgc.org.au/members/auth.asp');
			loginFORM.setAttribute('method','post');
			//loginFORM.action = "members/auth.asp";
			loginDIV.appendChild(loginFORM);

			// Create USERNAME for Login
			login01Fieldset = document.createElement('fieldset');
			loginFORM.appendChild(login01Fieldset);

				login01Label = document.createElement('label');
				login01Label.setAttribute('for','username');
				login01Fieldset.appendChild(login01Label);
					login01LabelText = document.createTextNode('Username');
					login01Label.appendChild(login01LabelText);

				login01Input = document.createElement('input');
				login01Input.setAttribute('type','text');
				login01Input.setAttribute('id','username');
				login01Input.setAttribute('name','username');
				login01Input.setAttribute('size','27');
				login01Input.setAttribute('value','Username');
				login01Input.onfocus = function () { if (login01Input.value == 'Username') { login01Input.setAttribute('value',''); } }
				login01Input.onblur = function () { if (login01Input.value == '') { login01Input.setAttribute('value','Username'); } }
				login01Fieldset.appendChild(login01Input);

			// Create PASSWORD for Login
			login02Fieldset = document.createElement('fieldset');
			loginFORM.appendChild(login02Fieldset);

				login02Label = document.createElement('label');
				login02Label.setAttribute('for','password');
				login02Fieldset.appendChild(login02Label);
					login02LabelText = document.createTextNode('Password');
					login02Label.appendChild(login02LabelText);

				login02Input = document.createElement('input');
				login02Input.setAttribute('type','text');
				login02Input.setAttribute('id','password');
				login02Input.setAttribute('name','password');
				login02Input.setAttribute('size','27');
				login02Input.setAttribute('value','Password');
				login02Input.onfocus = function () { if (login02Input.value == 'Password') { login02Input.setAttribute('value',''); } }
				login02Input.onblur = function () { if (login02Input.value == '') { login02Input.setAttribute('value','Password'); } }
				login02Fieldset.appendChild(login02Input);

			// Create SUBMIT for Login
			login03Fieldset = document.createElement('fieldset');
			loginFORM.appendChild(login03Fieldset);

				login03Input = document.createElement('input');
				login03Input.setAttribute('type','submit');
				login03Input.setAttribute('className','button');
				login03Input.setAttribute('value','LOGIN');
				login03Fieldset.appendChild(login03Input);

		}
	}
}



// XHTML External Link Script
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}



// Toggle Function
function toggle(obj) {
	if (document.getElementById(obj).style.display == "none") {
		document.getElementById(obj).style.display = "";
	} else {
		document.getElementById(obj).style.display = "none";
	}
}



// Pop Up Image Window
function PopupPic(sPicURL, sPicTitle) {
	window.open('image.html?'+sPicURL+'&'+sPicTitle, 'imagePopUp', 'resizable=1,width=20,height=20');
}



// Focus on Input Fields
// inputFocus = function () {
function inputFocus() {
	var focusEl = document.getElementsByTagName("INPUT");
	for (var i=0; i<focusEl.length; i++) {
		if ((focusEl[i].getAttribute("type") == "text")||(focusEl[i].getAttribute("type") == "password")) {
			focusEl[i].onfocus=function() {
				this.className+="sffocus";
			}
			focusEl[i].onblur=function() {
				this.className=this.className.replace(new RegExp("sffocus\\b"), "");
			}
		}
	}
	var focusTx = document.getElementsByTagName("TEXTAREA");
	for (var i=0; i<focusTx.length; i++) {
		focusTx[i].onfocus=function() {
			this.className+="sffocus";
		}
		focusTx[i].onblur=function() {
			this.className=this.className.replace(new RegExp("sffocus\\b"), "");
		}
	}
}
// if (window.attachEvent) window.attachEvent("onload", inputFocus);



// Global Cookie Function
function setCookie(NameOfCookie, value, expiredays, path, domain, secure) {
	//The first lines in the function converts the number of days to a valid date.
	var ExpireDate = new Date();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays*24*3600*1000));

	//The next line stores the cookie, simple by assigning
	//the values to the document.cookie-object
	//Note the date is converted to Greenwich Meantime using
	//the 'toGMTstring()'-function

	document.cookie = NameOfCookie +"="+ escape(value) +
	((expiredays == null)? "": ";expires="+ ExpireDate.toGMTString()) +
	((path == null)? "": (";path=" + path)) +
	((domain == null) ? "" : (";domain=" + domein)) +
	((secure == true) ?";secure":"");
}

function getCookie(NameOfCookie) {
	if(document.cookie.length > 0) {
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if(begin != -1) {
			// our cookie was set.
			// The value stored in the cookie is returned from the function
			begin += NameOfCookie.length + 1;
			end = document.cookie.indexOf(";",begin);
			if(end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin,end));
		}
	}
	return null;
	// Our cookie was not set.
	// The value "null" is returned from the function
}

function delCookie(NameOfCookie) {
	// The function simply checks if the cookie is set. If so expiredate is set to Jan. 1st 1970
	if(getCookie(NameOfCookie)) {
		document.cookie = NameOfCookie +"=" +
		";expires= Thu, 01-Jan-70 00:00:01 GMT";
	}
}



// Form Validation
function checkform(of) {
// Test if DOM is available and there is an element called required
	if(!document.getElementById || !document.createTextNode){return;}
	if(!document.getElementById('required')){return;}

// Define error messages and split the required fields
	var errorID='errormsg';
	var errorClass='error'
	var errorMsg='Please enter or change the fields marked with a ';
	var errorImg='images/alert.gif';
	var errorAlt='Error';
	var errorTitle='This field has an error!';
	var reqfields=document.getElementById('required').value.split(',');

// Cleanup old mess
	// if there is an old errormessage field, delete it
	if(document.getElementById(errorID)) {
		var em=document.getElementById(errorID);
		em.parentNode.removeChild(em);
	}
	// remove old images and classes from the required fields
	for(var i=0;i<reqfields.length;i++) {
		var f=document.getElementById(reqfields[i]);
		if(!f){continue;}
		if(f.previousSibling && /img/i.test(f.previousSibling.nodeName)) {
			f.parentNode.removeChild(f.previousSibling);
		}
		f.className='';
	}
// loop over required fields
	for(var i=0;i<reqfields.length;i++) {
// check if required field is there
		var f=document.getElementById(reqfields[i]);
		if(!f){continue;}
// test if the required field has an error,
// according to its type
		switch(f.type.toLowerCase()) {
			case 'text':
				if(f.value=='' && f.id!='email'){cf_adderr(f)}
// email is a special field and needs checking
				if(f.id=='email' && !cf_isEmailAddr(f.value)){cf_adderr(f)}
			break;
			case 'textarea':
				if(f.value==''){cf_adderr(f)}
			break;
			case 'checkbox':
				if(!f.checked){cf_adderr(f)}
			break;
			case 'select-one':
				if(!f.selectedIndex && f.selectedIndex==0){cf_adderr(f)}
			break;
		}
	}
	return !document.getElementById(errorID);

	/* Tool methods */
	function cf_adderr(o) {
		// create image, add to and colourise the error fields
		var errorIndicator=document.createElement('img');
		errorIndicator.alt=errorAlt;
		errorIndicator.src=errorImg;
		errorIndicator.title=errorTitle;
		o.className=errorClass;
		o.parentNode.insertBefore(errorIndicator,o);

	// Check if there is no error message
		if(!document.getElementById(errorID)) {
		// create errormessage and insert before submit button
			var em=document.createElement('div');
			em.id=errorID;
			var newp=document.createElement('p');
			newp.appendChild(document.createTextNode(errorMsg))
			// clone and insert the error image
			newp.appendChild(errorIndicator.cloneNode(true));
			em.appendChild(newp);
			// find the submit button
			for(var i=0;i<of.getElementsByTagName('input').length;i++) {
				if(/submit/i.test(of.getElementsByTagName('input')[i].type)) {
					var sb=of.getElementsByTagName('input')[i];
					break;
				}
			}
			if(sb) {
				sb.parentNode.insertBefore(em,sb);
			}
		}
	}
	function cf_isEmailAddr(str) {
		return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
	}
}



// Multiple Onload Functions to be called
function multipleOnload() {
	externalLinks();
	inputFocus();
	createContent();
}
window.onload = multipleOnload;

