/*
* 2008
* Copyright GProxy Team, Steve McFred and Jeremy Gillick (author of SwitchProxy)
*/

/*
*
* 	ERROR CODES
*	100 - 149: gproxy_setProxy()
*	150 - 199: gproxy_removeProxy()
*	200 - 249: gproxy_clearCookies()
*	250 - 299: gproxy_editProxyDialog()
*	300	- 349: gproxy_manualUpgradCheck()
*
*/

var gSwitchP_List;
var gSwitchP_ListPopup;
var gSwitchP_ContextList;
var gSwitchP_StatusBar;
var gSwitchP_MenuList;
var gSwitchP_ManageList;
var gSwitchP_ElementList;
var gSwitchP_ElementButton;
var gSwitchP_LastItem;
var gSwitchP_ProxyCount		= 0;
var gSwitchP_InManager		= false;
var gSwitchP_Loaded			= false;
var gSwitchP_Cycle			= null;
var gSwitchP_CommandEnd		= true;
var gSwitchP_Prefs			= null;
var gSwitchP_NoneLabel		= "None";

var gSwitchPTabMgr	= null;

function gproxy_initProxy(event){
	
	window.removeEventListener("load", gproxy_initProxy, true);

	if(!gSwitchP_Loaded){

		gSwitchP_List			= document.getElementById('proxy-list')
		gSwitchP_ListPopup		= document.getElementById('proxy-list-popup');
		gSwitchP_ManageList		= document.getElementById('manage-proxy-list');
		gSwitchP_ContextList	= document.getElementById('context-proxy-list');
		gSwitchP_StatusBar		= document.getElementById('gproxy-status');
		gSwitchP_MenuList		= document.getElementById('gproxy-menu-list');
		gSwitchP_ElementList	= document.getElementById('gproxy-element-list');
		gSwitchP_ElementButton	= document.getElementById('gproxy-element-button');
		gSwitchP_Prefs			= Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
		
		gSwitchP_NoneLabel		= gproxy_getString("common.proxy.none");
		
		//Is in Proxy Manager
		if(gSwitchP_ManageList != null){
			gSwitchP_InManager = true;
			gSwitchP_List = gSwitchP_ManageList;
		}
		
		gproxy_showMenus(true);
						
		//Cleanup RDF File
		gproxy_cleanupRdf();
		
		//Populate Proxy List
		gproxy_populateList();
		
		//Set Last Proxy Selected
		if(gSwitchP_List != null)
			gSwitchP_LastItem = gSwitchP_List.selectedItem;
		
		//Add Preferences Listener
		var oProxyObserver = {
			observe : function(subject, topic, data){ gproxy_populateList(); }
		};
		var oMenuObserver = {
			observe : function(subject, topic, data){ gproxy_showMenus(false); }
		};
		//var oPrefBranch	= Components.classes["@mozilla.org/preferences-service;1"].createInstance(Components.interfaces.nsIPrefBranchInternal);
			////oPrefBranch.addObserver("network.proxy", oPrefObserver, false);
			//oPrefBranch.addObserver("gproxy.proxy.rdf.lastupdate", oProxyObserver, false);
		 //oPrefBranch.addObserver("gproxy.display", oMenuObserver, false);

  //if(navigator.userAgent.search(/Thunderbird/gi) > -1){
  //	oPrefBranch	= Components.classes["@mozilla.org/preferences-service;1"].createInstance(Components.interfaces.nsIPrefBranchInternal);
	//}
  //Else{		
oPrefBranch = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);		
//}
oPrefBranch.addObserver("gproxy.proxy.rdf.lastupdate", oProxyObserver, false);	
//oPrefBranch.addObserver("gproxy.display", oMenuObserver, false);	
//oPrefBranch.addObserver("network.proxy", oPrefObserver, false);

		//Auto-Update Anonymous Proxy Lists (after 10 minutes)
		//gAnonUpdateTimout = setTimeout("gproxy_anon_autoUpdateLists()", 600000);
		
		// Start Anon Rotation
		//setTimeout("gproxy_anon_nextProxy()", 1000);
		
		gSwitchP_Loaded = true;
	}
}

// Return Tab Manager
function gproxy_getTabManager(){
	return gSwitchPTabMgr;
}

//Used for debugging removed


//Displays the context menu based on what 
// the user selects in the options
function gproxy_showMenus(onStartup){
	try{	
		//Show/Hide menus
		var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
					
		// Status Bar
		var isHidden = (oPrefs.prefHasUserValue("gproxy.display.statusbar") && !oPrefs.getBoolPref("gproxy.display.statusbar"))
		document.getElementById("gproxy-status").setAttribute("collapsed", isHidden);
		
		// If not thunderbird
		if(navigator.userAgent.search(/Thunderbird/gi) < 0){
		
			//Context
			var isHidden = (oPrefs.prefHasUserValue("gproxy.display.context") && !oPrefs.getBoolPref("gproxy.display.context"))
			document.getElementById("gproxy-context-menu").setAttribute("collapsed", isHidden);
			document.getElementById("gproxy-context-separator").setAttribute("hidden", isHidden);
		
			//Toolbar
			if(onStartup){
				oPrefs.setBoolPref("gproxy.display.toolbar", (!document.getElementById("proxy-toolbar").getAttribute("collapsed")));
			}
			else{
				var isHidden = (oPrefs.prefHasUserValue("gproxy.display.toolbar") && !oPrefs.getBoolPref("gproxy.display.toolbar"))
				document.getElementById("proxy-toolbar").setAttribute("collapsed", isHidden);
			}
		}
				
	}catch(err){}
}

