// constants to define the title of the alert and button text.
var ALERT_TITLE = "";
var ALERT_TEXT = "";
var ALERT_ICON = "";
var ALERT_TYPE = "default";
var ALERT_FUNCTIONS = Array();
var ALERT_BUTTONS = Array();
var ButtonsFound = 0;
var xmlHttp = null;
var url = "";
// over-ride the alert method only if this a newer browser. Older browser will see standard alerts

function buildAlert(aTitle,aText,aType,aFunctions,aButtons,aEndText) {
	// resest the button array, button counter, and functions array
	ALERT_BUTTONS = null;
	ALERT_BUTTONS = Array();
	ALERT_FUNCTIONS = Array();
	ButtonsFound = 0;
	
	// split the alert message
	//var e = alertString.split("][")
	var output = "";
	var button = Array();
	
	ALERT_TITLE = aTitle;
	ALERT_TEXT = aText;
	ALERT_TYPE = aType;
	ALERT_ICON = "/images/customAlert/" + aType + ".jpg";
	
	ALERT_FUNCTIONS = aFunctions;
	
	for (var i = 0; i < (aButtons.length); i++) { // Get The Buttons
		// Break apart the button
		generateButton(aButtons[i][0],aButtons[i][1],aButtons[i][2]);
	}
	ALERT_TEXT = ALERT_TEXT + aEndText
	createCustomAlert();
}

function generateButton(type,text,linkto) {
	ALERT_BUTTONS[ButtonsFound] = new Array();
	ALERT_BUTTONS[ButtonsFound][0] = type + "Button";
	ALERT_BUTTONS[ButtonsFound][1] = text;
	ALERT_BUTTONS[ButtonsFound][2] = linkto;
	ButtonsFound++;
}

function createCustomAlert(alertString) {
	
	//if the alert box already exists, remove it and continue.
	if(document.getElementById("customAlertBox")) removeCustomAlert();

	// create the alert box div as a child of the BODY element
	rootObj = document.getElementsByTagName("body")[0].appendChild(document.createElement("div"));
	rootObj.id = "customAlertBox";
	
	// create the DIV that will be the alert 
	alertObj = rootObj.appendChild(document.createElement("div"));
	alertObj.id = "customAlertContainer";

	// create top section - title and icon
	alertTop = alertObj.appendChild(document.createElement("div"));
	alertTop.id = "customAlertTop";

	// create title bar - and add it to the top section object we just made
	alertTitle = alertTop.appendChild(document.createElement("div"));
	alertTitle.className = "customAlertTitle " + ALERT_TYPE + "Alert";
	alertTitle.appendChild(document.createTextNode(ALERT_TITLE));
	
	// create alert icon - if required - and add it to the top section object we just made
	if (ALERT_TYPE !== "default") {
		alertType = alertTop.appendChild(document.createElement("div"));
		alertType.id = "customAlertIcon";
		alertIcon = alertType.appendChild(document.createElement("img"));
		alertIcon.src = ALERT_ICON;
		alertIcon.border = 0;
		if (ALERT_TYPE == "cancel") {
			alertIcon.title = "Close";
			alertIcon.alt = "Close";
			alertIcon.setAttribute("onclick","removeCustomAlert()");
			alertIcon.style.cursor = "pointer";
			alertIcon.parentNode.innerHTML = alertIcon.parentNode.innerHTML;
		} else {
			alertIcon.alt = "";
			alertIcon.title = "";
		}
	}

	// create main text area - and add it to the alert object
	alertContent = alertObj.appendChild(document.createElement("div"));
	alertContent.id = "customAlertContent";
	//Split the alert text on <br /> and create new br elements
	parsedContent = ALERT_TEXT.split("<br />")
	var parsedLinks = "";
	for (i = 0; i < parsedContent.length; i++) {
			/*alertContent.appendChild(document.createTextNode(parsedContent[i]));
			alertContent.appendChild(document.createElement("br"));
		}*/
		
		// Parse any links that may be in the parsed content text
		parsedLinks = parsedContent[i];
		parsedLinks = parsedLinks.split("<a>");
		
		if (parsedLinks.length > 0) {// 1 or more links were found, parse the link and text
			_linkHref = "";
			_linkText = "";
			alertContent.appendChild(document.createTextNode(parsedLinks[0])); // add parsed text, then add the link found
			linkElements = parsedLinks[1];
			//alert(linkElements);
			if (linkElements != null) {
				linkElements = linkElements.split("<link>")
				if (linkElements != null) {
					if (linkElements.length > 0) {
						linkElements = linkElements[1].split("</link>")
						_linkHref = linkElements[0];
						linkElements = linkElements[1].split("</text>")
						_linkText = linkElements[0].replace("<text>","")
						postText = linkElements[1].replace("</a>","") //Any text after the link
						newLink = alertContent.appendChild(document.createElement("a")) // create a new link element and get a reference to it
						newLink.href = _linkHref;
						newLink.innerHTML = _linkText;
					}
				}
			}
		}
		alertContent.appendChild(document.createElement("br"));			
	}
	errorMessageHolder = alertContent.appendChild(document.createElement("span"));	
	errorMessageHolder.id = "errorMessageHolder";
	
	/* run any passed javascript functions 
	This works by evaluating the text sent - being the function name + ()
	This sees it as a function and runs it 
	Evolutions of this are to include multi dimenions arrays passed like the buttons array,
	this can then pass variables to the function */
	if (ALERT_FUNCTIONS.length > 0) { //Functions were passed;
		for (i = 0; i < ALERT_FUNCTIONS.length; i++) {
			eval(ALERT_FUNCTIONS[i]);
		}
	}
	/* end functions */
	
	// create button area - and add it to the alert object
	alertButtons = alertObj.appendChild(document.createElement("div"));
	alertButtons.id = "customAlertButtons";
	// loop through button array and add them to the alert object
	for (i = (ALERT_BUTTONS.length-1); i >= 0; i--) { // Read the buttons in backwards due to floating right
		// create new div for each button and add it to the alert button object
		newButton = alertButtons.appendChild(document.createElement("a"))
		newButton.className = ALERT_BUTTONS[i][0];
		buttonText = newButton.appendChild(document.createElement("a"))
		buttonText.className = ALERT_BUTTONS[i][0] + "Text";
		buttonText.appendChild(document.createTextNode(ALERT_BUTTONS[i][1]))
		if (ALERT_BUTTONS[i][2] == "#") {
			buttonText.href = "javascript:removeCustomAlert();"
		} else {
			buttonText.href = "javascript:window.location.href='"+ALERT_BUTTONS[i][2]+"'"
		}
	}	
	
	resizeAlert(rootObj);
	
	var e = document.getElementsByTagName("select") // hide all drop downs so they don't show over the top of the alert
	for (var i = 0; i < e.length; i++) {
		e[i].style.visibility = "hidden";	
	}
	e = null;
		
	rootObj = null;
	alertObj = null;
	alertTop = null;
	alertIcon = null;
	alertType = null;
	alertButtons = null;
	parsedContent = null;
	alertContent = null;
	newButton = null;
	buttonText = null;
	
	ALERT_TITLE = "";
	ALERT_TEXT = "";
	ALERT_ICON = "";
	ALERT_TYPE = "default";
	ALERT_FUNCTIONS = null;
	ALERT_BUTTONS = null;
	ButtonsFound = 0;

	
	document.getElementById("pageContainer").className = "pageContainer dimDown";
}

