var LOGIN_IF = false;
//加载指定的页面
function loadPage(myUrl,spanId)
{
	dojo.io.bind({
		url: myUrl, // 指定将form提交到哪个页面
		preventCache:true,
		timeout:10000, // 指定超时时间
			load: function(type, data, evt){
				document.getElementById(spanId).innerHTML=data;

			}
		});
}

function setCookie(name,value,type)
{
	if(type==0)
	{
		document.cookie = name + "="+ escape(value);
	}
	else if(type==1)
	{
		var exp = new Date(); 
		exp.setTime(exp.getTime() + 24*60*60*1000);
		document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString() + "; path=/";
		//document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString() + "; path=/ ; domain=epopos.com ; secure";
	}
	else if(type==2)
	{
		var exp = new Date();
		exp.setTime(exp.getTime() + 30*24*60*60*1000);
		document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString() + "; path=/";
		//document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString() + "; path=" + "/" + "; domain=epopos.com; secure";
	}
	else if(type==3)
	{
		var exp = new Date(); 
		exp.setTime(exp.getTime() +365*24*60*60*1000);
		document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString() + "; path=/";
		//document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString() + "; path=" + "/" + "; domain=epopos.com; secure";
	}
}

function getCookie(name)
{
	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr != null) return unescape(arr[2]); return null;
}
function delCookie(name)
{
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval=getCookie(name);
	if(cval!=null) document.cookie=name +"="+cval+";expires="+exp.toGMTString();
}
/****设置方式***/
function loginMethodSet()
{
	var loginMethod = document.getElementById("loginMethod").value;
	if(loginMethod==0)
	{
		document.getElementById("userNameSpan").innerHTML="用户名：";
	}
	else
	{
		document.getElementById("userNameSpan").innerHTML="EP号码：";
	}
	
}
//登陆
     function subForm(){
		var loginMethod = document.getElementById("loginMethod").value;
     	var username = dojo.byId("loginUsername");
     	var password = dojo.byId("loginPassword");
     	var loginTd = dojo.byId("loginTd");
     	if(username.value=="")
     	{
			if(loginMethod==0)
     			loginTd.innerHTML = "<img src='/assets/images/error.gif'/>&nbsp;请输入用户名！";
			else if(loginMethod==1)
				loginTd.innerHTML = "<img src='/assets/images/error.gif'/>&nbsp;请输入EP号码！"
			username.focus();
		    return false;
		}
		else
		{
			loginTd.innerHTML = "";
		}

	    if(password.value=="")
     	{
			loginTd.innerHTML = "<img src='/assets/images/error.gif'/>&nbsp;请输入密码！";
			password.focus();
		    return false;
		}
		else
		{
			loginTd.innerHTML = "";
		}
	  	loginTd.innerHTML='正在提交登录信息...<img src="/assets/images/load.gif">';
	    dojo.io.bind({
	  			url: "userLogin.os", // 指定将form提交到哪个页面
	  			formNode: dojo.byId("userLoginForm"), // 指定提交哪个form
	  			mimetype:"text/html", // 指定用什么编码方式提交
	  			method:"POST", // 指定是post还是get,当然此处要提交,当然就要用post了
	  			encoding:"utf-8", // 指定提交内容的编码
	  			timeout:10000, // 指定超时时间
	  			load: function(type, data, evt){
					if(data=="failed")
					{
						var loginTd = dojo.byId("loginTd");
						loginTd.innerHTML="<img src='/assets/images/error.gif'/>&nbsp;登录失败，账户或者密码错误！";
					
						return;
					}
					var loginOption = dojo.byId("loginOption").value;
					setCookie("epoposLoginMethod",loginMethod,loginOption);
					setCookie("epoposUserName",username.value,loginOption);
					setCookie("epoposPasswrod",password.value,loginOption);
					loadPage("login.jsp","loginDiv");
	  			},
	  			error: function(type, error){
	  				
	  			},
	  			timeout: function(type, data, evt){
	  				
				}
   			});
   			return false;
}

