var NEED_VERIFY_CODE_LEVEL = 2;
var NEED_VERIFY_CODE_UINMAX = 200000000;

//for showing the verify code. 
var g_bNeedVerifyCode = false;

//for reset after login
var g_adminLoginWindow = null;
var g_normalLoginWindow = null;

//for bbs comm operation
var g_iTimeStart = 0;
var g_iTimeLast  = 0;
var g_iTimeEnd   = 0;

// global values for user logo
var online      = new Array();		//for check user is on line or not
var g_facelist  = new Array();
var g_facelist1 = new Array();

function loadscript(rURL,rID) {
	var oldscript = document.getElementById(rID);
	var newscript = document.createElement("script");
	newscript.setAttribute("id", rID)
	newscript.setAttribute("src", rURL);
	oldscript.parentNode.replaceChild(newscript, oldscript);
}

function CheckIsUnloadId(iGroupId){
	if(iGroupId == 40055|| iGroupId ==40042||iGroupId ==40044||iGroupId ==40054||iGroupId ==40057||iGroupId ==40004||iGroupId ==40043||iGroupId ==11231){
		return true;
	}
	return false;
}

function CheckNeedToRefreshForAdminLogin(){
	if(g_adminLoginWindow!=null && g_adminLoginWindow.closed){
		clearInterval("CheckNeedToRefreshForAdminLogin()");
		document.location.reload(1);
	}
}

function CheckNeedToRefreshForNormalLogin(){
	if(g_normalLoginWindow!=null && g_normalLoginWindow.closed){
		clearInterval("CheckNeedToRefreshForNormalLogin()");
		document.location.reload(1);
	}
}

function GetRadioValue(radioName){
    var radios = document.getElementsByName(radioName);
    for(var i=0;i<radios.length;i++){
        if(radios[i].checked) return radios[i].value;
    }
    return null;
}

function TimeMarkStart(){
	var objDate = new Date();
	g_iTimeStart = objDate.getTime();
	g_iTimeLast = g_iTimeStart;
}

function TimeMarkCheckAndPost(sCgiName,iPart){
	var objDate = new Date();
	g_iTimeEnd = objDate.getTime();
	var usedTime = g_iTimeEnd-g_iTimeLast;
	var queryString = '<img width=0 height=0 style="display:none" src="http://bbs.paipai.com/cgi-bin/bbs/show/time_mark?name='+sCgiName+'&part='+iPart+'&time='+usedTime+'"/>';
	document.write(queryString);
	g_iTimeLast = g_iTimeEnd;
}

function InsertAtCaret (textEl, text){
	if (textEl.createTextRange && textEl.caretPos){
		var caretPos = textEl.caretPos;
		caretPos.text += caretPos.text.charAt(caretPos.text.length - 2) == ' ' ? text + ' ' : text;
	} else if(textEl) {
		textEl.value += text;
	} else {
		textEl.value = text;
	}
}

//show user logo face
function ContentShowFace(login_name,faceid,recvmsg,no){
	var face_image  = "http://img.qq.com/face/s/"+faceid+"-3.gif";
	var face_image1 = "http://img.qq.com/face/s/"+faceid+"-2.gif";
	var tempString = "";
	if(recvmsg=="1"){
		tempString+=("<a href='http://wpa.qq.com/msgrd?V=1&Uin="+login_name+"&Site=拍拍论坛(bbs.paipai.com)&Menu=yes' target='_blank'>");
	}
	tempString+=("<img src='"+face_image+"' id='online"+no+"' align=\"absmiddle\" hspace=\"1\"/>");
	g_facelist[no] = face_image;
	g_facelist1[no] = face_image1;
	if(recvmsg=="1"){
		tempString+=("</a>");
	}
	document.write(tempString);
}

//show user logo face
function ShowFace(login_name,face_image,face_image1,recvmsg,no){
	var iResultNo = no;
	var tempString = "";
	if(recvmsg=="1"){
		tempString+=("<a href='http://wpa.qq.com/msgrd?V=1&Uin="+login_name+"&Site=拍拍论坛(bbs.paipai.com)&Menu=yes' target='_blank'>");
	}
	tempString+=("<img src='"+face_image+"' id='online"+iResultNo+"' align=\"absmiddle\" hspace=\"1\"/>");
	g_facelist[iResultNo] = face_image;
	g_facelist1[iResultNo] = face_image1;
	if(recvmsg=="1"){
		tempString+=("</a>");
	}
	document.write(tempString);	
}

//check online faces
function CheckOnlineFaces(){
	if(online.length==0){
		setTimeout("CheckOnlineFaces()",300);
	}
	for (i=0;i<online.length;i++){
		if(document.getElementById("online"+i)){
			if(online[i]==0){
				eval("document.getElementById('online"+i+"').src='"+g_facelist[i]+"'");
			} 	
			else{ 
				eval("document.getElementById('online"+i+"').src='"+g_facelist1[i]+"'");
			}
		}
	}	
}

function DoLogin(sLoginUrl){
	if( (sLoginUrl.indexOf("bbs_login") == -1) )	{
		writeCookie("returnurl",sLoginUrl,10,"paipai.com");
	}
	else{
		writeCookie("returnurl","http://bbs.paipai.com",10,"paipai.com");
	}
	window.location = "http://member.paipai.com/cgi-bin/login_entry";
}

/****************************************************************
弹出管理员密码验证窗口
****************************************************************/
function AdminLogin(group_id){
	g_adminLoginWindow = window.open("http://bbs.paipai.com/cgi-bin/bbs/admin/bbs_admin_show_login?groupid="+group_id,'','width=400,height=300,left=100,top=100');
	setInterval("CheckNeedToRefreshForAdminLogin()",300);
}

