	// common js functions
	
	var isIE = navigator.userAgent.indexOf('Internet Explorer') >= 0;
	var isN = document.getElementById && !document.all;

	function showElement(elmnt, display){show(elmnt, (!display ? "inline" : display));}
	function hideElement(elmnt){hide(elmnt);}
	
	function hide()
	{
		if(hide.arguments.length > 0)
		{
			var el = hide.arguments[0];
			el = (typeof(el) == "string") ? document.getElementById(el) : el;
			if(el && typeof(el) == "object") el.style.display = "none";
		}
		return false;
	}//hide
	
	function newsletterGo()
	{
		var email = document.getElementById('newsletter_email');
		if(!email.value || email.value == 'E-mail...')
		{
			alert('Please enter email address.');
			return false;
		}
		jQuery.post("/fitch/index.php", {cmd:"ajax_newsletter",email:email.value},
		  function(data){
		    if(parseInt(data) == 1)
		    {
		    	hide("newsletter_frm");
		    	show("newsletter_thk");
		    }
		    else alert("An error was happened. Please try again later or contact support.");
		  });
	}
	
	function show()
	{
		if(show.arguments.length > 0)
		{
			var display = show.arguments.length > 1? show.arguments[1] : "block";
			var el = show.arguments[0];
			el = (typeof(el) == "string") ? document.getElementById(el) : el;
			if(el && typeof(el) == "object") el.style.display = display;
		}
		return false;
	}//show

	function openPopup(url,winName,winWidth,winHeight,showScrollbars)
	{
		if(winName == null) winName = '';
		if(!(winWidth > 0)) winWidth = 700;
		if(!(winHeight > 0)) winHeight = 550;

		showScrolls = showScrollbars == true ? "1" : "0";

		if(url != null)
			window.open(url,winName,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=' + showScrolls + ',resizable=1,width=' + winWidth + ',height=' + winHeight);
	}//openPopup



	function deleteRecord(recordID, recordName)
	{
		if(recordID > 0 && (frm = document.getElementById('form_records')))
		{
			if(!recordName) recordName = "record";
			if(confirm("Are you sure you want to delete this " + recordName + "?"))
			{
				frm.elements.id_to_delete.value = recordID;
				frm.submit();
			}
		}
		return false;
	} // /deleteRecord()
	
	function publishRecord(recordID)
	{
		if(recordID > 0 && (frm = document.getElementById('form_records')))
		{
			frm.elements.id_to_publish.value = recordID;
			frm.submit();
		}
		return false;
	} // /publishRecord()
	
	function moveRecord(position, move_to)
	{
		if(position > 0 && (frm = document.getElementById('form_records')))
		{
			frm.elements.id_to_move.value = position;
			frm.elements.move_to.value = move_to;
			frm.submit();
		}
		return false;
	} // /moveRecord()
	
	function doAction(action, id)
	{
		if(id > 0 && (frm = document.getElementById('form_records')))
		{
			eval("frm.elements.id_to_"+action+".value = id");
			frm.submit();
		}
		return false;
	}//doAction



	////
	// moves up/down a record (changing position)
	//
	function changePosition(id, direction)
	{
		if (id > 0 && (direction == "move_up" || direction == "move_down") && (frm = document.getElementById('form_position')))
		{
			frm.elements.mode.value = direction;
			frm.elements.id_to_move.value = id;
			frm.submit();
		}

		return false;
	} // /changePosition()
	
	function submitForm(id, rf)
	{
		var frm = document.getElementById(id);
		if(!rf || validateFields(frm, rf)) frm.submit();
		return false;
	}//submitForm
	
	function include(filename)
	{
		var scripts = document.getElementsByTagName("script");
		for(var i in scripts) if(scripts[i].src == filename) return false;
		
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', filename);
    html_doc.appendChild(js);
    return false;
	}//include
	
	
	function printPage()
	{
    if(typeof(window.print) != 'undefined') window.print();
	}//printPage
	
	document.createElementCopy = document.createElement;
	document.createElement = function(tagName, name)
	{
		var obj;
		if(!name)
		{
			obj = document.createElementCopy(tagName);
		}
		else if(isIE)
		{
			obj = document.createElementCopy('<'+tagName+' name="'+name+'"/>');
		}
		else
		{
			obj = document.createElementCopy(tagName);
			obj.name = name;
		}
		return obj;
	}//createElement
	
	function remove(el)
	{
		el = (typeof(el) == "string") ? document.getElementById(el) : el;
		if(el) el.parentNode.removeChild(el);
		return false;
	}
	
	function setElementOpacity(elem, opacity)
	{
	  var opacityProperty = getOpacityProperty();	
	  if (!elem || !opacityProperty) return;
	  if (opacityProperty == "filter")
	  {
	    elem.style.filter = "alpha(opacity=" + (opacity * 100) + ")";
	  }
	  else
	  {
	    elem.style[opacityProperty] = opacity;
	  }
	}

	function getOpacityProperty()
	{
	  if (typeof document.body.style.opacity == 'string') return 'opacity'; // CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9)
	  else if (typeof document.body.style.MozOpacity == 'string') return 'MozOpacity'; // Mozilla 1.6-, Firefox 0.8 
	  else if (typeof document.body.style.KhtmlOpacity == 'string') return 'KhtmlOpacity'; // Konqueror 3.1, Safari 1.1
	  else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) return 'filter'; // Internet Exploder 5.5+
	  return false;
	}
	
	var currentSubNav, timeoutSubNav;
	function subNavInit()
	{
		var l = document.links, s;
		for(var i = 0; i < l.length; i++)
		{
			if(l[i].id && l[i].id.match(/^site_nav_\d+$/) && (s = document.getElementById("site_sub_nav_"+(l[i].id.replace(/[^\d]+/g, '')))))
			{
				l[i].onmouseover = function(){
					hideSubNav();
					currentSubNav = this.id.replace(/[^\d]+/g, '');
					showSubNav();
				};
				l[i].onmouseout = hideSubNavTimeout;
				s.onmouseover = clearSubNavTimeout;
				s.onmouseout = hideSubNavTimeout;
			}
		}
	}//subNavInit
	function hideSubNav(){clearSubNavTimeout();if(currentSubNav) hide(document.getElementById("site_sub_nav_"+currentSubNav));}
	function showSubNav(){show(document.getElementById("site_sub_nav_"+currentSubNav));}
	function hideSubNavTimeout(){timeoutSubNav = setTimeout("hideSubNav();", 100);}
	function clearSubNavTimeout(){if(timeoutSubNav) clearTimeout(timeoutSubNav);}
	
	function sortRecords()
	{
		document.form_filter.filter_by.value = document.getElementById("procedure_cat_id").value;
		document.getElementById('form_filter').submit();
		return false;
	}//sortRecords
	
	function siteSearchInput(i)
	{
		if(i.value == 'Search...') i.value = '';
	}//siteSearchInput
	
	function siteSearch(id)
	{
		var frm = document.getElementById(id);
		var i = frm.keyword;
		if(i.value && i.value != 'Search...')
		{
			//frm.submit();
			var s = "/search/keyword/"+i.value+"/";
			if(frm.type && frm.type.value) s += "type/"+frm.type.value+"/";
			if(frm.category && frm.category.value) s += "category/"+frm.category.value+"/";
			document.location = s;
			return false;
		}
		else alert("Please input keyword.");
	}//siteSearch
	
	function altBox()
	{
		var pat = /\balt_box\b/i;
		var tmp = document.getElementsByTagName("*"), alt_box_overlay;
		for(var i in tmp)
		{
			if(!(tmp[i].className
					 && tmp[i].className.match(pat)
					 && tmp[i].id
					 && document.getElementById("alt_box_overlay__"+tmp[i].id.split("__")[1]))) continue;
			tmp[i].onmouseover = function(){
				alt_box_overlay = document.getElementById("alt_box_overlay__"+this.id.split("__")[1]);
				show(alt_box_overlay);
			};
			document.onmousemove = function(eventN){
				if(alt_box_overlay)
				{
					var scrollTop = 0, scrollLeft = 0, b;
					if(document.body && (document.body.scrollTop || document.body.scrollLeft)) b = document.body;
					else if(document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) b = document.documentElement;
					if(b)
					{
						scrollTop = b.scrollTop;
						scrollLeft = b.scrollLeft;
					}
					alt_box_overlay.style.top = (isN ? eventN.pageY : scrollTop + event.clientY) + 5 + "px";
					alt_box_overlay.style.left = (isN ? eventN.pageX : scrollLeft + event.clientX) + 5 + "px";
				}
			};
			tmp[i].onmouseout = function(){hide(alt_box_overlay);alt_box_overlay = null;};
		}
	}//altBox
