var vvVisitPrefCount = 5;
var vvPub2Click = 0;

function vvSaveUrl(url)//保存来自软件的需要刷新的url
{
	var vv_url_1	=vvGetStrPref('vvisit.url1', '');
	var vv_url_2	=vvGetStrPref('vvisit.url2', '');
	var vv_url_3	=vvGetStrPref('vvisit.url3', '');
	var vv_url_4	=vvGetStrPref('vvisit.url4', '');
	var vv_url_5	=vvGetStrPref('vvisit.url5', '');
	var vv_url_6	=vvGetStrPref('vvisit.url6', '');
	var vv_url_7	=vvGetStrPref('vvisit.url7', '');
	var vv_url_8	=vvGetStrPref('vvisit.url8', '');
	var vv_url_9	=vvGetStrPref('vvisit.url9', '');
	var vv_url_10	=vvGetStrPref('vvisit.url10', '');
	var vv_url_11	=vvGetStrPref('vvisit.url11', '');
	var vv_url_12	=vvGetStrPref('vvisit.url12', '');
	var vv_url_13	=vvGetStrPref('vvisit.url13', '');
	var vv_url_14	=vvGetStrPref('vvisit.url14', '');
	var vv_url_15	=vvGetStrPref('vvisit.url15', '');
	vvSetStrPref('vvisit.url1',url);
	if(vv_url_1.length>0)	{	vvSetStrPref('vvisit.url2',vv_url_1);	}
	if(vv_url_2.length>0)	{	vvSetStrPref('vvisit.url3',vv_url_2);	}
	if(vv_url_3.length>0)	{	vvSetStrPref('vvisit.url4',vv_url_3);	}
	if(vv_url_4.length>0)	{	vvSetStrPref('vvisit.url5',vv_url_4);	}
	if(vv_url_5.length>0)	{	vvSetStrPref('vvisit.url6',vv_url_5);	}
	if(vv_url_6.length>0)	{	vvSetStrPref('vvisit.url7',vv_url_6);	}
	if(vv_url_7.length>0)	{	vvSetStrPref('vvisit.url8',vv_url_7);	}
	if(vv_url_8.length>0)	{	vvSetStrPref('vvisit.url9',vv_url_8);	}
	if(vv_url_9.length>0)	{	vvSetStrPref('vvisit.url10',vv_url_9);	}
	if(vv_url_10.length>0)	{	vvSetStrPref('vvisit.url11',vv_url_10);	}
	if(vv_url_11.length>0)	{	vvSetStrPref('vvisit.url12',vv_url_11);	}
	if(vv_url_12.length>0)	{	vvSetStrPref('vvisit.url13',vv_url_12);	}
	if(vv_url_13.length>0)	{	vvSetStrPref('vvisit.url14',vv_url_13);	}
	if(vv_url_14.length>0)	{	vvSetStrPref('vvisit.url15',vv_url_14);	}
};

function vvGetIntPref(prefName, defval)
{
   var result = defval;
   var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
   var prefs = prefservice.getBranch("");
   if (prefs.getPrefType(prefName) == prefs.PREF_INT) {
       try { result = prefs.getIntPref(prefName); }catch(e){}
   }
   return(result);
};

function vvSetIntPref(prefName, value)
{
   var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
   var prefs = prefservice.getBranch("");
   try { prefs.setIntPref(prefName, value); } catch(e){}
};

function vvGetBoolPref(prefName, defval)
{
   var result = defval;
   var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
   try { result = prefservice.getBoolPref(prefName); }
      catch(e){ prefservice.setBoolPref(prefName, defval); }
      return(result); 
};

function vvSetBoolPref(prefName, value)
{
   var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
   var prefs = prefservice.getBranch("");
   try	{	   prefs.setBoolPref(prefName, value);   } catch(e){}
};

function vvGetStrPref(prefName, defval)
{
	var result = defval;
	var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
	var prefs = prefservice.getBranch("");
	if (prefs.getPrefType(prefName) == prefs.PREF_STRING)
	{
		try { result = prefs.getComplexValue(prefName, Components.interfaces.nsISupportsString).data; }catch(e){}
	}
	return(result);
};

function vvSetStrPref(prefName, value)
{
	var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
	var prefs = prefservice.getBranch("");
	var sString = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
	sString.data = value;
	try
	{
		prefs.setComplexValue(prefName, Components.interfaces.nsISupportsString, sString);
	}
	catch(e){}
};

function vvWriteFile(FileName,FileBody)
{
	var file = Components.classes["@mozilla.org/file/directory_service;1"]
						.getService(Components.interfaces.nsIProperties)
						.get("AChrom", Components.interfaces.nsIFile);
	file.append(FileName);

	var strm = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
	strm.QueryInterface(Components.interfaces.nsIOutputStream);
	strm.QueryInterface(Components.interfaces.nsISeekableStream); 

	strm.init( file, 0x04 | 0x08 | 0x10, 420, 0 );
	strm.write(FileBody,FileBody.length );
	strm.flush();
	strm.close();
};

function vvReadFile(FileName)	//输入文件名，返回文件内容
{
	var file =Components.classes["@mozilla.org/file/directory_service;1"]
						.getService(Components.interfaces.nsIProperties)
						.get("AChrom", Components.interfaces.nsIFile);
	file.append(FileName);

	var is =  Components.classes["@mozilla.org/network/file-input-stream;1"]
						.createInstance(Components.interfaces.nsIFileInputStream);
    is.init(file, 0x04 | 0x08, 420 , null);
	
	var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
						.createInstance(Components.interfaces.nsIScriptableInputStream);
	sis.init(is);

	var output =sis.read(sis.available()); 
	sis.close();
	return output;
};

