// JavaScript Document
var fm_ajaxURL="/f/apps/forum/fm_ajax.php";
var st_ajaxURL="/f/apps/sticker/st_ajax.php";
var ajax_boxStartingTop=0;
var ajax_boxStartingLeft=0;
/************************************** 
ajax 
**************************************/
function GetXmlHttpObject()
{
	try
	{ // Firefox, Opera 8.0+, Safari
		var fm_xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{ // Internet Explorer
		try
		{
			fm_xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		 catch (e)
		{
			try 
			{
				fm_xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX, please try with another browser or on different computer!");
			}
		}
	}
	return fm_xmlHttp;
}
/**************************************************************************/
//end of ajax
/**************************************************************************/
function fm_getOffsetTop(elm) 
{
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;	
	while(mOffsetParent)
	{
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}	
	return mOffsetTop;
}
function fm_getOffsetLeft(elm) 
{
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;

	while(mOffsetParent)
	{
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}
function fm_colse(id_name)
{
	document.getElementById(id_name).innerHTML='loading...';
	document.getElementById(id_name).style.display='none';
}
function fm_closeBox(id_name)
{
	return "<div id='"+id_name+"_move' style='cursor:move;float:left;padding:2px' class='fm_btn'>Move</div><div align=right style='padding:0 0 2px 5px;'> <img src='/images/close_window.gif' onclick=\"fm_colse('"+id_name+"')\" style='cursor:pointer'></div>";
	//return "<table cellspacing=0 cellpadding=0 border=0><tr><td><div id='"+id_name+"_move' style='cursor:move' class='fm_btn' style='padding:3px'>Move</div></td><td align=right style='padding:0 0 2px 5px;'> <img src='/images/close_window.gif' onclick=\"fm_colse('"+id_name+"')\" style='cursor:pointer'></td></tr></table>";
}
function isArray(obj) {
    return obj.constructor == Array;
}

//type 0 box showing right side cur obj, 1 same left and top as cur obj
//array show in the middle of the page 0 is width 1 is height
//type <0 box showing lef side cur obj, abs(type) is the width of cur obj
//if url =-1 no ajax
function fm_contenBoxMaker(id_name,url,cur_obj,type,frame_html)
{
	if (document.getElementById(id_name)==null)
	{
		var fm_frame=document.createElement('div');//frame
		document.body.appendChild(fm_frame);
		fm_frame.id=id_name;
		fm_frame.style.position="absolute";	
		fm_frame.style.textAlign="left";	
		fm_frame.style.backgroundColor="#FFFFFF";	
		fm_frame.style.border="2px solid #88b6f2";	
		fm_frame.style.padding="5px";	
	}		
	else	var fm_frame=document.getElementById(id_name);
	//set size
	if (ajax_boxStartingTop)
		fm_frame.style.top=ajax_boxStartingTop;
	else if (isArray(type))	
		fm_frame.style.top=(fm_getYOffset()+(document.documentElement.clientHeight-type[1])/2)+"px";		
	else	fm_frame.style.top=fm_getOffsetTop(cur_obj)+"px";	
	
	if (ajax_boxStartingLeft)
		fm_frame.style.left=ajax_boxStartingLeft;
	else
	{
		if (isArray(type))	fm_frame.style.left=(fm_getXOffset()+(document.documentElement.clientWidth-type[0])/2)+"px";
		else if (type<0)	fm_frame.style.left=(fm_getOffsetLeft(cur_obj)-Math.abs(parseInt(type))-15)+"px";
		else if (type==1)	fm_frame.style.left=fm_getOffsetLeft(cur_obj)+"px";
		else	fm_frame.style.left=(fm_getOffsetLeft(cur_obj)+cur_obj.offsetWidth+5)+"px";
	}
	fm_frame.style.display="block";
	if (url==-1)	
	{
		fm_frame.innerHTML=fm_closeBox(id_name)+frame_html;
		if (id_name)	Drag.init(document.getElementById(id_name+"_move"),document.getElementById(id_name));
	}	
	else
	{
		fm_frame.innerHTML="<center>Loading...</center>";
		fm_getAjaxContent(fm_frame,url,id_name);
	}
	
}
//id_name nothing, with value show close btn
//id_name -1 add value to innerHTML
function fm_getAjaxContent(obj,url,id_name)
{
	var fm_xmlHttp=GetXmlHttpObject();
	if (fm_xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	} 
	url=url+"&y="+Math.random();
	fm_xmlHttp.onreadystatechange=function (){
		if (fm_xmlHttp.readyState==4 || fm_xmlHttp.readyState=="complete")
		{
			if (id_name==-1)	obj.innerHTML+=fm_xmlHttp.responseText;
			else if (id_name)	
			{
				obj.innerHTML=fm_closeBox(id_name)+fm_xmlHttp.responseText;
				if (id_name)	Drag.init(document.getElementById(id_name+"_move"),document.getElementById(id_name));
			}
			else	obj.innerHTML=fm_xmlHttp.responseText;
		}
	};
	fm_xmlHttp.open("GET",url,true);
	fm_xmlHttp.send(null);	
}
function fm_poorTasteViewer(type,id,cur_obj)
{
	fm_contenBoxMaker('fm_poorTasteViewer',fm_ajaxURL+"?type=3&option=2&entry_type="+type+"&id="+id,cur_obj,1,"");
}
function fm_closepoorTasteViewer()
{
	document.getElementById('fm_poorTasteViewer').innerHTML='loading...';
	document.getElementById('fm_poorTasteViewer').style.display="none";
}
function fm_firePoorTaste(type,id,poor)
{
	var rt_xmlHttp=GetXmlHttpObject();
	if (rt_xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	} 
	var url=fm_ajaxURL+"?type="+type+"&option=1&id="+id+"&poor="+poor;
	url=url+"&y="+Math.random();
	rt_xmlHttp.onreadystatechange=function (){
		if ((rt_xmlHttp.readyState==4 || rt_xmlHttp.readyState=="complete") && rt_xmlHttp.responseText)
		{
			fm_closePoorTaste();
			if (rt_xmlHttp.responseText)	alert(rt_xmlHttp.responseText);
			else alert('Ajax error please contact site admin');
		}
	};
	rt_xmlHttp.open("GET",url,true);
	rt_xmlHttp.send(null);	

}
function fm_poorTaste(type,id,cur_obj)
{
	var id_name='fm_poorTaste';
	if (document.getElementById(id_name)==null)
	{
		var fm_frame=document.createElement('div');//frame
		document.body.appendChild(fm_frame);
		fm_frame.id=id_name;
		fm_frame.style.position="absolute";	
		fm_frame.style.textAlign="center";	
		fm_frame.style.backgroundColor="#FFFFFF";	
		fm_frame.style.border="2px solid #88b6f2";	
		fm_frame.style.padding="5px";	
	}		
	else	var fm_frame=document.getElementById(id_name);
	//set size
	fm_frame.style.top=fm_getOffsetTop(cur_obj)+"px";	
	fm_frame.style.left=(fm_getOffsetLeft(cur_obj)-150)+"px";
	fm_frame.style.display="block";	
	
	var fm_xmlHttp=GetXmlHttpObject();
	if (fm_xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	} 
	var url=fm_ajaxURL+"?type=3&option=1";
	url=url+"&y="+Math.random();
	fm_xmlHttp.onreadystatechange=function (){
		if ((fm_xmlHttp.readyState==4 || fm_xmlHttp.readyState=="complete") && fm_xmlHttp.responseText)
		{
			fm_frame.innerHTML=fm_xmlHttp.responseText
			fm_frame.innerHTML+="<input type='button' value='Submit' onclick=\"fm_firePoorTaste("+type+","+id+",document.getElementById('fm_poorTasteOption').value)\" class='fm_btn'> <input type='button' value='Cancel' onclick=\"fm_closePoorTaste()\" class='fm_btn'>";
		}
	};
	fm_xmlHttp.open("GET",url,true);
	fm_xmlHttp.send(null);	
}
function fm_closePoorTaste()
{
	document.getElementById('fm_poorTaste').innerHTML="loading..";
	document.getElementById('fm_poorTaste').style.display="none";
}
function fm_imagePreview(cur_obj,path,is_video)
{
	if (path)
	{
		var id_name='fm_imageReviewer';
		if (document.getElementById(id_name)==null)
		{
			var fm_frame=document.createElement('div');//frame
			document.body.appendChild(fm_frame);
			fm_frame.id=id_name;
			fm_frame.style.position="absolute";		
		}		
		else	var fm_frame=document.getElementById(id_name);
		//set size
		fm_frame.style.top=fm_getOffsetTop(cur_obj)+"px";	
		fm_frame.style.left=(fm_getOffsetLeft(cur_obj)+cur_obj.offsetWidth+5)+"px";
		if (is_video)	fm_frame.innerHTML="<embed src=\""+path+"\" flashvars=\"\" quality=\"high\" scale=\"noscale\" width=\"320\" height=\"200\"  salign=\"LT\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"  wmode=\"transparent\">";
		else fm_frame.innerHTML="<img src='"+path+"'>";
		fm_frame.style.display="block";
	}
}
function fm_imagePreviewClose()
{
	document.getElementById('fm_imageReviewer').innerHTML="loading..";
	document.getElementById('fm_imageReviewer').style.display="none";
}	
function fm_gotoPage(page_url,obj_id,t_pg)
{
	document.getElementById(obj_id)
	var pg_n=document.getElementById(obj_id).value;
	pg_n=parseInt(pg_n);
	if (pg_n>t_pg) pg_n=t_pg;
	if (pg_n>1)	page_url+=("page"+pg_n+"/");
	window.location.href=page_url;
}
function fm_gotoPageGet(page_url,obj_id,t_pg)
{
	document.getElementById(obj_id)
	var pg_n=document.getElementById(obj_id).value;
	pg_n=parseInt(pg_n);
	if (pg_n>t_pg) pg_n=t_pg;
	page_url+=pg_n;
	window.location.href=page_url;
}
function fm_gotoGetPage(page_url,obj_id,t_pg)
{
	document.getElementById(obj_id)
	var pg_n=document.getElementById(obj_id).value;
	pg_n=parseInt(pg_n);
	if (pg_n>t_pg) pg_n=t_pg;
	if (pg_n>1)	page_url+=("&pager_id="+pg_n);
	window.location.href=page_url;
}
function fm_scrollto(id)
{
	window.scrollTo(fm_getOffsetLeft(document.getElementById(id)),fm_getOffsetTop(document.getElementById(id)))
}
function st_hideShow(cur_obj,id)
{
	fm_contenBoxMaker('st_fullMessage',st_ajaxURL+"?option=1&id="+id,cur_obj,1,"");
}
function st_loadMessages(total,cur_page,mpp,group_id)
{
	var fm_frame=document.getElementById('st_messageDisplay');
	var url=st_ajaxURL+"?option=2&total="+total+"&cur_page="+cur_page+"&mpp="+mpp+"&group_id="+group_id;
	fm_getAjaxContent(fm_frame,url,0);
}
function st_sticky(id)
{
	var rt_xmlHttp=GetXmlHttpObject();
	if (rt_xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	} 
	var url=st_ajaxURL+"?option=3&id="+id;
	url=url+"&y="+Math.random();
	rt_xmlHttp.onreadystatechange=function (){
		if ((rt_xmlHttp.readyState==4 || rt_xmlHttp.readyState=="complete") && rt_xmlHttp.responseText)
		{
			if (rt_xmlHttp.responseText)	location.reload();
			else alert('Ajax error please contact site admin');
		}
	};
	rt_xmlHttp.open("GET",url,true);
	rt_xmlHttp.send(null);	

}
function fr_addFrindForm(friend_id,cur_obj)
{
	fm_contenBoxMaker("fr_frame","/f/apps/friends/fr_ajax.php?option=1&friend_id="+friend_id,cur_obj,1,"");	
}
function fr_acceptFriend(id)
{
	var rt_xmlHttp=GetXmlHttpObject();
	if (rt_xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	} 
	var url="/f/apps/friends/fr_ajax.php?option=2&id="+id;
	url=url+"&y="+Math.random();
	rt_xmlHttp.onreadystatechange=function (){
		if ((rt_xmlHttp.readyState==4 || rt_xmlHttp.readyState=="complete") && rt_xmlHttp.responseText)
		{
			if (rt_xmlHttp.responseText==1)	
			{
				alert("You have accepted the request");
				location.reload();
			}
			else alert('Ajax error please contact site admin');
		}
	};
	rt_xmlHttp.open("GET",url,true);
	rt_xmlHttp.send(null);	
}
function fm_getYOffset() 
{
    var pageY;
    if(typeof(window.pageYOffset)=='number') 
	{	pageY=window.pageYOffset;}
    else {	pageY=document.documentElement.scrollTop;}
    return pageY;
}
function fm_getXOffset() 
{
    var pageX;
    if(typeof(window.pageXOffset)=='number') 
	{	pageX=window.pageXOffset;}
    else {	pageX=document.documentElement.scrollLeft;}
    return pageX;
}
function fr_replyBox(url,mode)
{
	var frame_width=500;
	if (mode)	var frame_height=560;
	else	var frame_height=590;
	var id_name="fm_replybox";
	if (document.getElementById(id_name)==null)
	{
		var fm_frame=document.createElement('div');//frame
		document.body.appendChild(fm_frame);
		fm_frame.id=id_name;
		fm_frame.style.position="absolute";	
		fm_frame.style.textAlign="left";	
		fm_frame.style.backgroundColor="#FFFFFF";	
		fm_frame.style.border="2px solid #88b6f2";	
		fm_frame.style.padding="5px";	
	
	}		
	else	var fm_frame=document.getElementById(id_name);
	//set size
	fm_frame.style.top=(fm_getYOffset()+(document.documentElement.clientHeight-frame_height)/2)+"px";	
	fm_frame.style.left=(fm_getXOffset()+(document.documentElement.clientWidth-frame_width)/2)+"px";	
	
	fm_frame.style.display="block";
	fm_frame.innerHTML=fm_closeBox(id_name)+"<iframe id='fm_topicFrame' src=\""+url+"\" frameborder=0 scrolling='no' allowtransparency='true' width='"+frame_width+"px' height='"+frame_height+"px'></iframe>";
	Drag.init(document.getElementById(id_name+"_move"),document.getElementById(id_name));
	
}
function pr_showState(obj)
{
	fm_getAjaxContent(document.getElementById('pr_stateBox'),"/f/apps/profile/pr_ajax.php?option=1&id="+obj.options[obj.selectedIndex].value,0);
}
var Drag = {
	obj : null,move : null,
	init : function(o,target)
	{//target is the moving target instead of o, mousedown on o but move target
		Drag.move=o;
		Drag.move.onmousedown= Drag.start;	
		if (target)	Drag.obj=target;
		else Drag.obj=o;
	},

	start : function(e)
	{
		//var o =Drag.obj= this;
		e = Drag.fixE(e);
		var y = parseInt(Drag.obj.style.top);
		var x = parseInt(Drag.obj.style.left);
		Drag.obj.lastMouseX	= e.clientX;
		Drag.obj.lastMouseY	= e.clientY;
		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;
		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		
		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(Drag.obj.style.top);
		var x = parseInt(Drag.obj.style.left);
		var nx, ny;

		nx = x + ex - Drag.obj.lastMouseX;
		ny = y + ey - Drag.obj.lastMouseY;

		Drag.obj.style.left = nx + "px";
		Drag.obj.style.top= ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;		
		ajax_boxStartingTop=parseInt(Drag.obj.style.top);
		ajax_boxStartingLeft=parseInt(Drag.obj.style.left);
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};
function day_filler(day_id,month_id,year_id)
{			
	var day=document.getElementsByName(day_id)[0];
	var month=document.getElementsByName(month_id)[0];
	var year=document.getElementsByName(year_id)[0];
	//in javascript Jan is 0	
	var days=32-new Date(year.options[year.selectedIndex].value,month.options[month.selectedIndex].value-1 , 32).getDate();
	var selected_day=day.selectedIndex;
	//alert(selected_day);
	if (day.length>1)
	{//clear the existing list			
		//remove from the last to the first
		for (i=day.length;i>=0;i--)
			 day.remove(i)
	}	
	for (i=1;i<=days;i++)
	{//add new ones
		var add_day=document.createElement('option');
		add_day.text=i;
		add_day.value=i;
		try
		{
			day.add(add_day,null) //standards
		}
		catch (ex)
		{
			day.add(add_day) //only for IE
		}
	}
	//set the selection back
	if (selected_day<day.length) 
		day.selectedIndex=selected_day;
}
function st_addMessage(cur_obj,playgroup_id,edit_id)
{
	var width=501;
	var height=380;
	var type=0;
	if (edit_id)	type=new Array(width,height);	
	fm_contenBoxMaker('st_addMesgBox',-1,cur_obj,type,"<iframe src=\"/f/apps/sticker/st_mesg_edit.php?playgroup_id="+playgroup_id+"&edit="+edit_id+"\" frameborder=0 scrolling='no' allowtransparency='true' width='"+width+"px' height='"+height+"px'></iframe>")
}
function rt_imagePreview(cur_obj,path)
{
	fm_contenBoxMaker('rt_ajaxContentViewer',-1,cur_obj,0,"<img src='"+path+"'>");
}
function pr_composeMesg(cur_obj,user_id)
{
	var width=501;
	var height=420;
	type=new Array(width,height);	
	fm_contenBoxMaker('st_addMesgBox',-1,cur_obj,type,"<iframe src=\"/f/apps/message/ms_iframeTopic.php?user_id="+user_id+"\" frameborder=0 scrolling='no' allowtransparency='true' width='"+width+"px' height='"+height+"px'></iframe>");
}
function pr_reply(cur_obj,topic_id)
{
	var width=280;
	var height=290;
	type=new Array(width,height);	
	fm_contenBoxMaker('st_addMesgBox',-1,cur_obj,type,"<iframe src=\"/f/apps/message/ms_reply.php?topic_id="+topic_id+"\" frameborder=0 scrolling='no' allowtransparency='true' width='"+width+"px' height='"+height+"px'></iframe>");
}
var add_child_cnt=0;
function pr_addChild(id,cur_obj,cnt)
{
	if (!add_child_cnt)	add_child_cnt=cnt;
	else add_child_cnt++;
	fm_getAjaxContent(document.getElementById(id),"/f/apps/profile/pr_ajax.php?option=2&cnt="+add_child_cnt,-1);
}
function pager_gotoPageSeo(page_url,obj_id,t_pg,last_option)
{
	document.getElementById(obj_id)
	var pg_n=document.getElementById(obj_id).value;
	pg_n=parseInt(pg_n);
	pg_n=t_pg-pg_n+1;
	if (pg_n<=0 || pg_n>t_pg)	pg_n=t_pg;
	if (pg_n!=t_pg)	page_url+=(last_option+pg_n+"/");
	window.location.href=page_url;
}
function pager_gotoPage(page_url,obj_id,t_pg,last_option)
{
	document.getElementById(obj_id)
	var pg_n=document.getElementById(obj_id).value;
	pg_n=parseInt(pg_n);
	if (pg_n<=0 || pg_n>t_pg)	pg_n=1;
	page_url+=[page_url.indexOf("?")==-1?"?":"&"]+last_option+"="+pg_n;
	window.location.href=page_url;
}
function pg_showFullText(id,cur_obj)
{
	 fm_contenBoxMaker('pg_showFullTextBox','/f/apps/playgroup/pg_ajax.php?option=1&id='+id,cur_obj,-1,'');
}
function pg_join(id)
{
	if (confirm('Are You Sure You Want To Join This Playgroup?'))
	{
		var rt_xmlHttp=GetXmlHttpObject();
		if (rt_xmlHttp==null)
		{
		  alert ("Browser does not support HTTP Request");
		  return;
		} 
		var url="/f/apps/playgroup/pg_ajax.php?option=2&id="+id;
		url=url+"&y="+Math.random();
		rt_xmlHttp.onreadystatechange=function (){
			if ((rt_xmlHttp.readyState==4 || rt_xmlHttp.readyState=="complete"))
			{
				if (rt_xmlHttp.responseText==-1) alert('Ajax error please contact site admin');
				else if (rt_xmlHttp.responseText==1)	alert("You are already a member of this playgroup");
				else if (rt_xmlHttp.responseText==3)	alert("Your request had been reject. Please contact orgnizer");
				else if (rt_xmlHttp.responseText==0)	alert("You have applied to join this playgroup, you are now in the approval list awaiting for playgroup admin's approval.\n\nYou can go to MY PLAYGROUPS botton on the top of the page to see your member status of this playgroup");
				else alert('Thank you for applying to join this playgroup. The organizer will be reviewing your request shortly. Please make sure to fill out your profile as much as possible so they can get to know you better.');
			}
		};
		rt_xmlHttp.open("GET",url,true);
		rt_xmlHttp.send(null);	
	}
	return false;
}
function pf_email(cur_obj,playgroup_id)
{
	var width=501;
	var height=350;
	type=new Array(width,height);	
	fm_contenBoxMaker('st_addMesgBox',-1,cur_obj,type,"<iframe src=\"/f/apps/playgroup_finder/pf_email.php?playgroup_id="+playgroup_id+"\" frameborder=0 scrolling='no' allowtransparency='true' width='"+width+"px' height='"+height+"px'></iframe>");
}
function group_email(cur_obj,playgroup_id)
{
	var width=501;
	var height=350;
	type=new Array(width,height);	
	fm_contenBoxMaker('st_addMesgBox',-1,cur_obj,type,"<iframe src=\"/f/apps/playgroup/group_email.php?playgroup_id="+playgroup_id+"\" frameborder=0 scrolling='no' allowtransparency='true' width='"+width+"px' height='"+height+"px'></iframe>");
}
function pf_signNotification(cur_obj,zipcode,radius)
{
	var width=501;
	var height=350;
	type=new Array(width,height);	
	fm_contenBoxMaker('st_addMesgBox',-1,cur_obj,type,"<iframe src=\"/f/apps/playgroup_finder/pf_subscribe.php?zipcode="+zipcode+"&radius="+radius+"\" frameborder=0 scrolling='no' allowtransparency='true' width='"+width+"px' height='"+height+"px'></iframe>");
}
function pf_signNotification_old(zipcode,radius)
{
	var rt_xmlHttp=GetXmlHttpObject();
	if (rt_xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	} 
	var url="/f/apps/playgroup_finder/pf_ajax.php?option=1&zipcode="+zipcode+"&radius="+radius;
	url=url+"&y="+Math.random();
	rt_xmlHttp.onreadystatechange=function (){
		if ((rt_xmlHttp.readyState==4 || rt_xmlHttp.readyState=="complete"))
			alert(rt_xmlHttp.responseText);
	};
	rt_xmlHttp.open("GET",url,true);
	rt_xmlHttp.send(null);	

}