﻿/// <reference path="jquery-1.3.2-vsdoc.js" />

function IDPJsLogin() {
	// action		//IDP身份认证页面
	this.IDPLoginPage = "";
	// spid			//SP编码
	this.SPID = "";
	// backurl		//登陆成功页面
	this.BackUrl = "";
	// type			//验证类型
	this.SignType = "";
	// errorPage	//出错提示页面
	this.ErrorPage = "";
	// password		//密码
	this.Password = "";
	// mobileno		//账号
	this.MobileNo = "";
	// valid		//验证码
	this.ValidCode = "";
	// 哪个门户登陆的
	this.MobileLoginType = "";
	// 等于SessionID
	this.Token = "";
	// 登陆的表单页面
	this.LoginFormPage = "/ADCECPortal/TokenManage/IDP_LoginForm.html"; // 不能乱修改，这个修改时，相应的文件也需要修改
	// 框架的ID
	this.FrameID = "my_IDPLoginIframeID_b888-b8c7c765d16f";
	// 显示错误信息的元素ID
	this.ShowErrorTextID = "";
	// 是否用警告框的形式来提示
	this.ShowAlert = "false";
	// 出错后跳转页面,只有在 this.ShowDialog 的取值为 True 才有用
	this.ErrorJumpUrl = "";


	// 持久化时，数据存在哪个ID的元素里
	var PersistenceID = "my_IDPLoginParameters_3242_a3924c_a32b3d94ef";
	// 集团登陆选择层
	var ChooseGroupDialogID = "ShowChooseGroupDialog"; // 不能乱修改，这个修改时，相应的文件也需要修改
	// 进度条元素ID
	var ProcessGuageID = "ProcessGuageID_32260c1-9b8c-42d3"
	// iframe对象
	this.Iframe;

	// 保存变量到文档对象中，可以认为是持久化
	this.Persistence = function() {
		var ParamViewState = jQuery("#" + PersistenceID);
		var IsEmpty = IsEmptyObject(ParamViewState);
		if (IsEmpty)
			ParamViewState = jQuery('<div id="' + PersistenceID + '" style="width:0px; height:0px; display:none;"></div>');

		ParamViewState.attr("IDPLoginPage", this.IDPLoginPage);
		ParamViewState.attr("SPID", this.SPID);
		ParamViewState.attr("BackUrl", this.BackUrl);
		ParamViewState.attr("SignType", this.SignType);
		ParamViewState.attr("ErrorPage", this.ErrorPage);
		ParamViewState.attr("IdentityID", this.Password);
		ParamViewState.attr("MobileNo", this.MobileNo);
		ParamViewState.attr("ValidCode", this.ValidCode);
		ParamViewState.attr("MobileLoginType", this.MobileLoginType);
		ParamViewState.attr("Token", this.Token);
		ParamViewState.attr("LoginFormPage", this.LoginFormPage);
		ParamViewState.attr("FrameID", this.FrameID);
		ParamViewState.attr("ShowErrorTextID", this.ShowErrorTextID);
		ParamViewState.attr("ShowAlert", this.ShowAlert);
		ParamViewState.attr("ErrorJumpUrl", this.ErrorJumpUrl);

		if (IsEmpty)
			ParamViewState.appendTo("body");
	}

	// 加载持久化的变量数据
	this.Load = function() {
		var Param = jQuery("#" + PersistenceID);
		if (!IsEmptyObject(Param)) {
			this.IDPLoginPage = Param.attr("IDPLoginPage");
			this.SPID = Param.attr("SPID");
			this.BackUrl = Param.attr("BackUrl");
			this.SignType = Param.attr("SignType");
			this.ErrorPage = Param.attr("ErrorPage");
			this.Password = Param.attr("IdentityID");
			this.MobileNo = Param.attr("MobileNo");
			this.ValidCode = Param.attr("ValidCode");
			this.MobileLoginType = Param.attr("MobileLoginType");
			this.Token = Param.attr("Token");
			this.LoginFormPage = Param.attr("LoginFormPage");
			this.FrameID = Param.attr("FrameID");
			this.ShowErrorTextID = Param.attr("ShowErrorTextID");
			this.ShowAlert = Param.attr("ShowAlert");
			this.ErrorJumpUrl = Param.attr("ErrorJumpUrl");
		}
	}

	// 发送登陆数据到IDP身份认证页面
	this.IDPLogin = function() {

		if (this.BackUrl.indexOf("?") < 0)
			this.BackUrl = this.BackUrl + "?InFrame=true"; // 该参数表明调用是在框架内完成的
		else
			this.BackUrl = this.BackUrl.replace("?", "?InFrame=true&"); // 该参数表明调用是在框架内完成的
		this.Password = enString(this.Password);
		//this.ErrorPage = escape(this.ErrorPage);

		// 持久化参数
		this.Persistence();

		jQuery("#" + this.FrameID).remove();

		//		this.Iframe = $("#" + this.FrameID); // 获取框架，如果有的话，就不再创建了
		//		var IsEmpty = IsEmptyObject(this.Iframe)
		//		if (IsEmpty) {
		this.Iframe = jQuery('<iframe id="' + this.FrameID + '"></iframe>')
			.attr("src", this.LoginFormPage)
			.attr("width", "0")
			.attr("height", "0");
		this.ShowGuage();

		// 绑定完成事件 该函数不属于IDPJsLogin类，因此，
		// 不能直接在该函数内直接引用this指针，而必须new一个对象来用
		this.Iframe.load(function() {
			IDPObj = new IDPJsLogin();
			IDPObj.Load();
			try {
				IDPObj.Iframe = jQuery("#" + IDPObj.FrameID).contents();
			}
			catch (e) {// 获取失败，可能是因为没有权限。因为跨域
				return;
			}

			var MsgString;

			var PageIdentity = IDPObj.Iframe.find("#PageIdentity").attr("value");
			switch (PageIdentity) {
				case "IDPLoginFormPage": // 填写登陆必须数据
					IDPObj.Iframe.find("#backurl").attr("value", IDPObj.BackUrl);
					IDPObj.Iframe.find("#spid").attr("value", IDPObj.SPID);
					IDPObj.Iframe.find("#sid").attr("value", IDPObj.Token);
					IDPObj.Iframe.find("#type").attr("value", IDPObj.SignType);

					// 旧规范参数
					IDPObj.Iframe.find("#errorPage").attr("value", IDPObj.ErrorPage);
					IDPObj.Iframe.find("#password").attr("value", IDPObj.Password);
					IDPObj.Iframe.find("#mobileno").attr("value", IDPObj.MobileNo);
					IDPObj.Iframe.find("#valid").attr("value", IDPObj.ValidCode);
					
					// 新规范参数
					IDPObj.Iframe.find("#errorurl").attr("value", IDPObj.ErrorPage);
					IDPObj.Iframe.find("#servicePassword").attr("value", IDPObj.Password);
					IDPObj.Iframe.find("#mobileNum").attr("value", IDPObj.MobileNo);
					IDPObj.Iframe.find("#validCode").attr("value", IDPObj.ValidCode);
					
					IDPObj.Iframe.find("#form1")
						.attr("method", "post")
						.attr("action", IDPObj.IDPLoginPage);
						
					IDPObj.Iframe.find("#form1").submit();
					break;
				case "IDPErrorPage": // 登陆错误时调用该功能
					MsgString = IDPObj.Iframe.find("#IDPErrorMsg").text();

					if (IDPObj.ShowAlert == "true") {
						alert(MsgString);
						if (IDPObj.ErrorJumpUrl != "") window.parent.location.href = IDPObj.ErrorJumpUrl;
					} else {
						jQuery("#" + IDPObj.ShowErrorTextID).text(MsgString);
						jQuery("#" + IDPObj.ShowErrorTextID).show();
					}
					if (typeof (IDPJsLogin.prototype.OnErrorValidEvent) == "function") {
						IDPJsLogin.prototype.OnErrorValidEvent();
					}
					IDPObj.HiddenGuage();
					break;
				case "IDPAssertSuccessPage": 	// 成功登陆
					//IDPObj.HiddenGuage();
					IDPObj.ChooseGroup();
					break;
				case "IDPAssertSuccessBySinglePage": // 成功登陆
					MsgString = IDPObj.Iframe.find("#IDPAssertionErrorMsg").text();
					if (IDPObj.MobileLoginType == "QDL") {
						window.parent.location.href = MsgString;
					}
					else {
						window.location.href = MsgString;
					}

					break;
				case "IDPAssertADCNoOrderPage": // 未订购产品
					IDPObj.ShowDialog("ShowNoOrderProductDialog");
					break;
				case "IDPAssertADCErrorPage": // ADC处理过中出现错误
				case "IDPAssertIDPErrorPage":
					IDPObj.HiddenGuage();
					MsgString = IDPObj.Iframe.find("#IDPAssertionErrorMsg").text();

					if (IDPObj.ShowAlert == "true") {
						alert(MsgString);
						if (IDPObj.ErrorJumpUrl != "") window.parent.location.href = IDPObj.ErrorJumpUrl;
					} else {

						jQuery("#" + IDPObj.ShowErrorTextID).text(MsgString);
						jQuery("#" + IDPObj.ShowErrorTextID).show();
					}
					if (typeof (IDPJsLogin.prototype.OnErrorValidEvent) == "function") {
						IDPJsLogin.prototype.OnErrorValidEvent();
					}
					IDPObj.HiddenGuage();
					break;
				default:
					if (IDPObj.ShowAlert == "true") {
						alert("认证服务器错误！登陆失败");
						if (IDPObj.ErrorJumpUrl != "") window.parent.location.href = IDPObj.ErrorJumpUrl;
					} else {
						jQuery("#" + IDPObj.ShowErrorTextID).text("认证服务器错误！登陆失败");
						jQuery("#" + IDPObj.ShowErrorTextID).show();
					}
					
					IDPObj.HiddenGuage();
					break;
			}
		});

		this.Iframe.appendTo("body");
		//		}
		//		else if (IDPObj.IsObject(this.Iframe)) {
		//			//this.Iframe.attr("src", this.LoginFormPage);
		//			var obj = this.Iframe.get(0);
		//			obj.contentWindow.location.reload();
		//		}
	}

	// 全屏遮蔽显示登陆进度条
	this.ShowGuage = function() {
	    
	   if (this.MobileLoginType == "QDL") 
	   {
			var topDocument = jQuery(window.parent.document.body);
			topDocument.find("#" + ProcessGuageID).remove();
			topDocument.append('<div id="' + ProcessGuageID + '" style="width: 180px; margin-left: -5px;overflow:hidden;"><div id="' + ProcessGuageID + '" style="margin-bottom: 20px; width: 100%;"><img src="/ADCECPortal/TokenManage/include/loadingAnimation.gif" style="width: 200px;height:13px;" border="0" /></div><div id="' + ProcessGuageID + '" style="text-align: center; width: 100%;">正在处理中，请稍候。。。</div></div>');

			window.parent.tb_show("进度显示", "#TB_inline?TB_iniframe=true&height=60&width=200&inlineId=" + ProcessGuageID + "&modal=true", false);
			window.parent.focus();
		}
		else
		{
			var topDocument = jQuery(window.document.body);
			topDocument.find("#" + ProcessGuageID).remove();
			topDocument.append('<div id="' + ProcessGuageID + '" style="width: 180px; margin-left: -5px;overflow:hidden;"><div id="' + ProcessGuageID + '" style="margin-bottom: 20px; width: 100%;"><img src="/ADCECPortal/TokenManage/include/loadingAnimation.gif" style="width: 200px;height:13px;" border="0" /></div><div id="' + ProcessGuageID + '" style="text-align: center; width: 100%;">正在处理中，请稍候。。。</div></div>');

			window.tb_show("进度显示", "#TB_inline?TB_iniframe=true&height=60&width=200&inlineId=" + ProcessGuageID + "&modal=true", false);
			window.focus();
		}
	}

	// 关闭登陆进度条的显示
	this.HiddenGuage = function() {

	if (this.MobileLoginType == "QDL") {
	   
			var topDocument = jQuery(window.parent.document.body);
			topDocument.find("#" + ProcessGuageID).remove();

			window.parent.tb_remove();
		}
	 else
	    {
			var topDocument = jQuery(window.document.body);
			topDocument.find("#" + ProcessGuageID).remove();

			window.tb_remove();
	    }
	}

	// 全屏遮蔽显示用户选择集团
	this.ChooseGroup = function() {
		if (this.MobileLoginType == "QDL") {
			var divObj = this.Iframe.find("#" + ChooseGroupDialogID);
			var topDocument = jQuery(window.parent.document.body);
			topDocument.find("#" + ChooseGroupDialogID).remove();

			topDocument.find("#" + ProcessGuageID).remove();
			topDocument.append(divObj.get(0).outerHTML);

			window.parent.tb_show("登陆集团选择", "#TB_inline?TB_iniframe=true&height=240&width=420&inlineId=" + ChooseGroupDialogID + "&modal=true", false);
			window.parent.focus();
		}
		else {
			var divObj = this.Iframe.find("#" + ChooseGroupDialogID);
			var topDocument = jQuery(window.document.body);
			topDocument.find("#" + ChooseGroupDialogID).remove();

			topDocument.find("#" + ProcessGuageID).remove();
			topDocument.append(divObj.get(0).outerHTML);

			window.tb_show("登陆集团选择", "#TB_inline?TB_iniframe=true&height=240&width=420&inlineId=" + ChooseGroupDialogID + "&modal=true", false);
			window.focus();
		
		}
	}

	// 全屏遮蔽显示信息
	this.ShowDialog = function(ShowDivID) {
		if (this.MobileLoginType == "QDL") {
			var divObj = this.Iframe.find("#" + ShowDivID);
			var topDocument = jQuery(window.parent.document.body);
			topDocument.find("#" + ShowDivID).remove();
			topDocument.find("#" + ProcessGuageID).remove();

			topDocument.append(divObj.get(0).outerHTML);

			window.parent.tb_show("", "#TB_inline?TB_iniframe=true&height=240&width=420&inlineId=" + ShowDivID + "&modal=true", false);
			window.parent.focus();
		}
		else {
			var divObj = this.Iframe.find("#" + ShowDivID);
			var topDocument = jQuery(window.document.body);
			topDocument.find("#" + ShowDivID).remove();
			topDocument.find("#" + ProcessGuageID).remove();

			topDocument.append(divObj.get(0).outerHTML);

			window.tb_show("", "#TB_inline?TB_iniframe=true&height=240&width=420&inlineId=" + ShowDivID + "&modal=true", false);
			window.focus();
		}
	}

	function IsEmptyObject(obj) {
		return (obj == null || typeof (obj) === "undefined" || typeof (obj.length) === "undefined" || obj.length <= 0)
	}
}

