function setState(element,state){
  if ('ok'==state) {
    globalstate.set(element.getProperty('id'),'ok');
  } else if ('error'==state) {
    globalstate.set(element.getProperty('id'),'error');
  } else if ('normal'==state) {
    if ('myqq'==element.getProperty('id')||'mymsn'==element.getProperty('id')||'homepage'==element.getProperty('id')||'mail_no'==element.getProperty('id')){
      globalstate.set(element.getProperty('id'),'ok');
    } else {
      globalstate.set(element.getProperty('id'),'normal');
    }
  }
  element.getParent().getParent().className = state;
}

function setTips(element,state,tips){
  element.getParent().getParent().getElement('i.i_'+state).setText(tips);
}

function checkloginname(e){
  if (null==e&&''==$('loginname').value){
    setState($('loginname'),'normal');
    return true;
  }
  if (null==e)
  {
	setState($('loginname'),'normal');
	return true;
  }
  if (''==$('loginname').getValue()) {
	setState($('loginname'),'error');
	setTips($('loginname'),'error','会员登录名不能为空。');
	return false;
  }
  if ($('loginname').getValue().length<6) {
	setState($('loginname'),'error');
	setTips($('loginname'),'error','会员登录名太短（不要少于6个字符）。');
	return false;
  }
  if ($('loginname').getValue().length>20) {
	setState($('loginname'),'error');
	setTips($('loginname'),'error','会员登录名太长（不要多于20个字符）。');
	return false;
  }
  if ($('loginname').getValue().test('^[0-9].*$','i')) {
    setState($('loginname'),'error');
    setTips($('loginname'),'error','会员登录名不要使用数字开头。');
    return false;
  }
  if (!$('loginname').getValue().test('^[a-z][a-z0-9]{5,19}$','i')) {
    setState($('loginname'),'error');
    setTips($('loginname'),'error','会员登录名包含了非法字符（只允许使用字母和数字）。');
    return false;
  }
  var url = '/ajax/checkloginname.html?loginname='+encodeURIComponent(this.value)+'&'+Math.random();
  var ajax = new Ajax(url, {
    method: 'get',
    onComplete: function (){
      if (0==this.response.text) {
        setState($('loginname'),'ok');
      } else {
        setState($('loginname'),'error');
        setTips($('loginname'),'error','会员登录名已存在，请重新输入会员登录名。');
      }
    }
  }).request();
  return true;
}

function checkpassword(e) {
  if (null==e&&''==$('password').getValue()){
    setState($('password'),'normal');
    return true;
  }
  if (''==$('password').getValue()) {
    setState($('password'),'error');
    setTips($('password'),'error','密码不能为空。');
    return false;
  }
  if ($('password').getValue().length<6) {
    setState($('password'),'error');
    setTips($('password'),'error','密码太短（不要少于6个字符)。');
    return false;
  }
  if ($('password').getValue().length>20) {
    setState($('password'),'error');
    setTips($('password'),'error','密码太长（不要多于20个字符)。');
    return false;
  }
  if ($('password').getValue()==$('loginname').getValue()) {
    setState($('password'),'error');
    setTips($('password'),'error','密码不要与用户名相同。');
    return false;
  }
  if (!$('password').getValue().test('^[0-9a-z]{6,20}$','i')) {
    setState($('password'),'error');
    setTips($('password'),'error','密码包含了非法字符（只允许使用字母数字组合）。');
    return false;
  }
  if (''!=$('repassword').getValue() && $('password').getValue()!=$('repassword').getValue()) {
    setState($('password'),'error');
    setState($('repassword'),'error');
    setTips($('password'),'error','两次输入的密码不一致，请重新输入。');
    setTips($('repassword'),'error','两次输入的密码不一致，请重新输入。');
    return false;
  }
  setState($('password'),'ok');
  if (''!=$('repassword').getValue()) {
    setState($('repassword'),'ok');
  }
  return true;
}

function checkrepassword(e){
  if (null==e&&''==$('repassword').getValue()) {
    setState($('repassword'),'normal');
    return true;
  }
  if (''==$('password').getValue()) {
    setState($('repassword'),'error');
    setState($('password'),'error');
    setTips($('password'),'error','密码不能为空。');
    setTips($('repassword'),'error','密码不能为空。');
    return false;
  }
  if ($('repassword').getValue()!=$('password').getValue()) {
    setState($('repassword'),'error');
    setState($('password'),'error');
    setTips($('password'),'error','两次输入的密码不一致，请重新输入。');
    setTips($('repassword'),'error','两次输入的密码不一致，请重新输入。');
    return false;
  }
  setState($('repassword'),'ok');
  setState($('password'),'ok');
  return true;
}

function checkusername(e){
  if (null==e&&''==$('username').getValue()){
    setState($('username'),'normal');
    return true;
  }
  if (!$('username').getValue().test('^[^0-9][^!&\^|* 　]{1,14}$','i')) {
    setState($('username'),'error');
    return false;
  }
  setState($('username'),'ok');
  return true;
}

