function show_area(p) {
	v = p.options[p.selectedIndex].value;
	window.location = 'index.php?action=view_menu&id=' + v;
}

function swap_img(s, t) {
	s.src = t;
}

function toggle_name(n) {
	objs = document.getElementsByName(n);

	for(x = 0; x < objs.length; x++) {
		if(objs[x].style.display == '')
			objs[x].style.display = 'none';
		else
			objs[x].style.display = '';
	}
}

popup_count = 0;
function toggle_element(id) {
	
	if(typeof(dd) != "undefined") {
		if(dd.elements[id].visible) {
			popup_count--;
			dd.elements[id].setZ(1);
			dd.elements[id].hide();
			if(popup_count == 0)
				show_select();
		} else {
			popup_count++;
			dd.elements[id].maximizeZ();
			dd.elements[id].show();
			hide_select(id);
		}
	} else {
	
		obj = document.getElementById(id);
		if(obj.style.display == '') {
			popup_count--;
			obj.style.display = 'none';
			if(popup_count == 0)
				show_select();
		} else {
			popup_count++;
			obj.style.display = '';
			hide_select(id);
		}
	}
}


function toggle_element2(id) {
	obj = document.getElementById(id);
	if(obj.style.display == '') {
		popup_count--;
		obj.style.display = 'none';
		//if(popup_count == 0)
		//show_select();
	} else {
		popup_count++;
		obj.style.display = '';
		//hide_select(id);
	}
}

function moveBy(mId, tId, ofsx, ofsy) {
	if(!dd.elements[mId] || !dd.elements[tId])
		return false;
		
	ml = dd.elements[mId];
	mt = dd.elements[tId];

	ml.moveTo(mt.x + ofsx, mt.y + ofsy);
}

function hide_select(id) {
	for (j=0; j<document.forms.length; j++) {
		var theForm = document.forms[j]
		for(i=0; i<theForm.elements.length; i++){
			if(theForm.elements[i].type == "select-one")
				theForm.elements[i].style.visibility = "hidden";		
		}
	}

	var theObj = document.getElementById(id).getElementsByTagName("select");
	for(j=0; j<theObj.length; j++) 
		theObj[j].style.visibility = "visible";
}

function show_select() {
	for (j=0; j<document.forms.length; j++) {
		var theForm = document.forms[j]
		for(i=0; i<theForm.elements.length; i++){
			if(theForm.elements[i].type == "select-one")
				theForm.elements[i].style.visibility = "visible";			
		}
	}
}

function get_id_from_name(id) {
	num_id = id.substr(id.indexOf('_') + 1);
	return num_id;
}

function get_iframe(id) {
	var oIframe = document.getElementById(id);
	var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
	if (oDoc.document)
		oDoc = oDoc.document;
	return oDoc;
}

area_list_titles = [];
category_list_titles = [];
section_list_titles = [];

function category_assign_list(id, title) {
	if(id.match(/area/)) {
		type = "area";
		working_list = area_list_titles;
	} else if(id.match(/category/)) {
		type = "category";
		working_list = category_list_titles;
	} else if( id.match(/section/) ) {
		type = "section";
		working_list = section_list_titles;
	} else {
		return false;
	}
	
	num_id = get_id_from_name(id);
	
	found = false;
	for(key in working_list) {
		if(key == num_id)
			found = true;
	}

	iframe 	= get_iframe('assign_category_iframe');
	
	if( !title ) {
		cell	= document.getElementById(id);
		spans	= cell.getElementsByTagName('span');
		title 	= spans[0].innerHTML;
	}

	category_disp_list = iframe.getElementById('category_list_div');
	category_disp_list.innerHTML = '';
	category_list_var = iframe.assign_categories_form.categories;

	if(!found) {
		working_list[num_id] = title;
		category_list_var.value += type + "_" + num_id + ',';
	} else {
		clv 	= category_list_var.value;
		pos 	= clv.indexOf(type + "_" + num_id + ',');
		s 		= clv.substr(0, pos);
		e		= clv.substr(pos + (type + "_" + num_id).length + 1);
		category_list_var.value = s + e;
		delete working_list[num_id];
	}

	
	for(key in area_list_titles) {
		category_disp_list.innerHTML += "<a href='javascript: void(0);' onclick=\"parent.category_assign_list('" + "area" + "_" + key + "', '" + area_list_titles[key] + "')\">" + area_list_titles[key] + '<br>';
	}

	for(key in category_list_titles) {
		category_disp_list.innerHTML += "<a href='javascript: void(0);' onclick=\"parent.category_assign_list('" + "category" + "_" + key + "', '" + category_list_titles[key] + "')\">" + category_list_titles[key] + '<br>';
	}

	for(key in section_list_titles) {
		category_disp_list.innerHTML += "<a href='javascript: void(0);' onclick=\"parent.category_assign_list('" + "section" + "_" + key + "', '" + section_list_titles[key] + "')\">" + section_list_titles[key] + '<br>';
	}
	
	switch( type ) {
		case "area":
			area_list_titles = working_list;
		break;

		case "category":
			category_list_titles = working_list;
		break;
		
		case "section":
			section_list_titles = working_list;
		break;
	}
}

