function GetAnchorX( el )
{
	if (document.getElementById) {
		el = document.getElementById(el)
		}
	else if (document.all) {
		el = document.all[el];
		}
	else if (document.layers) {
		el = document.layers[el];
		}
	x = 0;
	if (el.offsetParent)
			{
				while(el.offsetParent)
				{
					x += el.offsetLeft;
					el = el.offsetParent;
				}
			}
	else
		x += el.offsetLeft;
	return x;
}
function GetAnchorWidth( el )
{
	if (document.getElementById) {
		el = document.getElementById(el)
		}
	else if (document.all) {
		el = document.all[el];
		}
	else if (document.layers) {
		el = document.layers[el];
		}
	return el.offsetWidth;
}
function GetAnchorY( el )
{
	if (document.getElementById) {
		el = document.getElementById(el)
		}
	else if (document.all) {
		el = document.all[el];
		}
	else if (document.layers) {
		el = document.layers[el];
		}
	y = 0;
	if (el.offsetParent)
			{
				while(el.offsetParent)
				{
					y += el.offsetTop;
					el = el.offsetParent;
				}
			}
	else
		y += el.offsetTop;
	return y;
}
function ComboBox_Position()
{
	if ( document.getElementById(this.anchor) )
	{
		this.width = GetAnchorWidth(this.anchor);
		this.x = GetAnchorX(this.anchor);
		this.y = GetAnchorY(this.anchor)+document.getElementById(this.anchor).offsetHeight-1;
	}
}
function ComboBox_RePosition()
{
	for (var i=0; i<ComboBoxObjects.length; i++) 
	{
		if ((ComboBoxObjects[i] != null)&&(ComboBoxObjects[i].visible))
		{
			var p = ComboBoxObjects[i];
			p.reposition();
			if (p.divname != null) {
				if (p.use_gebi) {
					document.getElementById(p.divname).style.left = p.x + "px";
					document.getElementById(p.divname).style.top = p.y + "px";
					}
				else if (p.use_css) {
					document.all[p.divname].style.left = p.x;
					document.all[p.divname].style.top = p.y;
					}
				else if (p.use_layers) {
					document.layers[p.divname].left = p.x;
					document.layers[p.divname].top = p.y;
					}
				}
		}
	}
}
function ComboBox_setData()
{
	i = arguments[0];
	if(this.show == "name")
		selected = this.data[i][1];
	if(this.show == "value_name")
		selected = this.data[i][2] + " " + this.data[i][1];
	if(this.show == "name_value")
		selected = this.data[i][1] + " " + this.data[i][2];
	document.getElementById(this.focus).innerHTML = selected;
	if ( this.associate )
	{
		this.associate.selectedIndex = i;
	}
	if (this.redirect)
		document.location = this.data[i][2];
	this.hide();
	if (window.calendarevent)
		window.calendarevent();
}
function ComboBox_autohide()
{
	this.autoHide = true;
}
function ComboBox_DropDown()
{
	if (!this.ready)
		return;
	if ( this.display_cal )
		this.populate_assoc();
	if (this.visible)
	{
		this.hide();
		return;
	}
	this.reposition();
	if (this.divname != null) {
		if (this.use_gebi) {
			document.getElementById(this.divname).style.left = this.x + "px";
			document.getElementById(this.divname).style.top = this.y + "px";
			document.getElementById(this.divname).style.visibility = "visible";
			document.getElementById(this.divname).style.width = this.width + "px";
			if ( this.display_cal )
				document.getElementById(this.divname).style.height = "auto";
			else
				document.getElementById(this.divname).style.height = this.Height + "px";
			}
		else if (this.use_css) {
			document.all[this.divname].style.left = this.x;
			document.all[this.divname].style.top = this.y;
			document.all[this.divname].style.width = this.width;
			if ( this.display_cal )
				document.all[this.divname].style.height = "auto";
			else
				document.all[this.divname].style.height = this.Height;
			document.all[this.divname].style.visibility = "visible";
			}
		else if (this.use_layers) {
			document.layers[this.divname].left = this.x;
			document.layers[this.divname].top = this.y;
			document.layers[this.divname].width = this.width;
			if ( this.display_cal )
				document.layers[this.divname].height = "auto";
			else
				document.layers[this.divname].height = this.Height;
			document.layers[this.divname].visibility = "visible";
			}
		}
	this.visible = true;
	this.refresh();
}
function ComboBox_hide()
{
	if (this.use_gebi)
	{
		document.getElementById(this.divname).style.left = 0;
		document.getElementById(this.divname).style.top = 0;
		document.getElementById(this.divname).style.visibility = "hidden";
	}
	else if (this.use_css) 
	{
		document.all[this.divname].style.left = 0;
		document.all[this.divname].style.top = 0;
		document.all[this.divname].style.visibility = "hidden";
	}
	else if (this.use_layers)
	{
		document.layers[this.divname].left = 0;
		document.layers[this.divname].top = 0;
		document.layers[this.divname].visibility = "hidden";
	}
	this.visible = false;
}
function ComboBox_attachListener() {
	if (document.layers) {
		document.captureEvents(Event.MOUSEUP);
		}
	window.ComboBoxOldEventListener = document.onmouseup;
	window.ComboBoxOldResize = window.onresize;
	if (window.CopmboBoxOldEventListener != null) {
		window.cbOldClickListener = document.onmouseup;
		document.onmouseup = ComboBox_hidePopupWindows;
		document.onresize = ComboBox_RePosition;
		}
	else {
		window.cbOldClickListener = null;
		document.onmouseup = ComboBox_hidePopupWindows;
		window.onresize = ComboBox_RePosition;
		}
	}