//检测是否登录
function checkLoginIf()
{
	dojo.io.bind({
		url: "/checkLoginIf.jsp", // 指定将form提交到哪个页面
		preventCache:true,
		timeout:10000, // 指定超时时间
			load: function(type, data, evt){
				if(data=="false")
					autoLogin();
			}
		});
}

/**
*windId 窗口ID
*width,窗口宽度
*height 窗口高度
*winTitle 窗口标题
*winUrl 窗口链接
*关闭窗口时需要执行的命令,不执行 为空
**/
var zIndex = 5000;
function createWindow(winId,width,height,winTitle,winUrl,command,scrollIf)
{
	var cw = document.documentElement.clientWidth;
	var ch = document.documentElement.clientHeight;

	
	
	var loadDiv = document.getElementById("loadDiv");
	var st =document.documentElement.scrollTop||document.body.scrollTop;
	loadDiv.style.top=0;
	loadDiv.style.zIndex = zIndex++;
	loadDiv.style.height = document.documentElement.scrollHeight+"px";
	loadDiv.style.display = "block";
	var loadFrame = document.getElementById("loadFrame");
	loadFrame.style.top=0;
	loadFrame.style.height = document.documentElement.scrollHeight+"px";
	loadFrame.style.display = "block";
	this.startX = (cw-width)/2;
	if(startX<0)
		startX=0;
	this.startY= (ch-height)/2;
	if(startY<=0)
		startY=0;
	var newWindow=document.createElement("div");
	newWindow.className="popWindow";
	newWindow.id = winId;	
	newWindow.style.zIndex = zIndex++;
	newWindow.onmouseover=function()
	{
		drag(winId,1)	
	}

	 document.body.appendChild(newWindow);

	 newWindow.style.left = startX+"px";
	
	 newWindow.style.top = (startY+st)+"px";
	 newWindow.style.height = height+100;
	 newWindow.style.width = width;

	var htmlStr = '<table height="'+height+'" width="'+width+'" border="0" cellspacing="0" cellpadding="0">'+
 ' <tr>'+
    '<td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">'+
     ' <tr>'+
        '<td width="10"><img src="/assets/new/images/window/popWin_03.png" width="10" height="30" /></td>'+
        '<td background="/assets/new/images/window/popWin_04.png" style="color:#1b3642">'+winTitle+'</td>'+
        '<td width="44"><div align="right"><img src="/assets/new/images/window/popWin_06.png" width="44" height="30" style="cursor:pointer" onclick="eval(\''+command+'\');closeWindow(\''+winId+'\')" title="关闭"/></div></td>'+
      '</tr>'+
    '</table></td>'+
  '</tr>'+
  '<tr>'+
    '<td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">'+
      '<tr>'+
        '<td width="6" background="/assets/new/images/window/popWin_08.png"><img src="/assets/new/images/window/popWin_08.png"></td>'
	if(scrollIf==1)
	{
		htmlStr+='<td><iframe src="'+winUrl+'" id="PF'+winId+'" name="PF'+winId+'" width="'+width+'" height="'+height+'" frameborder="0" scrolling="auto"></iframe></td>';
	}
	else
	{
		htmlStr+='<td><iframe src="'+winUrl+'" id="PF'+winId+'" name="PF'+winId+'" width="'+width+'" height="'+height+'" frameborder="0" scrolling="no"></iframe></td>';
	}
        
        htmlStr+='<td width="6" background="/assets/new/images/window/popWin_11.png"><img src="/assets/new/images/window/popWin_11.png"></td>'+
      '</tr>'+
   ' </table></td>'+
  '</tr>'+
  '<tr>'+
    '<td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">'+
      '<tr>'+
       ' <td height="6" width="6"><img src="/assets/new/images/window/popWin_10.png" width="6" height="6" /></td>'+
        '<td background="/assets/new/images/window/popWin_17.png" height="6" width="100%"></td>'+
        '<td height="6" width="6"><img src="/assets/new/images/window/popWin_18.png" width="6" height="6" /></td>'+
     ' </tr>'+
   ' </table></td>'+
  '</tr>'+
'</table>';
	newWindow.innerHTML = htmlStr;
}
//关闭窗口
function closeWindow(winId)
{

	var myWindow = document.getElementById(winId);
	myWindow.style.display = "none";
	var popFrame = document.getElementById("PF"+winId);
	popFrame.src = "";
	var loadDiv = document.getElementById("loadDiv");
	loadDiv.style.display = "none";
	var loadFrame = document.getElementById("loadFrame");
	loadFrame.style.display = "none";
	document.body.removeChild(myWindow);
}