sponsor_list_titles = [];
function sponsor_assign_list(id, title) {
	found = false;
	for(key in sponsor_list_titles) {
		if(key == id)			
			found = true;		
	}

	iframe 	= get_iframe('assign_sponsor_iframe');
	
	sponsor_disp_list = iframe.getElementById('assigned_sponsors_div');
	sponsor_disp_list.innerHTML = '';
	sponsor_disp_list_var = iframe.sponsor_assign_form.sponsors;

	if(!found) {
		sponsor_list_titles[id] = title;
		sponsor_disp_list_var.value += id + ',';
	} else {
		clv 	= sponsor_disp_list_var.value;
		pos 	= clv.indexOf(id + ',');
		s 		= clv.substr(0, pos);
		e		= clv.substr(pos + id.toString().length + 1);
		sponsor_disp_list_var.value = s + e;		
		delete sponsor_list_titles[id];
	}

	for(key in sponsor_list_titles)
		sponsor_disp_list.innerHTML += "<div style='cursor: pointer' onclick=\"parent.sponsor_assign_list(" + key + ", '" + sponsor_list_titles[key] + "')\">" + sponsor_list_titles[key] + '</div>';	
}

function new_section(id,title) {
	if(!id.match(/area/))
		return false;

	document.getElementById('parent_area_div').innerHTML = title;
	document.new_section_form.parent_area.value	= get_id_from_name(id);
}

function new_category(id,title) {
	if(!id.match(/section/))
		return false;

	document.getElementById('parent_section_div').innerHTML = title;
	document.new_category_form.parent_section.value	= get_id_from_name(id);
}

function new_article(id,title) {
	if(!id.match(/category|section|area/))
		return false;

	document.getElementById('parent_item_div').innerHTML = title;
	num_id = id.substr(id.indexOf('_') + 1);
		
	document.new_article_form.item_id.value	= get_id_from_name(id);
	
	if(id.match(/category/))
		document.new_article_form.type.value = "category";
	if(id.match(/section/))
		document.new_article_form.type.value = "section";		
	if(id.match(/area/))
		document.new_article_form.type.value = "area";				
}

active_func = false;
function cell_click(id,t) {
	active_cell 	= id;
	active_title 	= t;

    if(active_func)
      	active_func(id,t);
}

function stats_selected_row(id) {
	obj = document.getElementById(id);
	css = obj.className;

	if(!css.match(/selected/))
		css += " selected";
	else
		css = css.replace(/selected/,'');
		
	obj.className = css;
}

function admin_location_change(ptr) {
	action = ptr.options[ptr.selectedIndex].value;	
	window.location = "index.php?action=" + action;
}

function add_class(ptr, c) {
	v = ptr.className;
	if(v.indexOf(c) > -1) {
		f = c;
		v = v.replace(f,'');
		ptr.className = v;
	} else
		ptr.className += ' ' + c;
}

function adjust_rating(x, rid) {	
	i 	= 0;	
	
	for(; i <= x; i++) {
		b = document.getElementById('rating_' + i + '_' + rid).style.background;
		if(b.indexOf('saved') == -1)
			document.getElementById('rating_' + i + '_' + rid).style.background = "url('images/stars/star_selected.png')";
	}
	for(; i < 5; i++) {
		b = document.getElementById('rating_' + i + '_' + rid).style.background;
		if(b.indexOf('saved') == -1)
			document.getElementById('rating_' + i + '_' + rid).style.background = "url('images/stars/star_empty.png')";		
	}
}

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

