// JavaScript Document
var activeTarget;
var edit_div;
function loginSuccess(msg)
{
	if (getQuery("section") && getQuery("section")=='forgotpassword')
	{
		window.setTimeout(function ()
		{
			window.location = window.location.toString().split("&section")[0];//return to login page
		},1200);
	}
	else
	{
		window.setTimeout(function ()
		{
			if (edit_div)
			{
				cancel_edit();
				window.location = window.location.toString(); //refresh
			}
			else
			{
				var link = "";
				if (getQuery("forum_id"))
					link = "&forum_id=" + getQuery("forum_id");
				else if (getQuery("topic_id"))
					link = "&topic_id=" + getQuery("topic_id");
				
				window.location = window.location.toString().split("&")[0] + link; //go home page
			}
		},1500);
	}
}
function editUserSuccess(id)
{
	if (edit_div)
	{
		cancel_edit();
		window.setTimeout(function (){
		window.location = window.location.toString(); //refresh
									},1500);
	}
	//else		window.location = window.location.toString().split("&")[0]; //go home page
}
function editUserAccountSuccess(id)
{
	if (edit_div) //this section doesn't require refresh
	{
		cancel_edit();
	}
	//else		window.location = window.location.toString().split("&")[0]; //go home page
}


function editForumSuccess(id)
{
	if (edit_div)
	{
		cancel_edit();
		window.location = window.location.toString(); //refresh
	}
	else
			window.location = window.location.toString().split("&")[0]; //go home page
	
}
function editTopicSuccess(id)
{	
	if (edit_div)
		cancel_edit();
	window.setTimeout(function()
	{
		window.location = window.location.toString(); //refresh
	},1500);
}
function registerSuccess()
{
	loading_progress(true,'registration succeeded. please check your email for your activation code',3)
	window.setTimeout(function ()
	{
			window.location = window.location.toString().split("&")[0]+"&p=login&activation=true&msg="+escape('registration succeeded. please check your email for your activation code'); //go login page page
	},2500);
}
var notifyLink;
function notifyTopic(obj,topic_id)
{
	notifyLink = obj;
	
	// postPage(serPage,obj,params,callback,failcallback,validateFunc,noloading)
	postPage("index.php?page=forums/index.php&p=editaboutme",'',"&sender=notifytopic&topic_id="+topic_id,notifySuccess);
	
}
function notifySuccess()
{
	if (notifyLink)
		notifyLink.parentNode.removeChild(notifyLink);
}
function loading_progress(load,msg,time,center)
{
	var loading = document.getElementById('loadingDiv');
	if (time || !loading)
	{
		loading 				= document.createElement("DIV");
		loading.id				= time?'loadingDivTimer':'loadingDiv';
		loading.style.position 	= 'absolute';
		loading.innerHTML		= msg || '<img src="images/loading.gif"/>';
		
		document.getElementsByTagName("BODY")[0].appendChild(loading);
		loading.style.left		= '0px';
		loading.style.top		= '0px';
		
		loading.style.border='solid 2px red';
		
	}
	if (center)
		setCenter(loading);
	else
	{
	loading.style.left		= mouse_click_x+'px';
	loading.style.top		= mouse_click_y+'px';
	}
	loading.style.zIndex	= 99;
	if (time)
	{
		loading.style.border 	= 'double 3px #333';
		loading.style.background='#ccc';
		loading.style.color		= '#333';
		window.setTimeout(function () { document.getElementById("loadingDivTimer").parentNode.removeChild(document.getElementById("loadingDivTimer")); },time * 1000);
	}
	
	if (load)
		loading.style.display	= '';
	else
		loading.style.display	= 'none';
		
	
		
}

function createXMLHTTPRequest()
{
	getScreen();
	try{	return new ActiveXObject("Msxml2.XMLHTTP");  }catch (e)    {}
	try{	return new ActiveXObject("Microsoft.XMLHTTP");}catch (e)  {}
	try{	return new XMLHttpRequest();}catch (e)  {}
	
	alert("XMLHttpRequest is not support");
	
	return false;
}

function getPage(serPage,container,callback,noloading,center)
{
	if (!container)
		container = document.createElement("DIV");

	if (serPage && container)
	{
		var xmlhttp = createXMLHTTPRequest();
		serPage += ((serPage.indexOf("?") < 0) ? "?":"&") + "t=" + new Date().getTime();
		
		//serPage += '&screenWidth=' + aval_width + '&screenHeight=' + aval_height;
		if (!noloading)
		loading_progress(true);

		xmlhttp.open("get", serPage,true);
		xmlhttp.onreadystatechange = function () 
		{		
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{
				loading_progress(false);
				container.style.display='';
				container.innerHTML = xmlhttp.responseText;
				if (center)
					setCenter();
				////open rich text/////
				var textAreas = container.getElementsByTagName('TEXTAREA');
				for (var i=0;i<textAreas.length;i++)
					if (textAreas[i].onmouseover)
						textAreas[i].onmouseover();
				
				if (callback)
					callback(container.innerHTML);
			}
		}
		xmlhttp.send(null);
	}
}

