/******************************************
* AWD JS Library
* Set: Standard Funtions
* Author: Matthew Camp
* Created: 15th February 2009
* Last Updated: 22nd February
* ________________________________________________________________________________
* MAIN FUNCTION LIST, call each function within the HTML using the class attribute of the element
* ________________________________________________________________________________
* JavaScript:Alert - [* tags,body(preferred)] displays a page alert and reads the value within the 'title' attribute
* JavaScript:Confirm - [* tags with 'rel' attribute] displays a confirming to verify execution, reads the value within the 'rel' attribute
* JavaScript:Prompt - [* tags with 'rel' attribute] displays a prompt to request input, reads the value within the 'rel' attribute
* JavaScript:SetFocus - [input-text only] sets focus to the called text field to set the initial tab index
* JavaScript:Print - [* tags] Prints the body of the web page
* JavaScript:ExternalLink - [a only] opens a new browser window/tab for links pointing a an external website
/******************************************
* INITIATE GLOBAL VARIABLES AND ADD EVENT LISTENERS
*******************************************/
// add events to html
AddOnloadEvent(Alert);
AddOnloadEvent(Confirm);
AddOnloadEvent(Prompt);
AddOnloadEvent(SetFocus);
AddOnloadEvent(Print);
AddOnloadEvent(ExternalLink);
AddOnloadEvent(DetectIE6);

/******************************************
* BASIC FUNCTIONS FOR ALL WEB PAGES
*******************************************/
// display a standard alert message
function Alert(){
	var elements = document.getElementsByTagName("*");
	for(var i = 0; i < elements.length; i ++){
		if(elements[i].className.match("JavaScript:Alert")){
			alert(elements[i].title);
			elements[i].removeAttribute("title");
		}
	}
}
// displays a confirmation message
function Confirm(){
	var elements = document.getElementsByTagName("*");
	for(var i = 0; i < elements.length; i ++){
		if(elements[i].className.match("JavaScript:Confirm")){
			elements[i].onclick = function(){
				return confirm(this.rel);
			}
		}
	}
}
// displays a prompt
function Prompt(){
	var elements = document.getElementsByTagName("*");
	for(var i = 0; i < elements.length; i ++){
		if(elements[i].className.match("JavaScript:Prompt")){
			var result = prompt(elements[i].rel, "");
			if(result){
				return result;
			}
			else{
				return false;
			}
		}
	}
}
// sets a text field into focus on page load
function SetFocus(){
	var elements = document.getElementsByTagName("input");
	for(var i = 0; i < elements.length; i ++){
		if(elements[i].className.match("JavaScript:SetFocus")){
			elements[i].focus();
			break;
		}
	}
}
// prints the web page
function Print(){
	var elements = document.getElementsByTagName("*");
	for(var i = 0; i < elements.length; i ++){
		if(elements[i].className.match("JavaScript:Print")){
			elements[i].onclick = function(){
				window.print();
				return false;
			}
		}
	}
}
// opens external websites in a new window
function ExternalLink() {
	if(!document.getElementsByTagName){ 
		return false;
	}
	var links = document.getElementsByTagName("a");
	for(var i = 0; i < links.length; i ++){
		if(links[i].className.match("JavaScript:ExternalLink")){
			links[i].onclick = function(){
				window.open(this.href);
				return false;
			}
		}
	}
}
// displays message for ie6 browsers
function DetectIE6(){
	var bodyElement = document.getElementsByTagName("body")[0];
	if(bodyElement.className.match("JavaScript:DetectIE6")){
		isIE6 = navigator.userAgent.toLowerCase().match('msie 6');
		if(isIE6){
			var message = "Unfortunately you are currently running an absolute browser that does not support modern functionality. Please upgrade your browser by installing the latest version from the following recommendations \n\n";
			message += "    Safari (highly recommended)\n";
			message += "    Firefox (highly recommended)\n";
			message += "    Opera (highly recommended)\n";
			message += "    Chrome\n";
			message += "    Internet Explorer\n\n";
			message += "Would you like to upgrade your browser now!!";
			var upgrade = confirm(message);
			if(upgrade){
				var upgradeMessage = "Please type the name of the browser you would like to install:\n";
				upgradeMessage += "Please enter either: safari, firefox, opera, chrome, internet explorer";
				var result = prompt(upgradeMessage, "safari");
				switch(result){
					case "safari":
						return location.href = "http://www.apple.com/safari/";
						break;
					case"firefox":
						return location.href = "http://www.mozilla.com/firefox/";
						break;
					case"opera":
						return location.href = "http://www.opera.com/";
						break;
					case"chrome":
						return location.href = "http://www.google.com/chrome/";
						break;
					case"internet explorer":
						return location.href = "http://www.microsoft.com/windows/Internet-explorer/download-ie.aspx";
						break;
					default:
						return location.href = "http://www.apple.com/safari/";
						break;
				}
			}
		}
	}
}