function checkemail(e){
  if (null==e&&''==$('email').getValue()){
    setState($('email'),'normal');
    return true;
  }
  if (!$('email').getValue().test('^[a-z0-9._\-]*@[a-z0-9\-]*[.][.a-z0-9\-]+$','i')){
    setState($('email'),'error');
    return false;
  }
  setState($('email'),'ok');
  return true;
}

function checkmobile(e){
  if (null==e&&''==$('mobile').getValue()){
    setState($('mobile'),'normal');
    return true;
  }
  if (!$('mobile').getValue().test('^[0-9]{6,20}$','i')) {
    setState($('mobile'),'error');
    return false;
  }
  setState($('mobile'),'ok');
  return true;
}

function checkim(){
  if(''==$('im').getValue()){
    setState($('im'),'normal');
    return true;
  }
  setState($('im'),'ok');
  return true;
}

function checkmyqq(){
  if(''==$('myqq').getValue()){
    setState($('myqq'),'normal');
    return true;
  }
   if (!$('myqq').getValue().test('^[0-9]{6,15}$','i')) {
    setState($('myqq'),'error');
    return false;
  }
  setState($('myqq'),'ok');
  return true;
}

function checkmymsn(){
  if(''==$('mymsn').getValue()){
    setState($('mymsn'),'normal');
    return true;
  }
  if (!$('mymsn').getValue().test('^[a-z0-9._\-]*@[a-z0-9\-]*[.][.a-z0-9\-]+$','i')){
    setState($('mymsn'),'error');
    return false;
  }
  setState($('mymsn'),'ok');
  return true;
}

function checkposition(e){
  if (null==e&&''==$('position').getValue()){
    setState($('position'),'normal');
    return true;
  }
  if(!$('position').getValue().test('^([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[\u2010-\u33D5]|[\uFE30-\uFFE5]|[\x00-\x7F]|[0-9]){2,30}$','i')){
    setState($('position'),'error');
    return false;
  }
  setState($('position'),'ok');
  return true;
}

function checksignup(e){
  if (null==e&&''==$('signup').getValue()){
    setState($('signup'),'normal');
    return true;
  }
  var url = '/ajax/checkcheckmark.html?signup=' + encodeURIComponent($('signup').getValue()) + '&' + Math.random();
  var ajax = new Ajax(url, {
      method: 'get',
      onComplete: function () {
 	 	 if (0==this.response.text) {
              setState($('signup'), 'ok');
          } else {
              setState($('signup'), 'error');
          }
      }
  }).request();
  return true;
}

function checkhomepage(){
  if (''==$('homepage').getValue()) {
    setState($('homepage'),'normal');
    return true;
  }
  if (!$('homepage').getValue().test('^[a-z0-9\-]*[.][a-z.0-9\-]*$','i')) {
    setState($('homepage'),'error');
    return false;
  }
  setState($('homepage'),'ok');
  return true;
}

function checkcomintro(e){
  if (null==e&&''==$('com_intro').getValue()){
    setState($('com_intro'),'normal');
    return true;
  }
  if (''==$('com_intro').getValue()) {
    setState($('com_intro'),'error');
    setTips($('com_intro'),'error','公司简介不要为空。');
    return false;
  }
  if ($('com_intro').getValue().length<15) {
    setState($('com_intro'),'error');
    setTips($('com_intro'),'error','公司简介太短（不要少于15个汉字）。');
    return false;
  }
  if (!$('com_intro').getValue().test('^([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[\u2010-\u33D5]|[\uFE30-\uFFE5]|[\x00-\x7F]|[0-9]){15,4000}$','i')) {
    setState($('com_intro'),'error');
    setTips($('com_intro'),'error','请认真填写贵公司的介绍信息，让您的业务伙伴更详细的了解您的业务（不得少于15个汉字）。');
    return false;
  }
  setState($('com_intro'),'ok');
  return true;
}

function checkfax(e){
  if (null==e&&''==$('fax').getValue()){
    setState($('fax'),'normal');
    return true;
  }
  if (!$('fax').getValue().test('^([\x21-\x40 \-]|[\x5B-\x60 \-]|[\x7B-\x7E \-]){6,30}$','i')) {
    setState($('fax'),'error');
    return false;
  }
  setState($('fax'),'ok');
  return true;
}

function checktel(e){
  if (null==e&&''==$('tel').getValue()){
    setState($('tel'),'normal');
    return true;
  }
  if (!$('tel').getValue().test('^([\x21-\x40 \-]|[\x5B-\x60 \-]|[\x7B-\x7E \-]){6,30}$','i')) {
    setState($('tel'),'error');
    return false;
  }
  setState($('tel'),'ok');
  return true;
}