// Show Element List
function gproxy_showElementList(event){
 
	var oContext	= document.getElementById('context-proxy-list');
	if(oContext == null){
		return;
	}
	
}

//Returns is the entered menu is currently showing
//	values are 'context' or 'toolbar'
function gproxy_menuShowing(sMenu){
	var oObj = null;
	
	if(sMenu == "context"){
		oObj = document.getElementById("gproxy-context-menu");
	}
	else if(sMenu == "toolbar"){
		oObj = document.getElementById("proxy-toolbar");
	}
	
	if(oObj == null)
		return false;
	
	return !eval(oObj.getAttribute("collapsed"));
}


//Clears current Proxy List
function gproxy_clearList(){
	
	gSwitchP_ProxyCount = 0;
	
	//Toolbar List
	if(!gSwitchP_InManager && gSwitchP_List != null && gSwitchP_List.removeAllItems && !gSwitchP_List.open){
		gSwitchP_List.removeAllItems();
	}
	//Manage Proxy Box
	if(gSwitchP_InManager && gSwitchP_List != null){
		aNodes = gSwitchP_List.childNodes;
		for(var i = aNodes.length-1; i >= 0; i--){
			gSwitchP_List.removeChild(aNodes[i]);
		}
	}
	//Context List
	if(gSwitchP_ContextList != null){
		aNodes = gSwitchP_ContextList.childNodes;
		for(var i = aNodes.length-1; i >= 0; i--){
			gSwitchP_ContextList.removeChild(aNodes[i]);
		}
	}
	//Toolbar Element List
	if(gSwitchP_ElementList != null){
		aNodes = gSwitchP_ElementList.childNodes;
		for(var i = aNodes.length-1; i >= 0; i--){
			if(aNodes[i].getAttribute("class") == "proxy-menu-item")
				gSwitchP_ElementList.removeChild(aNodes[i]);
		}
	}
	// Tools Menu List
	if(gSwitchP_MenuList != null){
		aNodes = gSwitchP_MenuList.childNodes;
		for(var i = aNodes.length-1; i >= 0; i--){
			if(aNodes[i].getAttribute("class") == "proxy-menu-item")
				gSwitchP_MenuList.removeChild(aNodes[i]);
		}
	}
}

//Add Item to lists
function gproxy_appendToList(sLabel, sValue, isSelected, iType){
	var oItem = null;
	
	gSwitchP_ProxyCount++;

	try{
	
		//Toolbar List
		if(!gSwitchP_InManager && gSwitchP_List != null && !gSwitchP_List.open){
			oItem = gSwitchP_List.appendItem(sLabel, sValue);
			oItem.setAttribute("oncommand", "gproxy_selectProxy();");	//gproxy_anon_stopRotation(); 
			oItem.setAttribute("proxyType", iType);	
		}
		
		//Manage Proxy Box
		if(gSwitchP_InManager && gSwitchP_List != null && sLabel != gSwitchP_NoneLabel){
			oItem = gSwitchP_List.appendItem(sLabel, sValue);
			oItem.setAttribute("proxyType", iType);	
		}
		
		//Context List
		if(gSwitchP_ContextList != null){
			oItem = document.createElement("menuitem");
			oItem.setAttribute("label", sLabel);
			oItem.setAttribute("value", sValue);
			oItem.setAttribute("type", "checkbox");
			oItem.setAttribute("autocheck", "false");
			oItem.setAttribute("proxyType", iType);	
			oItem.setAttribute("oncommand", "gproxy_queueSetProxy(this);");
			gSwitchP_ContextList.appendChild(oItem);
		}
		
		//Toolbar Element List
		if(gSwitchP_ElementList != null){
			//Get separator, which is after proxy list
			var oSeparator = document.getElementById("gproxy-element-list-separator");
			oItem = document.createElement("menuitem");
			oItem.setAttribute("id", sValue);
			oItem.setAttribute("label", sLabel);
			oItem.setAttribute("value", sValue);
			oItem.setAttribute("type", "checkbox");
			oItem.setAttribute("autocheck", "false");
			oItem.setAttribute("proxyType", iType);	
			oItem.setAttribute("class", "proxy-menu-item");
			oItem.setAttribute("oncommand", "gproxy_queueSetProxy(this);");
			gSwitchP_ElementList.insertBefore(oItem, oSeparator);
		}
		
		// Tools Menu List
		if(gSwitchP_MenuList != null){
			//Get separator, which is after proxy list
			var oSeparator = document.getElementById("gproxy-menu-list-separator");
			oItem = document.createElement("menuitem");
			oItem.setAttribute("id", sValue);
			oItem.setAttribute("label", sLabel);
			oItem.setAttribute("value", sValue);
			oItem.setAttribute("type", "checkbox");
			oItem.setAttribute("autocheck", "false");
			oItem.setAttribute("proxyType", iType);	
			oItem.setAttribute("class", "proxy-menu-item");
			oItem.setAttribute("oncommand", "gproxy_queueSetProxy(this);");
			gSwitchP_MenuList.insertBefore(oItem, oSeparator);
		}
	
		//Select
		if(isSelected)
			gproxy_selectItem(sLabel);
		
	}catch(err){
		if(isSelected) {
			gproxy_selectItem(gSwitchP_NoneLabel);
		}
		
		alert(gproxy_getString("error.unknown") +"\n("+ err +")");
	}
}

