var obj;
var winModalWindow;

function LaunchXiaoi(AppID)
{
	CheckMessenger();
	LaunchApp(AppID, 'ibot@live.cn');
}

function LaunchBot(AppID, BotID)
{
	CheckMessenger();
	LaunchApp(AppID, BotID);
}

function LaunchApp(AppID, BotID)
{
	if (obj != null)
	{
		obj.LaunchApp(AppID, BotID);
	}
}

function CheckMessenger()
{
	eval ('try {obj = new ActiveXObject("MSNMessenger.P4QuickLaunch"); } catch (e) {obj = null;}');
	//try {obj = new ActiveXObject("MSNMessenger.P4QuickLaunch"); } catch (e) {alert(0);obj = null;}
	var strErrorPage = "http://www.xiaoi.com/launch/msnhelp.html?i@xiaoi.com";
	var isError = "";
	if (!ajaxInited) l_initAjax();
	
	if (!document.all)
	{
		isError = "notSupported";
		//ShowWindow(strErrorPage, 600, 230);
		shownosupported();
	}	
	else if (obj == null)
	{
		isError = "notSupported";
		shownosupported();
		//ShowWindow(strErrorPage, 600, 230);
	}
	
	if(document.referrer != ''){
		l_setCookie('from',document.referrer,1,'xiaoi.com');
		l_setCookie('search','?id=LaunchApp',1,'xiaoi.com');
		l_requestAjax("http://www.xiaoi.com/util/logServlet","id",window.location, "url","ibot@live.cn","from" ,document.referrer,'search','?id=LaunchApp','error',isError);
	}
	else{
		l_setCookie('from',document.location,1,'xiaoi.com');
		l_setCookie('search','?id=LaunchApp',1,'xiaoi.com');
		l_requestAjax("http://www.xiaoi.com/util/logServlet","id",window.location, "url","ibot@live.cn","from" ,document.location,'search','?id=LaunchApp','error',isError);
	}
}

function shownosupported(){
	var myDocument=top.document;		
	launcherroriframe_div = myDocument.getElementById("launcherroriframe_div");
	launcherroriframe = myDocument.getElementById("launcherroriframe");
	launcherroriframe.src = "http://www.xiaoi.com/launch/msnhelp.html?i@xiaoi.com";
	launcherroriframe.style.display = "block";
	var h1=myDocument.body.clientHeight;
	var h2=myDocument.documentElement.clientHeight;
	var _top=0;
	var _stop=0;
	if(navigator.appName=='Netscape'){
		var isXhtml=(h2<=h1&&h2!=0)?true:false;
		_top=isXhtml?h2/2:h1/2;
		_stop=isXhtml?myDocument.documentElement.scrollTop:myDocument.body.scrollTop;
	}else if(navigator.appName=='Opera'){
		_top=h1/2;
		_stop=myDocument.documentElement.scrollTop;
	}
	var top1=_top-launcherroriframe_div.clientHeight/2+_stop;
	var left1=myDocument.body.clientWidth/2-launcherroriframe_div.clientWidth/2;
	launcherroriframe_div.style.top=top1-60+"px";
	launcherroriframe_div.style.left=left1-150+"px";
	launcherroriframe_div.style.display='block';
}
 
function IgnoreEvents(e)
{
  return false;
}
 
//Display error message if the MSN Messenger client 6.2 is not installed or the browser is not Internet Explorer
function ShowWindow(strError, width, height)
{
  if (window.showModalDialog)
  {
   
    window.showModalDialog(strError,null,
    "dialogWidth="+width+"px;dialogHeight="+height+"px;help=no;dialogLeft=160");
  }
  else
  {

    var ah = screen.availHeight;

    var y = (ah - height) / 2;

    window.top.captureEvents (Event.CLICK|Event.FOCUS);
    window.top.onfocus=HandleFocus ;
    winModalWindow = window.open (strError,"ModalChild",
       "dependent=yes,width="+width+",height="+height+",top="+y+",left=160,screenX=160,screenY="+y);
    winModalWindow.focus();
  }
}

 
function HandleFocus()
{
  if (winModalWindow)
  {
    if (!winModalWindow.closed)
    {
      winModalWindow.focus();
    }
    else
    {
      window.top.releaseEvents (Event.CLICK|Event.FOCUS);
    }
  }
  return false;
}



//********************************************************************************


function l_requestAjax(url){
		if (url == null || url.length==0 || url.indexOf("http://") < 0)  return;
		
		
		if ("?" != url.substring(url.length-1,"?")) url = url + "?";
		
		var l_msnimajaxform = document.getElementById("l_msnimajaxform");
		l_msnimajaxform.action = url;
		l_msnimajaxform.method = "post";
		var innerHTMLBuffer = "";
		for(var i=1;i<arguments.length;i=i+2){
			innerHTMLBuffer += "<input type=hidden name='"+arguments[i]+"' value='"+arguments[i+1]+"'>";
		}
		l_msnimajaxform.innerHTML = innerHTMLBuffer;
		l_msnimajaxform.submit();//no callback :P
}
	ajaxInited = false;
function l_initAjax(){
		ajaxInited = true;
		//init iframe-based ajax
		var div = document.createElement("DIV");
		div.innerHTML = '<form id="l_msnimajaxform" name="l_msnimajaxform" target="l_msnimajaxiframe" method="post" action="">' +
				'<input type="hidden" name="info" value="">' +
				'</form>';
		div.style.display = "none";
		document.body.appendChild(div);
		
		div = document.createElement("DIV");
		div.innerHTML = '<iframe id="l_msnimajaxiframe" src="blank.htm" name="l_msnimajaxiframe" style="display:none" onload="l_msnimajaxiframe_onload()"></iframe>';
		div.style.display = "none";
		document.body.appendChild(div);
		
		
		//notsupported
		var div_e=top.document.createElement("DIV");
		div_e.id="launcherroriframe_div";
		div_e.style.position="absolute";
		div_e.style.display="none";
		div_e.innerHTML='<iframe id="launcherroriframe" name="launcherroriframe" src="" border="0" height="160" width="330" frameBorder=0 scrolling="no"></iframe>';
		top.document.body.appendChild(div_e);
	}

function l_msnimajaxiframe_onload(){
	var l_msnimajaxiframe = document.getElementById("l_msnimajaxiframe");
	try{
		if (l_msnimajaxiframe.contentWindow.location.href.indexOf("blank.htm") == -1) throw "avoid refreshing";
	}catch(e){
		//history.back();
	}
}


//***************************************************************
function l_setCookie(name,value,days,domain) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/; domain="+domain;
}

function l_getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function l_deleteCookie(name) {
	setCookie(name,"",-1);
}

function l_msnimajaxiframe_onload(){
	var l_msnimajaxiframe=document.getElementById("l_msnimajaxiframe");
	try{
		if(l_msnimajaxiframe.contentWindow.location.href.indexOf("blank.htm")==-1)throw"avoid refreshing";
		
	}catch(e){
		
	}
}