// Automatically attach a listener to the window onload, to convert the trees
//addEvent(window,"load",convertTrees);

// Utility function to add an event listener
function addEvent2(o,e,f){
	if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}

// utility function to set a global variable if it is not already set
function setDefault(name,val) {
	if (typeof(window[name])=="undefined" || window[name]==null) {
		window[name]=val;
	}
}

// Search the document for UL elements with the correct CLASS name, then process them
function convertTrees() {
	setDefault("treeClass","mktree");
	setDefault("nodeClosedClass","liClosed");
	setDefault("nodeOpenClass","liOpen");
	setDefault("nodeBulletClass","liBullet");
	setDefault("nodeLinkClass","bullet");
	setDefault("preProcessTrees",true);
	if (preProcessTrees) {
		if (!document.createElement) { return; } // Without createElement, we can't do anything
		uls = document.getElementsByTagName("ul");
		for (var uli=0;uli<uls.length;uli++) {
			var ul=uls[uli];
			if (ul.nodeName=="UL" && ul.className==treeClass) {
				processList(ul);
			}
		}
	}
}

// Process a UL tag and all its children, to convert to a tree
function processList(ul) {
	if (!ul.childNodes || ul.childNodes.length==0) { return; }
	// Iterate LIs
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
		var item = ul.childNodes[itemi];
		if (item.nodeName == "LI") {
			// Iterate things in this LI
			var subLists = false;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL") {
					subLists = true;
					processList(sitem);
				}
			}
			var s= document.createElement("SPAN");
			var t= '\u00A0'; // &nbsp;
			s.className = nodeLinkClass;
			if (subLists) {
				// This LI has UL's in it, so it's a +/- node
				if (item.className==null || item.className=="") {
					item.className = nodeClosedClass;
				}
				// If it's just text, make the text work as the link also
				if (item.firstChild.nodeName=="#text") {
					t = t+item.firstChild.nodeValue;
					item.removeChild(item.firstChild);
				}
				s.onclick = function () {
					if (this.parentNode.className == nodeClosedClass)	{									
						var ul = document.getElementById('tree1');
						expandCollapseList(ul,nodeClosedClass);
					}
					this.parentNode.className = (this.parentNode.className==nodeOpenClass) ? nodeClosedClass : nodeOpenClass;
					return false;
				}
			}
			else {
				// No sublists, so it's just a bullet node
				item.className = nodeBulletClass;
				s.onclick = function () { return false; }
			}
			s.appendChild(document.createTextNode(t));
			item.insertBefore(s,item.firstChild);
		}
	}
}

function expandCollapseList(ul,cName,itemId) {
	if (!ul.childNodes || ul.childNodes.length==0) { return false; }
	// Iterate LIs
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
		var item = ul.childNodes[itemi];
		if (itemId!=null && item.id==itemId) { return true; }
		if (item.nodeName == "LI") {
			// Iterate things in this LI
			var subLists = false;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL") {
					subLists = true;
					var ret = expandCollapseList(sitem,cName,itemId);
					if (itemId!=null && ret) {
						item.className=cName;
						return true;
					}
				}
			}
			if (subLists && itemId==null) {
				item.className = cName;
			}
		}
	}
}

function validateConForm(theForm) {
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	var missing = "";
	
	if (theForm.fname.value == "") missing += "First Name\n";
	if (theForm.lname.value == "") missing += "Last Name\n";
	if (theForm.title.value == "") missing += "Title\n";
	if (theForm.address.value == "") missing += "Address\n";
	if (theForm.phone.value == "") missing += "Phone\n";
	if (theForm.email.value == "") missing += "Email\n";
	if (theForm.enquiry.value == "") missing += "Enquiry\n";

	if (missing != "") {
	 	alert("You missed the following essential elements\n\n"
	 	+ missing
	 	+ "\nPlease complete and resubmit");
		return false;
	}
	else if (!theForm.email.value.match(emailExp))	{
	 	alert("Please enter a valid email address");
		return false;
	}
	else	return true;
}

function validateBusiForm(theForm) {
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	var missing = "";
	
	if (theForm.name.value == "") missing += "Name\n";
	if (theForm.email.value == "") missing += "Email Address\n";
	if (theForm.enquiry.value == "") missing += "Comment or Instructions\n";

	if (missing != "") {
	 	alert("You missed the following essential elements\n\n"
	 	+ missing
	 	+ "\nPlease complete and resubmit");
		return false;
	}
	else if (!theForm.email.value.match(emailExp))	{
	 	alert("Please enter a valid email address");
		return false;
	}
	else	return true;
}



function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function addEvent( obj, type, fn ) {
	if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	} else {
		obj.addEventListener( type, fn, false );
	}
}

function validatePresent() {
	for(i=0;i<arguments.length;i++) {
		if( ($(arguments[i]).getAttribute('type')=='text') && ($(arguments[i]).value == '') ) {
			alert('Please ensure you have filled in all required fields');
			return false;
		}
	}
	return true;
}

function validateMatch(f1,f2) {
	if( $(f1).value != $(f2).value ) {
		alert('Confirmation fields do not match');
		return false;
	}
	return true;
}
function validateEmail(field) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test($(field).value) == false) {
		alert('Not a valid email address');
		return false;
	}
	return true;
}

function cancel(event) {
	if (event.preventDefault) {
		event.preventDefault();
		event.stopPropagation();
	} else {
		event.returnValue = false;
		event.cancelBubble = true;
	}
	return false;
}

Array.prototype.in_array = function ( obj ) {
	var len = this.length;
	for ( var x = 0 ; x <= len ; x++ ) {
		if ( this[x] == obj ) return true;
	}
	return false;
}

function selectAll(excludes) {
	var inputs = document.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++) {
		if (inputs[i].type == 'checkbox' && !excludes.in_array(inputs[i].id)) {
			inputs[i].checked = "checked";
		}
	}
	var other = '';
	if( $('other_requirements').value == '' ) {
		other = $('other_requirements').value;
	} else {
		other = $('other_requirements').value + "\r\n";
	}
	$('other_requirements').value = other + "General Enquiry";
}

function deselectAll() {
	var inputs = document.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++) {
		inputs[i].checked = "";
	}
}


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") == "v_win") {
			anchor.onclick = function() { 
				if( screen.width > 800 ) {
					width = 910;
					height = 680;
				} else {
					width = 700;
					height = 500;
				}
				window.open(this.href,'_blank',"height="+height+",width="+width+",status=no,toolbar=no,menubar=yes,scrollbars=no,resizable=yes"); return false; };
		}
		if( anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.setAttribute('target','_blank');
		}
	}
}

window.onload = externalLinks;
window.onload = convertTrees;

