
var gIsEdit   = false;
var gList;
var gProxyURI;
var gProxy;
var gProxyConfig;
var gProxyNameField;
var gFromProxyManager;

function gproxy_initVals(){
  
  
  //Set Globals
  gProxyNameField  = document.getElementById("proxy-name");
  
  //Edit
  if(window.arguments.length > 1 && window.arguments[0] == "edit" && !gproxy_isEmpty(window.arguments[1])){
    
    //Proxy URI
    gProxyURI = window.arguments[1];    
    
    gIsEdit  = true;
    try{
      gProxy = gproxy_ds_getResource(gProxyURI);
    
      //Load Fields
      var gProxyConfig = gproxy_ds_getPropertyValuesFor(gProxyURI);
      for(key in ggproxy_options){
        var oField  = document.getElementById(key);
        var sValue  = gProxyConfig[gSProxyRdfNodeUriRoot + "#" + key];
        
        oField.value = sValue;
      }
      
      //Label
      gProxyNameField.value = gProxyConfig[gSProxyRdfNodeName];
      
      //Set Socks Radio
      oSocks = document.getElementById("networkProxySOCKSVersion");
      if(oSocks.value == "4")
        oSocks.selectedItem = document.getElementById("networkProxySOCKSVersion4");
      else
        oSocks.selectedItem = document.getElementById("networkProxySOCKSVersion5");
        
       //Share one proxy
       oShare = document.getElementById("networkProxyHTTPShare");
       if (oShare.value == "true")
         //oShare.checked = true;
          document.getElementById("networkProxyHTTPShare").checked = oShare.value;

        else  
         oShare.checked = false;

      //Sharing box
     //if (document.getElementById("networkProxyHTTPShare").value == "true");
       


    }catch(err){ alert(gproxy_getString("error.unknown")); self.close(); }
  }
  
  //Enable Fields
  
  gproxy_enableShareSettingOnStart();
  gproxy_enableOptions();
  
  // Thunderbird
  if(navigator.userAgent.search(/Thunderbird/gi) > -1){
  	document.getElementById("ftp_row").style.display = "none";
  	document.getElementById("gopher_row").style.display = "none";
  	document.getElementById("none_row").style.display = "none";
  	document.getElementById("none_example_row").style.display = "none";	
  }
}

function gproxy_saveProxy(){
  
  try{
    
    /*
    * Validation
    */
      //Empty
      if(gProxyNameField.value == ""){
        alert(gproxy_getString("error.add.empty"));
        
        gProxyNameField.focus();
        gProxyNameField.setSelectionRange(0, gProxyNameField.textLength);
        
        return false;
      }
      //Special Chars
      else if(!gproxy_allowedChars(gProxyNameField.value)){
        alert(gproxy_getString("error.add.invalid"));
        return false;
      }
      //Can't be named 'None' or 'GPass'
      else if(gproxy_simplify(gProxyNameField.value) == gproxy_simplify(gproxy_getString("common.proxy.none"))){
        alert(gproxy_getString("error.add.duplicate"));
        return false;
      }
      else if((!gIsEdit) && (gproxy_simplify(gProxyNameField.value) == gproxy_simplify("gpass"))){
        alert(gproxy_getString("error.add.duplicate"));
        return false;
      }
      else if((!gIsEdit) && (gproxy_simplify(gProxyNameField.value) == gproxy_simplify(gproxy_getString("common.proxy.gpass")))){
        alert(gproxy_getString("error.add.duplicate"));
        return false;
      }
        
      //Is This a Duplicate Label?
      var oTestProxy = gproxy_ds_getElementForValue(gSProxyRdfNodeName, gProxyNameField.value);
      if(oTestProxy != null && (!gIsEdit  || oTestProxy.Value != gProxy.Value)){
        alert(gproxy_getString("error.add.duplicate"));
        return false;
      }
    
         //save share setting
      document.getElementById("networkProxyHTTPShare").value = document.getElementById("networkProxyHTTPShare").checked;
    
    /*
    * Edit
    */
      if(gIsEdit){
        //Update RDF Properties
        for(key in ggproxy_options){
          gproxy_ds_changePropertyValue(gProxy, (gSProxyRdfNodeUriRoot + "#" + key), document.getElementById(key).value);
        }
        
        //Update Name
        gproxy_ds_changePropertyValue(gProxy, gSProxyRdfNodeName, gProxyNameField.value);
      }
    /*
    * Add
    */
      else{
        var sProxyUri   = opener.gproxy_getUniqueProxyUri();
        var oProxy    = gproxy_ds_getResource(sProxyUri);
        
        //Add Element
        gproxy_ds_addElement(sProxyUri);
        
        //Add Properties
        for(key in ggproxy_options){
          var oProp = gproxy_ds_getResource(gSProxyRdfNodeUriRoot + "#" + key);
          gproxy_ds_addProperty(oProxy, oProp, document.getElementById(key).value, true);
        }
        
        //Add Name
        gproxy_ds_addProperty(oProxy, gproxy_ds_getResource(gSProxyRdfNodeName), gProxyNameField.value, true);
      }
    /*
    * Finish
    */      
      opener.gproxy_populateList();

	  if(gIsEdit){
        
        //Is this proxy is in use, refresh proxy
        var oPrefs  = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
        if(oPrefs.prefHasUserValue("gproxy.proxy.current") 
          && oPrefs.getCharPref("gproxy.proxy.current") == gProxy.Value){
          
          opener.gproxy_doCommand("gproxy_setProxy()");
        }
        //If not in use, select proxy
        else{
          opener.gproxy_doCommand("gproxy_selectItem('"+ gProxyNameField.value +"')");
        }
        
        //alert("Proxy Info Changed");
      }
      else{
        opener.gproxy_doCommand("gproxy_selectItem('"+ gProxyNameField.value +"')"); 
        //alert("Proxy Added");
      }

      
  }catch(e){
    alert(gproxy_getString("error.unknown") +"\n("+ e +")");
    return;
  }
  
  gproxy_openerFocus()  
  return true;
}


