	// 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 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,n,w,h,s){
		if(n == null) n = '';
		if(!w) w = 700;
		if(!h) h = 550;
		s = s == true ? "1" : "0";
		if(url) window.open(url,n,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+s+',resizable=1,width='+w+',height='+h);
	}//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 archiveRecord(recordID){
		if(recordID > 0 && (frm = document.getElementById('form_records'))){
			frm.elements.id_to_archive.value = recordID;
			frm.submit();
		}
		return false;
	} //archiveRecord()

	function unarchiveRecord(recordID){
		if(recordID > 0 && (frm = document.getElementById('form_records'))){
			frm.elements.id_to_unarchive.value = recordID;
			frm.submit();
		}
		return false;
	} //unarchiveRecord()

	function changeStatus(recordID){
		if(recordID > 0 && (frm = document.getElementById('form_records'))){
			frm.elements.id_to_change_status.value = recordID;
			frm.submit();
		}
		return false;
	} //changeStatus()

	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



	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

	function remove(el){
		el = (typeof(el) == "string") ? document.getElementById(el) : el;
		if(el && el.parentNode) 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;
	}

	function lightBox(content, width, height){
		if(!content) return;
		if(!width) width = 100;
		if(!height) height = 100;
		$('.overlay').parent().remove();
		var o = overlay('#6060a4');
		var popup = createElements({
			tagName:"div",
			id: 'lightBox',
			innerHTML: content,
			"style.width": width + "px",
			"style.height": height + "px",
			onclick:function(){$('.overlay').parent().remove();}
		}, o);
		var t = Math.round(scrollHeight() + ((windowHeight() - popup.offsetHeight) / 2));
		var l = Math.round(scrollWidth() + ((windowWidth() - popup.offsetWidth) / 2));
		popup.style.top = (t < 0 ? 0 : t) + "px";
		popup.style.left = (l < 0 ? 0 : l) + "px";
		return false;
	}//lightBox

	var popupBoxzIndex = 9999;
	var popupBoxMoves = false;
	var popupBoxMovesEvent = false;
	function popupBox(parameters){
		if(!parameters) parameters = {};
		var t = this, popup, head, title, content, x, ontop = false, relative = false, loading, clear = '<div class="c"></div>';

		if(!popupBoxMovesEvent){
			$(document).mouseup(function(){popupBoxMoves = false;});
			$(document).mousemove(function(e){
				if(popupBoxMoves) popupBoxMoves.popup.offset({left: e.pageX-popupBoxMoves.offset.left, top: e.pageY-popupBoxMoves.offset.top});
				return true;
			});
		}
		popupBoxMovesEvent = true;

		t.close = function(){popup.remove();if(parameters.onclose) parameters.onclose();}
		t.loading = function(a){if(a) popup.addClass('loading');else popup.removeClass('loading');}
		t.title = function(a){title.text(a||'');}
		t.content = function(a){content.html(a||'').append(clear);}
		t.ontop = function(a){ontop = a;}
		t.relative = function(a){relative = a;}
		t.className = function(a){popup.removeClass().addClass('popupBox').addClass(a);}
		t.width = function(a){popup.width(a||'auto');content.width(a||'auto');}
		t.height = function(a){content.height(a||'auto');}
		t.left = function(a){
			if(typeof(a)!='number') a = $(document).scrollLeft() + ($(window).width() - popup.outerWidth()) / 2;
			var p = popup.offset();
			if(relative && $(relative).length) a += $(relative).offset().left;
			p.left = Math.max(0, a);
			popup.offset(p);
		}
		t.top = function(a){
			if(typeof(a)!='number') a = $(document).scrollTop() + ($(window).height() - popup.outerHeight()) / 2;
			var p = popup.offset();
			if(relative && $(relative).length) a += $(relative).offset().top;
			p.top = Math.max(0, a);
			popup.offset(p);
		}
		t.offset = function(a){
			t.left(a.left);t.top(a.top);}
		t.load = function(url, p){
			t.loading(true);
			$.get(url, p, function(data){
				t.content(data);
				t.loading();
			});
		}

		t.ontop(parameters.ontop);
		t.relative(parameters.relative);

		x = $('<a href="javascript:void(0);">X</a>');
		x.click(t.close);
		x = $('<div class="fr"></div>').append(x);
		title = $('<div class="fl pr10"></div>');
		head = $('<div class="popupBoxTitle"></div>').append(title).append(x).append(clear).mousedown(function(e){
			var a = popup.offset();
			popupBoxMoves = {popup:t, offset:{left:e.pageX-a.left, top:e.pageY-a.top}};
			return false;
		});
		content = $('<div class="popupBoxContent"></div>').append(clear);
		if(parameters.width) content.width(parameters.width);
		if(parameters.height) content.height(parameters.height);
		loading = $('<div class="popupBoxLoading"></div>').append(content);
		popup = $('<div class="popupBox"></div>').append(head).append(loading).css('zIndex', ontop ? popupBoxzIndex+1000 : popupBoxzIndex).mouseover(function(){
			if(!ontop) popup.css('zIndex', popupBoxzIndex++);
		});

		$('body').append(popup);
		t.title(parameters.title);t.content(parameters.content);
		t.width(parameters.width);t.height(parameters.height);
		t.left(parameters.left);t.top(parameters.top);
		t.className(parameters.className);
		t.element = popup;
		return t;
	}//popupBox

	function messageBox(m, t){
		new popupBox({title: t || 'Message', content: '<div class="center fs11 cs_green p10">'+(m || '&nbsp;')+'</div>', ontop: true, width: 300});
	}//messageBox

	function errorBox(m, t){
		new popupBox({title: t || 'Error', content: '<div class="center fs11 cs_red p10">'+(m || '&nbsp;')+'</div>', className: 'errorBox', ontop: true, width: 300});
	}//errorBox

	function siteTopMenu(){
		var t,m;
		function mopen(e){if(e.length){mcancelclosetime();if(!m||m.get(0)!=e.get(0)){mclose();m=e;m.slideDown('fast');}}}
		function mclose(){if(m){m.slideUp('fast');m=null;}}
		function mclosetime(){t=setTimeout(mclose,300);}
		function mcancelclosetime(){if(t){clearTimeout(t);t=null;}}
		$(document).click(mclose);
		$('#site_top_menu .item').each(function(i,e){
			$('a',e).first().mouseover(function(){mopen($('.items',e));});
			$('a',e).first().mouseout(mclosetime);
			$('.items',e).mouseover(mcancelclosetime);
			$('.items',e).mouseout(mclosetime);
		});
	}//siteTopMenu

	function initSearch(e, keyword, def){
		e = $(e);
		var t = def || 'Enter keyword';

		function stat(){
			return !e.val() || e.val() == t && e.hasClass('i');
		}//stat

		if(keyword){
			e.val(keyword);
			e.removeClass('i');
		}

		e.focus(function(){
			if(stat()){
				e.val('');
				e.removeClass('i');
			}
		});

		e.blur(function(){
			if(stat()){
				e.val(t);
				e.addClass('i');
			}
		});

		$(e.attr('form')).submit(function(){
			if(stat()){
				errorBox('Please enter keyword.');
				return false;
			}
		});

	}//initSearch

	function initBlankSearch(e, keyword){
		e = $(e);
		var t = '';

		function stat(){
			return !e.val() || e.val() == t && e.hasClass('i');
		}//stat

		if(keyword){
			e.val(keyword);
			e.removeClass('i');
		}

		e.focus(function(){
			if(stat()){
				e.val('');
				e.removeClass('i');
			}
		});

		e.blur(function(){
			if(stat()){
				e.val(t);
				e.addClass('i');
			}
		});

	}//initBlankSearch

	var _ChangeCenters;
	function ChangeCenters(e){
		if(_ChangeCenters) return;
		e = $(e).offset();
		_ChangeCenters = new popupBox({
			title: ($('#user_favorite_center').html() == 'N/A' ? 'Select' : 'Change') + ' Your Preferred Center',
			width: 360,
			height: 42,//+230
			//relative: e,
			top: e.top+22,
			left: e.left-243,
			onclose: function(){_ChangeCenters=false;}
		});
		_ChangeCenters.load('/ajax/change-center/');
	}//ChangeCenters

	function ChangeCentersSubmit(frm){
		_ChangeCenters.loading(true);
		$.post('/ajax/change-center/', {keyword:frm.keyword.value}, function(data){
			_ChangeCenters.content(data);
			_ChangeCenters.height(272);
			_ChangeCenters.loading();
		});
		return false;
	}//ChangeCentersSubmit

	function ChangeCentersApply(id, t, u, img){
		_ChangeCenters.loading(true);
		_ChangeCenters.close();
		user_favorite_center(id, t, u, img);
	}//ChangeCentersApply

	function user_favorite_center(id, t, u, img){
		if(img) $('#user_favorite_center_image_small').attr('src', '/images/centers/orig/'+img).show();
		else $('#user_favorite_center_image_small').hide();
		$('#user_favorite_center').html('<a class="b fs14 cs_green" href="/centers/'+u+'/">'+t+'</a>');
		$('#change_user_favorite_center').html('Change Centers');
		$('#fav_center').html(t);
		setCookie('user_favorite_center', id, 1000, "/");
		//messageBox('Your Center was changed successfully.', 'Change Your Preferred Center');
	}//user_favorite_center

	var OVERLAY;
	function overlay(c,o){
		if(OVERLAY) remove(OVERLAY);
		if(!c)c="#000000";if(!o)o=50;
		var bg=createElements({tagName:"div",className:"overlay",onclick:function(){remove(this.parentNode);},"style.backgroundColor":c,"style.width":documentWidth()+"px","style.height":documentHeight()+"px"});
		setOpacity(bg,o);
		OVERLAY = createElements({tagName:"div",childNodes:bg}, document.body);
		return OVERLAY;
	}//overlay



	function makeCols(bs,cs){
		var n=0,c,weights=[],blocks=$(bs);
		blocks.each(function(i,e){weights[i]=$(e).outerHeight(true);});
		c=Math.round(eval(weights.join('+'))/$(cs).length);
		blocks.each(function(i,e){$(cs+':eq('+Math.min(Math.floor(n/c),Math.floor((n+=weights[i])/c))+')').append(e);});
	}//makeCols


	function setOpacity(e, v){
	  var p = getOpacityProperty();
	  if(!e || !p) return;
		if(v > 100) v = 100;
		else if(v < 0) v = 0;
	  if(p == "filter") e.style.filter = "alpha(opacity=" + v + ")";
	  else e.style[p] = v / 100;
	}//setOpacity

	function getOpacity(e){
		var r, p = getOpacityProperty();
	  if(!e || !p) return;
	  if(p == "filter" && (r = e.style.filter)) r = r.match(/opacity=([\d\.]+)/)[1];
	  else if(r = e.style[p]) r = r * 100;
	  r = r ? r : 100;
	  return r;
	}//getOpacity

	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;
	}//getOpacityProperty

	function unique(){return String(Math.random()).replace("0.","")+"_"+(new Date()).getTime();}

	function createElements(o, p){
		if(typeof(p) == "string") p = document.getElementById(p);
		function create(e){
			var el;
			if(typeof(e.parentNode) != 'undefined') el = e;
			else{
				el = createNewElement(e.tagName, e.name ? e.name : "");
				for(var i in e){
					if(i == "name" || i == "tagName") continue;
					else if(i == "childNodes") createElements(e["childNodes"], el);
					else if(i == "innerHTML" && typeof(e[i]) == "object") el.appendChild(e[i]);
					else eval("el."+i+"=e[i];")
				}
			}
			return el;
		}//create

		var elm;
		if(o.tagName) o = [o];
		for(var i in o){
			elm = create(o[i]);
			if(p) p.appendChild(elm);
		}
		return elm;
	}//createElements
