
var profile_username_toolong = '用户名超过 15 个字符';
var profile_username_tooshort = '用户名小于3个字符';
var lastusername = lastpassword = lastemail = lastinvitecode = '';
var lang = new Array();
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var is_mac = userAgent.indexOf('mac') != -1;
var ajaxdebug = 0;


function toprov(ocity,oprov)
  {
      for(i=0;i<citylist.length;i+=3)
       if (citylist[i+2]==ocity.value)
        {
          for(j=0;j<provlist.length;j+=2)
          {
          	if (provlist[j]==citylist[i])
          	{
          		for (sj=0;sj<oprov.length;sj++)
          		{
          		  if (oprov.options[sj].value==provlist[j+1])
          		  oprov.selectedIndex=sj;
          		    
          		}
          	}
          	
          }
        }
  }
  


function toprov2(ocity,oprov)
  {
      for(i=0;i<citylist.length;i+=3)
       if (citylist[i+2]==ocity.value)
        {
          for(j=0;j<provlist.length;j+=2)
          {
          	if (provlist[j]==citylist[i])
          	{
          		for (sj=0;sj<oprov.length;sj++)
          		{
          		  if (oprov.options[sj].value==provlist[j])
          		  oprov.selectedIndex=sj;
          		    
          		}
          	}
          	
          }
        }
  }
  

function changesele(cid,frmx)
  {

	  frmx.length=0;

    if (cid!=0)
    {

      for(i=0;i<citylist.length;i+=3)
       if (citylist[i]==cid)
        {
          frmx.length++;
          frmx.options[frmx.length-1].value=citylist[i+1];
          frmx.options[frmx.length-1].text=citylist[i+2];
        }
    }
  }
  
  
function changesele2(cid,frmx)
  {
    frmx.length=0;
    if (cid!=0)
    {

      for(i=0;i<citylist.length;i+=3)
       if (citylist[i]==cid)
        {
          frmx.length++;
          frmx.options[frmx.length-1].value=citylist[i+1];
          frmx.options[frmx.length-1].text=citylist[i+2];
        }
    }
  }
  
function changesele3(cid,frmx)
  {
    frmx.length=0;
    if (cid!=0)
    {

      for(i=0;i<citylist.length;i+=3)
       if (citylist[i]==cid)
        {
          frmx.length++;
          frmx.options[frmx.length-1].value=citylist[i+2];
          frmx.options[frmx.length-1].text=citylist[i+2];
        }
    }
  }


  
function changesele4(oprov,frmx,dcity)
{
  if (dcity=='')
	{changesele3(2,frmx)	}
  else
  {
      for(i=0;i<citylist.length;i+=3)
       if (citylist[i+2]==dcity)
        {
          for(j=0;j<provlist.length;j+=2)
          {
          	if (provlist[j]==citylist[i])
          	{
          		for (sj=0;sj<oprov.length;sj++)
          		{
          		  if (oprov.options[sj].value==provlist[j])
          		  oprov.selectedIndex=sj;
          		}
          	}
          	
          }
        }  	
    frmx.length=0;
    for(i=0;i<citylist.length;i+=3)
       if (citylist[i]==oprov.options[oprov.selectedIndex].value)
        {
          frmx.length++;
          frmx.options[frmx.length-1].value=citylist[i+2];
          frmx.options[frmx.length-1].text=citylist[i+2];
          if (citylist[i+2]==dcity)
          	frmx.options[frmx.length-1].selected=true;
        }
    }
}



function CheckForm()
{
if(document.getElementById('username').value==""||document.getElementById('username')==null)
{
	document.getElementById('username').focus();
	alert("请输入用户名才可以进行注册！");
	return false;
}
if(document.getElementById('password')==null||document.getElementById('password').value=="")
{
	document.getElementById('password').focus();
	alert("请输入用户密码才可以进行注册！");
	return false;
}
if(document.getElementById('password2')==null||document.getElementById('password2').value=="")
{
	document.getElementById('password').focus();
	alert("请输入用户密码验证才可以进行注册！");
	return false;
}
if(document.getElementById('password2').value!=document.getElementById('password').value)
{
	document.getElementById('password2').focus();
	alert("您输入的2次密码不一样，请重新输入一次，以保证密码正确！");
	return false;
}
return true;
}