//Sets List item with labe sLabel, to be selected/checked
function gproxy_selectItem(sLabel){

	try{
	
		//Toolbar List
		if(!gSwitchP_InManager && gSwitchP_List != null){
			oItem = gSwitchP_List.getElementsByAttribute("label", sLabel);
			if(oItem != null){
				oItem = oItem[0];
				gSwitchP_List.selectedItem = oItem;
				oItem.setAttribute("selected", true);
			}
		}
		
		//Context
		if(gSwitchP_ContextList != null){
			oItem = gSwitchP_ContextList.getElementsByAttribute("label", sLabel);
			if(oItem != null){
				//Uncheck Other
				var aChecked = gSwitchP_ContextList.getElementsByAttribute("checked", "true");
				for(var c = 0; c < aChecked.length; c++){
					aChecked[c].setAttribute("checked", false);
				}
			
				//Select
				oItem = oItem[0];
				gSwitchP_ContextList.selectedItem = oItem;
				oItem.setAttribute("checked", true);
			}
		}
		
		//Toolbar Element List
		if(gSwitchP_ElementList != null){
			oItem = gSwitchP_ElementList.getElementsByAttribute("label", sLabel);
			if(oItem != null && oItem.length > 0){
				
				//Uncheck Other
				var aChecked = gSwitchP_ElementList.getElementsByAttribute("checked", "true");
				for(var c = 0; c < aChecked.length; c++){
					aChecked[c].setAttribute("checked", false);
				}
				
				//Check Selected
				oItem = oItem[0];
				gSwitchP_ElementList.selectedItem = oItem;
				oItem.setAttribute("selected", true);
				oItem.setAttribute("checked", true);
				
				//Change Button Label
				gSwitchP_ElementButton = document.getElementById('gproxy-element-button');	
				if(gSwitchP_ElementButton != null){
					gSwitchP_ElementButton.setAttribute("label", sLabel);
				}
			}
		}
		
		// Tools Menu List
		if(gSwitchP_MenuList != null){
			oItem = gSwitchP_MenuList.getElementsByAttribute("label", sLabel);
			if(oItem != null && oItem.length > 0){
			
				//Uncheck Other
				var aChecked = gSwitchP_MenuList.getElementsByAttribute("checked", "true");
				for(var c = 0; c < aChecked.length; c++){
					aChecked[c].setAttribute("checked", false);
				}
				
				//Check Selected
				oItem = oItem[0];
				gSwitchP_MenuList.selectedItem = oItem;
				oItem.setAttribute("selected", true);
				oItem.setAttribute("checked", true);
			}
		}
		
		// Status Bar
		if(gSwitchP_StatusBar != null){
			gSwitchP_StatusBar.setAttribute("label", gproxy_getString("common.label.proxy")+" "+sLabel);
		}
		gproxy_clearTimeout(gSwitchP_Cycle);
		
	}catch(err){
	//	gproxy_debug(err);
	}
}

//Return menuitem for the given sUri
function gproxy_getMenuItem(sUri){
	var oItem = null;
	
	//Toolbar
	oItem = (gSwitchP_List != null) ? gSwitchP_List.getElementsByAttribute("value", sUri) : null;
	if(oItem != null && oItem[0] != null)
		return oItem[0];
	
	//Context
	oItem = (gSwitchP_List != null) ? gSwitchP_ContextList.getElementsByAttribute("value", sUri) : null;
	if(oItem != null && oItem[0] != null)
		return oItem[0];
	
	//Toolbar Element
	oItem = (gSwitchP_List != null) ? gSwitchP_ElementList.getElementsByAttribute("value", sUri) : null;
	if(oItem != null && oItem[0] != null)
		return oItem[0];
	
	
	//Default
	return null;
}

function gproxy_cycle(sAction){
	if(gSwitchP_Cycle != null){
	
		gproxy_clearTimeout(gSwitchP_Cycle);
		gSwitchP_CommandEnd = true;

		if(sAction == 'change' && gSwitchP_LastItem != null && gSwitchP_List != null && gSwitchP_List.selectedItem.label != gSwitchP_LastItem.label && gSwitchP_List.selectedIndex > 0){
			gSwitchP_List.label					= gSwitchP_LastItem.label;
			gSwitchP_List.selectedItem			= gSwitchP_LastItem;
			gSwitchP_List.selectedItem.value	 = gSwitchP_LastItem.value;
			
			gproxy_setProxy();
		}
		else if(sAction == 'select' && gSwitchP_List != null && gSwitchP_List.selectedIndex > -1){
			gproxy_setStatus(gproxy_getString("toolbar.notApplied"), "#F00");
			setTimeout("gproxy_setStatus(gproxy_getString('toolbar.loading'))", 3000);
			setTimeout("gproxy_populateList('"+ gSwitchP_LastItem.label +"');", 4000);
			setTimeout("gproxy_setProxy();", 4500);
		}
		
	}
}