function createNewElement(t,n){if(!n)return document.createElement(t);var e=null;try{e=document.createElement('<'+t+' name="'+n+'">');}catch(e){}
if(!e||e.nodeName!=t.toUpperCase()){e=document.createElement(t);e.name=n;}return e;}
function scrollHeight(){
	var r = 0;
	if(typeof(window.pageYOffset) == 'number') r = window.pageYOffset;
  else if(document.body && document.body.scrollTop) r = document.body.scrollTop;
  else if(document.documentElement && document.documentElement.scrollTop) r = document.documentElement.scrollTop;
  return r;
}
function documentHeight(){return Math.max(Math.max(document.body.scrollHeight,document.documentElement.scrollHeight),Math.max(document.body.offsetHeight,document.documentElement.offsetHeight),Math.max(document.body.clientHeight,document.documentElement.clientHeight));}
function documentWidth(){return Math.max(Math.max(document.body.scrollWidth,document.documentElement.scrollWidth),Math.max(document.body.offsetWidth,document.documentElement.offsetWidth),Math.max(document.body.clientWidth,document.documentElement.clientWidth));}
function scrollWidth(){
	var r = 0;
	if(typeof(window.pageXOffset) == 'number') r = window.pageXOffset;
  else if(document.body && document.body.scrollLeft) r = document.body.scrollLeft;
  else if(document.documentElement && document.documentElement.scrollLeft) r = document.documentElement.scrollLeft;
  return r;
}
function windowHeight(){
	if(typeof document.clientHeight == "number") return document.clientHeight;
	else if(document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
  return document.body.clientHeight;
}
function windowWidth(){
	if(typeof document.clientWidth == "number") return document.clientWidth;
	else if(document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
  return document.body.clientWidth;
}
function setCookie(c_name,value,expiredays,domain){var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+"="+escape(value)+((typeof expiredays == "undefined") ? "" : "; expires="+exdate.toGMTString())+(domain?"; path="+domain:"");}
function getCookie(c_name){if(document.cookie.length>0){c_start=document.cookie.indexOf(c_name + "=");
if(c_start!=-1){c_start=c_start + c_name.length+1;c_end=document.cookie.indexOf(";",c_start);
if(c_end==-1) c_end=document.cookie.length;return unescape(document.cookie.substring(c_start,c_end).replace(/\+/g, " "));}}return"";}
