var retrievedMonths = [];
function mygetdatefunc(day) {
	var mth = day.getFullYear()+''+(day.getMonth()+1);
	if (retrievedMonths.search(mth) == -1) {
		retrievedMonths[retrievedMonths.length] = mth;
		loadXMLDoc(xmldocurl+(xmldocurl.search(/\?/) == -1 ? '?' : '&')+'companyid='+companyid+'&year='+day.getFullYear()+'&month='+(day.getMonth()+1), updateCalendarLinks, this);
	}
	return this.originalGetDateFunc(day);
}

var req = null;
var reqObj = null;
function updateCalendarLinks() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var dates = req.responseXML.getElementsByTagName('date');
			for (var i = 0; i < dates.length; i++) {
				if (dates[i].firstChild) {
					reqObj.setDateFunc(dates[i].firstChild.nodeValue, loadCal);
				}
			}
			reqObj.refresh();
		}
	}
}

function loadXMLDoc(url, func, obj) {
	req = false;
	if (window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch (e) {
			req = false;
		}
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject('Msxml2.XMLHTTP');
		} catch(e) {
			try {
				req = new ActiveXObject('Microsoft.XMLHTTP');
			} catch(e) {
				req = false;
			}
		}
	}
	if (req) {
		reqObj = obj;
		req.onreadystatechange = func;
		req.open('GET', url, true);
		req.send('');
	}
}

function loadCal(day) {
	window.open(calDayURL+(calDayURL.search(/\?/) == -1 ? '?' : '&')+'year='+day.getFullYear()+'&month='+(day.getMonth()+1)+'&day='+day.getDate()+'&companyid='+companyid+'&'+Date.UTC(), '', 'width=600,height=420');
}

function setCal(day) {
	frm = document.forms[0];
	frm['day'  ].value = day.getDate();
	frm['month'].value = day.getMonth()+1;
	frm['year' ].value = day.getFullYear();
	
	cal.resetDateFunc();
	cal.setDateFunc(day, setCal);
	cal.refresh();
}

function gotoCompany(sel) {
	if (sel == null || sel == 'undefined') {
		return;
	}
	
	if (sel.selectedIndex > -1 && sel.options[sel.selectedIndex].value > 0) {
		location.href = '/admin/subscriberidx.php?companyid='+sel.options[sel.selectedIndex].value;
	}
}

var searchString = function (data) {
	for (var i=0;i<data.length;i++)	{
		var dataString = data[i].string;
		var dataProp = data[i].prop;
		this.versionSearchString = data[i].versionSearch || data[i].identity;
		if (dataString) {
			if (dataString.indexOf(data[i].subString) != -1)
				return data[i].identity;
		}
		else if (dataProp)
			return data[i].identity;
	}
}

/** Menu system **/

var version = '';
var agent   = '';

if (navigator.userAgent.search('MSIE') >= 0) {
	if (navigator.userAgent.search('MSIE 4') >= 0 || navigator.userAgent.search('MSIE 3') >= 0) {
		var agent = '';
	} else {
		var agent = 'ie';
		version = parseFloat(navigator.userAgent.substr(navigator.userAgent.indexOf('MSIE ')+5));
	}
} else if (navigator.userAgent.search('Konqueror') >= 0) {
	var agent = 'konqueror';
} else if (navigator.userAgent.search('Safari') >= 0) {
	//check if the userAgent is Chrome
	var is_chrome = searchString([{string: navigator.userAgent,subString: "Chrome",identity: true}]);
	if (is_chrome==true)var agent = 'chrome';
	else
	var agent = 'safari';
} else if (navigator.userAgent.search('Gecko') >= 0) {
	var agent = 'gecko';
} else if (navigator.appName == 'Netscape' && navigator.userAgent.search('Mozilla/4') >= 0) {
	var agent = 'ns4';
} else {
	var agent = '';
}

if (navigator.userAgent.search('Mac') >= 0) {
	var os = 'Mac';
} else {
	var os = '';
}

if (agent == 'ie' || agent == 'gecko' || agent == 'konqueror' || agent == 'chrome') {
	var menuCapable = true;
} else {
	var menuCapable = false;
}

function Point(x,y) {
	this.x=x; this.y=y; return this;
}

function getPagePos(node) {
	if (agent == 'ns4') return new Point(node.pageX, node.pageY);
	var x = 0;
	var y = 0;
	while (node && node.tagName != 'BODY') {
		x += node.offsetLeft;
		y += node.offsetTop;
		if (node.tagName == 'TD' && os == 'Mac' && agent == 'ie') y += node.parentNode.offsetTop;
		node = node.offsetParent;
	}
	// Bug is fixed on MacOS X IE5.21 (maybe earlier?)
	
	if (!node && os == 'Mac' && agent == 'ie' && version < 5.21) {
		x += (document.body.leftMargin*1);
		y += (document.body.topMargin*1);
	}
	
	return new Point(x, y);
}


