var popups=new Array();

function openwin(width,height,scrollbars,url,name) {
	if (!(height<1000&&height>100)) {
		height=450;
	}
	if (!(width<2000&width>100)) {
		width=500;
	}

	newwin=window.open(url,name,"toolbar=0,status=0,directories=0,scrollbars="+scrollbars+",resizable=yes,menubar=0,location=0,width="+width+",height="+height);
}

function rollSetup() {
	/** string added to "on" state images */
	var post = "_o";
	/** fetch the image tag array from the DOM */
	var imgTags = document.getElementsByTagName("img");

	/** loop through the images for find appropriate preloads */
	for (i=0; i < imgTags.length; i++) {
		/** make sure there is a source defined */
		if (imgTags[i].src != '') {
			/** make sure the image tag has a class */
			tmpClass = imgTags[i].className;
			if (tmpClass) {
				/** match the image rollover class */  
				if (tmpClass.match("rollover")) {
					/** store the image source to the 'imgOut' attribute */    
					imgTags[i].imgOut = new Image();
					imgTags[i].imgOut.src = imgTags[i].src;

					/** pull apart the source string */
					srcLength = imgTags[i].src.length;

					srcName = imgTags[i].src.substr(0, srcLength-4);
					srcExt = imgTags[i].src.substr(srcLength-4, srcLength);

					/** store the image source to the 'imgOver' attribute */
					imgTags[i].imgOver = new Image();
					imgTags[i].imgOver.src = srcName + post + srcExt;

					/** set up the 'onmouseover' and 'onmouseout' event handlers */     
					imgTags[i].onmouseover = function() {
						this.src = this.imgOver.src;
					}
					imgTags[i].onmouseout = function() {
						this.src = this.imgOut.src;
					}
				}
			}
		}
	}
	/** fetch the image tag array from the DOM */
	var imgTags = document.getElementsByTagName("input");

	/** loop through the images for find appropriate preloads */
	for (i=0; i < imgTags.length; i++) {
		/** make sure there is a source defined */
		if (imgTags[i].src != '') {
			/** make sure the image tag has a class */
			tmpClass = imgTags[i].className;
			if (tmpClass) {
				/** match the image rollover class */  
				if (tmpClass.match("rollover")) {
					/** store the image source to the 'imgOut' attribute */    
					imgTags[i].imgOut = new Image();
					imgTags[i].imgOut.src = imgTags[i].src;

					/** pull apart the source string */
					srcLength = imgTags[i].src.length;

					srcName = imgTags[i].src.substr(0, srcLength-4);
					srcExt = imgTags[i].src.substr(srcLength-4, srcLength);

					/** store the image source to the 'imgOver' attribute */
					imgTags[i].imgOver = new Image();
					imgTags[i].imgOver.src = srcName + post + srcExt;

					/** set up the 'onmouseover' and 'onmouseout' event handlers */     
					imgTags[i].onmouseover = function() {
						this.src = this.imgOver.src;
					}
					imgTags[i].onmouseout = function() {
						this.src = this.imgOut.src;
					}
				}
			}
		}
	}
}

function fancySelectRollSetup() {
	var trTags = document.getElementsByTagName("div");

	/** loop through the images for find appropriate preloads */
	for (i=0; i < trTags.length; i++) {
		/** make sure the image tag has a class */
		tmpClass = trTags[i].className;
		if (tmpClass) {
			/** match the image rollover class */  
			if (tmpClass.match("fancy_select_item")) {
				trTags[i].trOut = "";

				trTags[i].trOver= "url(gfx/fancy_select_item_bg.jpg)";

				/** set up the 'onmouseover' and 'onmouseout' event handlers */     
				trTags[i].onmouseover = function() {
					this.style.backgroundImage= this.trOver;
				}
				trTags[i].onmouseout = function() {
					this.style.backgroundImage= this.trOut;
				}
			}
		}
	}

	var trTags = document.getElementsByTagName("body");
	trTags[0].onclick=function() {
		for (a=0;a<popups.length;a++) {
			o=document.getElementById(popups[a]+"_popup");
			o.style.visibility="hidden";
		}
	}
}

function trRollSetup() {
	var trTags = document.getElementsByTagName("tr");

	/** loop through the images for find appropriate preloads */
	for (i=0; i < trTags.length; i++) {
		/** make sure the image tag has a class */
		tmpClass = trTags[i].className;
		if (tmpClass) {
			/** match the image rollover class */  
			if (tmpClass.match("rollover")) {
				trTags[i].trOut = "";

				trTags[i].trOver= "url(gfx/tr_bg.jpg)";

				/** set up the 'onmouseover' and 'onmouseout' event handlers */     
				trTags[i].onmouseover = function() {
					this.style.backgroundImage= this.trOver;
				}
				trTags[i].onmouseout = function() {
					this.style.backgroundImage= this.trOut;
				}
			}
		}
	}
}

