﻿ 
//var g_i = 0;
//window.addNamespace("LeadingMIS.Cookie");
LeadingMIS.Cookie = function(){};

LeadingMIS.Cookie.Services = function (objSent, CallBack)
{
// alert("LeadingMIS.Cookie.Services");
	if( objSent == null || typeof(objSent.name) == 'undefined' || typeof(objSent.type) == 'undefined') return false;
 	var oXmlDoc = new ActiveXObject("Msxml2.DOMDocument");
	oXmlDoc.async = false;
	var oRoot = oXmlDoc.createElement("info");
	oXmlDoc.appendChild(oRoot);
	for(var key in objSent){
		oRoot.setAttribute(key, objSent[key]);
	}
	var strURL = "/Common/Cookie/CookieXmlHttp.aspx";
	var xmlHttp;
	
	if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
	var async = typeof(CallBack) == 'function';
	
	xmlHttp.open('POST', strURL, async); 
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	var d = null;
 	if(async) xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			eval('d='+ xmlHttp.responseText + ';');
			CallBack(d);
		}
	}
 //alert(oRoot.xml);
 	xmlHttp.send(oRoot.xml);
  	if(!async)
	{
		eval('d='+ xmlHttp.responseText + ';');
		return d;
	}		
}

LeadingMIS.Cookie.severCookie = function (objSent, CallBack)
{
	if(typeof(objSent.expires) == 'undefined')
	{
		var objDate = new Date();
		objDate.setTime(30 * 24 * 60 * 60 * 1000);
		objSent.expires = objDate.getTime();
	}
	
	if(typeof(objSent.path) == 'undefined')
	{
		objSent.path = '/';
	}
	
	if(typeof(objSent.value) == 'undefined')
	{
		objSent.value = '';
	}
	
	var strSent = "<root name='" + objSent.name 
	+ "' type='" + objSent.type 
	+ "' value='" + objSent.value 
	+ "' expires='" + objSent.expires 
	+ "' path='" + objSent.path
	+ "'>";
	
	if(objSent.Values && objSent.Values.length>0)
	{
		for(var i=0 ; i<objSent.Values.length; i++)
		{
			strSent += "<row name='" + objSent.Values[i].name + "' value='" + objSent.Values[i].value + "'/>";
		}
	}
	strSent += "</root>";
 	//top.document.title = (g_i++) + " _ "+ strSent;
	var strURL = "/Common/Cookie/CookieXmlHttp.aspx";
	var xmlHttp = new XMLHttpRequest();
	var async = typeof(CallBack) == 'function';
	xmlHttp.open('POST', strURL, async); 
  	if(async) xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
			CallBack(xmlHttp.responseText);
	}
	xmlHttp.send(strSent);
  	if(!async)
	{
		return xmlHttp.responseText;
	}	
}

//****************************************************
//名称:SetCookie(nameTemp,valueTemp)
//作者:刘晖
//类型:Public
//参数:输入		nameTemp		string		名字
//				valueTemp		string		值
//说明:设置cookie中的值
//****************************************************
LeadingMIS.Cookie.setCookie = function(nameTemp,valueTemp,eventWindow)
{
alert("in");
	eventWindow = (typeof(eventWindow) == "undefined")?window:eventWindow;
	if(LeadingMIS.UI.isWindow(eventWindow) == true)
	{
		var objDate = new Date();
		objDate.setTime(objDate.getTime() + 3 * 30 * 24 * 60 * 60 * 1000);
		eventWindow.document.cookie = nameTemp + "=" + escape(valueTemp) + ";path=/;expires=" + objDate.toGMTString();
	}
	else
	{
		;
	}
}
//****************************************************
//名称:GetCookie(nameTemp)
//作者:刘晖
//类型:Public
//参数:输入		nameTemp		string		名字
//说明:得到cookie中的值
//****************************************************
LeadingMIS.Cookie.getCookie = function(nameTemp,eventWindow)
{
	eventWindow = (typeof(eventWindow) == "undefined")?window:eventWindow;
	if(LeadingMIS.UI.isWindow(eventWindow) == true)
	{
		if(eventWindow.document.cookie.length != 0)
		{
			var arrCookie = eventWindow.document.cookie.split(";");
			for(var i = 0; i < arrCookie.length; i++)
			{
				var arrTemp = arrCookie[i].split("=");
				if(escape(nameTemp) == arrTemp[0].trim())
				{
					return (arrTemp[1]==null)?"":unescape(arrTemp[1].trim());
				}
			}
			return "";
		}
		else
		{
			return "";
		}
	}
	else
	{
		return "";
	}
}
//****************************************************
//名称:DelCookie(nameTemp)
//作者:刘晖
//类型:Public
//参数:输入		nameTemp		string		名字
//说明:删除cookie中的值
//****************************************************
LeadingMIS.Cookie.delCookie = function(nameTemp,eventWindow)
{
	eventWindow = (typeof(eventWindow) == "undefined")?window:eventWindow;
	if(LeadingMIS.UI.isWindow(eventWindow) == true)
	{
		if(this.getCookie(nameTemp,eventWindow).length != 0)
		{
			this.setCookie(nameTemp,"",eventWindow);
		}
	}
	else
	{
		;
	}
}