IDPJsLogin.prototype.IsObject = function(obj) {
	return !(obj == null || typeof (obj) === "undefined" || typeof (obj.length) === "undefined" || obj.length <= 0)
}

// 全屏遮蔽显示登陆进度条
IDPJsLogin.prototype.LockScreen = function(unlockEvent) {
	var ProcessGuageID = "8aaec0ae28c8613e0128c8647ec60001GuageID";
	var topDocument = jQuery(window.parent.document.body);
	topDocument.find("#" + ProcessGuageID).remove();
	topDocument.append('<div id="' + ProcessGuageID + '" style="width: 180px; margin-left: -5px;"><div id="' + ProcessGuageID + '" style="margin-bottom: 20px; width: 100%;"><img src="/ADCECPortal/TokenManage/include/loadingAnimation.gif" border="0" /></div><div id="' + ProcessGuageID + '" style="text-align: center; width: 100%;">正在处理中，请稍候。。。</div></div>');

	window.parent.tb_show("进度显示", "#TB_inline?TB_iniframe=true&height=60&width=220&inlineId=" + ProcessGuageID + "&modal=true", false);
	window.parent.focus();
	// 当ID为unlockEvent的元素改变时，解锁
	//jQuery("#" + unlockEvent).change(IDPJsLogin.prototype.UnLockScreen);
	IDPJsLogin.prototype.IsEmpty(unlockEvent);
}

// 关闭登陆进度条的显示
IDPJsLogin.prototype.UnLockScreen = function() {
	var ProcessGuageID = "8aaec0ae28c8613e0128c8647ec60001GuageID";
	var topDocument = jQuery(window.parent.document.body);
	topDocument.find("#" + ProcessGuageID).remove();

	window.parent.tb_remove();
}

// 监控某个值是否不为空
IDPJsLogin.prototype.IsEmpty = function(unlockEvent) {
	if (jQuery("#" + unlockEvent).text() != "")
		IDPJsLogin.prototype.UnLockScreen();
	else
		setTimeout("IDPJsLogin.prototype.IsEmpty('" + unlockEvent + "')", 2000);

}

//	// 私有方法
//	function showErr() {
//		alert("访问超时");
//	}
//}
////添加公共方法   
//IDPJsLogin.prototype.pubMethod = function() {
//	return "pubMethod()";
//}
//JsObject = new JsClass("priMember", "pubMember");
// 错误时验证码刷新
IDPJsLogin.prototype.OnErrorValidEvent = null;
