window.onload = init;

window.onerror = function() {
    window.status = 'Error on page.';
}

function init() {
	// fixEolas();
	msgStatusAllLinks();
	startlist();
}

// ----- fixEolas() -----
// Code Procedure:
// Step 1) Run through all the object tags in the HTML source code and retriev their outerHTML values
// Step 2) IE does not include any of the object's param tags in its outerHTML (or innerHTML), so extract them separately and store in strParamAttributes  
// Step 3) Insert strParamAttributes into the outerHTML code (i.e. splice it in)
// Step 4) Replace the old code on the page with the newly generated HTML.

function fixEolas() {
	// Ensure that only IE actions this code.
	if (document.all) { 
		
		// Declare and initialise variables.
		var arrObjects = document.getElementsByTagName("object");
		var objCurrentObj;
		var strCurrentObjHtml = "";
		var strParamAttributes = "";
		var objCurrentObjChildNode;
		var strObjectTag = "";
		var strNewObject = "";
	
		// Step 1) Run through all the object tags in the HTML source code and retriev their outerHTML values
		for (var i = 0; i < arrObjects.length; i++){
			
			// Initialise variables
			strParamAttributes = "";	
				    
		    // Get the outerHTML of the current object.
			objCurrentObj = arrObjects[i]; 
			strCurrentObjHtml = objCurrentObj.outerHTML; 

			// Step 2) IE does not include any of the object's param tags in its outerHTML (or innerHTML), so extract them separately and store in strParamAttributes  
			for (var j = 0; j < objCurrentObj.childNodes.length; j++) { 
				
				// Create an object containing all child nodes for the current object.
				objCurrentObjChildNode = objCurrentObj.childNodes[j]; 
				
				// Access the PARAM nodes for the current object and extract their outerHTML, appending their value to the string strParamAttributes.
				if (objCurrentObjChildNode.tagName.toUpperCase() == "PARAM"){ 
					strParamAttributes += objCurrentObjChildNode.outerHTML; 
				} 
			}

			// Step 3) Insert strParamAttributes into the outerHTML code (i.e. splice it in)
			strObjectTag = strCurrentObjHtml.split(">")[0] + ">"; 
			strNewObject = strObjectTag + strParamAttributes + objCurrentObj.innerHTML + " </OBJECT>";
			
			// Step 4) Replace the old code on the page with the newly generated HTML.
			objCurrentObj.outerHTML = strNewObject;
		}
	}
}

// Dropdown Submenu
// submenus function in modern browsers via CSS only;
// however, IE5, IE5.5, IE6 require this Javascript in order to function

function startlist() {
            if (document.all&&document.getElementById) {
                        if (document.getElementById("nav")) {
                                    navRoot = document.getElementById("nav");
                                    for (i=0; i<navRoot.childNodes.length; i++) {
                                                node = navRoot.childNodes[i];
                                                if (node.nodeName=="LI") {
                                                            node.onmouseover=function() {
                                                                        this.className+=" over";
                                                            }
                                                            node.onmouseout=function() {
                                                                        this.className=this.className.replace(" over", "");
                                                            }
                                                }
                                    }
                       }
            }
}

function checkSearchForm(oForm) {
	if(oForm.txtSearchKeyword.value.length < 3) {
		alert('Please enter a search term at least three (3) characters in length.');
		oForm.txtSearchKeyword.focus();
		return false;
	} else {
		oForm.submit();
	}
}

function attachFocus() {

	if (this.className.indexOf(" focus")<0) {
		this.className+=" focus";
	}

	if (this.tempOnFocus) {
		// call existing onFocus event.
		this.tempOnFocus();
	}

}

function attachBlur() {

	if (this.className.indexOf(" focus")>0) {
		this.className = this.className.substring(0,this.className.indexOf(" focus"));
	} else {
		this.className.replace(" focus","");
	}

	if (this.tempOnBlur) {
		// call existing onBlur event.
		this.tempOnBlur();
	}

}

function msgStatus() {
	if (this.tagName=="INPUT") {
		switch(this.type) {
			case 'image': // form button is an <input type="image"> (graphic);
				if (this.getAttribute('src').indexOf('_a.')!=-1) {
					this.src = this.getAttribute('src').replace('_a.','_b.');
				}
				if (this.getAttribute('src').indexOf('-a.')!=-1) {
					this.src = this.getAttribute('src').replace('-a.','-b.');
				}

				txtStatus=this.getAttribute('alt');

				break;
			case 'button': // form button is an <input type="button"> (webtext);
			case 'submit': // form button is an <input type="submit"> (webtext);
			case 'reset': // form button is an <input type="reset"> (webtext);
			case 'file': // form button is an <input type="file"> (webtext);
				//this.className="bnover";
				txtStatus=this.getAttribute('value');
				break;
			default:
				break;
		}
	} else {
		if (this.name) {
			for (i=0;i<document.getElementsByName(this.name).length;i++) {
				if (document.getElementsByName(this.name)[i]!=this) {
					if (document.getElementsByName(this.name)[i].tagName=='A') {
						if (document.getElementsByName(this.name)[i].getElementsByTagName('img').length!=0) {
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('_a.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('_a.','_b.');
							}
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('-a.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('-a.','-b.');
							}
						} else {
							document.getElementsByName(this.name)[i].className="rollover";
						}
					}
				}
			}
		}
		if (this.innerText) {
			txtStatus=this.innerText;
		} else {
			if (this.text) txtStatus=this.text;
		}
		if (this.getElementsByTagName('img').length!=0) {
			txtStatus=this.getElementsByTagName('img')[0].getAttribute('alt');
			if (this.getElementsByTagName('img')[0].src.indexOf('_a.')!=-1) {
				this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('_a.','_b.');
			}
			if (this.getElementsByTagName('img')[0].src.indexOf('-a.')!=-1) {
				this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('-a.','-b.');
			}
		} else {
			if (this.tagName=="AREA") {
				txtStatus=this.getAttribute('alt');
			}
		}
	}
	// txtStatus=toTitleCase(txtStatus);
	window.status=toTitleCase(txtStatus); return true;
}