function ComboBox_test()
{
	alert("RESULT");
}
function ComboBox_isClicked(e) {
	if (this.divname != null) {
		if (this.use_layers) {
			var clickX = e.pageX;
			var clickY = e.pageY;
			var t = document.layers[this.divname];
			if ((clickX > t.left) && (clickX < t.left+t.clip.width) && (clickY > t.top) && (clickY < t.top+t.clip.height)) {
				return true;
				}
			else { return false; }
			}
		else if (document.all) {
			var t = window.event.srcElement;
			while (t.parentElement != null) {
				if (t.id==this.divname) {
					return true;
					}
				t = t.parentElement;
				}
			return false;
			}
		else if (this.use_gebi && e) {
			var t = e.originalTarget;
			while (t.parentNode != null) {
				if (t.id==this.divname) {
					return true;
					}
				t = t.parentNode;
				}
			return false;
			}
		return false;
		}
	return false;
	}
function ComboBox_hideIfNotClicked(e) {
	if (this.autoHide && !this.isClicked(e)) { //
		this.hide();
		}
	}
function ComboBox_hidePopupWindows(e) {
	for (var i=0; i<ComboBoxObjects.length; i++) {
		if (ComboBoxObjects[i] != null) {
			var p = ComboBoxObjects[i];
			p.hideIfNotClicked(e);
			}
		}
	if(window.cbOldClickListener)
		window.cbOldClickListener(e);
	}