//When user selects a Proxy from the menu list
function gproxy_selectProxy(){

	gproxy_clearTimeout(gSwitchP_Cycle);
	if(gSwitchP_List == null || gSwitchP_List.selectedItem == null || gSwitchP_InManager)
		return;
		
	//if(gSwitchP_List.selectedItem.label != gSwitchP_NoneLabel){
		//document.getElementById('edit-button').disabled = false;
		//document.getElementById('remove-button').disabled = false;
		//document.getElementById('edit-button').image = "chrome://gproxy/content/icons/pencil.png";
		//document.getElementById('remove-button').image = "chrome://gproxy/content/icons/cross.png";
	
	//}
	//else{
		//document.getElementById('edit-button').disabled = true;
		//document.getElementById('remove-button').disabled = true;
		//document.getElementById('edit-button').image = "chrome://gproxy/content/icons/pencil_disabled.png";
		//document.getElementById('remove-button').image = "chrome://gproxy/content/icons/cross_disabled.png";
	//}
	
	if(typeof(gSwitchP_List.selectedItem.value) != 'undefined'){
		//If this item is not being used by the browser, 
		//	show a helpful message and cycle
		var oPrefs	= Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
		if((!oPrefs.prefHasUserValue("gproxy.proxy.current") || oPrefs.getCharPref("gproxy.proxy.current") != gSwitchP_List.selectedItem.value) && gSwitchP_LastItem != null && gSwitchP_LastItem != gSwitchP_List.selectedItem){
			gproxy_setStatus(gproxy_getString("toolbar.clickApply"), "#060");
		
			//Cycle if user doesn't click 'Apply'
			//if(gSwitchP_Cycle == null)
			//	gSwitchP_Cycle = setTimeout("gproxy_cycle('select')", 10000);
		}
		//If it is being used by the browser
		else if(oPrefs.prefHasUserValue("gproxy.proxy.current") && oPrefs.getCharPref("gproxy.proxy.current") == gSwitchP_List.selectedItem.value){
			gproxy_clearTimeout(gSwitchP_Cycle);
			
			//Anonomous
			if(gSwitchP_List.selectedItem.getAttribute("proxyType") == "3"){
			//	if(gproxy_anon_getCurrent() == "unknown")
			//		gproxy_setProxy(false);
			//		
			//	gproxy_setStatus(gproxy_getString("toolbar.using") +" "+ gSwitchP_List.selectedItem.label +" ["+ gproxy_anon_getCurrent() +"] ("+ gproxy_anon_getCurrentCount() +")");
			}
			//Standard
			else{
				gproxy_setStatus(gproxy_getString("toolbar.using") +" "+ gSwitchP_List.selectedItem.label);
			}
		}
		else{
			gproxy_clearTimeout(gSwitchP_Cycle);
			gproxy_setStatus(gproxy_getString("toolbar.using.none"));
		}
	}
	
	return true;
}

//Queue Set Proxy
function gproxy_queueSetProxy(oMenuItem){
	gproxy_clearTimeout(gSwitchP_Cycle);
	setTimeout("gproxy_setProxy(true, true, null, '"+ oMenuItem.label +"', '"+ oMenuItem.getAttribute("value") +"', '"+ oMenuItem.getAttribute("proxyType") +"')", 10);
}

