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()
{
	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.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.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";
			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;
			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;
			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) {
		//document.onmouseup = new Function("window.ComboBoxOldEventListener(); ComboBox_hidePopupWindows();");
		//window.onresize = new Function("window.ComboBoxOldResize(); ComboBox_RePosition();");
		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) {
	//alert("Hiding combo boxes...");
	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 )
{
/*	if (this.use_gebi)
	{
		document.getElementById(this.focus).innerHTML = "Loading...";
	}
	else if (this.use_css) 
	{
		document.all[this.focus].innerHTML = "Loading...";
	}
	else if (this.use_layers)
	{
		document.layers[this.focus].innerHTML = "Loading...";
	}*/
	for(var i=0;i<content.length;i++)
	{
		var im = new Image();
		//im.onload = this.name+".ImgLoaded()";
		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='15' width='25' class='drop_downf' onClick='"+this.name+".setData("+i+");' onMouseOver='"+this.name+".Hover("+i+")'; onMouseOut='"+this.name+".Clear("+i+")'><img alt=\"cbe\" height=\"12\" width=\"22\" 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.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 (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;
	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;
		}
        alert(query);
		//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.tagName == "DIV") && (el.title == 'required') && (el.innerHTML == ''))
            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;
}

