xdbj = 0;

function checkclientinstall()
{
	obj = null;
	try{
		obj = new ActiveXObject("OLEBARSHELL.olebarshellCtrl.1");
		return true;
	}
 	catch(e)
  	{
		return false;
	}
}

//连接手机
function contentphone(aId,aType,re) {

  try
  {	
   	if(aId<=0 && aType<=0)
	{
		return;	
	}

	if(UA=="ie" || UA=="ie7" || UA=="ie8")
	{
		var obj = window.frames.toolspan.document;
		if(checkclientinstall())
		{
			obj.getElementById("clienthint1").innerHTML = "客户端使用说明";
			obj.getElementById("clienthint1").href = "http://www.crossmo.com/faq.php";
			obj.getElementById("clienthint2").innerHTML = "十字猫客户端使用说明";
			obj.getElementById("clienthint2").href = "http://www.crossmo.com/faq.php";
		}
	}
	
	window.frames.toolspan.document.getElementById("phonebtn").onclick = function () {reconnectphone(aId,aType,re)};
	window.frames.toolspan.document.getElementById("tmpdown").onclick = function () {cdown(aId,aType)};
	window.frames.toolspan.document.getElementById("chargedown").onclick = function () {chargedown(aId,aType)};
	window.frames.toolspan.document.getElementById("loginbtn").onclick = function () {userlogin('contentphone('+aId+','+aType+',true)')};
	if(!$.cookie('PhoneID') || !$.cookie('ConnectType'))
	{
		agent.call("","ajax_UserStatus","callback_userstatus",aId,aType);		
	}
	else
	{ 	
		var phoneid = parseInt($.cookie('PhoneID'));	  
//alert(parseInt($.cookie('PhoneID')));
	      save2phone(aId,aType,phoneid);
        
      	return;

	}

	this.callback_userstatus = function(userstatus) { //回调函数
	    var rect = fGetViewPortSize();
	    var screenHeight = rect[1];
	    var screenWidth = rect[0];
	    var w = $('#wrap').width();
	    var scrollLeft = rect[2];
	    var scrollTop = rect[3];
	    bgshow(rect);
	    var obj = window.frames.toolspan.document;
	    var objstyle = document.getElementById("toolspan");

	    if (userstatus == 0 || userstatus == 1) {
	        try {
	            var re = window.external.CB_ShowLoginDlg("");
	            if (re) {
	                $("#PageCover").hide();
	            }
	            else {
	                throw "对象不支持此属性或方法";
	            }
	            //$("#PageCover").hide();
	        }
	        catch (e) {
	            //obj.getElementById("loginpan").style.display = "block";
	            //obj.getElementById("phonesel").style.display = "none";
	            //obj.getElementById("clienttip").style.display = "none";
	            //obj.getElementById("uid").value = "";
	            //	obj.getElementById("pwd").value = "";	
	            //	obj.getElementById("reginfo").innerHTML="";

	            //obj.getElementById("youke").style.display = "block";
	            //	obj.getElementById("download").style.display = "none";

	            obj.getElementById("login").style.display = "block";

	            var panleft = (w < screenWidth) ? (screenWidth - obj.getElementById("login").offsetWidth) / 2 : (w - obj.getElementById("login").offsetWidth) / 2;
	            var pantop = (screenHeight - obj.getElementById("login").offsetHeight) / 2 + scrollTop;
	            objstyle.style.left = panleft + "px";
	            objstyle.style.top = pantop + "px";
	            objstyle.style.width = obj.getElementById("login").offsetWidth + "px";
	            objstyle.style.height = obj.getElementById("login").offsetHeight + "px";

	        }

	    }
	    else {
	       
	        obj.getElementById("phonesel").style.display = "block";
	        obj.getElementById("login").style.display = "none";
	        obj.getElementById("clienttip").style.display = "none";
	        var str = '<div class="hint_qr"></div>';
	        if (userstatus == 2) {
	            obj.getElementById("phonelist").innerHTML = str;
	            obj.phoneform.connectparam.checked = true;
	            obj.phoneform.manufacturer.disabled = false;
	            obj.phoneform.phone.disabled = false;
	        }
	        else { 
	            phonelist = userstatus;
	            if (phonelist) {
	                for (var i = 0; i < phonelist.length; i++) {
	                    str += '<div class="hint_qr01"><span class="qr0b font_blue12">';
	                    str += '<input name="connectparam" id="connectparam" type="radio" value="' + phonelist[i]["phoneid"] + '|' + phonelist[i]["connectparam"] + '" ' + (phonelist[i]['isdefault'] == 't' ? 'checked="checked"' : '') + ' onclick="pclick(this)"/>' + phonelist[i]["manufacturer_name"] + '</span>';
	                    str += '<span class="qr0c font_blue12">' + phonelist[i]["phonename"] + '</span>';
	                    str += '<span class="qr0d"><img src="../' + phonelist[i]["phoneimage"] + '" alt="" width="34" height="34" /></span></div>';
	                }
	            }
	            obj.getElementById("phonelist").innerHTML = str;
	            for (i = 0; i < obj.phoneform.connectparam.length; i++) {
	                if (obj.phoneform.connectparam[i].value == '0') {
	                    obj.phoneform.connectparam[i].checked = false;
	                    obj.phoneform.manufacturer.disabled = true;
	                    obj.phoneform.phone.disabled = true;
	                }
	            }

	        }


	        var panleft = (w < screenWidth) ? (screenWidth - obj.getElementById("phonesel").offsetWidth) / 2 : (w - obj.getElementById("phonesel").offsetWidth) / 2;
	        var pantop = (screenHeight - obj.getElementById("phonesel").offsetHeight) / 2 + scrollTop;
	        objstyle.style.left = panleft + "px";
	        objstyle.style.top = pantop + "px";
	        objstyle.style.width = obj.getElementById("phonesel").offsetWidth;
	        objstyle.style.height = obj.getElementById("phonesel").offsetHeight;

	    }
	};
  }
  catch(e)
  {
	setTimeout("contentphone("+aId+","+aType+","+re+")",1000);  
  }
}