function msgStatusOff() {
	txtStatus=window.defaultStatus;
	if (this.tagName=="INPUT") {
		switch(this.type) {
			case 'image': // form button is an <input type="image"> (graphic);
				if (this.getAttribute('src').indexOf('_b.')!=-1) {
					this.src = this.getAttribute('src').replace('_b.','_a.');
				}
				if (this.getAttribute('src').indexOf('-b.')!=-1) {
					this.src = this.getAttribute('src').replace('-b.','-a.');
				}
				break;
			case 'button': // form button is an <input type="button"> (webtext);
			case 'submit': // form button is an <input type="submit"> (webtext);
			case 'reset': // form button is an <input type="reset"> (webtext);
				this.className+=" bnout";
				break;
			default:
				break;
		}
	} else {
		if (this.name) {
			for (i=0;i<document.getElementsByName(this.name).length;i++) {
				if (document.getElementsByName(this.name)[i]!=this) {
					if (document.getElementsByName(this.name)[i].tagName=='A') {
						if (document.getElementsByName(this.name)[i].getElementsByTagName('img').length!=0) {
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('_b.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('_b.','_a.');
							}
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('-b.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('-b.','-a.');
							}
						} else {
							document.getElementsByName(this.name)[i].className="";
						}
					}
				}
			}
		}
	}
	if (this.getElementsByTagName('img').length!=0 && this.getElementsByTagName('img')[0].src.indexOf('_b.')!=-1 && this.getElementsByTagName('img')[0].name.indexOf('img_')==-1) {
		this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('_b.','_a.');
	}
	if (this.getElementsByTagName('img').length!=0 && this.getElementsByTagName('img')[0].src.indexOf('-b.')!=-1 && this.getElementsByTagName('img')[0].name.indexOf('img_')==-1) {
		this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('-b.','-a.');
	}
	window.status=txtStatus; return true;
}

function bodyResize() {
	window.location.href = window.location.href;
}

function msgStatusAllLinks() {
	for(i=0; i<document.links.length; i++) {
		if (!document.links[i].onmouseover) {
			document.links[i].onmouseover=msgStatus;
			document.links[i].onmouseout=msgStatusOff;
		}
	}
	for(i=0; i<document.getElementsByTagName('input').length; i++) {
		switch(document.getElementsByTagName('input')[i].type) {
			case 'image': // form button is an <input type="image"> (graphic);
			case 'button': // form button is an <input type="button"> (webtext);
			case 'submit': // form button is an <input type="submit"> (webtext);
			case 'reset': // form button is an <input type="reset"> (webtext);
				document.getElementsByTagName('input')[i].onmouseover=msgStatus;
				document.getElementsByTagName('input')[i].onmouseout=msgStatusOff;
				break;
			case 'text':
			case 'password':

				// Add element's existing blur/focus event (if it has one) before adding a new blur/focus event 'attachBlur/Focus'
				// This ensures that any existing onBlur/onFocus event the element has is still executed, as well as the 'attachBlur/Focus' event.
				// Check if IE or Netscape and use appropriate syntax accordingly.
				if (navigator.appName == 'Microsoft Internet Explorer') {

					if (document.getElementsByTagName('input')[i].onblur) {
						// Assign existing onBlur to a temp event 'tempOnBlur' - Later executed in attachBlur function.
						document.getElementsByTagName('input')[i].tempOnBlur = document.getElementsByTagName('input')[i].onblur;
					}

					if (document.getElementsByTagName('input')[i].onfocus) {
						// Assign existing onBlur to a temp event 'tempOnFocus' - Later executed in attachFocus function.
						document.getElementsByTagName('input')[i].tempOnFocus = document.getElementsByTagName('input')[i].onfocus;
					}

					// attach new event. This will cause the input field colour to change.
					document.getElementsByTagName('input')[i].onfocus=attachFocus;
					document.getElementsByTagName('input')[i].onblur=attachBlur;

				}
				else {
				
					// attach existing blur/focus event.
					document.getElementsByTagName('input')[i].addEventListener('focus',attachFocus,false);
					document.getElementsByTagName('input')[i].addEventListener('blur',attachBlur,false);

				}		
				break;
			default:
				break;
		}
	}
	for(i=0; i<document.getElementsByTagName('textarea').length; i++) {
		document.getElementsByTagName('textarea')[i].onfocus=attachFocus;
		document.getElementsByTagName('textarea')[i].onblur=attachBlur;
	}
}

// toTitleCase
function toTitleCase(strString) {
	// strString = fnTrim(strString);
	var firstSpace = strString.indexOf(' ');
	var currentWord;
	strString = strString.charAt(0).toUpperCase()+strString.substring(1,strString.length);
	if (firstSpace > -1) strString=strString.substring(0,firstSpace+1) + toTitleCase(strString.substring(firstSpace+1,strString.length));
	return strString;
}