/*************************************************************************/
//custmize user options
/*************************************************************************/
var xmlHttp_writer

function writer_option(url,option)
{
	xmlHttp_writer=GetXmlHttpObject();
	if (xmlHttp_writer==null)
    {
	  alert ("Browser does not support HTTP Request");
	  return;
	} 
	url=url+"?option="+option;
	url=url+"&y="+Math.random();
	xmlHttp_writer.onreadystatechange=stateChanged_wirter;
	xmlHttp_writer.open("GET",url,true);
	xmlHttp_writer.send(null);	
} 

function stateChanged_wirter() 
{
if ((xmlHttp_writer.readyState==4 || xmlHttp_writer.readyState=="complete") && xmlHttp_writer.responseText)
 { 
		document.getElementById("writer_option").innerHTML=xmlHttp_writer.responseText;
 } 
}
/*************************************************************************/
//end of custmize user options
/*************************************************************************/
/*************************************************************************/
//catgory picker
/*************************************************************************/
// JavaScript Document
var lowest_level=0;
var hiddren_file_id="the_cat_id";//id of the hidden file which stores the category id from the category picker
var category_data_url="";//file name of the php code
var cat_picker_container_id="catgory_pickers";//id of the container of the category picker
//str_cat_ids, ids of the category from Grandpa to Grandchild
function auto_generate_cats(str_cat_ids,php_url)
{	
	if (category_data_url=="")	category_data_url=php_url;
	var arr_cat_ids=str_cat_ids.split(",");
	if (arr_cat_ids.length==1)//exception for root category
		showCat_list(0,0);
	else
	{
		for(var i=0;i<(arr_cat_ids.length-1);i++)
		{//dont know why  but if i call the function directly, wired thing happen
			setTimeout("showCat_list("+arr_cat_ids[i]+","+arr_cat_ids[i+1]+")",i*300);			
		}
	}
	document.getElementById(hiddren_file_id).value=arr_cat_ids[arr_cat_ids.length-1];
}
function selected_cat(cat_id,cat_name,obj_select)
{	
	if (cat_id!=-1) 
		document.getElementById(hiddren_file_id).value=cat_id;				
	else 
		document.getElementById(hiddren_file_id).value=0;	

	var cur_level=parseInt(obj_select.id);
	for (var cnt=lowest_level;cnt>cur_level;cnt--)
	{
		var temp_child=document.getElementById("category_picker_cat_list_level_"+cnt);
		if (temp_child)
		{		
			document.getElementById(cat_picker_container_id).removeChild(temp_child);			
			lowest_level-=1;
		}
	}
	showCat_list(cat_id,0);
	var picker_parent=document.getElementById(cat_picker_container_id);

}
/************************************** 
ajax 
**************************************/
var xmlHttp
//cat_id,id of the category,selected:which category is selected
//php_url or the php page, only need for the first time
function showCat_list(parent_cat_id,selected)
{
	if (category_data_url=="")	category_data_url=php_url;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	} 
	var url=category_data_url;
	url=url+"?parent_cat_id="+parent_cat_id;
	url=url+"&level="+(lowest_level+1);
	url=url+"&y="+Math.random();
	if (selected!=0)	url=url+"&selected_id="+selected;
	xmlHttp.onreadystatechange=stateChanged ;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChanged() 
{ 
	if ((xmlHttp.readyState==4 || xmlHttp.readyState=="complete") && xmlHttp.responseText)
 	{
		lowest_level+=1;
 		var new_child_div = document.createElement("div");
		new_child_div.style.float="left";
		new_child_div.id="category_picker_cat_list_level_"+lowest_level;
		new_child_div.innerHTML=xmlHttp.responseText;
 		document.getElementById(cat_picker_container_id).appendChild(new_child_div);
 	} 
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{ // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{ // Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		 catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
/**************************************************************************/
//end of ajax
/**************************************************************************/
/*********************************************************************/
//end of category picker
/*********************************************************************/
function advance_editor(obj_self,obj_id)
{
	var obj=document.getElementById(obj_id);
	//for default is hide
	if(obj.style.display=="" || obj.style.display=="none")
	{//to show
		obj.style.display="block";
		obj_self.innerHTML="-hide";
	}
	else
	{
		obj.style.display="none";
		obj_self.innerHTML="+Add pictures and video to your post";
	}
	/*
	//for default is show
	if(obj.style.display=="" || obj.style.display=="block")
	{
		obj.style.display="none";
		obj_self.innerHTML="... more";
	}
	else
	{//to show
		obj.style.display="block";
		obj_self.innerHTML=" hide";
	}
	*/
}
function pager_submit(page_num)
{
alert(document.forms[1].name);	
}
function poor_selector(obj,new_url)
{
window.location.href='admin.php?option=Poor%20Taste&amp;sort='+obj.options[obj.selectedIndex].value;
}
//type 1 is for topic pager (reverse pager) 0 is for reply pager
function goto_page(page_url,obj_id,tail,ttp,type)
{
	document.getElementById(obj_id)
	var pg_n=document.getElementById(obj_id).value;
	if (type)	pg_n=ttp-pg_n+1
	page_url+=(pg_n+".html"+tail);
	window.location.href=page_url;
}
function goto_page_admin(page_url,obj_id,ipp)
{
	document.getElementById(obj_id)
	var pg_n=document.getElementById(obj_id).value;
	page_url+=("&page="+pg_n+"&ipp="+ipp);
	window.location.href=page_url;
}
//for select item per page
function select_ipp(page_url,obj)
{
	page_url+=("&ipp="+obj.options[obj.selectedIndex].value);
	window.location.href=page_url;
}
//for select min signup times for protential spammers
function select_mst(page_url,obj)
{
	page_url+=("&count="+obj.options[obj.selectedIndex].value);
	window.location.href=page_url;
}
//for selector 
function goto_page_v2(page_url,obj,tail)
{
	page_url+=obj.options[obj.selectedIndex].value+".html"+tail;
	window.location.href=page_url;
}
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 checkRadioButton(radio_name,btn_name)
{
			var radio=document.getElementsByName(radio_name);
			for (var i=0;i<radio.length;i++)
			{
				if (radio[i].value==btn_name) radio[i].checked=true;
			}
			//alternative document.report.elements[button_name][1].checked = true; 
}
	//generate the domain or doctor list
function nav_control(ul_id,pic_path)
{
	var img_name="img_"+ul_id;
	var ul=document.getElementById(ul_id);
	var img=document.getElementsByName(img_name)[0];

	nav_sub_control(ul,img,pic_path);

}
function nav_sub_control(ob_ul,ob_img,pic_path)
{
	if (ob_ul)
	{
		if (ob_ul.style.display=="block")
		{
			ob_img.src=pic_path+"image/cat_close.gif";
			ob_ul.style.display="none";			
		}
		else 
		{
			ob_img.src=pic_path+"image/cat_open.png";
			ob_ul.style.display="block";		
		}		

	}	
}
//select/unselect all the posts by user
function select_all(form_name,cur_check,chk_key)
{
	var all_elements=document.getElementsByName(form_name)[0];

	var cur_status=document.getElementsByName(cur_check)[0].checked;
	for (var i=0;i<all_elements.length;i++)
	{
	
		if (all_elements.elements[i].name.indexOf(chk_key)!=-1 && all_elements.elements[i].type=='checkbox')
		{
			all_elements.elements[i].checked=cur_status;
		}		
	}

}
//Javascript for txtEditor
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//scripts for javascrpt
function InitToolbarButtons() {
  var kids = document.getElementsByTagName('DIV');

  for (var i=0; i < kids.length; i++) {
    if (kids[i].className == "imagebutton") {
      kids[i].onmouseover = tbmouseover;
      kids[i].onmouseout = tbmouseout;
      kids[i].onmousedown = tbmousedown;
      kids[i].onmouseup = tbmouseup;
      kids[i].onclick = tbclick;
    }
  }
}

function tbmousedown(e)
{
  var evt = e ? e : window.event; 

  this.firstChild.style.left = 2;
  this.firstChild.style.top = 2;
  this.style.border="inset 2px";//changeable
  if (evt.returnValue) {
    evt.returnValue = false;
  } else if (evt.preventDefault) {
    evt.preventDefault( );
  } else {
    return false;
  }
}

function tbmouseup()
{
  this.firstChild.style.left = 1;
  this.firstChild.style.top = 1;
  this.style.border="outset 2px";
}

function tbmouseout()
{
  this.style.border="solid 2px #3C3C3C";
}

function tbmouseover()
{
  this.style.border="outset 2px";
}

 
//use to get the top offset of the passed node
function getOffsetTop(elm) {

  var mOffsetTop = elm.offsetTop;
  var mOffsetParent = elm.offsetParent;

  while(mOffsetParent){
    mOffsetTop += mOffsetParent.offsetTop;
    mOffsetParent = mOffsetParent.offsetParent;
  }
 
  return mOffsetTop;
}
//use to get the left offset of the passed node
function getOffsetLeft(elm) {

  var mOffsetLeft = elm.offsetLeft;
  var mOffsetParent = elm.offsetParent;

  while(mOffsetParent){
    mOffsetLeft += mOffsetParent.offsetLeft;
    mOffsetParent = mOffsetParent.offsetParent;
  }
 
  return mOffsetLeft;
}

function tbclick()
{

  if ((this.id == "orange" || this.id == "grey" || this.id == "black")) {
	document.getElementById('forum_txteditor_edit').contentWindow.document.execCommand("forecolor", false, this.id);
  }
  else {
    document.getElementById('forum_txteditor_edit').contentWindow.document.execCommand(this.id, false, null);
  }
}

function Select(selectname)
{
  var cursel = document.getElementById(selectname).selectedIndex;
  /* First one is always a label */
  if (cursel != 0) {
    var selected = document.getElementById(selectname).options[cursel].value;
    document.getElementById('forum_txteditor_edit').contentWindow.document.execCommand(selectname, false, selected);
    document.getElementById(selectname).selectedIndex = 0;
  }
  document.getElementById("forum_txteditor_edit").contentWindow.focus();
}

function dismisscolorpalette()
{
  document.getElementById("colorpalette").style.visibility="hidden";
}

function Start() 
{
	if (document.getElementById('forum_txteditor_edit'))
	{
	  document.getElementById('forum_txteditor_edit').contentWindow.document.designMode = "on";
	  try 
	  {
		document.getElementById('forum_txteditor_edit').contentWindow.document.execCommand("undo", false, null);
	  } 
	  catch (e) 
	  {
		alert("This editor is not supported on your version of explorer.");
	  }
	  InitToolbarButtons();
	}
}
function viewsource(source)
{
  var html;
  if (source) {
	if (document.addEventListener)
	{
		html = document.createTextNode(document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerHTML);
		document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerHTML = "";
		//the importNode only works for O not IE and F
	   // html = document.getElementById('forum_txteditor_edit').contentWindow.document.importNode(html,false);
		document.getElementById('forum_txteditor_edit').contentWindow.document.body.appendChild(html);
		//document.getElementById("toolbar1").style.visibility="hidden";
	}
	else if (document.attachEvent) 
	{
	 	iHTML = document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerHTML;
		document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerText = iHTML;
	}

    document.getElementById("toolbar2").style.visibility="hidden";

  } else {
  	if (document.addEventListener)
	{
		html = document.getElementById('forum_txteditor_edit').contentWindow.document.body.ownerDocument.createRange();
		html.selectNodeContents(document.getElementById('forum_txteditor_edit').contentWindow.document.body);
		document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerHTML = html.toString();
	}
	else if (document.attachEvent) 
	{
		iText = document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerText;
		document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerHTML = iText;
	}

    //document.getElementById("toolbar1").style.visibility="visible";
    document.getElementById("toolbar2").style.visibility="visible";  
  }
}
//used for transfering content on the iframe to a textarea
//works but find a better solution which is to add overflow:hidden to the td
//1/4/08 use existing hid_body 
//to rename the hid_body textarea to fit the variable name
function fire(form_name,frame_name,textarea_name)
{
	var max_len=24;
	var cur_form=document.getElementById(form_name);	
	var input_frame=document.getElementById(frame_name);	
	if (document.getElementById('source_flag').checked)
	{
		document.getElementById('source_flag').checked=false;
		viewsource(false);
	}
	document.getElementById("hid_body").name=textarea_name;
	document.getElementById("hid_body").value= document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerHTML;
}
//used for transfering content on the iframe to a textarea
//works but find a better solution which is to add overflow:hidden to the td
function fire_old(form_name,frame_name)
{
	var max_len=24;
	var cur_form=document.getElementById(form_name);	
	var input_frame=document.getElementById(frame_name);	
	var new_textarea=document.createElement('textarea');
	new_textarea.name="reply";
	if (document.getElementById('source_flag').checked)
	{
		document.getElementById('source_flag').checked=false;
		viewsource(false);
	}

	new_textarea.value= document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerHTML;
	new_textarea.style.display="none";
	cur_form.appendChild(new_textarea);
}
//used for transfering content on the iframe to a textarea
//works but find a better solution which is to add overflow:hidden to the td
function fire_v2(form_name,frame_name)
{
	var max_len=24;
	var cur_form=document.getElementById(form_name);	
	var input_frame=document.getElementById(frame_name);	
	var new_textarea=document.createElement('textarea');
	new_textarea.name="reply";
	if (document.getElementById('source_flag').checked)
	{
		document.getElementById('source_flag').checked=false;
		viewsource(false);
	}
		//alert(document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerText);
		//new_textarea.value= document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerText;
var str=document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerText.split(" ");
var new_str="";
var count=0;
var flag=0;
for (i=0; i<str.length; i++)
{
	if (count=Math.floor(str[i].length/(max_len+0.1)))
	{
		flag=1;
		for(i1=1;i1<=count;i1++)
			str[i]=str[i].slice(0,(i1*max_len+i1-1))+" "+str[i].slice((i1*max_len+i1-1));
	}
	new_str+=(str[i]+" ");
	//if (str[i].length>)
	//str[i].slice
}

if (flag)
	new_textarea.value=new_str;
else 
	new_textarea.value= document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerHTML;
			//alert(document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerHTML);
//alert(new_textarea.value);
//new_textarea.value= document.getElementById('forum_txteditor_edit').contentWindow.document.body.innerHTML;

	new_textarea.style.display="none";
	cur_form.appendChild(new_textarea);
//	return false;
}
function alert_mesg($mesg,$new_url)
{
	if (window.confirm($mesg))
		this.location.href=$new_url;
}
//for fill 
function fill_up(source_id,iframe_name)
{
	if(document.addEventListener)
	{//firefox
		if (document.getElementById(source_id).value)
		{
		document.getElementById('forum_txteditor_edit').contentDocument.open();
		document.getElementById('forum_txteditor_edit').contentDocument.write(document.getElementById(source_id).value);
		document.getElementById('forum_txteditor_edit').contentDocument.close();
		}
	 }
	 else 
	 {//IE

			window.frames[iframe_name].document.open();
			window.frames[iframe_name].document.write(document.getElementById(source_id).value);
			window.frames[iframe_name].document.close();
		 
	 }
	 if (document.addEventListener)
	 {	
	  //document.getElementById('forum_txteditor_edit').contentWindow.document.execCommand("styleWithCSS", false , true);
		 try 
		 { 
			 document.getElementById('forum_txteditor_edit').contentWindow.document.execCommand( 'styleWithCSS', false, false ) ; 
		 } 
		 catch (e) 
		 { 
		 document.getElementById('forum_txteditor_edit').contentWindow.document.execCommand( 'useCSS', false, true ) ; 
		 }
	 }
	 /*
		if (document.getElementsByName(iframe_name)[0])
	{  
		if (document.getElementById(source_id).value)
		{
		document.getElementById('forum_txteditor_edit').contentDocument.open();
		document.getElementById('forum_txteditor_edit').contentDocument.write(document.getElementById(source_id).value);
		document.getElementById('forum_txteditor_edit').contentDocument.close();
		}
		  if (document.addEventListener)
		  {	
			  //document.getElementById('forum_txteditor_edit').contentWindow.document.execCommand("styleWithCSS", false , true);
			  try 
			  { 
				 document.getElementById('forum_txteditor_edit').contentWindow.document.execCommand( 'styleWithCSS', false, false ) ; 
			  } 
			  catch (e) 
			  { 
				 document.getElementById('forum_txteditor_edit').contentWindow.document.execCommand( 'useCSS', false, true ) ; 
			  }
		  }

		
	}

	if(document.addEventListener)
	{//firefox
	 //document.getElementById('fapp_text_edit').contentWindow.document.execCommand("styleWithCSS", false , true);
	 	if (document.getElementById('fapp_text_edit'))
		{
			document.getElementById('fapp_text_edit').contentWindow.open();
			document.getElementById('fapp_text_edit').contentWindow.write(document.getElementById(source_id).value);
			document.getElementById('fapp_text_edit').contentWindow.close();
		}
		try 
		{ 
			document.getElementById('fapp_text_edit').contentWindow.document.execCommand( 'styleWithCSS', false, false ) ; 
		} 
		catch (e) 
		{ 
			document.getElementById('fapp_text_edit').contentWindow.document.execCommand( 'useCSS', false, true ) ; 
		}
	}
	else
	{//IE
		if (window.frames[iframe_name])
		{
			window.frames[iframe_name].document.open();
			window.frames[iframe_name].document.write(document.getElementById(source_id).value);
			window.frames[iframe_name].document.close();
		}
	}
	
	if (window.frames[iframe_name])
	{
		window.frames[iframe_name].document.open();
		window.frames[iframe_name].document.write(document.getElementById(source_id).value);
		window.frames[iframe_name].document.close();
		  if(document.addEventListener)
		  {
			  //document.getElementById('fapp_text_edit').contentWindow.document.execCommand("styleWithCSS", false , true);
			  try 
			  { 
				 document.getElementById('fapp_text_edit').contentWindow.document.execCommand( 'styleWithCSS', false, false ) ; 
			  } 
			  catch (e) 
			  { 
				 document.getElementById('fapp_text_edit').contentWindow.document.execCommand( 'useCSS', false, true ) ; 
			  }
		  }
	}
	*/

}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//End of Javascript for txtEditor