var http_calendar 	= createRequestObject(); 
var http_default 	= createRequestObject(); 
function add_rating(x, uid, art, rid) {
	http_default.open('get', 'actions/add_rating.php?rating=' + x + '&uid=' + uid + '&aid=' + art + '&junk=' + (new Date()).valueOf());
	http_default.send(null);	
	
	for(i = 0; i < 5; i++)
		document.getElementById('rating_' + i + '_' + rid).style.background = "url('images/stars/star_empty.png')";
	
	document.getElementById('rating_' + x + '_' + rid).style.background = "url('images/stars/star_saved.png')";		
	adjust_rating(x, rid);
}

function show_calendar() {
	if (http_calendar.readyState == 4) {
		response = http_calendar.responseText;
		document.getElementById('calendar_container').innerHTML = response;	
	}
}

function get_calendar(uid, month, year) {
	http_calendar.open('get', 'actions/personal_calendar/calendar.php?uid=' + uid + '&month=' + month + '&year=' + year + '&junk=' + (new Date()).valueOf());
	//document.getElementById('calendar_container').innerHTML = 'actions/personal_calendar/calendar.php?uid=' + uid + '&month=' + month + '&year=' + year + '&junk=' + (new Date()).valueOf();	
	http_calendar.onreadystatechange = show_calendar; 
	http_calendar.send(null);	
}

function show_events() {
	if(http_default.readyState == 4) {
		response = http_default.responseText;
		document.getElementById('event_container').innerHTML = response;	
	}
}

function get_calendar_events(uid, month, day, year) {
	http_default.open('get', 'actions/personal_calendar/events.php?uid=' + uid + '&day=' + day + '&month=' + month + '&year=' + year + '&junk=' + (new Date()).valueOf());
	http_default.onreadystatechange = show_events; 
	http_default.send(null);	
}

function change_date(uid) {
	m = document.getElementById('month_selector');
	y = document.getElementById('year_selector');
	
	a 	= m.options[m.selectedIndex].value;
	b	= y.options[y.selectedIndex].value;

	cm = a;
	cy = b;
	
	if( cm < 10 ) cm = "0" + cm;
	
	document.getElementById("printable_cal_cell_1").innerHTML = '<td align="left"><a href="actions/create_print_cal_pdf.php?month='+cm+'&year='+cy+'&type=c&uid='+uid+'" target="_blank"><img src="images/calendars/'+cm+''+cy+'c_t.png" border="0" width="108" height="75"></a></td>';
	document.getElementById("printable_cal_cell_2").innerHTML = '<td align="center"><span style="color:#d75927;"><span style="font-size:18px;">printable <b>calendars</b></span><br>(select color or b/w)</span></td>';
	document.getElementById("printable_cal_cell_3").innerHTML = '<td align="right"><a href="actions/create_print_cal_pdf.php?month='+cm+'&year='+cy+'&type=bw&uid='+uid+'" target="_blank"><img src="images/calendars/'+cm+''+cy+'bw_t.png" border="0" width="108" height="75"></a></td>';	
	
	get_calendar(uid, a, b);
}

date_cell = false;
function set_date(ptr, uid, year, month, day) {
	if(date_cell)
		add_class(date_cell, 'highlight');
		
	date_cell = ptr;
	add_class(date_cell, 'highlight');
	
	document.calendar_form.year.value 	= year;
	document.calendar_form.month.value  = month;
	document.calendar_form.day.value 	= day;
	
	document.getElementById('event_date').innerHTML = month + '/' + day + '/' + year;
	get_calendar_events(uid, month, day, year);
}

function confirm_event_add() {
	if(http_default.readyState == 4) {
		response = http_default.responseText;	
		response = response.split(',');		
		
		get_calendar_events(response[0],response[1],response[2],response[3]);
		get_calendar(response[0],response[1],response[3]);
	}
}

