var CometBirdUtility =
{
	showErrorMsg : function( data )
	{
		//alert ( data );
	},
	
	trim : function( str )
	{
		var l = 0; 
		var r = str.length -1;
		while( l < str.length && str[l] == " " )
		{
			l++; 
		}
		
		while( r > l && str[r] == " " )
		{     
			r--;     
		}
		return str.substring( l, r+1 ); 
	},
	
	compareTowWord : function( word1, word2 )
	{
		if ( typeof word1 != "string" ||  typeof word2 != "string" )
			return;
			
		word1 = CometBirdUtility.trim( word1 );
		word2 = CometBirdUtility.trim( word2 );
		
		var sbc_word1 = CometBirdUtility.DBC2SBC( word1 );
		var sbc_word2 = CometBirdUtility.DBC2SBC( word2 );
		
		var w1 = sbc_word1.replace(/\s/gi,"");
		var w2 = sbc_word2.replace(/\s/gi,"");
		
		return ( w1 == w2 );
	},
	
	DBC2SBC : function( str )
	{
		var result = '';
		for( var i=0; i<str.length; i++ )
		{
			code = str.charCodeAt(i);//获取当前字符的unicode编码
			if ( code >= 65281 && code <= 65373 )//在这个unicode编码范围中的是所有的英文字母已经各种字符
			{
				result += String.fromCharCode(str.charCodeAt(i) - 65248);//把全角字符的unicode编码转换为对应半角字符的unicode码
			}
			else if (code == 12288)//空格
			{
				result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
			}
			else
			{
				result += str.charAt(i);
			}
		}
		return result;
	},
	
	openURL : function( url )
	{
		var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
		var mainWindow = wm.getMostRecentWindow("navigator:browser");
		var gBrowser = mainWindow.getBrowser();
		
		var currentTab = gBrowser.getBrowserAtIndex( gBrowser.mTabContainer.selectedIndex );
		if ( currentTab.currentURI.spec == "about:blank" )// current tab
		{		
			gBrowser.loadURI( url, null, null );	
			return;
		}
		
		var browserEnumerator = wm.getEnumerator( "navigator:browser" );
		var browserWin = browserEnumerator.getNext();
		var tabbrowser = browserWin.getBrowser();

		var index = tabbrowser.browsers.length - 1;
		var lastBrowser = tabbrowser.getBrowserAtIndex( index );
		if ( lastBrowser.currentURI.spec == "about:blank" )
		{
			tabbrowser.selectedTab = tabbrowser.mTabs[index];	
			gBrowser.loadURI( url, null, null );
		}
		else
		{	
			tabbrowser.selectedTab = gBrowser.addTab( url, null, null );
		}
	},
	
	setHomePage : function( arguments )
	{
		if ( arguments )// open some url directly, arguments is array
			return;
			
		var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
        var prefs = prefService.getBranch("");
		if ( prefs.getIntPref( "browser.startup.page" ) != 3 )
			return;

		var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);  
		var enumerator = wm.getEnumerator( "navigator:browser" ); 
		var winCount = 0;
		while( enumerator.hasMoreElements() ) 
		{  
			var win = enumerator.getNext();  
			winCount++;
		} 
		if ( winCount > 1 )// open more than tow main window
			return;
		
		var mainWindow = wm.getMostRecentWindow("navigator:browser");
		var gBrowser = mainWindow.getBrowser();
		if ( gBrowser.mTabContainer.childNodes.length > 1 )
			return;
			
		// get home page
		var homePage = gHomeButton.getHomePage();
		//var homePage = prefs.getCharPref( "browser.startup.homepage" );

		var currentTab = gBrowser.getBrowserAtIndex( gBrowser.mTabContainer.selectedIndex );
		if ( currentTab.currentURI.spec.length == 0 || currentTab.currentURI.spec == "about:blank" )// current tab
		{		
			loadURI( homePage, null, null, true );
		}
	},
	
	createGuid : function()
	{
		var guid = "";
		for ( var i = 1; i <= 32; i++ )
		{
			var n = Math.floor( Math.random()*16.0 ).toString(16);
			guid += n;
			if( (i==8) || (i==12) || (i==16) || (i==20) )
			guid += "-";
		}
		return guid;    
	}
};