var opt_posxfixed = true;	// The root menu will never dynamically move on the x-axis
var opt_posyfixed = true;	// The root menu will never dynamically move on the y-axis
var opt_movetohide = false;	// Move the sub menu out of the way to hide it
var opt_prebuildsubs = 2;	// 0 = No prebuilding, 1 = Partial prebuilding, 2 = Full prebuilding
var opt_overlapx = 0;		// The default px to overlap the menus on the x axis
var opt_overlapy = 0;		// The default px to overlap the menus on the y axis
var opt_bginit = 0;		// 0 = Process all asap, 1 = Process all after render, 2 = Process in stages
var opt_dropshadow = false;	// Set true to have a drop show effect for non IE users.

if (menuCapable) {
	function NogginMenu(id) {
		this.items = new Array();
		this.parentIdx = null;
		this.currentItem = null;
		this.childdirection = 1;
		this.id = id;
		this.depth = 0;
		this.topMenu = this;
		this.zIndex = 100;
		this.imgSrc = '';
		this.lry = null;

		this.add = function _add(name, click, text) {
			this.items[this.items.length] = new NogginMenuItem(name, click, this, text);
		}

		this.install = function _install() {
			if (opt_bginit == 0) {
				this.doinstall();
			} else {
				setTimeout(this.id+'.doinstall()', 1);
			}
		}

		this.doinstall = function _doinstall() {
			this.lyr = document.getElementById(this.id);
			for (var i = 0; i < this.items.length; i++) {
				this.lyr.rows[0].appendChild(tmp = document.createElement('td'));
// 				img = new Image();
// 				img.src = this.imgSrc + '/' + this.items[i].name + '.png';
// 				tmp.appendChild(img);
				if (this.items[i].name == 'delimiter') {
					tmp.appendChild(document.createTextNode('|'));
					tmp.className = 'nav-delimiter';
				} else {
					tmp.appendChild(atag = document.createElement('a'));
					atag.appendChild(document.createTextNode(this.items[i].text));
					tmp.className = 'nav-' + this.items[i].name;
					tmp.style.cursor = 'pointer';
				}
				this.items[i].setTr(tmp);
				if (opt_prebuildsubs > 0 && this.items[i].sub != null) this.items[i].sub.build();
			}
			this.lyr.onmouseover = function _over(event) {
				if (agent == 'konqueror') {
					document.onmouseover = new Function('event', this.id+'.hide();');
				} else {
					document.onmouseover = new Function('event', this.id+'.hide();');
				}
			}
			//var after = new Date();
			//window.defaultStatus = (after-before);
		}

		this.hide = function _hide() {
			if (this.currentItem != null) {
				if (this.currentItem.sub != null) this.currentItem.sub.hide();
				this.currentItem.out();
			}
			if (agent == 'konqueror') {
				document.onmouseover = function _donothing() {};
			} else {
				document.onmouseover = function _donothing() {};
			}
		}

	}

	function NogginSubMenu(parentMenu, parentIdx) {
		parentMenu.items[parentIdx].sub = this;
		this.parentMenu = parentMenu;
		this.parentItem = parentMenu.items[parentIdx];
		this.parentIdx = parentIdx;
		if (arguments[2] > 0 || arguments[2] < 0) {
			this.childdirection = arguments[2];
		} else {
			this.childdirection = this.parentMenu.childdirection;
		}
		this.depth = parentMenu.depth+1;
		this.topMenu = parentMenu.topMenu;
		this.zIndex = parentMenu.zIndex+1;
		this.itemNodeTemplate = null;
		this.items = new Array();
		this.lyr = null;
		this.currentItem = null;
		this.built = false;
		this.imgSrc = this.parentMenu.imgSrc;

		this.add = function _add(name, click, text) {
			this.items[this.items.length] = new NogginMenuItem(name, click, this, text);
		}

		this.build = function _build() {
			if (this.built) return false;
			this.lyr = document.createElement('table');
			this.lyr.style.position = 'absolute';
			this.lyr.setAttribute('cellpadding', '0', 0);
			this.lyr.setAttribute('cellspacing', '0', 0);
			if (os == 'Mac' && agent == 'ie') {
				this.lyr.cellPadding = 0;
				this.lyr.cellSpacing = 0;
			}
			this.lyr.style.cursor = (agent == 'ie' ? 'hand' : 'pointer');
			this.lyr.style.zIndex = this.zIndex;
			var tmp, tbody, tr, maintr;
			tbody = document.createElement('tbody');
			for (var i = 0; i < this.items.length; i++) {
				tbody.appendChild(maintr = document.createElement('tr'));
				maintr.appendChild(tmp = document.createElement('td'));
// 				img = new Image();
// 				img.src = this.imgSrc + '/' + this.items[i].name + '.png';
// 				tmp.appendChild(img);
				if (this.items[i].name == 'delimiter') {
					tmp.appendChild(document.createTextNode('|'));
					tmp.className = 'nav-delimiter';
				} else {
					tmp.appendChild(atag = document.createElement('a'));
					atag.appendChild(document.createTextNode(this.items[i].text));
					tmp.className = 'nav-' + this.items[i].name;
					tmp.style.cursor = 'pointer';
				}
				tmp.style.border = '1px solid #949494';
				tmp.style.borderTop = '';
				tmp.style.cursor = 'pointer';
				this.items[i].setTr(tmp);
				if (this.items[i].sub != null && opt_prebuildsubs == 2) {
					this.items[i].sub.build();
				}
			}
			
			this.lyr.appendChild(tbody);
			document.body.appendChild(this.lyr);
			this.lyr.onmouseover = detectTarget;
			this.lyr.over = function _over(event) {}
			this.lyr.style.top = this.parentItem.subpos.y+'px';
			//this.lyr.style.left = this.parentItem.subpos.x+'px';
			this.built = true;
			this.hide();

			return true;
		}

		this.hide = function _hide() {
			if (!this.built) return false;
			if (opt_movetohide) {
				this.lyr.style.top = (-1*this.lyr.offsetHeight-10)+'px';
				this.lyr.style.left = (-1*this.lyr.offsetWidth-10)+'px';
			} else {
				this.lyr.style.visibility = 'hidden';
			}
			if (this.currentItem != null) this.currentItem.out();
		}

		this.show = function _show() {
			if (!this.built) return false;
			this.parentItem.setSubPos();
			this.lyr.style.visibility = 'visible';
			if (!opt_posyfixed || this.lyr.offsetTop != this.parentItem.subpos.y) this.lyr.style.top = this.parentItem.subpos.y+'px';
			if (!opt_posxfixed || this.lyr.offsetLeft != this.parentItem.subpos.x) this.lyr.style.left = this.parentItem.subpos.x+'px';
			if (opt_prebuildsubs == 1) {
				for (var i = 0; i < this.items.length; i++) {
				 	if (this.items[i].sub != null) this.items[i].sub.build();
				}
			}
		}

	}

	function NogginMenuItem(name, clickObj, parentMenu, text) {
		this.name = name;
		this.text = text;
		this.clickObj = clickObj;
		this.tr = null;
		this.parentMenu = parentMenu;
		this.sub = null;

		this.setTr = function _setTr(tr) {
			this.tr = tr;
			this.tr.item = this;
			this.tr.onmouseover = detectTarget;
			this.tr.onclick = detectTarget;
			if (this.sub != null) {
				this.setSubPos();
			}
		}
		
		this.setSubPos = function _setSubPos() {
			this.subpos = getPagePos(this.tr);
			if (this.parentMenu.parentIdx == null) {
				if (this.sub != null && this.sub.lyr && this.sub.childdirection < 0) {
					this.subpos.x += this.tr.offsetWidth-this.sub.lyr.offsetWidth-opt_overlapx;
				}
				this.subpos.y += this.tr.offsetHeight-opt_overlapy;
			} else {
				if (this.sub != null && this.sub.lyr && this.parentMenu.childdirection < 0) {
					this.subpos.x -= this.sub.lyr.offsetWidth-opt_overlapx;
				} else {
					this.subpos.x += this.tr.offsetWidth-opt_overlapx;
				}
				this.subpos.y += opt_overlapy;
			}
			
		}

		this.over = function _over(event) {
			if (this.parentMenu.currentItem != null) {
				if (this.parentMenu.currentItem.sub != null) this.parentMenu.currentItem.sub.hide();
				this.parentMenu.currentItem.out();
			} else if (this.parentMenu.parentIdx == null) {
				this.parentMenu.lyr.onmouseover(event);
			}
			
			if (this.tr != null) {
				this.parentMenu.currentItem = this;
				//this.tr.firstChild.src = this.parentMenu.imgSrc + '/' + this.name + '_rollover.png';
				if (this.name != 'delimiter') this.tr.className = 'nav-' + this.name + '-over';
				if (this.sub != null) {
					this.sub.build();
					this.sub.show();
				}
			}
		}

		this.out = function _out() {
			if (this.tr != null) {
				//this.tr.firstChild.src = this.parentMenu.imgSrc + '/' + this.name + '.png';
				if (this.name != 'delimiter') this.tr.className = 'nav-' + this.name;
				if (this.sub != null) {
					this.sub.hide();
				}
			}
			if (this.parentMenu.currentItem == this) this.parentMenu.currentItem = null;
		}

		this.click = function _click() {
			if (typeof this.clickObj == 'string') {
				window.location = this.clickObj;
			} else if (typeof this.clickObj == 'function') {
				this.clickObj();
			}
		}
	}

	function detectTarget(event) {
		if (agent == 'gecko') {
			var item = event.currentTarget.item;
		} else {
			var event = window.event;
			var item = this.item;
		}
		event.cancelBubble = true;
		if (item) {
			switch (event.type) {
				case 'mouseover':
					item.over(event);
					break;
				case 'click':
					item.click(event);
					break;
			}
		}
	}
}

