//<!--

    // globals
    var curOption = new Array();
    var isLoaded = new Array();
    var noValue = '-99';
    var strInitialPlugin;
    // this var will tell wether promotionid, plan, recurring, paymentterm or nextbilldate have changed
    var packageBillingChanged = false;

    var serverProperties = new Array();
    var packages = new Array();

    function emptyList( listName ){
      var lst = document.forms['mainform'][listName];
      lst.options.length = 0;
      //lst.onchange = null;
      lst.disabled = false;
      isLoaded[listName] = false;
      curOption[listName] = noValue;
    }

    function fillList( listName, strOptions ){
      // fill any list with options
      emptyList( listName );

      // always insert selection prompt
      var lst = document.forms['mainform'][listName];

      // options in form "value~displaytext|value~displaytext|..."
      var aOptionPairs = strOptions.split('|');
      for( var i = 0; i < aOptionPairs.length; i++ ){
        if (aOptionPairs[i].indexOf('~') != -1) {
          var aOptions = aOptionPairs[i].split('~');
          lst.options[i] = new Option(aOptions[1]+'  ', aOptions[0]);
          if (aOptions[3] == 1) {
              lst.options[i].selected = true;
          }
        }
      }
    }

    function SetPackages(requestObj, packageId)
    {
        newPackages = requestObj.responseText;
        unsetStatus(lang('Loaded'), 1000);
        if (newPackages) {
            packageId = 0;
            fillList('plan',newPackages);
            var aOptionPairs = newPackages.split('|');
            for (var i = 0; i < aOptionPairs.length; i++) {
                packages[i] = aOptionPairs[i].split('~');
                if (packages[i][3] == 1) {
                    packageId = i;
                }
            }
        } else {
            for (var i = 0; i < packages.length; i++) {
                if (packages[i][0] == packageId) {
                    break;
                }
            }
            packageId = i;
        }

        //check to see if domain info should be seen
        //domain_frame
         var ob1 = $('divserverplugin');
         var ob2 = $('divregistrarplugin');
         var ob3 = $('registrarplugin');
         var ob4 = $('serverplugin');
         if (packages[packageId][2] == 2){
             $('domain_frame').style.display="";
             if(ob1!=null) ob1.style.display="";
             if(ob2!=null) ob2.style.display="";
             if(ob3!=null) ob3.checked = true;
             if(ob4!=null) ob4.checked = true;
             document.forms['mainform'].isDomainPackage.value=1;
             ToggleDomainRequirement();
         }else{
             if(ob2!=null) {
                 ob2.style.display="none";
             }
             if(ob3!=null) {
                 ob3.checked = false;
             }
             $('domain_frame').style.display="none";
             document.forms['mainform'].isDomainPackage.value=0;
             ToggleDomainRequirement();
         }

         if (packages[packageId][4] != 0) {
            if(ob1!=null) {
                ob1.style.display="block";
             }
             if(ob4!=null) {
                 ob4.checked = true;
             }
            setServerList(packages[packageId][4]);
            $('server_frame').style.display = 'block';
            document.forms['mainform'].isServerPackage.value=1;
         } else {
             if(ob1!=null) {
                 ob1.style.display="none";
             }
             if(ob4!=null) {
                 ob4.checked = false;
             }
            $('server_frame').style.display = 'none';
            document.forms['mainform'].isServerPackage.value=0;
         }

         getPackageAddons();
	}

    function getPackageAddons()
    {
         if (document.forms['mainform'].elements['recurring'].value == 0) {
             var paymentTerm = 0;
         } else {
             var paymentTerm = document.forms['mainform'].elements['paymentterm'].value;
         }

         new Ajax.Updater('addonsDiv',
                          'index.php?fuse=clients&view=GetPackageAddons&packageTypeId='+document.forms['mainform'].elements['promotionid'].value+'&packageId='+document.forms['mainform'].elements['plan'].value+'&domainId='+document.forms['mainform'].elements['domainid'].value+'&newDomain='+(gAddingNew? 1 : 0)+'&billingCycle='+paymentTerm,
                          {onLoading     : function(request) {resetStatus(lang('Loading'))},
                           onComplete    : function(request) {unsetStatus(lang('Loaded'), 1000)}});
    }

    function switchPackages(packageId)
    {
        for (var i = 0; i < packages.length; i++) {
            if (packages[i][0] == packageId) {
                packages[i][3] = 1;
            } else {
                packages[i][3] = 0;
            }
        }
        SetPackages(false, packageId)
    }

    function setServerList(servers)
    {
        if (!servers) {
            emptyList('serverid');
            $('serverPluginName').innerHTML = '';
            document.forms['mainform'].CT_1.value =  '';

            // remove ips
            num = document.forms['mainform'].serverips.options.length;
            for (x=1;x<num;x++){
                 document.forms['mainform'].serverips.options[num-x] = null;
            }
            return;
        }

        var regexp = /></g;
        servers = servers.replace(regexp, '|');
        regexp = /%/g;
        servers = servers.replace(regexp, '~');
        fillList('serverid', servers);
        servers = servers.split('|');
        for (var i = 0; i < servers.length; i++) {
            serverProperties[i] = servers[i].split('~');
        }
        selectedServerId = setServerPluginName();
        PopulateServerIps(selectedServerId);
    }

    function switchServer(serverId)
    {
        for (var i = 0; i < serverProperties.length; i++) {
            if (serverProperties[i][0] == serverId) {
                serverProperties[i][3] = 1;
            } else {
                serverProperties[i][3] = 0;
            }
        }
        setServerPluginName();
        PopulateServerIps(serverId);
    }

    function setServerPluginName()
    {
        var serverId = false;
        for (var i = 0; i < serverProperties.length; i++) {
            if (serverProperties[i][3] == 1) {
                if (!serverProperties[i][2]) {
                    $('serverPluginName').innerHTML = lang('None');
                    $('serverPluginName').className = 'red';
                } else {
                    $('serverPluginName').innerHTML = serverProperties[i][2];
                }
                serverId = serverProperties[i][0];
            }
        }

        return serverId;
    }


    //Function is used to remove the required fields for the domain related fields if
    //clienttype(package type) does not allow for it
    function ToggleDomainRequirement(){
        if($('domain_frame').style.display=="none"){
            //remove the value on these so that they are not enforced
            var rDomainElm = $("r_DomainName");
            if(rDomainElm!=null)  rDomainElm.value="";
            var DDomainElm = $("D_DomainName");
            if(DDomainElm!=null)  DDomainElm.value="";
            var rUserNameElm = $("r_UserName");
            if(rUserNameElm!=null)  rUserNameElm.value="";
            var rPasswordElm = $("r_Password");
            if(rPasswordElm!=null)  rPasswordElm.value="";
        }else{
            var rDomainElm = $("r_DomainName");
            if(rDomainElm!=null)  rDomainElm.value="Domain Name";
            var DDomainElm = $("D_DomainName");
            if(DDomainElm!=null)  DDomainElm.value="Proper Domain Name Format";
            var rUserNameElm = $("r_UserName");
            if(rUserNameElm!=null)  rUserNameElm.value="User Name";
            var rPasswordElm = $("r_Password");
            if(rPasswordElm!=null)  rPasswordElm.value="Password is required";
        }
    }

    // add extra attributes values from ajax generated variables (because it wont be added automatically)
    function addExtraInfo()
    {
        // extra attributes for domain name
        var extraDomainAttributes = '';
        var tldSpecificFieldsSelect = $('tld_specific_fields').getElementsByTagName('select');
        for (var i = 0; i < tldSpecificFieldsSelect.length; i++) {
            extraDomainAttributes += '&'+ encodeURIComponent(tldSpecificFieldsSelect[i].name) + '=' + encodeURIComponent(tldSpecificFieldsSelect[i].value);
        }
        var tldSpecificFieldsInput= $('tld_specific_fields').getElementsByTagName('input');
        for (var i = 0; i < tldSpecificFieldsInput.length; i++) {
            extraDomainAttributes += '&'+ encodeURIComponent(tldSpecificFieldsInput[i].name) + '=' + encodeURIComponent(tldSpecificFieldsInput[i].value);
        }
        _addHiddenVar(document.forms['mainform'], 'domain_extra_attributes', extraDomainAttributes);

        // extra variables for package add-ons
        var extraAddonAttributes = new Array();
        var addonsFields = $('addonsDiv').getElementsByTagName('select');
        for (var i = 0; i < addonsFields.length; i++) {
            extraAddonAttributes.push(addonsFields[i].value);
        }
        _addHiddenVar(document.forms['mainform'], 'addons_selected', extraAddonAttributes.join(','));
    }

    function GetPackage(planId){
        packageid = document.forms['mainform'].elements['promotionid'].value;
        planId = planId || 0;
        window.status = 'Loading Options Selections...';
        new Ajax.Request('index.php?fuse=clients&action=GetPackages&packageid='+packageid+'&planId='+planId+'&selecteddomain='+document.forms['mainform'].elements['selectedDomain'].value,
                          {onLoading     : function(request) {resetStatus(lang('Loading'))},
                           onComplete    : SetPackages});
    }

    function AskAboutResettingRecurring(form){
        //ask if they want to reset recurring billing for this account
 		//only need to ask if recurring is set to true or there are package addons
 		if ((form.recurring.value==1 || thereAreAddons) && packageBillingChanged){
            var answer=confirm("Click OK if you want to recreate the recurring transaction for this account. Click Cancel to just update this account and not reset invoice generation.\n\nNote: If you click OK you will need to delete the current recurring transaction at the 3rd party processor, since the next generated invoice will re-create one.");
            if (answer){
                form.resetgenerateinvoice.value=1;
            }
        }
        return (true);
    }

    function AskAboutResettingRecurring2(form){
        //ask if they want to reset recurring billing for this account
        //only need to ask if recurring is set to true
        if (form.recurring.value==1){
            var answer=confirm("Click OK if you want to recreate the recurring transaction for this account.\n\nNote: If you click OK you will need to delete the current recurring transaction at the 3rd party processor, since the next generated invoice will re-create one.");
            if (answer){
                form.resetgenerateinvoice.value=1;
                return true;
            }
        }
        return (false);
    }

    function checkPassword(form){
        if ((form.confirm_password.value !="") && (form.new_password.value !="")){
            if (form.confirm_password.value != form.new_password.value){
                alert("Your confirmation password does not match your new password");
                return (false);
            }
        }else{
            if ((form.confirm_password.value !="") || (form.new_password.value !="")){
                alert("You must fill all password fields if you wish to change this password");
                return (false);
            }
        }
        return (true);
    }

    function togglerecurringfields(){
         if (document.forms['mainform'].recurring.value==1) $('recurringinfo').style.display="";
         else $('recurringinfo').style.display="none";
    }

    function DeleteDomain(){
        var answer=confirm("Click OK if you are sure you want to delete this package");
        if (answer){
            if (strInitialPlugin!="" && document.forms['mainform'].isServerPackage.value == 1){
                var answer2=confirm("Do you want to delete this domain using the "+strInitialPlugin+" plugin?");
                if (answer2) document.forms['deleteform'].deletewithplugin.value=1;
            }
            document.forms['deleteform'].submit();
        }
    }

    function RegisterDomain()
    {
        var answer=confirm("Click OK if you are sure you want to register this domain with your selected provider");
        if (answer){
            document.forms['registerdomainform'].submit();
        }
    }

     function SetIP(){
         if (document.forms['mainform'].serverips.selectedIndex==0) return false;
         document.forms['mainform'].CT_1.value = document.forms['mainform'].serverips.options[document.forms['mainform'].serverips.selectedIndex].value;
      }

      function EnableSelect(obj){
               document.forms['mainform'].serverips.disabled = obj.checked;
               document.forms['mainform'].serverips.selectedIndex = 0;
               if (document.forms['mainform'].serverid.selectedIndex == -1) {
                   return;
               }
               serverid = document.forms['mainform'].serverid.options[document.forms['mainform'].serverid.selectedIndex].value;
               if (obj.checked) document.forms['mainform'].CT_1.value =  serverips[serverid][0];
      }

      function isFunction(a) {
                return typeof a == 'function';
      }

      function isObject(a) {
                return (a && typeof a == 'object') || isFunction(a);
      }

      function PopulateServerIps(serverid)
      {
           strInitialPlugin = serverplugin[serverid]

           //delete all previous options
           num = document.forms['mainform'].serverips.options.length;
           for (x=1;x<num;x++){
                document.forms['mainform'].serverips.options[num-x] = null;
           }

           //add all the options only if serverips[serverid] exists
           for (x=1;x<serverips[serverid].length;x++){
                  var opt = document.createElement("OPTION");
                  opt.text= serverips[serverid][x];
                  opt.value=opt.text;
                  if (opt.value != "") {
                      if (isIE) document.forms['mainform'].serverips.add(opt);
                      else document.forms['mainform'].serverips.add(opt,null);
                  }
           }

           if (document.forms['mainform'].shared.checked) {
               document.forms['mainform'].CT_1.value =  serverips[serverid][0];
               document.forms['mainform'].serverips.disabled =  true;
           }
      }

      function _addHiddenVar(form, varName, varValue)
      {
        var inputElm = document.createElement('input');
        inputElm.setAttribute('type', 'hidden');
        inputElm.setAttribute('name', varName);
        inputElm.setAttribute('value', varValue);
        form.appendChild(inputElm);
      }

      function updateCustomFields()
      {
          form = document.forms['mainform'];
          for (i = 0; i < form.elements.length; i++) {
              if (form.elements[i].name.substring(0,3) == "CT_" && form.elements[i].name.substring(3) != "1") {
                  var fieldID = form.elements[i].name.substring(3);
                  packageTypeID = form.elements['promotionid'][form.elements['promotionid'].selectedIndex].value;
                  if (excludeFields[fieldID] != null && InArray(excludeFields[fieldID].split(","), packageTypeID)) {
                      document.getElementById('CT_'+fieldID+'_span').style.display='none';
                  } else {
                      document.getElementById('CT_'+fieldID+'_span').style.display='';
                  }
              }
          }

      }

//--> end hide JavaScript