function ComboBox_item_Hover( item )
{
	i = item;
	if (!this.visible)
		return;
	if (this.anim)
	{
		animated = this.data[i][0];
		animated = animated.slice(0, animated.length-4);
		animated += "_a.gif";
		document.getElementById("im_"+i).src=animated;
	}
	document.getElementById(i+"_2").className="drop_down_hover"; 
	document.getElementById(i+"_1").className="drop_down_fhover"; 
	document.getElementById("im_"+i).className="drop_down_img_hover";
}
function ComboBox_item_Clear( item )
{
	i = item;
	if (!this.visible)
		return;
	if (this.anim)
		document.getElementById("im_"+i).src=this.data[i][0];
	document.getElementById(i+"_2").className="drop_down"; 
	document.getElementById(i+"_1").className="drop_downf"; 
	document.getElementById("im_"+i).className="drop_down_img";
}
function ComboBox_PreLoadImg( content )
{
	/*
	for(var i=0;i<content.length;i++)
	{
		var im = new Image();
		im.src = content[i][0];
	}
	*/
	this.ready = true;
}
function ComboBox_populate( content )
{
	this.LoadImg(content);
	this.data = content;
	this.content = "<table cellspacing='0' cellpadding='1' width='100%' class='dd_table'>";
	for(var i=0;i<content.length;i++)
		this.content += "<tr><td id='"+i+"_1' align='center' valign='middle' height='20' width='25' class='drop_downf' onClick='"+this.name+".setData("+i+");' onMouseOver='"+this.name+".Hover("+i+")'; onMouseOut='"+this.name+".Clear("+i+")'><img alt=\"cbe\" id='im_"+i+"' src='"+content[i][0]+"' alt='img' class='drop_down_img'></td><td id='"+i+"_2' height=15 class='drop_down' onClick='"+this.name+".setData("+i+");'  onMouseOver='"+this.name+".Hover("+i+")'; onMouseOut='"+this.name+".Clear("+i+")'>"+content[i][1]+"</td></tr>";
	this.content += "</table>";
	this.Height = (content.length*15);
}
function ComboBox_refresh() 
{
	if (this.divname != null) 
	{
		if (this.use_gebi) {
			document.getElementById(this.divname).innerHTML = this.content;
			}
		else if (this.use_css) { 
			document.all[this.divname].innerHTML = this.content;
			}
		else if (this.use_layers) { 
			var d = document.layers[this.divname]; 
			d.document.open();
			d.document.writeln(this.content);
			d.document.close();
			}
	}
}

function ComboBox_decimages()
{
	this.images--;
	if (this.images == 0)
		this.ready = true;
}

function ComboBox( )
{
	if (!window.ComboBoxIndex) { window.ComboBoxIndex = 0; }
	if (!window.ComboBoxObjects) { window.ComboBoxObjects = new Array(); }
	if (!window.cblistenerAttached) {
		window.cblistenerAttached = true;
		ComboBox_attachListener();
		}
	this.index = ComboBoxIndex++;
	ComboBoxObjects[this.index] = this;
	this.divname = null;
	this.width = 100;
	this.Height = 100;
	this.show = "name";
	this.anim = false;
	this.populated = false;
	this.visible = false;
	this.autoHide = true;
	this.focus = null;
	this.ready = false;
	this.display_cal = false;
	
	// Redirect?
	this.redirect = false;
	
	this.use_gebi = false;
	this.use_css = false;
	this.use_layers = false;
	this.images = 0;

	this.content = "";
	//getting div

	this.divname = arguments[0];
	this.anchor = arguments[1];
	this.name = arguments[2];
	this.focus = arguments[3];
	if ( arguments[4] )
		this.associate = arguments[4];
	
	if (document.getElementById) { this.use_gebi = true; }
	else if (document.all) { this.use_css = true; }
	else if (document.layers) { this.use_layers = true; }
	
	this.dropDown = ComboBox_DropDown;
	this.hide = ComboBox_hide;
	this.populate = ComboBox_populate;
	this.refresh = ComboBox_refresh;
	this.reposition = ComboBox_Position;
	this.autohide = ComboBox_autohide;
	this.hideIfNotClicked = ComboBox_hideIfNotClicked;
	this.isClicked = ComboBox_isClicked;
	this.LoadImg = ComboBox_PreLoadImg;
	this.setData = ComboBox_setData;
	this.Hover = ComboBox_item_Hover;
	this.Clear = ComboBox_item_Clear;
	this.ImgLoaded = ComboBox_decimages;
	// Methods and variables used with associative dropdowns
	this.assoc = combo_associate;
	this.populate_assoc = combo_populate_assoc;
	this.get_cal = get_cal_table;
	this.holder = '';
	this.title = '';
	this.arrow = '';
	this.create = combo_create;
	this.reposition();
}