function CheckForm2()
{
	if(document.getElementById('username').value==""||document.getElementById('username')==null)
	{
		document.getElementById('username').focus();
		alert("请输入用户名才可以登陆！");
		return false;
	}
	if(document.getElementById('password')==null||document.getElementById('password').value=="")
	{
		document.getElementById('password').focus();
		alert("请输入用户密码才可以登陆！");
		return false;
	}
return true;
}

function $(id)
{
	return document.getElementById(id);
}
function trim(str) {
	return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}
function strlen(str) {
	return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}
function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}
function in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
					return true;
			}
		}
	}
	return false;
}

function messagehandle_register(divid,key, msg) {
$(divid).className = key == 1 ? 'onerror' : '';
$(divid).innerHTML = msg;
}

function CheckUserName() {
	var username = trim($('username').value);
	if(username == '' || username == lastusername) {
		return;
	} else {
		lastusername = username;
	}
	var unlen = username.replace(/[^\x00-\xff]/g, "**").length;
	if(unlen < 3 || unlen > 15) 
		{
		messagehandle_register("returnmessage1",1, unlen < 3 ? profile_username_tooshort : profile_username_toolong);
		return;
		}
    ajaxget('/user/ajax/?q=u&username=' + username, 'returnmessage1');
}
function CheckEmail() {
	var email = trim($('email').value);
	var reEmail = /^([A-Za-z0-9])(\w)+@(\w)+(\.)(com|com\.cn|net|cn|net\.cn|org|biz|us|info|gov|gov\.cn|edu|edu\.cn)/;
        if (!email.match(reEmail)&&email!="")
        {
                alert('Email不正确，请填写正确的Email地址!');
                return false;
        }
	
	
	if(email == '' || email == lastemail) {
		return;
	} else {
		lastemail = email;
	}
	var unlen = email.replace(/[^\x00-\xff]/g, "**").length;
	if(unlen < 9 || unlen > 35) 
		{
		messagehandle_register("returnmessage2",1,"Email格式不对，太短或太长！");
		return;
		}
    ajaxget('/user/ajax/?q=m&email=' + email, 'returnmessage2');
}


var Ajaxs = new Array();
var AjaxStacks = new Array(0, 0, 0, 0);
var attackevasive = isUndefined(attackevasive) ? 0 : attackevasive;

/*
 *@ url: 需求请求的 url
 *@ id : 显示的 id
 *@ waitid: 等待的 id，默认为显示的 id，如果 waitid 为空字符串，则不显示 loading...， 如果为 null，则在 showid 区域显示
 *@ linkid: 是哪个链接触发的该 ajax 请求，该对象的属性(如 ajaxdisplay)保存了一些 ajax 请求过程需要的数据。
*/
function ajaxget(url, showid, waitid, loading, display, recall) {  //默认display loading,recall 都是undefined
	waitid = typeof waitid == 'undefined' || waitid === null ? showid:waitid;
	var x = new Ajax();
	x.setLoading(loading);
	x.setWaitId(waitid);
	x.display = typeof display == 'undefined' || display == null ? '' : display;
	x.showId = $(showid);
	if(x.showId) x.showId.orgdisplay = typeof x.showId.orgdisplay === 'undefined' ? x.showId.style.display : x.showId.orgdisplay;

	if(url.substr(strlen(url) - 1) == '#') {
		url = url.substr(0, strlen(url) - 1);
		x.autogoto = 1;
	}

	var url = url + '&target=' + showid;
	//get里第二个参数是回调函数对象,里面的x是ajax对象本身,s是返回值
	x.get(url, function(s, x) {
		evaled = false;
		if(s.indexOf('ajaxerror') != -1) {  //如果返回值有错，则直接运行里面的js代码
			evalscript(s);
			evaled = true;
		}
		if(!evaled && (typeof ajaxerror == 'undefined' || !ajaxerror)) {
			if(x.showId) {
				x.showId.style.display = x.showId.orgdisplay;
				x.showId.style.display = x.display;
				x.showId.className="onerror";
				x.showId.orgdisplay = x.showId.style.display;
				ajaxinnerhtml(x.showId, s);
				ajaxupdateevents(x.showId);
				if(x.autogoto) scroll(0, x.showId.offsetTop);
			}
		}

		if(!evaled)evalscript(s);
		ajaxerror = null;
		if(recall) {eval(recall);}
	});
}