function vvStrInversion(str)	//反转字符串
{
	str = str.split("").reverse().join("");	//翻转字符串
	return str;
};

function vvGetDomain(url)	//输入url，返回域名
{
	if( url.length==0 )
		return "";
	var a1=url.indexOf('://');
	if(a1==4)
		url=url.substring(a1+3,url.length);
	else
		return "";
	var a2=url.indexOf('/');
	var a3=url.indexOf(':');
	if		( a3>0 )
		url=url.substring(0,a3);
	else if	( a2>0 )
		url=url.substring(0,a2);
	else
		return "";
		
	//详细检查
	var ret = '';
	var inversionUrl = vvStrInversion(url);	//翻转字符串
	var postPart = inversionUrl.split('.');
	var DNPF = Array('.com','.net','.cn','.mobi','.org','.gov.cn','.info','.com.cn','.net.cn','.org.cn','.name','.biz','.cc','.tv','.网络',
			'.公司','.asia','.hk','.cd','.ph','.com.ph','.net.ph','.org.ph','.us','.tw','.org.tw','.idv.tw','.com.tw','.中国','.ws','.tk');
	var dnpf_finger = -1;
	for( var i=0; i<DNPF.length; i++ )
	{
		var ceel_str = vvStrInversion( DNPF[i] );
		//alert(ceel_str+"                     "+postPart[0]+'.')
		if		( ceel_str==postPart[0]+'.' )
		{
			if( dnpf_finger<0 || (dnpf_finger>=0 && DNPF[i].length>DNPF[dnpf_finger].length))
			{
				dnpf_finger = i;
				ret = postPart[0]+'.'+postPart[1];
			}
		}
		else if ( ceel_str==postPart[0]+'.'+postPart[1]+'.' )
		{
			if( dnpf_finger<0 || (dnpf_finger>=0 && DNPF[i].length>DNPF[dnpf_finger].length))
			{
				dnpf_finger = i;
				ret = postPart[0]+'.'+postPart[1]+'.'+postPart[2];
			}
		}
	}
	ret = vvStrInversion( ret );
	if( dnpf_finger<0)
	{
		ret = url;
	}

	return ret;
};


function vvEnablePvOpen( domain )	//允许以产生PV的目的打开（打开主URL的时候，产生打开子链接的必要）
{
	for( var i=1; i<=vvVisitPrefCount; i++ )
	{
		var vvPrefDomain=vvGetStrPref("vvisit.url"+i, "");
		//alert(vvClk);
		var vvPrefSplit=vvPrefDomain.split("\t");
		//	(0)域名		(1)打开次数	(2)包月		(3)PV		(4)蜘蛛		(5)点显比	(6)2次点显比
		if( vvPrefSplit[0]==domain )
		{
			return vvPrefSplit[3]-vvPrefSplit[1];
		}
	}
	return -1;
};

function vvCheckDomainReg( domain )	//测试域名是否注册，返回： 0未注册，1有注册，2VIP包月
{
	if( domain.length==0 )
		return 0;
	for( var i=1; i<=vvVisitPrefCount; i++ )
	{
		var vvPrefDomain=vvGetStrPref("vvisit.url"+i, "");
		//alert(vvClk);
		var vvPrefSplit=vvPrefDomain.split("\t");
		//	(0)域名		(1)打开次数	(2)包月		(3)PV		(4)蜘蛛		(5)点显比	(6)2次点显比
		if( vvPrefSplit[0]==domain)
		{
			if( vvPrefSplit[2]==1 )
				return 2;
			else
				return 1;
		}
	}
	return 0;
};

function vvDomainAddSelf( domain )	//给域名的访问次数加一
{
	if( domain.length==0 )
		return;
	for( var i=1; i<=vvVisitPrefCount; i++)
	{
		var vvPrefUrlName = "vvisit.url"+i;
		var vvPrefDomain  = vvGetStrPref( vvPrefUrlName, "");
		var vvPrefSplit   = vvPrefDomain.split("\t");
		//	(0)域名		(1)打开次数	(2)包月		(3)PV		(4)蜘蛛		(5)点显比	(6)2次点显比
		if( vvPrefSplit[0]==domain)
		{
			//alert(vvPrefDomain);
			vvPrefSplit[1]=Number(vvPrefSplit[1])+1;
			//alert(vvPrefSplit[1]);
			vvSetStrPref( vvPrefUrlName, vvPrefSplit[0]+"\t"+vvPrefSplit[1]+"\t"+vvPrefSplit[2]+"\t"+vvPrefSplit[3]+"\t"+vvPrefSplit[4]+"\t"+vvPrefSplit[5]+"\t"+vvPrefSplit[6]);
		}
	}
};

function vvRand(num)	//随机数函数，返回0 - num-1
{
	var rd=Math.floor(Math.random()*(num+1));
	if(rd<=1)
		rd=0;
	if(rd>=num)
		rd=num-1;
	return rd;
};

function vvRandStr(str)	//从字符串数组中返回任意一个
{
	return str[vvRand(str.length)];
};