function WebForm_Submit()
{
	if(!this)
	{
		alert("Can't call this function directly.");
		return false;
	}
	check = this.FormCheck();
	if ( check != true )
	{
		if( check == -1 )
		{
			alert(this.Error);
			return false;
		}
		alert("Invalid form field: "+check);
		return false;
	}
	if(this.method == "get")
	{
		query = this.action;
		for(i=0;i<this.data.length;i++)
		{
			el = this.GetElement(this.data[i][0]);
			if(i == 0)
				element = "?";
			else
				element = "&";
			if(el.type == "text" || el.type == "textarea")
				element += this.data[i][1] + "=" +el.value;
			else
				element += this.data[i][1] + "=" + el.innerHTML;
			element = element.replace(/\n/,"");
			query += element;
		}
		document.location = query;
		return true;
	}
	else if(this.method == "post")
	{
		form = "<form method='post' id='"+this.name+"' name='"+this.name+"' action='"+this.action+"'>\n";
		for(i=0;i<this.data.length;i++)
		{
			el = this.GetElement(this.data[i][0]);
			if((el.type == "text") || (el.type == "textarea"))
			{
				element = "<input type='text' name='"+this.data[i][1]+"' value='"+el.value+"'/>";
			}
			else
			{
				element = "<input type='text' name='"+this.data[i][1]+"' value='"+el.innerHTML+"'/>";
			}
			element = element.replace(/\n/,"");
			form += element+"\n";
		}
		form += "</form>";
		el = this.GetElement( this.anchor );
		el.innerHTML = form
		el = this.GetElement(this.name);
		el.submit();
		return true;
	}
	alert("Unknown submit method.");
	return false;
}

function WebForm_GetElement( element )
{
	if(!this)
	{
		alert("Can't call this function directly");
		return false;
	}
	if(this.use_gebi)
		el = document.getElementById(element);
	else if (this.use_css)
		el = document.all[element];
	else if (this.use_layers)
		el = document.layers[element];
	else
		return false;
	return el;
}