function postPage(serPage,obj,params,callback,failcallback,validateFunc,noloading)
{
	var fobj;
	var confirmed 	= true;
	var valid		= true;
	serPage = serPage || obj.form.action;
	
	obj = activeTarget || obj;
	if (obj && (obj.value == 'Delete' || obj.value == 'delete'))
		confirmed	= confirm("are you sure?");
	
	if (valid && obj.form)
			valid = isValidForm(obj.form) && validateEmail(obj.form);
			
	if (valid && obj.form && validateFunc)
		valid = validateFunc((obj&&obj.form)?obj.form:'');
	
					
	if (serPage && confirmed && valid)
	{
		var xmlhttp = createXMLHTTPRequest();
		
		if (obj && obj.form)
			fobj		= obj.form;
		if (!fobj)
			fobj = document.createElement("FORM");

		params	= params || getFormValues(fobj);
		
		if (obj)
		params += "&sender=" + (obj.getAttribute("action")||obj.value);
		
		/*if (obj)
		{
			obj.setAttribute("orgValue",obj.value);
			obj.setAttribute("disabled","disabled");
			
			obj.value = 'please wait...';
			
			window.setTimeout(function () 
			{ 
				obj.removeAttribute("disabled");
				obj.value = obj.getAttribute("orgValue");
			},4000);
		}*/
		
		if (!noloading)
		loading_progress(true);
		
		xmlhttp.open("post", serPage,true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		xmlhttp.onreadystatechange = function ()
		{
			if(xmlhttp.readyState == 4)
			{								
				loading_progress(false);
				if (xmlhttp.status == 405)
				{
					alert('errors. please try again later.'+serPage);
				}
				else if (xmlhttp.status == 200)
				{
					postDone(xmlhttp.responseText,callback,failcallback);
				}
				activeTarget=null;
			}
		}
		xmlhttp.send(params);
	}
}
function postDone(text,callback,failcallback)
{
	var error = JSON.decode(text);
	
	
	if (error)
	{
		if (error['code'] <= 0)
		{
			loading_progress(true,error['msg'] || 'submit success',2);
			if (callback)
					callback(error['msg'] || 'submit success');
			else
				alert(error['msg'] || 'submit success');
					//if (fobj && obj && obj.value == 'Add')fobj.reset();
		}
		else
		{
			if (failcallback)
				failcallback(error['msg']);
			else
				alert('error(s):\n'+(error['msg']||text));
		}
	}
	
	
}
function getFormValues(fobj,URLquery)
{
	var str = "";
	var val;
	var query = window.location.search.substring(1);
  var vars = query.split("&");
  //var items=fobj.getElementsByTagName("input");
  if (fobj)
	for (var i=0;i<fobj.elements.length;i++)
	//for (var i=0;i<items.length;i++)
	{
		var ITEM = fobj.elements[i];//items[i];

		if (ITEM.name || ITEM.id)
		{
			if (ITEM.type == "checkbox")
			{
				if (ITEM.name.indexOf("[]")>0)
				{
					//for array name
					if (ITEM.checked)
						str += ITEM.name + "=" + ITEM.getAttribute("v") + "&";
				}
				else
				str += ITEM.name + "=" + escape(ITEM.checked) + "&";
			}
			else
			{
				if (ITEM.type == "textarea")
					try{
							if (document.getElementById("wysiwyg" + ITEM.name))//check if it's Rich Text
								getRT(ITEM.name);
							else
								ITEM.value = ITEM.value.replace(/\+/g,"&#43;");
							str += ITEM.name + "=" + escape(ITEM.value.replace(/\+/g,"&#43;")) + "&";
						}catch(err){}
	
				else if (ITEM.type == "radio")
				{
					if (ITEM.checked && str.indexOf(ITEM.name) < 0)
						str += ITEM.name + "=" + escape(ITEM.value) + "&";
				}
				else //if (!ITEM.getAttribute('generated')) //generated attribute set by build select option
				{	
						var value = ITEM.value || '';
						
						str += (ITEM.name || ITEM.id) + "=" + escape(value.replace(/\+/g,"&#43;")) + "&";
					
				}
	
			}
		}
 		//str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
	}
  	if (URLquery)
  	{
	  for(var i=0;i<vars.length;i++)
	  {
	  	str += vars[i] + "&";
	  }
	}
	if (!fobj.form)
		str+= "form=" + (fobj.id||fobj.name);
//	alert(str.replace(/&/g,'\n'));
	return str;
}
function edit(serPage,callback)
{
	edit_div 					= document.getElementById("EditDiv");
	edit_div.innerHTML 		= '';
	//edit_div.style.display	= '';
	getPage(serPage,edit_div,callback,'',true);	
}

function cancel_edit()
{
	if(edit_div)
	{
		edit_div.style.display	='none';
		edit_div.innerHTML 		= '';
		edit_div 				 	= false;
		
		//grayOut();//clear
	}
}