//Enable or disable Manual
//  + config options
function gproxy_enableOptions(){
  var disable = !(document.getElementById("networkProxyType").value == "1"); //if 1 then manual is selected
  
  //Set Radios
  oType = document.getElementById("networkProxyType");
  if(!disable){
    oType.selectedItem = document.getElementById("type-manual-radio");

  }
  else{
    oType.selectedItem = document.getElementById("type-auto-radio");
  }
  if (document.getElementById("networkProxyType").value == "2"){
  
  //Manual Options
  document.getElementById("networkProxyHTTP").disabled = true;
  document.getElementById("networkProxyHTTP_Port").disabled = true;
  document.getElementById("networkProxyHTTPShare").disabled = true;
  document.getElementById("networkProxySSL").disabled = true;
  document.getElementById("networkProxySSL_Port").disabled = true;
  document.getElementById("networkProxyFTP").disabled = true;
  document.getElementById("networkProxyFTP_Port").disabled = true;
  document.getElementById("networkProxyGopher").disabled = true;
  document.getElementById("networkProxyGopher_Port").disabled = true;
  document.getElementById("networkProxySOCKS").disabled = true;
  document.getElementById("networkProxySOCKS_Port").disabled = true;
  document.getElementById("networkProxySOCKSVersion4").disabled = true;
  document.getElementById("networkProxySOCKSVersion5").disabled = true;
  document.getElementById("networkProxyNone").disabled = true;

  //Auto
  document.getElementById("networkProxyAutoconfigURL").disabled = false;
 
}
else{
//When manual setting is checked
  gproxy_enableShareSetting();
  document.getElementById("networkProxyHTTP").disabled = false;
  document.getElementById("networkProxyHTTP_Port").disabled = false;
  document.getElementById("networkProxyHTTPShare").disabled = false;
  document.getElementById("networkProxyNone").disabled = false;
  document.getElementById("networkProxyAutoconfigURL").disabled = true;
}
}
//Set setting on start-up
function gproxy_enableShareSettingOnStart(){
var disabletextbox = (document.getElementById("networkProxyHTTPShare").value == "true");

  document.getElementById("networkProxySSL").disabled = disabletextbox;
  document.getElementById("networkProxySSL_Port").disabled = disabletextbox;
  document.getElementById("networkProxyFTP").disabled = disabletextbox;
  document.getElementById("networkProxyFTP_Port").disabled = disabletextbox;
  document.getElementById("networkProxyGopher").disabled = disabletextbox;
  document.getElementById("networkProxyGopher_Port").disabled = disabletextbox;
  document.getElementById("networkProxySOCKS").disabled = disabletextbox;
  document.getElementById("networkProxySOCKS_Port").disabled = disabletextbox;
  document.getElementById("networkProxySOCKSVersion4").disabled = disabletextbox;
  document.getElementById("networkProxySOCKSVersion5").disabled = disabletextbox;

}
//Disable below when check sharing setting
function gproxy_enableShareSetting(){
var disabletextbox = (document.getElementById("networkProxyHTTPShare").checked == true);

  document.getElementById("networkProxySSL").disabled = disabletextbox;
  document.getElementById("networkProxySSL_Port").disabled = disabletextbox;
  document.getElementById("networkProxyFTP").disabled = disabletextbox;
  document.getElementById("networkProxyFTP_Port").disabled = disabletextbox;
  document.getElementById("networkProxyGopher").disabled = disabletextbox;
  document.getElementById("networkProxyGopher_Port").disabled = disabletextbox;
  document.getElementById("networkProxySOCKS").disabled = disabletextbox;
  document.getElementById("networkProxySOCKS_Port").disabled = disabletextbox;
  document.getElementById("networkProxySOCKSVersion4").disabled = disabletextbox;
  document.getElementById("networkProxySOCKSVersion5").disabled = disabletextbox;
  
  //if (document.getElementById("networkProxyHTTPShare").value == "true"){
  //gproxy_copySetting();
//}
}
function gproxy_enableShareSettingSelect(){
var disabletextbox = (document.getElementById("networkProxyHTTPShare").checked == true);

  document.getElementById("networkProxySSL").disabled = disabletextbox;
  document.getElementById("networkProxySSL_Port").disabled = disabletextbox;
  document.getElementById("networkProxyFTP").disabled = disabletextbox;
  document.getElementById("networkProxyFTP_Port").disabled = disabletextbox;
  document.getElementById("networkProxyGopher").disabled = disabletextbox;
  document.getElementById("networkProxyGopher_Port").disabled = disabletextbox;
  document.getElementById("networkProxySOCKS").disabled = disabletextbox;
  document.getElementById("networkProxySOCKS_Port").disabled = disabletextbox;
  document.getElementById("networkProxySOCKSVersion4").disabled = disabletextbox;
  document.getElementById("networkProxySOCKSVersion5").disabled = disabletextbox;
  
  //if (document.getElementById("networkProxyHTTPShare").value == "true"){
  gproxy_copySetting();
//}
}
//Copy http proxy
function gproxy_copySetting(){
if (document.getElementById("networkProxyHTTPShare").checked == true){
  document.getElementById("networkProxySSL").value = document.getElementById("networkProxyHTTP").value;
  document.getElementById("networkProxySSL_Port").value = document.getElementById("networkProxyHTTP_Port").value;
  document.getElementById("networkProxyFTP").value = document.getElementById("networkProxyHTTP").value;
  document.getElementById("networkProxyFTP_Port").value = document.getElementById("networkProxyHTTP_Port").value;
  document.getElementById("networkProxyGopher").value = document.getElementById("networkProxyHTTP").value;
  document.getElementById("networkProxyGopher_Port").value = document.getElementById("networkProxyHTTP_Port").value;
  document.getElementById("networkProxySOCKS").value = document.getElementById("networkProxyHTTP").value;
  document.getElementById("networkProxySOCKS_Port").value = document.getElementById("networkProxyHTTP_Port").value;
}
else{
  document.getElementById("networkProxySSL").value = "";
  document.getElementById("networkProxySSL_Port").value = "";
  document.getElementById("networkProxyFTP").value = "";
  document.getElementById("networkProxyFTP_Port").value = "";
  document.getElementById("networkProxyGopher").value = "";
  document.getElementById("networkProxyGopher_Port").value = "";
  document.getElementById("networkProxySOCKS").value = "";
  document.getElementById("networkProxySOCKS_Port").value = "";
}	
}