function closeBuySpan()
{
	$("#buyspan").css("left","-9999px");
	$("#PageCover").hide();
}


//连接手机
function reconnectphone(aId,aType,re)
{
  try
  {
	obj = window.frames.toolspan.document;	

	ol = obj.phoneform.connectparam;
	var str="";
	if(ol.length == undefined){
		if(ol.checked)
		{
			str = ol.value;
		}
	}
	else
	{
		for(var i=0;i<ol.length;i++)
		{
			if(ol[i].checked)
			{
			   str = ol[i].value;
			   break;
			}
		}
	}
	
	
	if(str==""){
      //showtips(4,"请确认连接的机型");	   
      return;
	}
	
	if(str=="0")
	{
		if(obj.phoneform.manufacturer.options[0].selected)
		{
			alert("请选择手机厂商");
			return false;	
		}
		
		if(obj.phoneform.phone.options[0].selected)
		{
			alert("请选择手机型号");
			return false;
		}
		
		$.cookie('PhoneID', obj.phoneform.phone.value, { path: '/', domain: 'crossmo.com', secure: false});
		$.cookie('ConnectType', "Other", { path: '/', domain: 'crossmo.com', secure: false });
//		$.cookie('PhoneID', obj.phoneform.phone.value, { path: '/', secure: false });
//		$.cookie('ConnectType', "Other", { path: '/',  secure: false });
		//$.cookie('PhoneID', obj.formssch.phone.value, { path: '/' });

		//$.cookie('ConnectType', "Client", { path: '/'});			
		agent.call("","ajax_SetUserPhone","",obj.phoneform.phone.value);	
		closewindow();
		save2phone(aId,aType,obj.phoneform.phone.value);
		if(re) {
		
			url = window.location.href.replace(/phone=\d*/g,"phone="+obj.phoneform.phone.value);
			url = url.replace(/cate_exp_id=\d*/g,"cate_exp_id=0");
			url = url.replace(/#.*/g,"");
			window.location.href = url;
			//setTimeout("window.location.reload(true)",1000);	
			//window.external.CB_SetPhoneId(obj.phoneform.phone.value);
			
		}
		else
		{
			if(phonediv=document.getElementById("userphonearea"))
			{
				phonediv.innerHTML = $.cookie('PhoneName').replace("+", " ");
			}
			
		}		
		return;
		
	}
	else
	{
		var phonearr = str.split("|");
		
		$.cookie('PhoneID', phonearr[0], { path: '/', domain: 'crossmo.com', secure: false});
		$.cookie('ConnectType', phonearr[1], { path: '/', domain: 'crossmo.com', secure: false});
		//$.cookie('PhoneID', phonearr[0], { path: '/'});
		//$.cookie('ConnectType', phonearr[1], { path: '/'});	
		
		agent.call("","ajax_SetUserPhone","",phonearr[0]);		
			
		closewindow();
			
		//for download file or blog txt to phone
		save2phone(aId, aType, parseInt(phonearr[0]));
		if(re)
		{
			url = window.location.href.replace(/phone=\d*/g,"phone="+phonearr[0]);
			url = url.replace(/cate_exp_id=\d*/g,"cate_exp_id=0");
			url = url.replace(/#.*/g,"");
			window.location.href = url;
			//setTimeout("window.location.href=",1000);	
			//window.external.CB_SetPhoneId(phonearr[0]);

		}
		else
		{
			if(phonediv=document.getElementById("userphonearea"))
			{
				phonediv.innerHTML = $.cookie('PhoneName').replace("+", " ");;
			}
			
		}
		return;
	}
  }
  catch(e) {
  
  }
}


function save2phone(aId, aType, phoneid, re, wbuy){
		
//alert(phoneid);
	if(aId>0 && aType>0 && phoneid>0)
	{
		if(typeof(wbuy)!="number")
		{
			wbuy = 0;
		}
		//alert(xdbj)
		if(xdbj>0 && pose>poss)
		{
			agent.call("", "ajax_GetFileInfo", "callback_getfileinfo", aId, aType, phoneid, poss,pose);
		}
		else
		{ //alert(aId+" "+aType+" "+phoneid+ " " + re + " " + wbuy )
			agent.call("", "ajax_GetFileInfo2", "callback_getfileinfo", aId, aType, phoneid, 0, 0, wbuy);
		}
	}

	this.callback_getfileinfo = function(urlinfo)
	{ 
		//alert(urlinfo[1]);return;
		if(urlinfo[0])
		{
			try
			{
				
				if(UA=="ie" || UA=="ie7" || UA=="ie8")
				{
					var obj = window.frames.toolspan.document;
					if(!checkclientinstall())
					{
						//window.open("http://www.crossmo.com/olebartool/");
						var rect = fGetViewPortSize();
						var screenHeight = rect[1];
						var screenWidth = rect[0];
						var w = $('#wrap').width();	
						var scrollLeft = rect[2];
						var scrollTop = rect[3];
						bgshow(rect);
						var objstyle = document.getElementById("toolspan");
						obj.getElementById("loginpan").style.display = "none";
						obj.getElementById("phonesel").style.display = "none";
						obj.getElementById("clienttip").style.display = "block";
						var panleft = (w<screenWidth)?(screenWidth-obj.getElementById("clienttip").offsetWidth)/2 : (w-obj.getElementById("loginpan").offsetWidth)/2;
						var pantop = (screenHeight-obj.getElementById("clienttip").offsetHeight)/2+scrollTop;
						objstyle.style.left = panleft+"px";
						objstyle.style.top = pantop+"px";
						objstyle.style.width = obj.getElementById("clienttip").offsetWidth+"px";
						objstyle.style.height = obj.getElementById("clienttip").offsetHeight+"px";
						
						var urls = urlinfo[1].split("+");

						for(i=0; i<urls.length; i++ )
						{ 	
							
							
							urls[i] = "crossmo://"+urls[i]
						}
						
						date = new Date();
                		date.setTime(date.getTime() + (60 * 60 * 1000));
						//$.cookie('DownloadProtocal', urls, { expires: date, path: '/'});
						$.cookie('DownloadProtocal', urls, { expires: date, path: '/', domain: 'crossmo.com', secure: false});	
						return;
					}

				}
							
				//var url = "crossmo://"+urlinfo[1];//showtips(16,url);
				//document.frmdown.action=url;
				//document.frmdown.submit();
				
				var urls = urlinfo[1].split("+");
 				for(i=0; i<urls.length; i++ )
 				{ 	
 					delayTime = i*3000;					
 					setTimeout('document.frmdown.action="crossmo://"+"'+urls[i]+'";document.frmdown.submit();', delayTime);
 				}				

				if(re )
				{
					setTimeout("window.location.reload(true)",1000);
					//window.location.reload(true)
				}
			}
			catch(ee)
			{//alert(ee)
				//alert("未安装客户端！") 
			}		
		}
		else
		{//alert(urlinfo[2]);
			if(typeof(urlinfo[2])=="string")
			{
				if(urlinfo[2]=="buy")
				{
					//window.external.CB_ShowConfirmDialog(urlinfo[1],"确认",3)
					try{
						
						if(window.external.CB_ShowConfirmDialog(urlinfo[1],"确认",3))
						{
							this.showpay = function(){
								var rect = fGetViewPortSize();
								var screenHeight = rect[1];
								var h = $('#wrap').height();
								var screenWidth = rect[0];
								var w = $('#wrap').width();	
								var scrollLeft = rect[2];
								var scrollTop = rect[3];
								bgshow(rect);
								objstyle = document.getElementById("buyspan");
								var panleft = (w<screenWidth)?(screenWidth-objstyle.offsetWidth)/2 : (w-objstyle.offsetWidth)/2;
								var pantop = (screenHeight-objstyle.offsetHeight)/2+scrollTop;
								objstyle.style.left = panleft+"px";
								objstyle.style.top = pantop+"px";
								obj = window.frames.buyspan;
								//obj.location.href = "/space/tippay.php?id="+aId+"&type="+aType;
								obj.phoneid = phoneid;
							};	
							
							if(re )
							{
								window.frames.buyspan.location.href="http://www.crossmo.com/space/tippay.php?id="+aId+"&type="+aType;
							}
							setTimeout("this.showpay()",1000);
						}
					}
					catch(e)
					{
						window.location.href="http://www.crossmo.com/space/tippay.php?terminal=web&st=1&id="+aId+"&type="+aType;
					
					}
					//window.location.href="http://www.crossmo.com/space/tippay.php?terminal=web&st=1&id="+aId+"&type="+aType;	
//					try{
//						
//						//直接下载，先不支付
//						if(window.external.CB_ShowConfirmDialog(urlinfo[1],"确认",3))
//						{
///*							var urls = urlinfo[1].split("+");
//							for(i=0; i<urls.length; i++ )
//							{ 	
//								delayTime = i*3000;					
//								setTimeout('document.frmdown.action="crossmo://"+"'+urls[i]+'";document.frmdown.submit();', delayTime);
//							}*/
//							
//							window.frames.buyspan.location.href="http://www.crossmo.com/space/tippay.php?id="+aId+"&type="+aType;
//							//window.frames.buyspan.location.href="http://www.crossmo.com/space/tippay.php?id="+aId+"&type="+aType;
//							
//			
//						}
//
//					}
//					catch(e)
//					{}
//					
//					this.showpay = function(){
//						var rect = fGetViewPortSize();
//						var screenHeight = rect[1];
//						var h = $('#wrap').height();
//						var screenWidth = rect[0];
//						var w = $('#wrap').width();	
//						var scrollLeft = rect[2];
//						var scrollTop = rect[3];
//						bgshow(rect);
//						objstyle = document.getElementById("buyspan");
//						var panleft = (w<screenWidth)?(screenWidth-objstyle.offsetWidth)/2 : (w-objstyle.offsetWidth)/2;
//						var pantop = (screenHeight-objstyle.offsetHeight)/2+scrollTop;
//						objstyle.style.left = panleft+"px";
//						objstyle.style.top = pantop+"px";
//						obj = window.frames.buyspan;
//						//obj.location.href = "/space/tippay.php?id="+aId+"&type="+aType;
//						obj.phoneid = phoneid;
//					};	
//					
//					if(re )
//					{
//						window.frames.buyspan.location.href="http://www.crossmo.com/space/tippay.php?id="+aId+"&type="+aType;
//					}
//					setTimeout("this.showpay()",1000);

					
				}
				else if(urlinfo[2]=="reset")
				{
					alert(urlinfo[1]);
					//$.cookie('PhoneID', null);
      				//$.cookie('ConnectType', null);	
					$.cookie('PhoneID', null, { path: '/', domain: 'crossmo.com', secure: false});
      				$.cookie('ConnectType', null, { path: '/', domain: 'crossmo.com', secure: false});	
					//$.cookie('PhoneID', null, { path: '/', secure: false});
      				//$.cookie('ConnectType', null, { path: '/', secure: false});	
					//blank.location.target="_blank";
					contentphone(aId,aType);
					//window.location.href="/space/mymobile.php";
				}
				else
				{
					alert(urlinfo[1]);
				}
			}
			else
			{
				alert(urlinfo[1]);
				
			}
			
	   	}
  	}	;
}


function cpautobuy(aId, aType, phoneid)
{
	agent.call("", "ajax_cpbuy", "callback_cpbuy", aId, aType, phoneid);
	this.callback_cpbuy = function(ret)
	{
		//alert(ret)
		if(ret[0])
		{
			save2phone(aId, aType, phoneid);
		}
		else
		{
			save2phone(aId, aType, phoneid, false, 1);
		}
	}
}


function cdown(cid,cType)
{
	obj = window.frames.toolspan.document;
	
	if(obj.formssch.manufacturer.options[0].selected)
	{
		alert("请选择手机厂商");
		return false;	
	}
	
	if(obj.formssch.phone.options[0].selected)
	{
		alert("请选择手机型号");
		return false;
	}
	
	
	if(cid>0)
	{
		agent.call("","TmpUserReg","callback_TmpUserReg");
		
	}
	
	this.callback_TmpUserReg = function(userstatus){
		if(userstatus[0])	
		{
			$.cookie('PhoneID', obj.formssch.phone.value, { path: '/', domain: 'crossmo.com', secure: false});
      		$.cookie('ConnectType', "Client", { path: '/', domain: 'crossmo.com', secure: false});
			//$.cookie('PhoneID', obj.formssch.phone.value, { path: '/'});
      		//$.cookie('ConnectType', "Client", { path: '/'});			
			agent.call("","ajax_SetUserPhone","",obj.formssch.phone.value);	
			closewindow();
			save2phone(cid,cType,obj.formssch.phone.value,true);
			
		}
	}
	
}


function chargedown(id,type)
{
	if(id>0)
	{	
		agent.call("","getPackid","callback_getPackid",id,type);
	}
	
	this.callback_getPackid = function(ret)
	{
		if(ret)
		{//document.write(ret);
			window.location.href = "http://www.crossmo.com/guidedown.php?wmid=1&cid="+ret;
		}
	}

}

function confirmSpaceIsOk(url)
{
   var strFilesize = $.ajax({ url: "/getfilesize.php?url="+url, async: false }).responseText;
   var filesize = parseInt(strFilesize);
      
   if( filesize > 0 )
   {
      if( filesize > 1048576 ) // >1M 
      {   
         var siz = Math.round(filesize/1048576);
         var bOk = confirm("此文件需要占用您手机 "+siz+" MB大小的空间。\n\n按确定前请确认您的手机有足够空间！");
         showtips(11, "友情提示：文件较大("+siz+")请等待");
         return bOk;
      }
      else
      {
         return true;
      }      
   }
   else
   {
      return true;   
   }  
   
}
function SearchDown(aId,aType){
	var pnew=$.cookie("ChosePhoneID");
	var pold=$.cookie("PhoneID");

	if (pnew>0) {
	    if (pnew == pold) {
	        contentphone(aId, aType);
	    }
	    else {
	        agent.call("", "ajax_SetUserPhone", "setnew", pnew);
	        this.setnew = function() {

	            contentphone(aId, aType, 1);
	        }


	    }
	}
	else
	{
		contentphone(aId, aType);
	}
}


