function update_loginpane(){
  $('#logined_username').text($('#username').val());
  $('#login_wrap').hide();  
  $('#logined_wrap').show();
}

    function checkform() {
	    var user = document.getElementById("username").value;
        var pwd = document.getElementById("password").value;
        var rand = document.getElementById("memcode").value;
        if(user == null || user == "") {
    	    alert("请输入用户名");
		    return false;
        }
        if(user.length<4 || user.length>20) {
   	 	    alert("用户名只能在4到20个字符之间");
		    return false;
        }
        if(pwd == null || pwd == "") {
    	    alert("请输入密码");
		    return false;
        }
        if(rand == null || rand == "") {
    	    alert("请输入验证码");
		    return false;
        }
        return true;
    }

$(function(){

  // login
  $('#memcode').bind('focus',function(){ $("#memcodeimg").attr('src',$("#memcodeimg").attr('data')+'?date='+new Date().getTime()); })
  $('#memcodeimg').bind('click',function(){$(this).attr('src',$(this).attr('data')+'?date='+new Date().getTime());});
  $('form#login').bind('keyup',function(e){if(e.keyCode == 13){$("button[name='login']").trigger('click');
    };});

  $("button[name='login']").click(function(){
    if (!checkform()) return false;
     var data = $("#login input").serialize();
     //$.ajaxStop(function(){if(console){console.log('ajax stop');}})
     //$.ajaxComplete(function(){if(console){console.log('ajax stop');}})
     $.ajax({
       url:"index.php?a=login&c=user",
       data:data,
       success:function(resp){
         try {
           switch(resp.code){
             case 101: location.href = 'stage.php';break;
             case 102: location.href = 'admin.php';break;
	case 103: location.href = 'http://www.co-cm.com/app/union_sns/union_index_wap.php';break;
             default: alert(resp.msg);
           }
         } catch (e){ alert('登陆失败'); }
       },
       dataType:'json',
       timeout:10000,
       type:'POST',
       cache:false,
       complete:function(){if(console){console.log('请求完成');}},
       error:function(XMLHttpRequest, textStatus, errorThrown){
         if(console){ console.log(textStatus); }
         alert('请求错误');}
       });

     });

  //logout
  $("button[name='logout']").click(function(){
    $.post('index.php?a=logout&c=user',function(response){
      try {
        var resp = eval(response);
        if (resp.code == 0){
          $('#logined_username').text();
          $('#login_wrap').show();  
          $('#logined_wrap').hide();
        }
      }
      catch(e){
        alert('退出失败');
      }
    });
  });
  // update memcode

  // common 
})