//Set Proxy
function gproxy_setProxy(bClean, fromContextMenu, oMenuItem, sLabel, sUri, sType){

	//Start Command
	gSwitchP_CommandEnd = false;	
	gproxy_clearTimeout(gSwitchP_Cycle);
	//gproxy_anon_stopRotation();
	
	//If in Proxy Manager, do nothing
	if(gSwitchP_InManager){
		gSwitchP_CommandEnd = true;
		return;
	}
	
	var hasError	= false;
	
	//Set fromContextMenu
	if(fromContextMenu == null)
		fromContextMenu = false;
	
	//Get Proxy URI & Label
	var sProxyUri	= "";
	var sProxyLabel	= "";
	var sProxyType	= 0;
	if(sUri != null && sLabel != null){
		sProxyUri = sUri;
		sProxyLabel = sLabel;
		
		if(sType) sProxyType = sType;
	}
	else if(oMenuItem != null){
		sProxyUri	= oMenuItem.getAttribute("value");
		sProxyLabel	= oMenuItem.getAttribute("label");
		sProxyType	= oMenuItem.getAttribute("proxyType");
	}
	else{
		if(fromContextMenu == true){
			sProxyUri	= gSwitchP_ContextList.selectedItem.value;
			sProxyLabel	= gSwitchP_ContextList.selectedItem.label;
			sProxyType	= gSwitchP_ContextList.selectedItem.getAttribute("proxyType");
		}
		else if(gSwitchP_List != null){
			sProxyUri	= gSwitchP_List.selectedItem.value;
			sProxyLabel	= gSwitchP_List.selectedItem.label;
			sProxyType	= gSwitchP_List.selectedItem.getAttribute("proxyType");
		}
	}
	
	//Branch if Anonomous
	if(sProxyType == "3"){
		//gproxy_setStatus(gproxy_getString("toolbar.loading"));
		//gproxy_anon_loadProxy(sProxyUri, true);
	}
	else if(sProxyUri != ""){
		gproxy_setStatus(gproxy_getString("toolbar.loading"));
		try{
			//Always clear proxy settings first - this may mess up with selection...
			//gSwitchP_Prefs.setIntPref("network.proxy.type", 0);

			//Change Browser Preferences
			var aProps	= gproxy_ds_getPropertyValuesFor(sProxyUri);
			
			for(key in ggproxy_options){
				
				//Get pref type and update preference
				var sPrefVal = "";
				if(typeof(ggproxy_options_defaults[key]) == "number"){
					gSwitchP_Prefs.setIntPref(ggproxy_options[key], parseInt(aProps[gSProxyRdfNodeUriRoot +"#"+ key]));
				}
				else if(typeof(ggproxy_options_defaults[key]) == "boolean"){
					gSwitchP_Prefs.setBoolPref(ggproxy_options[key], eval(aProps[gSProxyRdfNodeUriRoot +"#"+ key]));
				}
				else{ //String
					gSwitchP_Prefs.setCharPref(ggproxy_options[key], aProps[gSProxyRdfNodeUriRoot +"#"+ key]);
				}
			}
			
			//Get Proxy Label
			sProxyLabel = aProps[gSProxyRdfNodeName];
			
			//Update Status
			gproxy_setStatus(gproxy_getString("toolbar.using") +" "+ sProxyLabel);
			
			//Update Preference
			gSwitchP_Prefs.setCharPref("gproxy.proxy.current", sProxyUri);	
		}
		catch(err){
			alert("100: "+ gproxy_getString("error.unknown")+ "\n("+ err +")");
			hasError = true;
			
			//Cycle back to 'None'
			//if(gSwitchP_Cycle == null && !fromContextMenu)
			//	gSwitchP_Cycle = setTimeout("gproxy_cycle('change')", 500);
		}
	}
	else{
		//Turn off proxy pref
		try{
			gSwitchP_Prefs.setIntPref("network.proxy.type", 0);
			
			//Update Status
			gproxy_setStatus(gproxy_getString("toolbar.using.none"));
			
			
			//Update Preference
			gSwitchP_Prefs.setCharPref("gproxy.proxy.current", "");
			
		}catch(e){
			alert("101: "+ gproxy_getString("error.unknown"));
			hasError = true;
		}
	}
	
	// Finalize
	if(hasError){
		//Update Status
		gproxy_setStatus(gproxy_getString("error.proxy.load") +" " + sProxyLabel, "#F00");
	}
	else{
	
		//gproxy_tab_notify(sProxyUri);
	
		//Select this proxy in all lists
		gproxy_selectItem(sProxyLabel);
		
		//Set Last Proxy
		gSwitchP_LastItem = gproxy_getMenuItem(sProxyUri);
		
		//Options
		try{
			if(bClean){
				// Clear Cookies
				if(typeof(gBrowser) != 'undefined' && gSwitchP_Prefs.prefHasUserValue("gproxy.clear.cookies") && gSwitchP_Prefs.getBoolPref("gproxy.clear.cookies")){
					gproxy_clearCookies();
				}
				
				// Reload Page
				if((typeof(gBrowser) != 'undefined' && (!gSwitchP_Prefs.prefHasUserValue("gproxy.reload.tab") || gSwitchP_Prefs.getBoolPref("gproxy.reload.tab")))){
					try{
						var oTab = gBrowser.mCurrentBrowser;
							oTab.webNavigation.reload(nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY | nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE);
					} catch(err) {	}
				}
				
				// Set Update Pref
				gSwitchP_Prefs.setIntPref("gproxy.proxy.rdf.lastupdate", (new Date()).getTime());
			}
			
		}catch(err){alert("102: "+ gproxy_getString("error.unknown") + "\n("+err+")");}
	}

	//End Command
	gSwitchP_CommandEnd = true;
	
}

