// JavaScript Document
var xmlhttp=null;
function CreatXMLHttp()//创建xmlhttprequest对象
{
    if (window.XMLHttpRequest) 
    { //兼容Mozilla、Safari等浏览器对象
           xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{ //兼容IE浏览器
           try {
               xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); //创建Msxml2.XMLHTTP控件对象
           } catch (e) {
               try {//创建Microsoft.XMLHTTP控件对象，该控件作用为获取指定URL的内容
                   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
               } catch (e) {}
           }
	}
	return xmlhttp;
}
/*Ajax section */
function  changeCode(id,path)
{
	document.getElementById(id).src='';
	document.getElementById(id).src=path;
}
//Login 
function doLogin(formId,name_id,pw_id)
{
	CreatXMLHttp();
	param="/submit/true/username/"+document.getElementById(name_id).value+"/password/"+document.getElementById(pw_id).value;
	if(document.getElementById("rememble") && document.getElementById("rememble").checked)
		param+="/rememble/"+document.getElementById("rememble").value;
	
	path=document.getElementById(formId).action;
	//alert(path);
	url=encodeURI(path+param);
	//url=path+param;
	//alert(url);
	xmlhttp.open("post",url,true);
	xmlhttp.onreadystatechange = handleLogin;
	xmlhttp.send(null);
	return false;
}
function handleLogin()
{
	document.getElementById("log_warning").innerHTML='登录中...';
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		r=xmlhttp.responseText;
		if(r>0)
			myrefresh();
		else
			document.getElementById("log_warning").innerHTML='登录失败';
	}
}

function doBigLogin(formId,name_id,pw_id)
{
	CreatXMLHttp();
	param="?submit=true&username="+document.getElementById(name_id).value+"&password="+document.getElementById(pw_id).value;
	path=document.getElementById(formId).action;
	xmlhttp.open("post",path+param,true);
	xmlhttp.onreadystatechange = handleLogin2;
	xmlhttp.send(null);
	return false;
}

function handleLogin2()
{
	document.getElementById("log_warning2").innerHTML='登录中...';
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		r=xmlhttp.responseText;
		if(r>0)
			myrefresh();
		else
			document.getElementById("log_warning2").innerHTML='登录失败';
	}
}
//////// Login end

/// Find password
function doFindPw(ele)
{
	username=document.getElementById("username").value;
	if(username.length>3)
	{
		CreatXMLHttp();
		param="?username="+username;
		path=ele.href;
		xmlhttp.open("post",path+param,true);
		xmlhttp.onreadystatechange = handleFindPw;
		xmlhttp.send(null);
	}
	else
	{
		document.getElementById("log_warning").innerHTML='请输入完整的用户名...';	
	}
	return false;
}
function handleFindPw()
{
	document.getElementById("log_warning").innerHTML='用户数据处理中...';
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		r=xmlhttp.responseText;
		if(r>0)
			document.getElementById("log_warning").innerHTML='已发送资料, 请返回登记时邮箱查收.';
		else
			document.getElementById("log_warning").innerHTML='找不到该用户.';
	}
}
////Find password end

// Remove article
function doRemove(path)
{
	CreatXMLHttp();
	xmlhttp.open("post",path,true);
	xmlhttp.onreadystatechange = handleRemove;
	xmlhttp.send(null);
	return false;
}
function handleRemove()
{
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		r=xmlhttp.responseText;
		if(r>0)
			myrefresh();
	}
}
///Remove article end

// Get comments
function getComments(path)
{
	CreatXMLHttp();
	xmlhttp.open("post",path,true);
	xmlhttp.onreadystatechange = handleComments;
	xmlhttp.send(null);
	return false;
}
function handleComments()
{
	document.getElementById("comments_layer").innerHTML='<div align="center"><img src="/images/loading2.gif"></div>';
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		r=xmlhttp.responseText;
		document.getElementById("comments_layer").innerHTML=r;
	}
}
////////////// end comment

/// Delete comment
function doRemoveComment(path)
{
	CreatXMLHttp();
	xmlhttp.open("post",path,true);
	xmlhttp.onreadystatechange = handleRemoveComment;
	xmlhttp.send(null);
	return false;
}
function handleRemoveComment()
{
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		r=xmlhttp.responseText;
		if(r>0)
			myrefresh();
	}
}
/// Delete comment end

// Check duplicate user name
function checkUsername(name,path)
{
	if(name.length>=2)
	{
		CreatXMLHttp();
		path+='/name/'+name;
		xmlhttp.open("post",path,true);
		xmlhttp.onreadystatechange = handleCheckUsername;
		xmlhttp.send(null);
	}
	else
	{
		document.getElementById("submit_btn").src='/images/regBtn_bw.gif';
		document.getElementById("submit_btn").onclick=function(){
			alert("用户名长度不足");
			return false;
		};
	}
	return false;
}

