/*
2009-10-13

方法调用说明：

总共包含五个参数，如果不想利用其中部分参数，使之为空即可，但必须保证参数名称有效
例如：var canclePath = ''；

//参数一：用户取消登录时，跳转的页面的地址（必须以“http://”为前缀）
var canclePath = 'http://appb.niwota.com/app_games/1_index.jsp';

//参数二：Form表单提交地址（包含“requestPath”参数）
var actionPath = 'http://www.niwota.com/user/login_page.jsp?requestPath=http://appb.niwota.com/app_games/loadGame.jsp?gameId=<%= gameId %>';

//参数三：登录窗口显示的提示信息
var message = '您现在还没有登录，不能继续玩小游戏了，请先登录。';

//参数四：从cookie中获取到的用户姓名
var userName = objJson.userName_cookie;

//参数五：用户注册来源
var sign = 'xiaoyouxi';

//方法调用
loginDialog(canclePath, actionPath, message, userName, sign);
*/

function loginDialog(canclePath, actionPath, message, userName, sign,obj1,obj2) {
	//alert(userName);

	//check UI
	if (document.getElementById('dialogWrap')) {
		return;
	}

        //initialize the UI
	var pageMask = document.createElement('div');
	pageMask.id = 'pageMask';
	var dialogWrap = document.createElement('div');
	dialogWrap.id = 'dialogWrap';
	var dialogWin = document.createElement('div');
	dialogWin.id = 'dialog';
	dialogWin.style.width = '400px';
	var dialogHead = document.createElement('div');
	dialogHead.id = 'dialogHead';
	var dialogBody = document.createElement('div');
	dialogBody.id = 'dialogBody';
	//var dialogFoot = document.createElement('div');
	//dialogFoot.id = 'dialogFoot';

	dialogHead.innerHTML =
        '<p id="dialogControl" onclick="javascript:removeDialog();hidenDiv(\''+obj1+'\',\''+obj2+'\');goURL(\'' + canclePath + '\');">' +
            '<a href="javascript:void(0);" class="close">X</a>' +
        '</p>';

	dialogBody.innerHTML =
        '<div id="dialog_login">' +
            '<div class="msgTips">' +
                '<p>' + message + '</p>' +
            '</div>' +
            '<form id="login_form" method="post" onsubmit="return checkLogin(this);" action="' + actionPath + '" >' +
                '<input type="hidden" name="doLogin" value="true"/>' +
                '<p>' +
                    '<label>帐户：<input type="text" id="user_name" style="width:120px" name="username" class="f-text" value="' + userName + '" /></label>' +
                '</p>' +
                '<p>' +
                    '<label>密码：<input type="password" id="user_password" name="password" class="f-text" style="width:120px" /></label>' +
                '</p>' +
                '<p class="f-act">' +
                    '<input type="submit" id="login_button" name="submitlogin" class="button button_submit" value="登录" /> ' +
                    '<input type="checkbox" name="remember" id="checkbox" checked="checked" />记住' +
                '</p>' +
            '</form>' +
            '<p align="center">' +
                '<a href="http://user.niwota.com/user/register.jsp?sign=' + sign + '" style="color:#f60;">还没有帐号？请点这里注册。</a>&nbsp;&nbsp;' +
                '<a href="http://www.niwota.com/user/getpw.jsp">找回密码</a>' +
            '</p>' +
        '</div>';


	  dialogWin.appendChild(dialogHead);
	  dialogWin.appendChild(dialogBody);

          //adjust position
	             var oS = pageMask.style;
	          oS.top = oS.left = oS.margin = oS.padding = "0px";
	          if (document.body.clientHeight)
	          {var wh = document.body.clientHeight + "px";}else if (window.innerHeight)
	          {var wh = window.innerHeight + "px";}else{var wh = "100%";}oS.width = "100%";
		oS.height = wh;oS.position = "absolute";oS.zIndex = "3";
	//  pageMask.style.height = document.body.scrollHeight;
          //pageMask.style.width = document.body.scrollWidth;
	  var wrapY = document.documentElement.scrollTop + 160;
	  dialogWrap.style.top = wrapY + 'px';

	  //show dialog
	  document.body.appendChild(pageMask);
	  document.body.appendChild(dialogWrap);
	  document.getElementById('dialogWrap').appendChild(dialogWin);
}

function removeDialog() {

        //移除弹出窗口
	if (document.getElementById('dialogWrap')) {
		document.body.removeChild(document.getElementById('dialogWrap'));
	}

        //移除背景层
	if (document.getElementById('pageMask')) {
		document.body.removeChild(document.getElementById('pageMask'));
	}
}

function goURL(canclePath){
  if(canclePath.length > 0){
    if(canclePath.indexOf('http://') == 0){
      window.location.href = canclePath;
    }
  }
}


  function hidenDiv(obj1,obj2){
    if(obj1!=""&&obj2!=""){
    document.getElementById(obj1).style.display="block";
    document.getElementById(obj2).style.display="none";
    }
    }

function checkLogin(form_login){
    var vUsername=form_login.username.value;
    var vPassword=form_login.password.value;

    if(vUsername.length==0) {
      window.alert('请输入用户名或邮箱');
      form_login.username.focus();
      return false;
    }
    if(vPassword.length==0) {
      window.alert('请输入密码');
      form_login.password.focus();
      return false;
    }
    return true;
  }