//Test if the RDF Element, oRef, is the current proxy being used
function gproxy_isSelected(oRef){
	
	try{
		var oPrefs		 = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
		var aProps		= gproxy_ds_getPropertyValuesFor(oRef.Value);
		var sCurrUri	= "";
		var iType		= parseInt(aProps[gSProxyRdfNodeUriRoot + "#networkProxyType"]);
		
		if(oPrefs.prefHasUserValue("gproxy.proxy.current") && (sCurrUri = oPrefs.getCharPref("gproxy.proxy.current")) == oRef.Value){
					
			//Verify this is really what's being used in the browser
			if(iType == oPrefs.getIntPref("network.proxy.type")){
				if(iType == 1){
					for(key in ggproxy_options){
						
						//Skip These Keys
						if(key == "networkProxyType" || key == "networkProxyAutoconfigURL" || key == "networkProxyHTTPShare" )
							continue;
						
						//Skip if RDF has Default Values
						if( (key == "networkProxySOCKSVersion" && (aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "5" || aProps[gSProxyRdfNodeUriRoot +"#"+ key] == ""))
							|| (key == "networkProxyNone" && aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "localhost, 127.0.0.1")){
							continue;								
						}
						
						//If Pref and RDF doesn't have a value for this
						if(!oPrefs.prefHasUserValue(ggproxy_options[key]) 
							&& (aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "" || aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "0")){
							continue;
						}
						
						// If pref doesn't have this and the RDF record does
						//	return false
						if(!oPrefs.prefHasUserValue(ggproxy_options[key]) 
							&& (aProps[gSProxyRdfNodeUriRoot +"#"+ key] != "" && aProps[gSProxyRdfNodeUriRoot +"#"+ key] != "0")){
							return false;
						}
						
						
						//Get pref and convert to string
						var sPrefVal = "";
						if(oPrefs.getPrefType(ggproxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_INT)
							sPrefVal = oPrefs.getIntPref(ggproxy_options[key]) + "";
						if(oPrefs.getPrefType(ggproxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_BOOL)
							sPrefVal = oPrefs.getBoolPref(ggproxy_options[key]) + "";
						if(oPrefs.getPrefType(ggproxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_STRING)
							sPrefVal = oPrefs.getCharPref(ggproxy_options[key]);
												
						//Return false if isn't equal
						if(sPrefVal != aProps[gSProxyRdfNodeUriRoot +"#"+ key]){
							return false;
						}
					}
					return true;
				}
				//If type is 2 and the PAC url is the same
				else if(iType == 2 && oPrefs.prefHasUserValue("gproxy.proxy.current") 
							&& oPrefs.getCharPref("network.proxy.autoconfig_url") == aProps[gSProxyRdfNodeUriRoot + "#networkProxyAutoconfigURL"]){
						return true;
				}	
			}
			//Anonomous
			else if(iType == 3 && oPrefs.prefHasUserValue("network.proxy.http") && oPrefs.prefHasUserValue("network.proxy.http_port")){
				//var sProxy = oPrefs.getCharPref("network.proxy.http") +":"+ oPrefs.getIntPref("network.proxy.http_port");
				
				//is sProxy in proxy list
				//oTestRes = gproxy_ds_getElementForValue(gSProxyRdfNodeUriRoot +"#proxy", sProxy);
				
				//if(oTestRes instanceof Components.interfaces.nsIRDFResource && oTestRes.Value == oRes.Value){
					//gproxy_anon_loadProxy(oRef.Value, true);
					//return true;
				//}
				
				return false;
			}
		}
		
	}catch(err){}
	
	
	return false;
}

function gproxy_removeProxy(){	
	gproxy_clearTimeout(gSwitchP_Cycle);
	var oItem	= gSwitchP_List.selectedItem;
	
	//Can't delete 'None'
	if((gSwitchP_List.selectedItem != null) && 
		((gSwitchP_List.selectedItem.label == gSwitchP_NoneLabel) || 
		 (gSwitchP_List.selectedItem.label == gproxy_getString("common.proxy.gpass")))){
		alert("150: "+ gproxy_getString("error.remove.forbidden"));
		return;
	}
	
	if(!confirm(gproxy_getString("confirm.remove")))
		return;
	
	try{
		//RDF
			//Remove
			gproxy_ds_removeElement(oItem.value);
			
			//Cleanup
			gproxy_cleanupRdf();
		
		//Update List
			gproxy_populateList();
			setTimeout("gproxy_setProxy();", 300);
	}
	catch(err){
		alert("151: "+ gproxy_getString("error.remove") + "\n("+ err +")");
	}
}

function gproxy_clearCookies(){
	try{
		var oCookies	= Components.classes["@mozilla.org/cookiemanager;1"].createInstance(Components.interfaces.nsICookieManager);
			oCookies.removeAll();
	}catch(err){
		alert("200: "+ gproxy_getString("error.cookie"));
	}
	return true;
}

function gproxy_add_proxy(sName, sPort, issocks, toset){
	try{ 

        var oTestProxy = gproxy_ds_getElementForValue(gSProxyRdfNodeName, sName);
        if(oTestProxy != null){
          return false;
        }
        
        var sProxyUri   = gproxy_getUniqueProxyUri();
        var oProxy    = gproxy_ds_getResource(sProxyUri);
        gproxy_ds_addElement(sProxyUri);
        
        if (issocks) { //socks
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyType"), "1", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTP"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTP_Port"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTPShare"), false, true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySSL"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySSL_Port"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyFTP"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyFTP_Port"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyGopher"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyGopher_Port"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKS"), "127.0.0.1", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKS_Port"), sPort, true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKSVersion"), "5", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyNone"), "localhost, 127.0.0.1", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyAutoconfigURL"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#name"), sName, true);
        }
		else { //regular http for all
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyType"), "1", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTP"), "127.0.0.1", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTP_Port"), sPort, true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTPShare"), true, true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySSL"), "127.0.0.1", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySSL_Port"), sPort, true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyFTP"), "127.0.0.1", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyFTP_Port"), sPort, true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyGopher"), "127.0.0.1", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyGopher_Port"), sPort, true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKS"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKS_Port"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKSVersion"), "5", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyNone"), "localhost, 127.0.0.1", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyAutoconfigURL"), "", true);
          gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#name"), sName, true);
        }
		if (toset) {
          gproxy_setProxy(true, null, null, sName, sProxyUri, 1);
  		  gproxy_selectItem(sName);
		}
        return true;
    }catch(e){
      alert(gproxy_getString("error.unknown") +"\n("+ e +")");
      return false;
    }
}