function WebForm_CheckIt( )
{
	if(!this)
	{
		alert("Can't call this function directly");
		return false;
	}
	for(i=0;i<this.data.length;i++)
	{
		if(this.use_gebi)
			el = document.getElementById(this.data[i][0]);
		else if(this.use_css)
			el = document.all[this.data[i][0]];
		else if(this.use_layers)
			el = document.layers[this.data[i][0]];
		else
			return this.data[i][0];
		if(!el)
			return this.data[i][0];
		//alert(el.tagName+" - ["+el.title+"] - ["+el.innerHTML+"]");
		if((el.tagName == "DIV") && (el.title != "") && (el.innerHTML == el.title))
			return -1;
		if((el.type == "text" || el.type == "textarea") && (el.title == "required") && (el.value == ""))
			return -1;
	}
	//return -1;
	return true;
}
function WebForm( )
{
	if (!window.WebFormsCount)	window.WebFormsCount = 0;
	if (!window.WebForms) window.WebForms = new Array();
	if(arguments.length < 5)
	{
		alert("Invalid arguments count passed.");
		return false;
	}
	
	if (document.getElementById) { this.use_gebi = true; }
	else if (document.all) { this.use_css = true; }
	else if (document.layers) { this.use_layers = true; }
		
	this.name = arguments[0];
	this.data = arguments[1];
	this.method = arguments[2];
	this.action = arguments[3];
	this.anchor = arguments[4];
	this.Error = "Please fill all required fields.";
	if((this.method != "post")&&(this.method != "get"))
	{
		alert("Invalid form method.");
		return false;
	}
	this.index = WebFormsCount++;
	WebForms[this.index] = this;
	
	this.Submit = WebForm_Submit;
	this.FormCheck = WebForm_CheckIt;
	this.GetElement = WebForm_GetElement;
}
/**
* Associate a ComboBox with a SELECT
*/
function combo_associate()
{
	this.associate = arguments[0];
}
/**
* Populate an associative dropdown
*/
function combo_populate_assoc()
{
	data_array = new Array();
	for ( i = 0; i < this.associate.length; i++ )
	{
		data_array[i] = new Array("images/search/bullet.gif", this.associate.options[i].text, this.associate.options[i].value);
	}
	this.populate(data_array);
	if ( this.display_cal )
		this.content = this.get_cal(this.display_cal[0].value, this.display_cal[1].value);
	this.associate.style.display = 'none';
	document.getElementById(this.anchor).style.display = 'block';
	this.setData(this.associate.selectedIndex);
}
/**
* Generate a calendar
*/
function get_cal_table(reqmonth, reqyear)
{
	date = new Date();
	date.setDate(1);
	date.setMonth(reqmonth-1);
	date.setFullYear(reqyear);
	
	if ( date.getDay() > 0 )
		date.setDate(date.getDate()-date.getDay()+1);
	else ( date.getDay() == 0 )
		date.setDate(date.getDate()-7);
	
	var string = '';
	var cycle = 0;
	
	string += '<table class="newcalendar" cellspacing="0" cellpadding="0"><tr>';
	if ( this.display_cal[2] )
	{
		for ( i = 0; i <= 6; i++ )
		{
			string += '<th>'+this.display_cal[2][i]+'</th>';
		}
		string += '</tr><tr>';
	}
	while ( (date.getMonth() <= reqmonth-1 && date.getFullYear() <= reqyear) || ( date.getMonth() == 11 && date.getFullYear() <= reqyear) )
	{
		if ( date.getDay() == 1 && cycle > 0 )
			string += '</tr><tr>';
		string += '<td>';
		if ( date.getMonth() == reqmonth-1 )
		{
			string += '<a href="javascript:'+this.name+'.setData('+ (date.getDate()-1) +');">';
			string += date.getDate();
			string += '</a>';
		}
		else
			string += '&nbsp;';
		string += '</td>';
		date.setDate(date.getDate() + 1);
		cycle++;
	}
	string += '</tr>';
	string += '</table>';
	return string;
}
/**
* Create the DIVs that represent a dropdown
*/
function combo_create()
{
	if ( this.associate )
	{
		// Get the parent node of the select box
		var parent = this.associate.parentNode;
		// Create an arrow
		this.arrow = document.createElement('div');
		this.arrow.className='arrow';
		// Create a title
		this.title = document.createElement('div');
		this.title.id = this.associate.id + '_title';
		// Create a holder
		this.holder = document.createElement('div');
		this.holder.id = this.associate.id + '_holder';
		this.holder.onclick = callDD;
		this.holder.style.display = 'none';
		this.holder.className = 'datedown';
		
		// get the title and the arrow into the holder
		void(this.holder.appendChild(this.title));
		void(this.holder.appendChild(this.arrow));
		// add the whole dropdown to the page
		void(parent.appendChild(this.holder));
	}
}
/**
* Global function for calling the dropDown() method of an associative ComboBox
*/
function callDD()
{
	if ( this.id.indexOf('_holder') == -1 )
		return;
	name = this.id.substring(0, this.id.indexOf('_holder'));
	eval('window.'+name+'.dropDown();');
}
/**
* Global function used to easily create associative Combos
* Example: window.df_month = AssociativeCombo('df_month', 'datedown dd_month', 'datetitle');
*/
function AssociativeCombo(name, dd_name)
{
	var object = new ComboBox(dd_name, name+'_holder', name, name+'_title');
	object.assoc(document.getElementById(name));
	object.create();
	if ( arguments[2] )
		object.holder.className = arguments[2];
	if ( arguments[3] )
		object.title.className = arguments[3];
	object.autohide();
	object.anim = true;
	object.populate_assoc();
	return object;
}