function add_event(uid) {
	frm = document.calendar_form;
	
	descriptionElement = frm.description;
	
	if( window.mce_editor_0 && window.mce_editor_0.document.body ) {
		descriptionElement = window.mce_editor_0.document.body;		
	}
	
	if((!frm.month.value || frm.month.value== "")) {
		alert('Please select a date for your event!');
		return false;
	}
	
	if((frm.hour.selectedIndex == 0 || frm.minute.selectedIndex == 0) && (!frm.all_day.checked)) {
		alert('Please select a time for your event!');
		return false;
	}
	
	mo 		= frm.month.value;
	d		= frm.day.value;
	y		= frm.year.value;
	all_day	= frm.all_day.checked;
	mi		= frm.minute.options[frm.minute.selectedIndex].value;
	h		= frm.hour.options[frm.hour.selectedIndex].value;
	title		= encodeURI(frm.title.value);
	description = encodeURI(descriptionElement.innerHTML);
	update_id	= frm.update_id.value;
		
	if( all_day ) {
		h = " 01";
		mi = "00";
	}
	
	parameters 	= 'uid=' + uid + '&update_id=' + update_id + '&title=' + title +	'&description=' + description + '&day=' + d + '&month=' + mo + '&year=' + y + '&minute=' + mi + '&hour=' + h + '&all_day= ' + all_day + '&junk=' + (new Date()).valueOf();
	
	// after update we are definately not updating again.
	frm.title.value 				= '';	
	descriptionElement.innerHTML 	= '';
	frm.hour.selectedIndex			= 0;
	frm.minute.selectedIndex		= 0;
//	frm.event_button.value					= "Add Event";	
	frm.update_id.value 			= '-1';
		
	http_default.open('post', 'actions/personal_calendar/add_event.php', true);
	http_default.onreadystatechange = confirm_event_add; 	
	http_default.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_default.setRequestHeader("Content-length", parameters.length);
	http_default.setRequestHeader("Connection", "close");
	http_default.send(parameters);
}

function add_metro_event(eid, uid) {	
	parameters = 'uid=' + uid + '&external=' + eid + '&junk=' + (new Date()).valueOf();
	
	http_default.open('post', 'actions/personal_calendar/add_event.php', true);	
	http_default.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_default.setRequestHeader("Content-length", parameters.length);
	http_default.setRequestHeader("Connection", "close");
	http_default.send(parameters);	
	
	toggle_element('event_add_' + eid);
	toggle_element('event_del_' + eid);	
}

function delete_metro_event(eid, cid) {
	http_default.open('get', 'actions/personal_calendar/delete_event.php?id=' + cid + '&junk=' + (new Date()).valueOf());	
	http_default.send(null);	
	
	toggle_element('event_add_' + eid);
	toggle_element('event_del_' + eid);
}

function confirm_delete_event() {
	if(http_default.readyState == 4) {
		response = http_default.responseText;	
		response = response.split(',');		
		
		get_calendar_events(response[0],response[1],response[2],response[3]);
		get_calendar(response[0],response[1],response[3]);		
	}
}

function delete_event(id) {
	http_default.open('get', 'actions/personal_calendar/delete_event.php?id=' + id + '&junk=' + (new Date()).valueOf());
	http_default.onreadystatechange = confirm_delete_event; 		
	http_default.send(null);	
}

function edit_event(mo, d, y, h, mi, title, description, id, all_day) {
	frm = document.calendar_form;

	descriptionElement = frm.description;
	
	if( window.mce_editor_0 && window.mce_editor_0.document.body ) {
		descriptionElement = window.mce_editor_0.document.body;		
	}

	frm.month.value 				= mo;
	frm.day.value 					= d;
	frm.year.value					= y;
	frm.hour.selectedIndex 			= h + 1;
	frm.minute.selectedIndex 		= mi / 15 + 1;
	frm.update_id.value				= id;
	
	frm.all_day.checked =			 all_day;
	
//	frm.event_button.value			= "Edit Event";
	
	frm.title.value 				= title;
	descriptionElement.innerHTML	= description;
}

function toggleEmailBubble( aSufix ) {
	if(!aSufix) aSufix = "";
		
	if( document.getElementById("email_bubble" + aSufix).style["display"] == "none" ) {
		showEmailBubble(aSufix);
	} else {
		hideEmailBubble(aSufix);
	}
}

function toggleZipBubble() {
	t = document.getElementById("zip_bubble");
	if(t.style["display"] == "none" )
		t.style.display = '';
	else
		t.style.display = 'none';	
}

function showEmailBubble(aSufix) {
	if(!aSufix) aSufix = "";

	document.getElementById("email_bubble" + aSufix).style["display"] = "block";
}