function gproxy_populateList(sSelectedLabel){
	gproxy_clearTimeout(gSwitchP_Cycle);
	
	var sProxyUri	= "";
	var iSelected	= (gSwitchP_List != null) ? gSwitchP_List.selectedIndex : -1;
	var aProxies	= new Array();
		
	//Don't populate if a local command has not finished or if list is open
	if(!gSwitchP_CommandEnd){
		gSwitchP_CommandEnd = true; //reset
		return;
	}
	else{ //Start Command
		gSwitchP_CommandEnd = false;
	}
	
	//Load Elements
	try{
		
		//Remove List Items & Add 'None'
		gproxy_clearList();
		gproxy_appendToList(gSwitchP_NoneLabel, "", true, 0);
		gproxy_selectItem(gSwitchP_NoneLabel);
		
		//Add GPass default proxies if not existed yet, and set proxy to it
        gproxy_add_proxy(gproxy_getString("common.proxy.gpass"), "8000", true, false);
		//Add other known services too
        gproxy_add_proxy(gproxy_getString("common.proxy.gtunnel"), "8081", true, false);
        gproxy_add_proxy(gproxy_getString("common.proxy.freegateF3"), "8580", false, false);
        //gproxy_add_proxy(gproxy_getString("common.proxy.freegate"), "8567", false, false); //Old version
        gproxy_add_proxy(gproxy_getString("common.proxy.wujie"), "9666", false, false);

		//Get and Sort Elements
		var aProxies	= gproxy_ds_getAllElements();
			aProxies.sort(gproxy_sortProxies);
		
		//Add to list
		for(i = 0; i < aProxies.length; i++){
		
			if(typeof(aProxies[i]) == 'undefined')
				continue; 
			
			try{
			
				oRes		= gproxy_ds_getResource(aProxies[i]);
				sProxyName	= gproxy_ds_getValueFor(oRes, gproxy_ds_getResource(gSProxyRdfNodeName));
				sProxyType	= gproxy_ds_getValueFor(oRes, gproxy_ds_getResource(gSProxyRdfNodeUriRoot+ "#networkProxyType"));
				
				if(sProxyName != null && sProxyName != gproxy_getString("common.proxy.freegate")){
					oItem = gproxy_appendToList(sProxyName, aProxies[i], false, sProxyType);

					//Select Item
					if((sSelectedLabel != null && sSelectedLabel == sProxyName) || (sSelectedLabel == null && gproxy_isSelected(oRes))){
						gproxy_selectItem(sProxyName);
					}
				}
				
			} catch(err) {}
		}
		
		//Finish
		gSwitchP_CommandEnd = true;
		setTimeout("gproxy_selectProxy();", 300);
		
	}catch(err){}
}

/*	
* Sort Proxy List
*	+ oResA and oResB are RDF URIs
*/
function gproxy_sortProxies(oResA, oResB){
	try{
		//Get Proxy Names
		sValA = gproxy_ds_getValueFor(gproxy_ds_getResource(oResA), gproxy_ds_getResource(gSProxyRdfNodeName));
		sValB = gproxy_ds_getValueFor(gproxy_ds_getResource(oResB), gproxy_ds_getResource(gSProxyRdfNodeName));
		
		
		if (sValA < sValB)
			return -1;
		if (sValA == sValB)
			return 0;
		if (sValA > sValB)
			return 1;
	}
	catch(e){ return 0; }
}

/* Cleanup unused gproxy data in RDF
*	 + Remove resources that have not been applied or used
*/
function gproxy_cleanupRdf(){
		
	try{
		swithproxy_upgrade_rdf();
		
		var aElements	= gproxy_ds_getAllElements();
		for(var e = 0; e < aElements.length; e++){
			oRes	 = gproxy_ds_getResource(aElements[e]);
			aProps	= gproxy_ds_getPropertyValuesFor(aElements[e]);
			
			//If does not have a name property, 
			//	then it is not listed -- so it's junk
			if(aProps[gSProxyRdfNodeName] == null){
				gproxy_ds_removeElement(aElements[e]);
			}
		}
	}catch(err){}
}