function handleCheckUsername()
{
	if(document.getElementById("submit_btn"))
	{
		document.getElementById("submit_btn").src='/images/regBtn_bw.gif';
		document.getElementById("submit_btn").onclick=function(){
			alert("用户名重复,请重新选择");
			return false;
		};
	}
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		r=xmlhttp.responseText;
		if(r<=0)
		{
			document.getElementById("submit_btn").src='/images/regBtn.gif';
			document.getElementById("submit_btn").onclick=function(){
			
			return true;
		};
		}
		else
		{
			alert("用户名重复,请重新选择");
		}
	}
}
//////////////////////////////////

// Check duplicate user name
function checkEmail(name,path)
{
	if(name.length>=2 && checkMail(name))
	{
		CreatXMLHttp();
		path+='/email/'+name;
		xmlhttp.open("post",path,true);
		xmlhttp.onreadystatechange = handleCheckEmail;
		xmlhttp.send(null);
	}
	else
	{
		document.getElementById("submit_btn").src='/images/regBtn_bw.gif';
		document.getElementById("submit_btn").onclick=function(){
			alert("用户名长度不足");
			return false;
		};
	}
	return false;
}

function handleCheckEmail()
{
	if(document.getElementById("submit_btn"))
	{
		document.getElementById("submit_btn").src='/images/regBtn_bw.gif';
		document.getElementById("submit_btn").onclick=function(){
			alert("邮件地址重复,请重新选择");
			return false;
		};
	}
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		r=xmlhttp.responseText;
		if(r<=0)
		{
			document.getElementById("submit_btn").src='/images/regBtn.gif';
			document.getElementById("submit_btn").onclick=function(){
			
			return true;
		};
		}
		else
		{
			alert("邮件地址重复,请重新选择");
		}
	}
}

/* Ajax end*/
function openSmallWindow(link)
{
	window.open(link,"Ng","height=200,width=300,toolbar= no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");
}

function myrefresh()
{
	window.top.location.reload();
}



function adBarControl(page,pin)
{
	var page_item_num=pin;
	var tpage=0;
	var tnum=0;

	if(page_item_num.length<=0 || page_item_num==null)
		page_item_num==8;
	
	
		
	if($(('tnum')))
		tnum=$('tnum').value;
	else
		tnum=0;
	if($('fpage'))
		curPage=$('fpage').value;
	else
		curPage=0;
		
	tpage=parseInt(tnum/page_item_num);
	if(tnum%page_item_num!=0)
		tnum++;
	tpage--;
		
	if($('ad-bar'))
	{
		if(page=='n')
			curPage++;
		if(page=='p')
			curPage--;
		if(curPage>tpage)
			curPage=tpage;
		if(curPage<0)
			curPage=0;
		$('fpage').value=curPage;
	}
	if($('temp_fri_'+curPage))
		$('ad-thumbs-front').innerHTML=$('temp_fri_'+curPage).innerHTML;
}


/// TO handle photos
function displayPhotos(photos_id,path,position_id)
{
	photos=document.getElementById(photos_id).value;
	if(photos.length<=4)
		document.getElementById(photos_id).value='';
	if(photos.indexOf(",")==0)
	{
		photos=photos.substr(1,photos.length);
	}
	if(photos.indexOf(",")==photos.length)
	{
		photos=photos.substr(0,photos.length-1);
	}
	arr=photos.split(",");
	content="";
	for(i=0;i<arr.length;i++)
	{
		content+="<a href='#' onclick=\"removePhoto('"+photos_id+"','"+path+"',"+i+",'"+position_id+"')\"><img src='"+path+arr[i]+"' width='80' /></a>";
	}
	document.getElementById(position_id).innerHTML=content;
}

function removePhoto(photos_id,path,tag,position_id)
{
	if(confirm("Sure to remove this record ?"))
	{
		photos=document.getElementById(photos_id).value;
		if(photos.indexOf(",")==0)
		{
			photos=photos.substr(1,photos.length);
		}
		if(photos.indexOf(",")==photos.length)
		{
			photos=photos.substr(0,photos.length-1);
		}
		arr=photos.split(",");
		if(arr[tag])
		{
			//splice
			arr.splice(tag,1);
		}
		t=arr.join();
		if(t.length<=1)
			t='';
		else
			t=','+t;
		//alert(arr.join());
		document.getElementById(photos_id).value=t;
		displayPhotos(photos_id,path,position_id);
	}
	
}

function openWindow(path)
{
	window.open (path, 'newwindow', 'height=800, width=1000, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no')
}