function hideEmailBubble(aSufix) {
	if(!aSufix) aSufix = "";

	document.getElementById("email_bubble" + aSufix).style["display"] = "none";
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function detectBrowser() {   
    var BO = new Object();   
    BO["ie"]        = false /*@cc_on || true @*/;   
    BO["ie4"]       = BO["ie"] && (document.getElementById == null);   
    BO["ie5"]       = BO["ie"] && (document.namespaces == null) && (!BO["ie4"]);   
    BO["ie6"]       = BO["ie"] && (document.implementation != null) && (document.implementation.hasFeature != null);   
    BO["ie55"]      = BO["ie"] && (document.namespaces != null) && (!BO["ie6"]);   
    /*@cc_on  
    BO["ie7"]       = @_jscript_version == '5.7';  
    @*/  
    BO["ns4"]       = !BO["ie"] &&  (document.layers != null) &&  (window.confirm != null) && (document.createElement == null);   
    BO["opera"]     = (self.opera != null);   
    BO["gecko"]     = (document.getBoxObjectFor != null);   
    BO["khtml"]     = (navigator.vendor == "KDE");   
    BO["konq"]      = ((navigator.vendor == 'KDE') || (document.childNodes) && (!document.all) && (!navigator.taintEnabled));   
    BO["safari"]    = (document.childNodes) && (!document.all) && (!navigator.taintEnabled) && (!navigator.accentColorName);   
    BO["safari1.2"] = (parseInt(0).toFixed == null) && (BO["safari"] && (window.XMLHttpRequest != null));   
    BO["safari2.0"] = (parseInt(0).toFixed != null) && BO["safari"] && !BO["safari1.2"];   
    BO["safari1.1"] = BO["safari"] && !BO["safari1.2"] && !BO["safari2.0"];   
    return BO;   
}   
  
var BO = new detectBrowser();  

function getStyle(el,styleProp) {
	x = el;
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
		
	return y;
}

function changeFont(d) {
	content = getElementsByClass('content');	
	els 	= content[0].getElementsByTagName('*');
	tag_list= "TD A DIV SPAN P";
	csstype = 'font-size';
	if(BO['ie'])
		csstype = "fontSize";
	for(i = 0; i < els.length; i++) {
		tag = new RegExp(els[i].tagName);
		if(tag_list.match(tag)) {
			
			s = getStyle(els[i],csstype);
			if(s && s.indexOf("px") > -1) {
				s = parseInt(s.substring(0, s.indexOf("px")));
				els[i].style.fontSize = (s + d) + "px";
			} else {
				s = parseInt(s.substring(0, s.indexOf("pt")));
				els[i].style.fontSize = (s + d) + "pt";
			}
		}
	}
}

function font_up() {
	changeFont(1);
}

function font_down() {
	changeFont(-1);
}

function buttonRollSwap( aElement, aElementSwap ) {
	
	aElement.style["display"] = "none";
	aElementSwap.style["display"] = "block";
}
function hideSelect(){
selects = document.getElementsByTagName("select");
 for (var i = 0; i < selects.length; i++) {
  o = selects[i];
   if (o.style) o.style.visibility = 'hidden';
 }
}

function restoreSelect(){
selects = document.getElementsByTagName("select");
 for (var i = 0; i < selects.length; i++) {
  o = selects[i];
   if (o.style) o.style.visibility = 'visible';
 }
}

function showOverlay(action) {
	var overlayElement = document.createElement("div");

	function getScrollXY() {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	  }
	  return [ scrOfX, scrOfY ];
	}
	
	
	var x,y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
	}
	else // Explorer Mac;
		 //would also work in Explorer 6 Strict, Mozilla and Safari
	{
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}			
	
	overlayElement.style["position"] = "absolute";
	
	overlayElement.style["left"] = "0px";
	
	overlayElement.style["top"] = "0px";
	
	overlayElement.style["width"] = x;
	overlayElement.style["height"] = y;
	
	document.body.appendChild(overlayElement);			
	
	var fnConn = new XHConn();
	
	var fnWhenDone = function (oXML) { overlayElement.innerHTML = oXML.responseText; hideSelect(); };
	
	var query_string = "action=" + action + "&top=" + parseInt(parseInt(getScrollXY()[1]) + parseInt(getScrollXY()[1]) * 0.1);
	
	fnConn.connect( "overlay.php", "GET", query_string, fnWhenDone);			
}