// Automatic Upgrade
/*
*	From Version 0.4
*	 + Add preference 'gproxy.version'
*	+ Add preference 'gproxy.proxy.current'
*	+ Change proxy's URI to random number
*	+ Convert #proxy to networkProxyAutoconfigURL
*	+ Add elements for full manual proxy configuration
*		+ view the globals.js ggproxy_options array for full list
*/


//Generates random number for proxy URI
function gproxy_getUniqueProxyUri(){
	var sUri = gSProxyRdfRoot + "/gproxy_" + Math.round((Math.random() * 200000));
	
	if(gproxy_ds_doesProxyElementExist(sUri))
		return gproxy_getUniqueProxyUri();
	
	return sUri;
}

function gproxy_setStatus(sMsg, sColor){
	if(!sColor)
		sColor = "#000";
	
	oStatus = document.getElementById('status-text');
	
	if(oStatus != null){
		document.getElementById('status-text').style.color = sColor;
		document.getElementById('status-text').value = sMsg;
	}
}

//Clear Timeout
function gproxy_clearTimeout(){
	clearTimeout(gSwitchP_Cycle);
	gSwitchP_Cycle = null;
}

//Execute a command when system is ready
//	iCurrCount is for internal use, do not
//	pass this argument
function gproxy_doCommand(sCommand, iLimitCount){
	
	if(iLimitCount == null)
		iLimitCount = 0;
	
	//Wait for current command to end
	if(!gSwitchP_CommandEnd && iLimitCount < 10){
		iLimitCount++
		setTimeout("gproxy_doCommand('"+ sCommand +"', "+ iCurrCount +")", 100);
	}
	else{
		gSwitchP_CommandEnd = true;
		setTimeout(sCommand, 100);
	}
}

/*
* Launch Dialogs
*/
	
	//Edit Proxy Dialog
	function gproxy_editProxyDialog(isNew){
		gproxy_clearTimeout(gSwitchP_Cycle);
		
		var sAction = null;
		
		//Nothing Selected
		var oEditItem = null
		if(!isNew && (oEditItem = gSwitchP_List.selectedItem) == null){
			alert("250: "+ gproxy_getString("error.edit.select"));
			return;
		}
		
		//Can't edit 'None'
		if(!gSwitchP_InManager && !isNew && gSwitchP_List != null && gSwitchP_List.selectedItem != null && gSwitchP_List.selectedItem.label == gSwitchP_NoneLabel){
			alert("251: "+ gproxy_getString("error.edit.forbidden"));
			return;
		}
		
		//Edit
		if(!isNew){
			
			//Anon
			//if(oEditItem.getAttribute("proxyType") == "3"){
			//	window.openDialog("chrome://gproxy/content/dialogs/editanon.xul","editproxy","centerscreen, chrome", "edit", oEditItem.value);
			//}
			//Standard
			//else{
			window.openDialog("chrome://gproxy/content/dialogs/editproxy.xul","editproxy","centerscreen, chrome", "edit", oEditItem.value);
			//}
		}
		//Add
		else{
			//window.openDialog("chrome://gproxy/content/dialogs/addproxytype.xul","editproxy","centerscreen, chrome", "add");
			 window.openDialog("chrome://gproxy/content/dialogs/editproxy.xul","addproxy","centerscreen, chrome", "add", gSwitchP_InManager);
		}
	}
	
	//Manage Proxy Dialog
	function gproxy_openProxyManager(){
		window.openDialog("chrome://gproxy/content/dialogs/manager.xul","manageproxies","centerscreen, chrome, resizable");
	}

/*
* Options
*/
	function gproxy_opengproxyPrefs(){		
		window.openDialog("chrome://gproxy/content/options/options.xul","gproxyPrefs","centerscreen, chrome");
	}
	
	function gproxy_goTogproxySite(fromDialog){
		
		// If Thunderbird
		if(navigator.userAgent.search(/Thunderbird/gi) > -1){
			var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance();         
				messenger = messenger.QueryInterface(Components.interfaces.nsIMessenger);
				messenger.launchExternalURL(ggproxy_DownloadSite +"?r=gproxy");  
		}
		else{
			opener.gBrowser.selectedTab = opener.getBrowser().addTab(ggproxy_DownloadSite +"?r=gproxy");
		}
		self.close();
	}
	
	function gproxy_goTogproxyAbout(){
		window.openDialog("chrome://gproxy/content/dialogs/about.xul","gproxyAbout","centerscreen, chrome, modal");
	}

/*
* Automatic Updator
*/
	
	//If there is a new version, returns the XPI url, else returns false

	//The user selects to check for updates

/*
* UNINSTALL
*/
	function gproxy_uninstall(oLogger){
		try{
			var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
				oPrefs.deleteBranch("gproxy");
		
			oLogger.addLog(new window.ExtuninstallLogItem(oLogger.REMOVE_ACTION, oLogger.SUCCESS_STATUS, "Preferences Branch 'gproxy'", null, null));
		}catch(err){
			oLogger.addLog(new window.ExtuninstallLogItem(oLogger.REMOVE_ACTION, oLogger.WARN_STATUS, "Preferences Branch 'gproxy'", 1001, err));
			throw 1001;
		}
	}