function Ajax(recvType, waitId) {
	for(var stackId = 0; stackId < AjaxStacks.length && AjaxStacks[stackId] != 0; stackId++);
	AjaxStacks[stackId] = 1;

	var aj = new Object();

	aj.loading = '正在检测中...';//public
	aj.recvType = recvType ? recvType : 'HTML';//public
	aj.waitId = waitId ? $(waitId) : null;//public

	aj.resultHandle = null;//private
	aj.sendString = '';//private
	aj.targetUrl = '';//private
	aj.stackId = 0;
	aj.stackId = stackId;

	aj.setLoading = function(loading) {  //如果外部指定了新的loading字符串，就显示指定的，否则就显示类中默认的
		if(typeof loading !== 'undefined' && loading !== null) aj.loading = loading;
	}

	aj.setRecvType = function(recvtype) {
		aj.recvType = recvtype;
	}

	aj.setWaitId = function(waitid) {
		aj.waitId = typeof waitid == 'object' ? waitid : $(waitid);
	}

	aj.createXMLHttpRequest = function() {
		var request = false;
		if(window.XMLHttpRequest) {
			request = new XMLHttpRequest();
			if(request.overrideMimeType) {
				request.overrideMimeType('text/xml');
			}
		} else if(window.ActiveXObject) {
			var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
			for(var i=0; i<versions.length; i++) {
				try {
					request = new ActiveXObject(versions[i]);
					if(request) {
						return request;
					}
				} catch(e) {}
			}
		}
		return request;
	}

	aj.XMLHttpRequest = aj.createXMLHttpRequest();
	aj.showLoading = function() {
		if(aj.waitId && (aj.XMLHttpRequest.readyState != 4 || aj.XMLHttpRequest.status != 200)) {
			aj.waitId.style.display = '';
			aj.waitId.innerHTML = '<span><img src="/img/loading.gif"> ' + aj.loading + '</span>';
		}
	}

	aj.processHandle = function() {
		if(aj.XMLHttpRequest.readyState == 4 && aj.XMLHttpRequest.status == 200) {
			for(k in Ajaxs) {  //ajaxs这个数组中存的是请求url列表
				if(Ajaxs[k] == aj.targetUrl) {
					Ajaxs[k] = null;  //请求完成以后把列表中该url删除
				}
			}
			if(aj.waitId) {   //如果请求完成，也把等待的div给关了
				aj.waitId.style.display = 'none';
			}
			if(aj.recvType == 'HTML') {
				//alert(aj.XMLHttpRequest.responseText);
				aj.resultHandle(aj.XMLHttpRequest.responseText, aj);
			} else if(aj.recvType == 'XML') {
				if(aj.XMLHttpRequest.responseXML.lastChild) {
					aj.resultHandle(aj.XMLHttpRequest.responseXML.lastChild.firstChild.nodeValue, aj);
				} else {
					if(ajaxdebug) {
						var error = mb_cutstr(aj.XMLHttpRequest.responseText.replace(/\r?\n/g, '\\n').replace(/"/g, '\\\"'), 200);
						aj.resultHandle('<root>ajaxerror<script type="text/javascript" reload="1">alert(\'Ajax Error: \\n' + error + '\');</script></root>', aj);
					}
				}
			}
			AjaxStacks[aj.stackId] = 0;
		}
	}

	aj.get = function(targetUrl, resultHandle) {

		setTimeout(function(){aj.showLoading()}, 250);
		if(in_array(targetUrl, Ajaxs)) {
			return false;
		} else {
			Ajaxs.push(targetUrl);
		}
		aj.targetUrl = targetUrl;
		aj.XMLHttpRequest.onreadystatechange = aj.processHandle;
		aj.resultHandle = resultHandle;
		var delay = attackevasive & 1 ? (aj.stackId + 1) * 1001 : 100;
		if(window.XMLHttpRequest) {
			setTimeout(function(){
			aj.XMLHttpRequest.open('GET', aj.targetUrl);
			aj.XMLHttpRequest.send(null);}, delay);
		} else {
			setTimeout(function(){
			aj.XMLHttpRequest.open("GET", targetUrl, true);
			aj.XMLHttpRequest.send();}, delay);
		}

	}
	aj.post = function(targetUrl, sendString, resultHandle) {
		setTimeout(function(){aj.showLoading()}, 250);
		if(in_array(targetUrl, Ajaxs)) {
			return false;
		} else {
			Ajaxs.push(targetUrl);
		}
		aj.targetUrl = targetUrl;
		aj.sendString = sendString;
		aj.XMLHttpRequest.onreadystatechange = aj.processHandle;
		aj.resultHandle = resultHandle;
		aj.XMLHttpRequest.open('POST', targetUrl);
		aj.XMLHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		aj.XMLHttpRequest.send(aj.sendString);
	}
	return aj;
}

function ajaxinnerhtml(showid, s) {
	if(showid.tagName != 'TBODY') {
		showid.innerHTML = s;
	} else {
		while(showid.firstChild) {
			showid.firstChild.parentNode.removeChild(showid.firstChild);
		}
		var div1 = document.createElement('DIV');
		div1.id = showid.id+'_div';
		div1.innerHTML = '<table><tbody id="'+showid.id+'_tbody">'+s+'</tbody></table>';
		$('append_parent').appendChild(div1);
		var trs = div1.getElementsByTagName('TR');
		var l = trs.length;
		for(var i=0; i<l; i++) {
			showid.appendChild(trs[0]);
		}
		var inputs = div1.getElementsByTagName('INPUT');
		var l = inputs.length;
		for(var i=0; i<l; i++) {
			showid.appendChild(inputs[0]);
		}
		div1.parentNode.removeChild(div1);
	}
}
function ajaxupdateevents(obj, tagName) {
	tagName = tagName ? tagName : 'A';
	var objs = obj.getElementsByTagName(tagName);
	for(k in objs) {
		var o = objs[k];
		ajaxupdateevent(o);
	}
}
function ajaxupdateevent(o) {
	if(typeof o == 'object' && o.getAttribute) {
		if(o.getAttribute('ajaxtarget')) {
			if(!o.id) o.id = Math.random();
			var ajaxevent = o.getAttribute('ajaxevent') ? o.getAttribute('ajaxevent') : 'click';
			var ajaxurl = o.getAttribute('ajaxurl') ? o.getAttribute('ajaxurl') : o.href;
			_attachEvent(o, ajaxevent, newfunction('ajaxget', ajaxurl, o.getAttribute('ajaxtarget'), o.getAttribute('ajaxwaitid'), o.getAttribute('ajaxloading'), o.getAttribute('ajaxdisplay')));
			if(o.getAttribute('ajaxfunc')) {
				o.getAttribute('ajaxfunc').match(/(\w+)\((.+?)\)/);
				_attachEvent(o, ajaxevent, newfunction(RegExp.$1, RegExp.$2));
			}
		}
	}
}
function evalscript(s) {
	if(s.indexOf('<script') == -1) return s;
	var p = /<script[^\>]*?>([^\x00]*?)<\/script>/ig;
	var arr = new Array();
	while(arr = p.exec(s)) {
		var p1 = /<script[^\>]*?src=\"([^\>]*?)\"[^\>]*?(reload=\"1\")?(?:charset=\"([\w\-]+?)\")?><\/script>/i;
		var arr1 = new Array();
		arr1 = p1.exec(arr[0]);
		if(arr1) {
			appendscript(arr1[1], '', arr1[2], arr1[3]);
		} else {
			p1 = /<script(.*?)>([^\x00]+?)<\/script>/i;
			arr1 = p1.exec(arr[0]);
			appendscript('', arr1[2], arr1[1].indexOf('reload=') != -1);
		}
	}
	return s;
}