/* This doesn't work - infact it crashes the browser (both IE and FF) */
function greyBorderSetup() {
	var tags = document.getElementsByTagName("table");

	for (t=0; t < tags.length; t++) {
		if (tags[t].className=="grey_border") {
			o=tags[t];
			p=o.parentNode;

			table=document.createElement("table");
			table.setAttribute("cellpadding","0");
			table.setAttribute("cellspacing","0");
			table.style.backgroundColor="#979797";

			p.insertBefore(table,o);
			p.removeChild(o);

			tr=document.createElement("tr");
			table.appendChild(tr);
			td=document.createElement("td");
			tr.appendChild(td);
			img=document.createElement("img");
			img.src="gfx/grey_tl.jpg"
			td.appendChild(img);
			td=document.createElement("td");
			tr.appendChild(td);
			td=document.createElement("td");
			tr.appendChild(td);
			img=document.createElement("img");
			img.src="gfx/grey_tr.jpg";
			td.appendChild(img);

			tr=document.createElement("tr");
			table.appendChild(tr);
			td=document.createElement("td");
			tr.appendChild(td);
			td=document.createElement("td");
			td.appendChild(o);
			tr.appendChild(td);
			td=document.createElement("td");
			tr.appendChild(td);

			tr=document.createElement("tr");
			table.appendChild(tr);
			td=document.createElement("td");
			img=document.createElement("img");
			img.src="gfx/grey_bl.jpg";
			td.appendChild(img);
			tr.appendChild(td);
			td=document.createElement("td");
			tr.appendChild(td);
			td=document.createElement("td");
			img=document.createElement("img");
			img.src="gfx/grey_br.jpg"
			td.appendChild(img);
			tr.appendChild(td);
		}
	}
}

function findPos(obj) {
	var curleft=curtop=0;
	if (obj.offsetParent) {
		curleft=obj.offsetLeft;
		curtop=obj.offsetTop;
		while (obj=obj.offsetParent) {
			curleft+=obj.offsetLeft;
			curtop+=obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function buildFancySelect(name,args, width,height) {

	if (typeof(height)=="undefined") {
		height="200px";
	}
	else {
		height=height+"px";
	}
	if (typeof(width)=="undefined") {
		width="auto";
	}
	else {
		width=width+"px";
	}
		
	value='';
	label='';
	for (t=0;t<args.length;t++) {
		if (typeof(args[t])=='object') {
			if (args[t].length==3) {
				value=args[t][0];
				label=args[t][1];
			}
		}
	}

	document.write("<table cellpadding='0' cellspacing='0'><tr><td style='padding: 0; margin: 0;'><input type='text' class='fancy_select' name='"+name+"_label' id='"+name+"_label' value=\""+label+"\" readonly='readonly'></td>\n");
	document.write("<td style='padding: 0; margin: 0;'><a href=\"javascript: popupSelect('"+name+"')\"><img src='gfx/fancy_select_button.jpg'></a></td></tr></table>\n");
	document.write("<input type='hidden' name='"+name+"' id='"+name+"' value='"+value+"'>\n");

	document.write("<div style='position: absolute; visibility: hidden; overflow: auto; height: "+height+"; width: "+width+"; background-color: #ffffff; border: 1px solid #cccccc;' id="+name+"_popup>\n");

	for (t=0;t<args.length;t++) {
		if (typeof(args[t])=="object") {
			document.write("<div class='fancy_select_item' onclick=\"triggerSelect('"+name+"','"+args[t][0]+"','"+args[t][1]+"')\">");
			document.write(args[t][1]);
			document.write("</div>");
		}
	}

	document.write("</div>\n")

	popups.push(name);
}

function popupSetup() {
	for (t=0;t<popups.length;t++) {
		setPopupLocation(popups[t]);
	}
}

function setPopupLocation(name) {
	o=document.getElementById(name+"_label");
	p=document.getElementById(name+"_popup");
	a=findPos(o);
	p.style.left=""+(a[0]+10)+"px";
	p.style.top=""+(a[1]+20)+"px";
}

function popupSelect(name) {
	setPopupLocation(name);
	o=document.getElementById(name+"_popup");
	o.style.visibility="visible";
}

function triggerSelect(name,value,label) {
	o=document.getElementById(name);
	o.value=value;
	o=document.getElementById(name+"_label");
	o.value=label;
	o=document.getElementById(name+"_popup");
	o.style.visibility="hidden";
}

function inputBoxIEFix() {
	var tags = document.getElementsByTagName("input");

	for (i=0; i < tags.length; i++) {
		if (tags[i].className=="dataField") {
			d=document.createElement("div");
			d.className="dataField";
			p=tags[i].parentNode;
			p.insertBefore(d,tags[i]);
			d.appendChild(tags[i]);
			//p.removeChild(tags[i]);
			tags[i].style.backgroundImage="none";
			tags[i].style.backgroundColor="transparent";
			tags[i].style.paddingLeft="0px";
			tags[i].style.paddingRight="0px";
		}
	}
}

function pageSetup() {
	rollSetup();
	trRollSetup();
	popupSetup();
	fancySelectRollSetup();
}

window.onload=pageSetup;