/****打开注册界面****/
function openRegWindow()
{
	createWindow("userRegWindow",530,327,"用户注册","/uReg.html",0);
}
/***密码查找页面****/
function openFindPassword()
{
	createWindow("forgetPasswordWindow",330,280,"查找密码","/desktop/findPassword.html",0);
}

function logout()
{
	setCookie("epoposLoginMethod",null);
	setCookie("epoposUserName",null);
	setCookie("epoposPasswrod",null);
	dojo.io.bind({
		url: "/window/logout.os", // 指定将form提交到哪个页面
		preventCache:true,
		timeout:10000, // 指定超时时间
			load: function(type, data, evt){
				loadPage("login.jsp","loginDiv");
			}
		});
}

//登陆
function autoLogin(){

	var username = getCookie("epoposUserName");
	var password = getCookie("epoposPasswrod");
	var loginMethod = getCookie("epoposLoginMethod");
	if(username=="null"||password=="null"||loginMethod=="null"||username==null||password==null||loginMethod==null)
		return;
	dojo.io.bind({
			url: "/userLogin.os?loginFrom=5&userName="+username+"&password="+password+"&loginMethod="+loginMethod,
			mimetype:"text/html", // 指定用什么编码方式提交
			method:"POST", // 指定是post还是get,当然此处要提交,当然就要用post了
			encoding:"utf-8", // 指定提交内容的编码
			timeout:10000, // 指定超时时间
			load: function(type, data, evt){
				loadPage("login.jsp","loginDiv");
			},
			error: function(type, error){
				
			},
			timeout: function(type, data, evt){
				
			}
		});
		return false;
 }
//*****拖动对象***/
function drag(o,s)
{
	if (typeof o == "string") o = document.getElementById(o);
	o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
	o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
	if(s!=0)
		o.orig_index = o.style.zIndex;
	o.onmousedown = function(a)
	{

	  this.style.cursor = "move";
	  if(s!=0)
	  	this.style.zIndex = zIndex;
	  var d=document;
	  if(!a)a=window.event;
	  var x = a.clientX+d.body.scrollLeft-o.offsetLeft;
	  var y = a.clientY+d.body.scrollTop-o.offsetTop;
	  d.ondragstart = "return false;"
	  d.onselectstart = "return false;"
	  d.onselect = "document.selection.empty();"
	  if(o.setCapture)
	   o.setCapture();
	  else if(window.captureEvents)
	   window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
	   
	  d.onmousemove = function(a)
	  {
	   if(!a)a=window.event;
	   o.style.left = a.clientX+document.body.scrollLeft-x;
	   o.style.top = a.clientY+document.body.scrollTop-y;
	   o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
	   o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
	  }
		
	  d.onmouseup = function()
	  {
	   if(o.releaseCapture)
		o.releaseCapture();
	   else if(window.captureEvents)
		window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
	   d.onmousemove = null;
	   d.onmouseup = null;
	   d.ondragstart = null;
	   d.onselectstart = null;
	   d.onselect = null;
	   o.style.cursor = "normal";
	   o.style.zIndex = o.orig_index;
	  }
	  
	}

	if (s)
	{
	  var orig_scroll = window.onscroll?window.onscroll:function (){};
	  window.onscroll = function ()
	  {
	   orig_scroll();
	   o.style.left = o.orig_x + document.body.scrollLeft;
	   o.style.top = o.orig_y + document.body.scrollTop;
	  }
	}

}


