var isIE = navigator.appName.indexOf("Microsoft") != -1;
try {
	var isSafari = navigator.vendor.indexOf("Apple") != -1;
}catch (e){
	isSafari = false;
}

var KEYUP = 38;
var KEYDN = 40;
var KEYLFT = 37;
var KEYRGT = 39;
var KEYSLSH = 191;
var KEYCLN = 59;
var KEYSPC = 32;
var KEYBAK = 8;
var KEYENT = 13;
var ESC = 27;
var TAB = 9;
var _widget = new Object();

if (!onupfunctions)
	var onupfunctions = new Array();

var position_interval = setInterval("position_buttons()", 250);

function position_buttons()
{
	for (id in _widget)
	{
		if (document.getElementById(_widget[id].e_id))
			try {
				if (_widget[id].kind == "SELECT")
					_widget[id].position_button();
			} catch (e) {}
	}
}

Array.prototype.indexOf= function(object) {
    for (var i = 0; i < this.length; i++)
      if (this[i] == object) return i;
    return -1;
  };

function get_key_code(e)
{
	if(e)			//Firefox
		return e.keyCode;
	if(window.event)	//IE
		return window.event.keyCode;
}

function get_event_source(ev)
{
	if(!isIE && ev)			//Moz
		return ev.target;

	if(isIE && window.event)	//IE
		return window.event.srcElement;
}
	
function get_key_char(e)
{
	var this_chr = String.fromCharCode(get_key_code(e));
	var re = /^[0-9-A-z]*$/;
	if (re.test(this_chr))	
		return this_chr;
	else
		return "";
}

function get_key_digit(e)
{
	key = get_key_code(e);
	key = (key >= 96 && key <= 105) ? key - 48 : key;
	var this_chr = String.fromCharCode(key);
	var re = /^[0-9]*$/;
	if (re.test(this_chr))	
		return this_chr;
	else
		return "";
}

function set_input_selection(element, start, end)
{
	element.focus();
	try {
		element.setSelectionRange(start,end);
	} catch (e) {
		var range = element.createTextRange();
		range.collapse(true);
		range.moveStart("character", start);
		range.moveEnd("character", end - start);
		range.select();
	}
}

function get_input_cursor_pos(element) {
	try {
		var range = document.selection.createRange();
		var isCollapsed = range.compareEndPoints("StartToEnd", range) == 0;
		if (!isCollapsed)
			range.collapse(false);
		var b = range.getBookmark();
		element.selectionEnd = b.charCodeAt(2) - 2;
	} catch (e) {
	}
	return element.selectionEnd;
};

function pad(str, pad_char, str_len)
{
	// make sure it's a string
	str = str + "";
	pad_char = pad_char + "";
	
	var padding = "";
	
	for (i = 0; i < str_len - str.length; i++)
	{
		padding += pad_char;
	}
	return padding + str;
}

function is_leap_year(year)
{
	return(year%4==0 && ((year%100!=0) || (year%400==0)));
}

function days_in_month(year)
{
	var days = new Array();
	days[0] = 31;
	days[1] = 31;
	days[2] = is_leap_year(year) ? 29 : 28;
	days[3] = 31;
	days[4] = 30;
	days[5] = 31;
	days[6] = 30;
	days[7] = 31;
	days[8] = 31;
	days[9] = 30;
	days[10] = 31;
	days[11] = 30;
	days[12] = 31;
	return days;
}

function fix_day(date)
{
	var days = days_in_month(date.year);
	var month_days = days[date.month];
	date.day = Math.min(date.day, month_days);
	return date;
}

// for beta try to figure out if inputs have been restyled
function is_new_style(id)
{
	var padding = getStyle(id, 'padding-top');
	if (!padding)
		padding = getStyle(id, 'paddingTop');
	
	if (padding == "3px")
		return true;
	else
		return false;
}

function is_edit_view()
{
	edit_form = document.getElementById('edit-event-form');
	
	if (edit_form)
		return true;
	else
		return false;
}


// catch mouse clicks to close the selects when clicking elsewhere on the page
var check_select = function(e)
{
	for (id in _widget)
	{
		if (_widget[id].kind == "SELECT")
			_widget[id].check_click_source(e);
	}
}
//onupfunctions.push(check_select);

if (document.addEventListener)
	document.addEventListener("mouseup", check_select, false);
else
	document.attachEvent("onmouseup", check_select);