function checkcompostcode(e){
  if (''==$('mail_no').getValue()){
    setState($('mail_no'),'normal');
    return true;
  }
  if (!$('mail_no').getValue().test('^([\x21-\x40]|[\x5B-\x60]|[\x7B-\x7E]){6}$','i')) {
    setState($('mail_no'),'error');
    return false;
  }
  setState($('mail_no'),'ok');
  return true;
}

function checkaddress(e){
  if (null==e&&''==$('address').getValue()){
    setState($('address'),'normal');
    return true;
  }
  if (!$('address').getValue().test('^[^0-9]([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[\u2010-\u33D5]|[\uFE30-\uFFE5]|[\x00-\x7F]|[0-9]){3,100}$','i')) {
    setState($('address'),'error');
    return false;
  }
  setState($('address'),'ok');
  return true;
}

function checkprovince(e){
  if (null==e&&''==$('province').getValue()){
    setState($('province'),'normal');
    return true;
  }
  if (''==$('city').getValue()){
    setState($('province'),'error');
    return false;
  }
  setState($('province'),'ok');
  return true;
}

function checkcity(e) {
  if (null==e&&(0==$('city').length||''==$('city').getValue())) {
    setState($('city'),'normal');
    return true;
  }
  if (''==$('city').getValue()){
    setState($('city'),'error');
    return false;
  }
  setState($('province'),'ok');
  setState($('city'),'ok');
  return true;
}

function checkmainoperation(e){
  if (null==e&&''==$('main_operation').getValue()){
    setState($('main_operation'),'normal');
    return true;
  }
  if (!$('main_operation').getValue().test('^([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[\u2010-\u33D5]|[\uFE30-\uFFE5]|[\x00-\x7F]|[0-9]){5,200}$$','i')) {
    setState($('main_operation'),'error');
    return false;
  }
  setState($('main_operation'),'ok');
  return true;
}

function checkmainoperationforlawyer(e){
  if (null==e&&''==$('main_operation').getValue()){
    setState($('main_operation'),'normal');
    return true;
  }
  if (!$('main_operation').getValue().test('^([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[\u2010-\u33D5]|[\uFE30-\uFFE5]|[\x00-\x7F]|[0-9]){2,200}$$','i')) {
    setState($('main_operation'),'error');
    return false;
  }
  setState($('main_operation'),'ok');
  return true;
}

function checkcomtype(e){
  if (null==e&&''==$('com_type').getValue()){
    setState($('com_type'),'normal');
    return true;
  }
  if (''==$('com_type2').getValue()){
    setState($('com_type'),'error');
    return false;
  }
  setState($('com_type'),'ok');
  return true;
}

function checkcomtype2(e) {
  if (null==e&&(0==$('com_type2').length || ''==$('com_type2').getValue())) {
    setState($('com_type2'),'normal');
    return true;
  }
  if (''==$('com_type2').getValue()){
    setState($('com_type2'),'error');
    return false;
  }
  setState($('com_type'),'ok');
  setState($('com_type2'),'ok');
  return true;
}

function checkcomcn(e){
  if (null==e&&''==$('com_cn').getValue()){
    setState($('com_cn'),'normal');
    return true;
  }
  if ($('com_cn').getValue().length>30) {
    setState($('com_cn'),'error');
    setTips($('com_cn'),'error','公司名称太长（不要超过30个字符）。');
    return false;
  }
  if (!$('com_cn').getValue().test('^([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[\u2010-\u33D5]|[\uFE30-\uFFE5]|[\x00-\x7F]|[0-9]){2,30}$','i')) {
    setState($('com_cn'),'error','请填写您所在公司名称，准确的公司名称信息可以让你的业务伙伴更快的找到您。');
    return false;
  }
  setState($('com_cn'),'ok');
  return true;
}

function checklawfirm(e){
  if (null==e&&(0==$('lawfirm').length||''==$('lawfirm').getValue())) {
    setState($('lawfirm'),'normal');
    return true;
  }
  if (''==$('lawfirm').getValue()){
    setState($('lawfirm'),'error');
    return false;
  }
  setState($('lawfirm'),'ok');
  return true;
}
function checkspeciality(e){
  if (null==e&&''==$('speciality').getValue()){
    setState($('speciality'),'normal');
    return true;
  }
  if (''==$('speciality').getValue()){
    setState($('speciality'),'error');
    return false;
  }
  setState($('speciality'),'ok');
  return true;
}
function dosubmit(url){
  error = null;
  globalstate.each(function(value,key) {
    if ('normal'==value || 'error'==value) {
	  if(key=='com_cn' && $('newLawfirm') && $('newLawfirm').getStyle('display')=='none'){
		  //alert(key);
	  }else{
		  $(key).getParent().getParent().className = 'error';
		  error = $(key);
	  }
    }
  });
  if (error!=null){
    error.focus();
    error.getParent().getParent().className = 'error';
    return false;
  }
  document.forms['regist_form'].action=url;
  //alert('提交');
  //return false;
  return true;
}
