var STYLE_SHEETS = new Array();

function hi()
{
	alert("Hello World")
}

function init() { }

function NavigateTo(url,targ)
{
	parent.central.location = url
	FormatBottomMenu(targ)
}

function FormatBottomMenu(targ)
{
	// 3936 - The bottom frame is called "footer", not "bottom".
	//var num_links=parent.bottom.document.links.length
	var num_links=parent.footer.document.links.length
	var target_url="javascript:Navigate('"+targ+"')"
	for (count=0; count<num_links;count++)
	{
		//if (parent.bottom.document.links[count].href == target_url)
		if (parent.footer.document.links[count].href == target_url)
		{
				document.links[count].style.color="red"
		}
		//else{ parent.bottom.document.links[count].style.color="#204080"}
		else
		{
			parent.footer.document.links[count].style.color="#204080"
		}
	}
	// 3936
}

function NavigatePublic(url, targ)
{
	parent.central.location = url
	FormatPublicLinks(url,targ)
}

function FormatPublicLinks(url,newtarg)
{
	var num_links=document.links.length
	var target_url="javascript:NavigatePublic('"+url+"','"+newtarg+"')"
	for (count=0; count<num_links;count++)
	{
		if (document.links[count].href == target_url)
		{
			document.links[count].style.color="red"
		}
		else{ document.links[count].style.color="#204080"}
	}
}

// 447
function popup(target)
{
	var newWin = window.open(target,'','width=800, height=600, scrollbars=1,resizable=1,status=1')
	newWin.moveTo(10,10)
}
// 447

// 457
var submitCount = 0;
function checkSubmit() {
	if(submitCount == 0) {
		submitCount++;
		return true;
	}
	return false;
}
// 457

// TEAM MANAGEMENT - 2
function setValueAndClose(object, value, form) {
	object.value = value;
	this.window.close();
	if(form) {
		return form.submit();
	}
	return true;
}
// TEAM MANAGEMENT - 2

function emailTeamWindow(id,treg_id,session_id,email,first_name,last_name,noStatus,eoSent) {
	var win = window.open('email_team.cgi?id='+id+'&treg_id='+treg_id+'&session_id='+session_id+'&email='+email+'&first_name='+first_name+'&last_name='+last_name+'&noStatus=1&eoSent=0', '', 'location=0,width=400,height=500,scrollbars,resizable')
}

// 2108 - missing close and refresh function
function close_and_refresh() {
	parent.opener.location.href = parent.opener.location;
	window.close();
}
// 2108

// 2659 - additional methods required for printing sections of
// the "Pay by Cheque" screen
// GetContent grabs the content in the div identified by "id",
// removes the "print_invoice" and "print_window" divs then returns
// the rest
function GetContent(id) {
	if(!id || !document.getElementById(id)) {
		return "";
	}
	var tmp = document.getElementById(id);
	var targ = tmp.cloneNode(true);
	RemoveFirstChild(targ, "print_invoice");
	RemoveFirstChild(targ, "print_window");
	var content = targ.innerHTML;
	//var print = content.print_window;
	return content;
}

// Removes all divs matching "^id" from mainDiv
function RemoveDivs(mainDiv, id) {
	if(!id) {
		return "";
	}
	var i = 0;
	while(RemoveFirstChild(mainDiv, id)) { i++; }
	return i;
}

// Remove the first child whose id matches "id". This doesn't
// guarantee that all instances of "id" will be removed
function RemoveFirstChild(node, id) {
	if(!node || !node.childNodes) {
		return null;
	}
	var tmpNode = null;
	var idRegex = "^"+id;
	for(var i = 0; i < node.childNodes.length; i++) {
		if(node.childNodes[i].id && 
			node.childNodes[i].id.match(idRegex)) {
			return node.removeChild(node.childNodes[i]);
		} else if(node.childNodes[i].childNodes) {
			tmpNode = RemoveFirstChild(node.childNodes[i], id);
			if(tmpNode) {
				return node;
			}	
		}
	}
	return null;
}

// Print the "id" content in a new window.
function PrintWindow(id) {
	var content = GetContent(id);

	// write into the popup:
	var struct = new Array();
	struct['printIcon'] = PrintIcon();	// a print icon
	struct['closeWindowIcon'] = CloseWindowIcon(); 	// an icon to close the window
	struct['css'] = PrintStyleSheets();
	struct['body'] = content;
	// 2765 - init() is not defined anywhere in the popup window, so
	// calling it gives an Javascript error.
	//struct['bodyTag'] = 'onLoad=\"init();\"';
	struct['bodyTag'] = '';

	var html = PrintablePage(struct);
	var win = WriteToWindow(html);
	win.print();
}

// Assemble the printable page by using all of the "struct" fields
function PrintablePage(struct){
	var html = "<head><link rel=stylesheet type=text/css href=css/ie_styles1.css></head>"
	html += struct['css'];
	html += "<body "+struct['bodyTag']+" >"
	html += "<table width=100%><tr><td align=left>"+struct['printIcon']+"</td><td align=right>"+struct['closeWindowIcon']+"</td></tr></table>"
	html += struct['body']
	html += "</body>"
	return html
}

// Write the provided HTML to a new window
function WriteToWindow(html){
// 3728
	var Win = window.open('','','height=500, width=600, titlebar=0, status=1, menubar=0,scrollbars=1,resizable=1');
	Win.document.write(html)
	Win.document.close()
	return Win;
}

// Generic print icon
function PrintIcon() {
	return "<a href=javascript:window.print()>PRINT</a>"
}

// Generic CloseWindow icon
function CloseWindowIcon() {
	return "<a href=javascript:window.close()>CLOSE</a>"
}

// Add a style sheet to the list of printable style sheets.
// This is mostly used for keeping track of style sheets that need
// to be linked when printing a new popup window
function AddStyleSheet(sheetPath) {
        if(sheetPath && sheetPath.length) {
                STYLE_SHEETS.push(sheetPath);
        }
        return STYLE_SHEETS.length;
}

// Print out a list of links to all of the stored style sheets
function PrintStyleSheets() {
        var out = '';
        if(STYLE_SHEETS.length) {
                for(var i = 0; i < STYLE_SHEETS.length; i++) {
                        out += "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + STYLE_SHEETS[i] + "\"></link>\n";

                }
        } else {
                out = "<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/pc.css\"></link>\n";
        }
        return out;
}