function NewSelect2(e_id, options)
{
	_widget[e_id] = this;
	this.id = e_id;
	this.hidden = document.getElementById(this.id);
	this.element = null;
	this.container = null;
	this.div = null;
	this.iframe = null;
	this.button = null;
	this.left = 0;
	this.top = 0;
	if (options)
		this.options = options;
	else
		this.options = new Array();
	this.selectedIndex = -1;
	this.hoverIndex = -1;
	
	this.kind = "SELECT";
	this.value = "";

	try{
		this.width = options.width;
	} catch(e) {
		this.width = null;
	}

	this.is_open = function()
	{
		if (_widget[e_id].div.style.display == "none")
			return false;
		else
			return true;
	};
	
	this.check_click_source = function(e)
	{
		if (!_widget[e_id].is_open())
			return;
		var my_ids = new Array(e_id, e_id + "-select-container", e_id + "-select-element", e_id + "-select-button", e_id + "-select-div");
		var clicked = _widget[e_id].get_event_source(e);
		if (my_ids.indexOf(clicked.id) == -1)
			_widget[e_id].toggle();
		
	};
	
	this.has_icons = function()
	{
		for (var i=0; i < this.options.length; i++)
		{
			if (this.options[i].icon && this.options[i].icon.length > 0)
				return true;
		}
		return false;
	};
	
	this.update_options = function(options)
	{
		var value = _widget[e_id].value;
		_widget[e_id].options = options;
		_widget[e_id].create_options();
		_widget[e_id].set_init_value(value);
	};

	this.set_disabled = function(is_disabled)
	{
		if (is_disabled)
		{
			_widget[e_id].container.style.backgroundColor = "InactiveCaption";
			_widget[e_id].container.style.color = "InactiveCaptionText";
		} else {
			_widget[e_id].container.style.backgroundColor = "";
			_widget[e_id].container.style.color = "";
		}
	};
	
	this.init = function(value)
	{		
		this.div = this.create_div();
		this.iframe = this.create_iframe();
		this.container = this.create_container();
		this.element = this.create_element();
		this.button = this.create_button();
		this.button.onclick = _widget[e_id].toggle;
		this.element.onclick = _widget[e_id].toggle;
		this.element.onkeypress = this.onkeypress;
		this.element.onkeydown = this.onkeydown;
		this.element.onkeyup = this.onkeyup;
		this.element.style.cursor = "default";
		this.create_options();
		this.init_display();
		
		if (value)
			this.set_init_value(value);
	};
	
	this.set_init_value = function(value)
	{
		_widget[e_id].selectedIndex = 0;
		_widget[e_id].hoverIndex = 0;
		for (var i=0; i < _widget[e_id].options.length; i++)
		{
			if (_widget[e_id].options[i].value == value)
			{
				_widget[e_id].selectedIndex = i;
				_widget[e_id].hoverIndex = i;
				_widget[e_id].set_value();
				return;
			}
		}
		// if value wasn't found index and value will be set to 0;
		_widget[e_id].set_value();
	};
	
	this.init_display = function()
	{
		for (var i=0; i < this.options.length; i++)
		{
			if (this.options[i].selected)
			{
				this.selectedIndex = i;
				this.hoverIndex = i;
				this.set_value();
			}
		}
		
		if (this.selectedIndex == -1)
		{
			this.selectedIndex = 0;
			this.hoverIndex = 0;
			this.set_value();
		}
	};
	
	this.fix_size = function()
	{
		
		if (!isIE && this.width != null)
		{
			_widget[e_id].container.style.width = _widget[e_id].container.clientWidth - 4 + "px";
		}
		
		_widget[e_id].element.style.width = _widget[e_id].container.clientWidth - 50 + "px";
		
		var diff = 44;
		if (!_widget[e_id].has_icons())
			var diff = 24;
		_widget[e_id].button.style.marginLeft = Math.max(_widget[e_id].container.clientWidth - diff - _widget[e_id].element.clientWidth, 0) + "px";
	}
	
	this.get_max_text_length = function()
	{
		var max = 0;
		for(var i=0; i < this.options.length; i++)
			if (this.options[i].text.length > max)
				max = this.options[i].text.length;
		
		return max;
	};
	
	this.create_container = function()
	{
		var container = document.createElement("SPAN");
		
		container.setAttribute("style", "display: -moz-inline-box;display: inline-block;height: 20px;border-style: inset;border-width: 2px;background-repeat: no-repeat;background-position: top left;");
		container.setAttribute("id", this.hidden.id + "-select-container");
		container.setAttribute("className", "agi-extrainput");
		container.setAttribute("class", "agi-extrainput");
		this.hidden.parentNode.appendChild(container);
		container.style.height = "20px";
		container.style.borderStyle = "inset";
		container.style.borderWidth = "2px";
		if (this.width != null)
			container.style.width = this.width;
		container.style.backgroundRepeat = "no-repeat";
		container.style.backgroundPosition = "top left";
		container.style.display = "inline-block";
		if (!isIE && this.width != null)
		{
			container.style.width = container.clientWidth - 4 + "px";
		}
		return container;
	};
	
	this.create_element = function()
	{
		var element = document.createElement("INPUT");
		element.setAttribute("type", "text");
		element.setAttribute("id", this.hidden.id + "-select-element");
		element.setAttribute("autocomplete", "off");
		if (isIE)
			element.setAttribute("size", this.get_max_text_length() - 2);
		else
			element.setAttribute("size", this.get_max_text_length() - 3);
		var margin_left = (this.has_icons()) ? "24" : "4";
		
		element.setAttribute("style", "margin-left: " + margin_left + "px;border-width: 0px;border-style: none;height: 18px;margin-bottom: 1px;margin-left: 3px;font-size: 10pt; padding: 0px;");
		this.container.appendChild(element);
		element.style.borderWidth = "0px";
		element.style.borderStyle = "none";
		element.style.height = "18px";
		element.style.marginBottom = "1px";
		element.style.marginLeft = margin_left + "px";
		element.style.fontSize = "10pt";
		element.style.padding = "0px";
		return element;
	};
	
	this.create_div = function()
	{
		var div = document.createElement("DIV");
		div.setAttribute("id", this.hidden.id + "-select-div");
		div.setAttribute("class", "agi-extrainput");
		div.setAttribute("className", "agi-extrainput");
		
		div.style.width = "0px";
		div.style.height = "0px";
		div.style.position = "absolute";
		div.style.display = "none";
		var list = document.createElement("UL");

		div.appendChild(list);
		document.body.appendChild(div);
		return div;
	};
	
	this.create_button = function()
	{
		var button = document.createElement("BUTTON");
		button.setAttribute("id", this.hidden.id + "-select-button");
		button.setAttribute("style", "background: ButtonFace; height: 20px;width: 20px;font-weight: bold;font-size: 7pt;");
		button.style.backgroundColor = "ButtonFace";
		button.style.height = "20px";
		button.style.width = "20px";
		var diff = 44;
		if (!this.has_icons())
			var diff = 24;
		button.style.marginLeft = Math.max(this.container.clientWidth - diff - this.element.clientWidth, 0) + "px";
		button.style.fontWeight = "bold";	
		button.style.fontSize = "7pt";
		if (isIE)
		{
			button.style.marginBottom = "-1px";
		} else {
			button.style.borderColor = "ThreeDHighlight";
		}

		var image = document.createElement("IMG");
		image.setAttribute("src", imghost + "/ag/reminders/arrow.gif");
		image.setAttribute("style", "width: 12px; height: 14px;");
		button.appendChild(image);

		this.container.appendChild(button);

		return button;
	}

	this.create_iframe = function()
	{
		var iframe = document.createElement("IFRAME");
		iframe.setAttribute("src", "javascript:void(null)");
		iframe.setAttribute("scrolling", "no");
		iframe.setAttribute("frameBorder", "0");
		iframe.setAttribute("id", "new-select-iframe");
		iframe.style.width = "0px";
		iframe.style.height = "0px";
		iframe.style.position = "absolute";
		iframe.style.display = "none";
		//this.hidden.parentNode.appendChild(iframe);
		document.body.appendChild(iframe);
		return iframe;
	};

	this.show = function()
	{
		var width = _widget[e_id].container.clientWidth + 4;
		var height = Math.min(100, (25 * _widget[e_id].options.length) + 2);
		
		_widget[e_id].div.style.background = "#FFFFFF";
		_widget[e_id].div.style.border = "outset #000000 1px";
		_widget[e_id].div.style.width = width + "px";
		_widget[e_id].iframe.style.width = width + "px";
		_widget[e_id].div.style.height = height + "px";
		_widget[e_id].div.style.overflow = "auto";
		_widget[e_id].iframe.style.height = height + "px";
		_widget[e_id].iframe.style.background = "#FF0000";

		_widget[e_id].div.style.zIndex = "600";
		_widget[e_id].iframe.style.zIndex = "599";
		_widget[e_id].position_select();
		_widget[e_id].div.style.display = "block";
		_widget[e_id].iframe.style.display = "block";
	};
	
	this.toggle = function()
	{
		_widget[e_id].button.blur();
		if (_widget[e_id].div.style.display == "none")
			_widget[e_id].show();
		else
			_widget[e_id].hide();
		return false;
	};
	
	this.hide = function()
	{
		_widget[e_id].div.style.display = "none";
		_widget[e_id].iframe.style.display = "none";
	};
	
	this.position_select = function()
	{		
		var elem = this.container;
		this.left = 0;
		this.top = elem.offsetHeight;

		//Walk up the DOM and add up all of the offset positions.
		while (elem.offsetParent && elem.tagName.toUpperCase() != 'BODY')
		{
			//if (elem.id != "lightbox")
			//{
				this.left += elem.offsetLeft;
				this.top += elem.offsetTop;
			//}
			elem = elem.offsetParent;
		}

		this.left += elem.offsetLeft;
		this.top += elem.offsetTop;
		
		this.div.style.left = this.left + 'px';
		this.div.style.top = this.top + 'px';
		this.iframe.style.left = this.left + 'px';
		this.iframe.style.top = this.top + 'px';
	};
	
	this.create_options = function()
	{
		var ul = document.createElement('ul');
		ul.style.listStyleType = "none";
		ul.style.margin = "0px";
		ul.style.padding = "0px";
		
		//Create an array of LI's for the words.
		for (i = 0; i < this.options.length; i++)
		{
			var linkText = this.options[i].text;
			linkText = linkText.replace(/ /, "&nbsp;");
			var j = i + 1;
			var li = document.createElement('li');
			li.setAttribute("class", "agi-extrainput");
			li.setAttribute("className", "agi-extrainput");

			var a = document.createElement('a');
			a.href="javascript:void(null)";
			a.innerHTML = linkText;
			a.style.textDecoration = "none";
			a.style.color = "#000000";
			a.style.cursor = "default";
			var paddingleft = (this.has_icons()) ? "24" : "4";
			
			li.style.padding = "0px 4px 0px " + paddingleft + "px";
			li.style.height  = "25px";
			if (this.options[i].icon)
				li.style.backgroundImage = "url('" + this.options[i].icon + "')";
			li.style.backgroundRepeat = "no-repeat";
			li.style.backgroundPosition = "center left";
			li.style.lineHeight = "25px";
			li.style.fontSize = "10pt";
			li.style.textAlign = "left";
			li.style.verticalAlign = "middle";
			li.appendChild(a);
	
			if (_widget[e_id].hoverIndex == i)
			{
				li.style.background = "#e7eff7";
				li.getElementsByTagName("A")[0].style.color = "#ffffff";
			}
	
			ul.appendChild(li);
		}
	
		this.div.replaceChild(ul,this.div.childNodes[0]);

		/********************************************************
		mouseover handler for the dropdown ul
		move the highlighted suggestion with the mouse
		********************************************************/
		ul.onmouseover = function(ev)
		{
			//Walk up from target until you find the LI.
			var target = _widget[e_id].get_event_source(ev);
			while (target.parentNode && target.tagName.toUpperCase() != 'LI')
			{
				target = target.parentNode;
			}
		
			var lis = _widget[e_id].div.getElementsByTagName('LI');
			
	
			for (i = 0; i < lis.length; i++)
			{
				var li = lis[i];
				if(li == target)
				{
					_widget[e_id].hoverIndex = i;
					break;
				}
			}
			_widget[e_id].change_selected();
		};

		/********************************************************
		click handler for the dropdown ul
		insert the clicked suggestion into the input
		********************************************************/
		ul.onclick = function(ev)
		{
			_widget[e_id].set_value();
			_widget[e_id].hide();
			_widget[e_id].cancel_event(ev);
			return false;
		};
		
	};

	this.set_value = function()
	{
		if (this.hoverIndex > -1)
		{
			this.selectedIndex = this.hoverIndex;
			
			this.value = this.options[this.selectedIndex].value;
			this.element.value = this.options[this.selectedIndex].text;
			if (this.options[this.selectedIndex].icon)
				this.container.style.backgroundImage = "url('" + this.options[this.selectedIndex].icon + "')";
			try {
				this.hidden.value = this.options[this.selectedIndex].value;
			} catch(e){}
			try {
				this.onchange();
			} catch(e){}
		}
	};

	this.get_value = function()
	{
		return this.options[this.selectedIndex].value;
	};
	
	this.change_selected = function()
	{
		var lis = this.div.getElementsByTagName('LI');
		for (i = 0; i < lis.length; i++)
		{
			var li = lis[i];

			if (this.hoverIndex == i)
			{
				li.style.backgroundColor = "Highlight";
				li.getElementsByTagName("A")[0].style.color = "HighlightText";
			}
			else
			{ try {
					li.style.backgroundColor = "";
					li.getElementsByTagName("A")[0].style.color = "#000000";
				} catch(e){}
			}
		}
	};
	
	this.get_event_source = function(ev)
	{
		if(!isIE && ev)			//Moz
			return ev.target;
	
		if(isIE && window.event)	//IE
			return window.event.srcElement;
	};
	
	this.cancel_event = function(ev)
	{
		if(ev)			//Moz
		{
			ev.preventDefault();
			ev.stopPropagation();
		}

		if(window.event)	//IE
			window.event.returnValue = false;
	};
	
	this.decrement = function()
	{
		if (_widget[e_id].selectedIndex < (_widget[e_id].options.length - 1))
		{
			_widget[e_id].selectedIndex++;
			_widget[e_id].set_value();
		}
	}
	
	this.increment = function()
	{
		if (_widget[e_id].selectedIndex > 0)
		{
			_widget[e_id].selectedIndex--;
			_widget[e_id].set_value();
		}
	}
	
	this.onkeypress = function(e){
		if (get_key_code(e) == TAB)
			return true;
		else if (get_key_code(e) == ESC)
			return false;
		else if (get_key_code(e) == KEYUP)
			_widget[e_id].increment();
		else if (get_key_code(e) == KEYDN)
			_widget[e_id].decrement();
		return false;
	};

	this.onkeydown = function(e){
		if (get_key_code(e) == TAB)
			return true;
		else if (get_key_code(e) == ESC)
			return false;
		if (isIE || isSafari)
		{
			if (get_key_code(e) == KEYUP)
				_widget[e_id].increment();
			else if (get_key_code(e) == KEYDN)
				_widget[e_id].decrement();
		}
		return false;
	};

	this.onkeyup = function(e){
		if (get_key_code(e) == TAB)
			return true;
		else if (get_key_code(e) == ESC)
			return false;
		else if (get_key_code(e) == KEYRGT || get_key_code(e) == KEYCLN || get_key_code(e) == KEYSPC)
			_widget[e_id].select_next();
		else if (get_key_code(e) == KEYLFT)
			_widget[e_id].select_prev();
		if (get_key_code(e) == KEYBAK || get_key_code(e) == KEYLFT || get_key_code(e) == KEYRGT || get_key_code(e) == KEYCLN ||  get_key_code(e) == KEYSPC || get_key_code(e) == KEYUP || get_key_code(e) == KEYDN)
		{
			_widget[e_id].typed = "";
		} else {
			_widget[e_id].typed += get_key_digit(e);
			//_widget[e_id].type_match();
		}

		return false;
	};
	
}

function OldSelect(e_id, options, selected, style)
{
	this.id = e_id;
	this.hidden = document.getElementById(this.id);
	this.element = null;
	this.style = style;
	if (options)
		this.options = options;
	else
		this.options = new Array();

	if (selected)
		this.selected = selected;
	else
		this.selected = '';
	
	this.kind = "OLD_SELECT";
	
	this.element = document.createElement("select");
	this.element.setAttribute("name", this.hidden.name);
	this.element.setAttribute("style", this.style);
	this.element.setAttribute("id", this.hidden.id + "-select-element");
	
	for (var i = 0; i < this.options.length; i++)
	{
		var new_option = get_option(this.options[i].value, this.options[i].text)
		if (this.options[i].value == this.selected)
			new_option.setAttribute('selected', 'true');
		this.element.appendChild(new_option)
	}
	this.hidden.parentNode.replaceChild(this.element, this.hidden);
	_widget[e_id] = this.element;
}

function update_select_options(widget, options)
{
	for (var i = widget.options.length - 1; i > -1; i--)
				widget.remove(i);
			
	for (var i=0; i < options.length; i++)
	{
		widget.appendChild(get_option(options[i].value, options[i].text));		
	}
}