function showVerifyCode(showCtrl,rowId,contentId){
	if(showCtrl == '1'){
		g_bNeedVerifyCode = true;
		document.getElementById(rowId).style.display="block";
		var htmlObj = document.getElementById(contentId);
		var allContent = '<input onblur="CheckVerifyCodeInput()" onfocus="CheckVerifyCodeInput()" name="verifycode" id="verifycode" type="text" size="4"> [ <img src="http://nverify.paipai.com/getimage?';
		allContent +=Math.random();
		allContent +='" /> ] ';
		htmlObj.innerHTML = allContent;
	}
}

function CheckTextContentWithLimit(iMinLenth,iMaxLenth,sTextId,sId){
	objWarningShow = document.getElementById(sId);
	objText        = document.getElementById(sTextId);
	
	var iLength = objText.value.replace(/[^\x00-\xff]/g,"aa").length;
	if(iLength<iMinLenth || iLength>iMaxLenth){
		if(objWarningShow.className!="warning"){
			objWarningShow.className = "warning";
			if(iMinLenth == iMaxLenth){
				objWarningShow.innerHTML = "请填写正确的内容,内容规定长度:"+iMaxLenth;
			}
			else{
				objWarningShow.innerHTML = "请填写正确的内容.长度范围:"+iMinLenth+"--"+iMaxLenth/2+"个汉字";
			}
		}
		return false;
	}
	else if(objText.value.replace(/ /g,'').length==0){
		objWarningShow.className = "warning";
		objWarningShow.innerHTML = "内容不能全为空";
		return false;
	}	
	else{
		if(objWarningShow.className!="notice"){
			objWarningShow.className = "notice";
			objWarningShow.innerHTML = "填写正确";			
		}
		return true;
	}
}

function BBSCheckIsNum(strValue){
	if(strValue.length<=0){
		return false;
	}
	for(i=0;i<strValue.length;i++){
		//alert(strValue[i]);
		if(strValue.charAt(i)<'0' || strValue.charAt(i)>'9'){
			return false;
		}
	}
	return true;
}


function CheckVerifyCodeInput(){
	return CheckTextContentWithLimit(4,4,'verifycode','verifyCodeWarning');
}



function CheckStatic(){
	var sCheckUrl = "http://bbs1.paipai.com";
	var sThisUrl  = document.URL;
	var p =sThisUrl.indexOf(sCheckUrl);
	if(p==-1){
		return false;
	}
	else{
		return true;
	}
}

var IS_STATIC =CheckStatic();
//var IS_STATIC = false;


function LoadRebornJs(sJsId,iType){
	document.getElementById("robornText").innerHTML = "正在尝试恢复中...";
	document.getElementById("rebornButton").disabled = true;
	document.getElementById("robornText").style.display="";
	var sUrl = "http://bbs.paipai.com/cgi-bin/bbs/post/bbs_my_last.js?type="+iType;
	loadscript(sUrl,sJsId);
	document.getElementById("rebornButton").disabled = false;
}

function ShowRebornButton(){
	document.getElementById("rebornButton").style.display="";
}

function RebornContent(bHasErr,aContent,iType){
	if(bHasErr){
		document.getElementById("robornText").innerHTML = "恢复内容失败.原因可能是现在离丢失帖子内容的时间隔得太长了.";
		return;
	}	
	
	var iCodeType = aContent["codeType"];
	var sContent  = aContent["content"];
	var sTitle    = aContent["title"];
	var iCopyRight= aContent["copyRight"];
	
	if(iType == 0 ){ // default is post add
		if(iCodeType == 0){ //ubb code
			document.getElementById("codeTypeHtml").checked = false;
			document.getElementById("codeTypeUbb").checked  = true;
			ChangeCodeType(0);
			document.bbsedit.text.value += sContent;
			document.getElementById("title").value += sTitle;
			CheckUbbEditorContent();			
		}
		else{ // html code
			document.getElementById("codeTypeUbb").checked = false;
			document.getElementById("codeTypeHtml").checked  = true;		
			ChangeCodeType(1);
			AddTextInHtmlCode(sContent);
			document.getElementById("title").value += sTitle;
		}
		if(iCopyRight == 1){
			document.getElementById("copyRightCopy").checked = false;
			document.getElementById("copyRightOrg").checked  = true;	
		}
		else if(iCopyRight == 0){
			document.getElementById("copyRightCopy").checked = true;
			document.getElementById("copyRightOrg").checked  = false;	
		}
		else{
			//do nothing;
		}
		CheckRadioContent('copyright','copyrightWarning');
		CheckTextContent(60,document.getElementById("title"),'titleWarning');
	}
	else if(iType ==1 ){ // for normal reply
		document.getElementById("title").value += sTitle;
		document.bbsedit.text.value += sContent;
		CheckUbbEditorContent();	
		CheckTextContent(60,document.getElementById("title"),'titleWarning');
	}
	else if(iType==2){ // for modify
		document.getElementById("title").value += sTitle;
		if(g_CodeType && g_CodeType==0){ // ubb code
			document.bbsedit.text.value += sContent;
			CheckUbbEditorContent();
		}
		else{ // html code;
			AddTextInHtmlCode(sContent);
		}
		CheckTextContent(60,document.getElementById("title"),'titleWarning');
	}
	else{ // for simple reply
		document.getElementById("text").value+= sContent;
	}
	document.getElementById("robornText").innerHTML ="恢复成功.";
}

