if (typeof (Silkroad) == "undefined")
	Silkroad = {};

Silkroad.Login = new function() {
	this.popup = true;
	this.opened = false;
	this.logout = function() {
		var myAjax = new Ajax.Request(base + "/login!logout.jspa", {
			method :'post',
			parameters :null,
			asynchronous :true,
			onComplete : function() {
				//this.closeLoginDialog();
				window.top.location=base+"/portal.jspa";
			}.bind(this)
		});
	};
	
	this.logout2 = function(){
		var myAjax = new Ajax.Request(base+"/login!logout.jspa", {
			method :'post',
			parameters :null,
			asynchronous :true,
			onComplete : function() {
				window.top.location=base+"/portal.jspa";
			}.bind(this)
		});
	}

	this.openLoginDialog = function() {
		 jQuery.nyroModalManual({
		 width:'200',
         height:'400',
	      url: base+'/poplogin.jspa?redirect='+document.URL
	    });
	   // return false;
	};
	
	this.openICLoginDialog = function() {
		 jQuery.nyroModalManual({
		 width:'300',
         height:'400',
	      url: base+'/poplogin.jspa?redirect='+document.URL+'&iclogin=1'
	    });
	   // return false;
	};
	
	this.openICardLoginDialog = function() {
		 var checkbus = document.getElementsByName("checkbus");
		 var j = "";
    	 for (var i = 0; i < checkbus.length; i++) {
         	if (checkbus[i].checked) {
           	 	j+=checkbus[i].value+",";
        	}
    	 }
    	 if ( j == "9,10," || j == "10,")
	    	 jQuery.nyroModalManual({       
	    	 width:'200',
              height:'400',
	          url: base+'/poplogin.jspa?num='+j+'&iclogin=1&redirect=icardbus!cardReaderAndCD.jspa'
	        });
	     else
			 jQuery.nyroModalManual({	
			 width:'200',
             height:'400',	 
		      url: base+'/poplogin.jspa?num='+j+'&iclogin=1&redirect=icardbus!choosePage.jspa'
		    });
	   // return false;
	};
	this.openPassDialog = function(){
		jQuery.nyroModalManual({
	      url: 'password.jspa'
	    });
	    return false;
	};
	this.ptpLogin = function(){
		window.location.href = "http://app.chinabeston.com/zjport/index.jspa?menuId=14792";
		//jQuery.nyroModalManual({
		//  height:'170',
	    //  url: 'http://cas.chinaport.gov.cn/cas/login?service=http://www.zjport.gov.cn/zjport/protected/caslogin'
	    //});
	   // return false;
	};
	this.chinaport=function(){
	
		jQuery.nyroModalManual({
		  height:'300',
		  width:'650',
	      url: 'http://www1.chinaport.gov.cn/subeport/zhejiang/pub/login.htm'
	    });
	}
	this.returnPassword = function(){
		if (null == $("loginusername").value || $("loginusername").value == "") {
			alert("请输入用户名！")
			$("loginusername").focus();
			return false;
		}
		if (null == $("loginemail").value || $("loginemail").value.length <=0) {
			alert("请输入邮箱！")
			$("loginemail").focus();
			return false;
		}else{
			if(!(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($("loginemail").value))){
				alert("邮箱格式错误！")
				$("loginemail").focus();
				return false;
			}
		}
		$("getPassButton").disable();
		var params = {
			"username" :$("loginusername").value,
			"email" :$("loginemail").value
		};
		var ajax = new Ajax.Request(base + "/login!returnPass.jspa", {
			"method" :"post",
			"parameters" :params,
			"onSuccess" : function(data) {
				$("getPassButton").enable();
				str= (data.responseText).split(";");
				str1 = str[1].split("#");
				if(str1[0]=="Success"){
					$("getPassButton").hide();
				}
				$("login_success").update(str1[1]);
			}.bind(this)
		});
	};
	
	this.register = function(){
		jQuery.nyroModalManual({
	      url: 'register.jspa'
	    });
	};

	this.closeLoginDialog = function(redirect) {
		if (this.popup)
			jQuery.weeboxs.close('#login_dialog');
		if (redirect == null)
			window.location.reload();
		else if (redirect != "")
			window.location.href = redirect;
	};

	this.checkLogin = function() {
		if (null == $("loginusername").value || $("loginusername").value == "") {
			alert("请输入用户名！")
			$("loginusername").focus();
			return false;
		}
		if (null == $("login_valid").value) {
			alert("请输入验证码！")
			$("login_valid").focus();
			return false;
		}
		$$(".login_op").each( function(el) {
			el.disable();
		});
		return true;
	};

	this.login = function(redirect) {
		if (!this.checkLogin())
			return;
		var param = {
			"username" :$("loginusername").value,
			"password" :$("loginpassword").value,
			"valid" :$("login_valid").value,
			"loginflag":"common",
			"adminlogin":"0"
		};
		if (redirect && redirect != "")
			param.redirect = redirect;
		var ajax = new Ajax.Request(base + "/login!comlogin.jspa", {
			"method" :"post",
			"parameters" :param,
			"onSuccess" : function(transport) {
				str= (transport.responseText).split(";");
				if(str[1]!=""){
					doChangePic1();
					$("login_failure1").show();
					jQuery("#login_failure1").html("<b style='color:red'><span align='center'>"+str[1]+"</span></b>");
				}else{
					redirect=str[0];
					if(redirect){
						window.top.location=redirect;
					}
					else
						window.top.location=base+"/portal.jspa";
				}
				
			}.bind(this),
			"onFailure" : function() {
				$("loginpassword").value = "";
				$("login_valid").value = "";
				$("loginusername").focus();
				$("login_failure").show();
				$$(".login_op").each( function(el) {
					el.enable();
				});
			}.bind(this)
		});
	};

	this.reloadValidCode = function() {
		$("login_valid_code").src = base + "/valid.code.image?" + Math.random();
	};
};

if (Silkroad.Login.popup != true) {
	jQuery(document).ready( function() {
		Silkroad.Login.reloadValidCode();
		$("loginusername").value = "";
		$("loginpassword").value = "";
		$("login_valid").value = "";
		$("login_failure").hide();
		$("loginusername").focus();
	});
}