function resizeAlert(rootObj) {
	if(document.getElementById("customAlertShadowBox")) removeCustomAlertShadow();
	rootObj.style.left = ((document.documentElement.clientWidth || document.innerWidth || 1)/2)-(rootObj.offsetWidth/2) + "px";
	rootObj.style.top = 120;//((document.documentElement.clientHeight || document.innerHeight || 1)/2)-(rootObj.offsetHeight/2) + "px";
	alertLeft = rootObj.offsetLeft;
	alertTop = 120;// rootObj.offsetTop;
	alertHeight = rootObj.offsetHeight;
		
	// create the shadow div as a child of the BODY element
	shadowObj = document.getElementsByTagName("body")[0].appendChild(document.createElement("div"));
	shadowObj.id = "customAlertShadowBox";
	shadowObj.style.left = alertLeft-17 + "px";
	shadowObj.style.top = alertTop-15 + "px";
	
	// create the DIV that will be the shadow 
	shadowContainerObj = shadowObj.appendChild(document.createElement("div"));
	shadowContainerObj.id = "customAlertShadowContainer";
	shadowContainerObj.style.height = (alertHeight + 30) + "px";

		// create top section
		shadowEl = shadowContainerObj.appendChild(document.createElement("div"));
		shadowEl.id = "customAlertShadowTop";
		
		// create middle section
		shadowEl = shadowContainerObj.appendChild(document.createElement("div"));
		shadowEl.id = "customAlertShadowBody";
		shadowEl.style.height = (alertHeight - 0) + "px";
		
		// create bottom section
		shadowEl = shadowContainerObj.appendChild(document.createElement("div"));
		shadowEl.id = "customAlertShadowBottom";
		//shadowEl.style.height = 400 + "px";	
		
	//Clear objects to free memory	
	shadowEl = null;
	shadowContainerObj = null;
	shadowObj = null;
	rootObj = null;
}


function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		if (xmlHttp.status == 200) {
			document.getElementById("customAlertContent").innerHTML+=xmlHttp.responseText;
			resizeAlert(document.getElementById("customAlertBox"));
			xmlHttp = null;
		}
		//xmlHttp = null;
	}
} 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
	objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

function getPage(thePage) {
	url = thePage + "&rnd="+Math.random();
	xmlHttp = GetXmlHttpObject()
	xmlHttp.onreadystatechange=stateChanged
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

// removes the custom alert from the DOM
function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("customAlertBox"));
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("customAlertShadowBox"));
	document.getElementById("pageContainer").className = "pageContainer";
	
	var e = document.getElementsByTagName("select") // show all drop downs
	for (var i = 0; i < e.length; i++) {
		e[i].style.visibility = "visible";	
	}
	e = null;
}

function removeCustomAlertShadow() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("customAlertShadowBox"));	
}