<?php

require_once 'modules/admin/views/SignupView.php';
require_once 'modules/clients/models/domain.php';

/**
* @package Admin
*/
class Signup_1_DomainNameForm extends SignupView
{
    var $permission     = '';

    function preHeaders()
    {
        $this->initialize();
    }
    
    function dispatch()
    {
        $bolAllowBack = false;

         if($this->package > 0 && $this->clienttype==0) {
             if (!$this->clienttype = $this->getClientType($this->package)) {
                 NE_Lib::redirectPage('signup.php');
             }
             $_REQUEST['clienttype'] = $this->clienttype;
         }


        if ($this->clienttype > 0 || $this->package > 0) {
             $_SESSION['AllowStep0'] = false;
        }

        require_once 'modules/admin/models/PackageType.php';

        //check to see if clienttype accepts domain functionality
        $packageType = new PackageType($this->clienttype);
        if (!$packageType->isTypeDomain()) {
            //This clienttype does not support domain functions
            //forward to next step
            require_once 'modules/admin/views/Signup_2_OrderForm.php';
            $stepTwo = new Signup_2_OrderForm('admin', 'Signup_2_OrderForm', $this->settings);
            $stepTwo->setUser($this->user);
            $stepTwo->setTpl($this->tpl);
            $stepTwo->preHeaders();
            $stepTwo->dispatch();
            return;
        } else {
            $this->templateInit();

            //affliates
            if (isset($_REQUEST['affcode'])) {
                $this->tpl->assign("AFFCODE",$_REQUEST['affcode']);
            }else{
                $this->tpl->assign("AFFCODE","0");
            }


            // standard assigns
            $this->tpl->assign("CLIENTTYPE",$this->clienttype);
            $this->tpl->assign("CURRENCY",htmlspecialchars(@$_REQUEST['currency']));
            $this->tpl->assign("SELECTEDCURRENCY",htmlspecialchars(@$_REQUEST['selectedcurrency']));
            $this->tpl->assign("DOMAIN",$this->domain);
            $this->tpl->assign("PACKAGE", htmlspecialchars(@$_REQUEST['package']));
            $this->tpl->assign("COUPONCODE", $this->couponcode);
            $this->tpl->assign("PAYMENTTERM",$this->paymentterm);
            
            // domain check assigns
            $this->tpl->assign("ERROR", $this->lang("Error"));
            $this->tpl->assign('ACTION_CHECKDOMAIN', 'CheckDomain');
            $this->tpl->assign("COMPANYNAME", $this->settings->get('Company Name'));
            $this->tpl->assign("MAINDOMAIN", $packageType->getMainDomain());

            //jsfunction to check domain availability
            if ($packageType->allowReg()) {
                $this->tpl->parse("ALLOWREG", "allowreg");
                $this->tpl->parse("ALLOWREG1", "allowreg1");
                $this->tpl->parse("ALLOWREG2", "allowreg2");
                $this->tpl->parse("ALLOWREG3", "allowreg3");
                $this->tpl->parse("ALLOWREG4", "allowreg4");
                $this->tpl->parse("ALLOWREG5", "allowreg5");
                $this->tpl->parse("ALLOWREG6", "allowreg6");
                $this->tpl->parse("ALLOWREG7", "allowreg7");
                $this->tpl->parse("ALLOWREG8", "allowreg8");
            }
            if ($packageType->allowTransfer()) {
                $this->tpl->parse("ALLOWTRANSFER", "allowtransfer");
                $this->tpl->parse("ALLOWTRANSFER1", "allowtransfer1");
                $this->tpl->parse("ALLOWTRANSFER2", "allowtransfer2");
                if ($packageType->allowSelfManage()) {
                    $this->tpl->parse("ALLOWSELFMANAGE3", "allowselfmanage3");
                }
                $this->tpl->parse("ALLOWTRANSFER3", "allowtransfer3");
                $this->tpl->parse("ALLOWTRANSFER4", "allowtransfer4");
                $this->tpl->parse("ALLOWTRANSFER5", "allowtransfer5");
                $this->tpl->parse("ALLOWTRANSFER6", "allowtransfer6");
                $this->tpl->parse("ALLOWTRANSFER7", "allowtransfer7");
                $this->tpl->parse("ALLOWTRANSFER8", "allowtransfer8");
                $this->tpl->parse("ALLOWTRANSFER9", "allowtransfer9");
            }
            if ($packageType->allowSelfManage()) {
                $this->tpl->parse("ALLOWSELFMANAGE", "allowselfmanage");
                $this->tpl->parse("ALLOWSELFMANAGE1", "allowselfmanage1");
                if (!$packageType->allowReg()) {
                    $this->tpl->parse("ALLOWSELFMANAGE2b", "allowselfmanage2b");
                    $this->tpl->parse("ALLOWSELFMANAGE4", "allowselfmanage4");
                } else {
                    $this->tpl->parse("ALLOWSELFMANAGE2", "allowselfmanage2");
                }
                $this->tpl->parse("ALLOWSELFMANAGE5", "allowselfmanage5");
                $this->tpl->parse("ALLOWSELFMANAGE6", "allowselfmanage6");
                $this->tpl->parse("ALLOWSELFMANAGE7", "allowselfmanage7");
                $this->tpl->parse("ALLOWSELFMANAGE8", "allowselfmanage8");
                $this->tpl->parse("ALLOWSELFMANAGE9", "allowselfmanage9");
                $this->tpl->parse("ALLOWSELFMANAGE10", "allowselfmanage10");
                $this->tpl->parse("ALLOWSELFMANAGE10", "allowselfmanage11");
            }
            
            $dot = strpos($this->domain, '.'); 
            if ($dot !== false) {
                $this->tpl->assign('DOMAINSLD', substr($this->domain, 0, $dot));
                $tld = substr($this->domain, $dot + 1);
            }

            //Get list of supported domains
            //Also populate strpopup to be used to show supported domains
            $query="SELECT DISTINCT(name) FROM tld WHERE active = 1 ORDER BY name ";
            $result = $this->db->query($query);
            $strPopUp = "";
            $jsArraySupportedDomains = "";
            $isOtherTLD = true;
            $i=0;
            while(list($tldname) = $result->fetch()) {
                $strPopUp .= " .".$tldname. ",";
                $jsArraySupportedDomains .= "'".$tldname."',";
                if (($dot !== false && $tldname == $tld) || ($dot === false && $tldname == $this->settings->get('Default TLD'))) {
                    $selected = 'selected="true"';
                    $isOtherTLD = false;
                } else {
                    $selected = '';
                }
                $this->tpl->setKey(array(
                    'assigneeId'            => $tldname,
                    'assigneeSelected'      => $selected
                ));
                $i=1;
                $this->tpl->parse("TLDLIST", ".tldextension");
            }
            if ($dot !== false) {
                if ($isOtherTLD) {
                    $this->tpl->assign('OTHERTLDVALUE', $tld);
                }
                $this->tpl->parse('CONCATDOMAIN', 'ConcatDomain');
            }

            $jsArraySupportedDomains = substr($jsArraySupportedDomains,0,strlen($jsArraySupportedDomains)-1);
            $strPopUp = substr($strPopUp,0,strlen($strPopUp)-1);
            $this->tpl->assign("JSARRAYSUPPORTEDDOMAINS", $jsArraySupportedDomains);
            $this->tpl->assign('REGEXDOMAIN', REGEXDOMAIN);
            $this->tpl->assign('REGEXSUBDOMAIN', REGEXSUBDOMAIN);
            $this->tpl->assign("STRPOPUP", $strPopUp);
            $this->tpl->parse("SIGNUP_1_CHECKDOMAIN", "signup_1_checkdomain");
            $this->tpl->FastPrint("SIGNUP_1_CHECKDOMAIN");
            
            if ($packageType->isSub() && ($packageType->allowReg() || $packageType->allowSelfManage() || $packageType->allowTransfer())) {
                // special handling when both regular domains and subdomains are enabled.
                $this->tpl->assign('subdomain_style', 'display:none');
                $this->tpl->assign('subdomain_prefix', 'sub');
                $this->tpl->parse('trash', 'dropdown_choice');
            }
            
            if ($packageType->isSub()) {
                
                $this->tpl->assign("MAINDOMAIN",$packageType->getMainDomain());
                $this->tpl->assign("SEARCHFUNCTION","SubmitStep2c");
                $this->tpl->parse("DROPDOWN1","dropdown2");
                $this->tpl->parse("SIGNUPBLOCKCONTENT","signup_1_dropdown");
            }
            if ($packageType->allowTransfer() || $packageType->allowSelfManage()) {
                $this->tpl->parse("ALLOWSELFMANAGETRANSFER", "allowselfmanagetransfer");
            }
            
            $this->tpl->parse("DROPDOWNONCHANGE",'dropdown_onchange');
            $this->tpl->parse("DROPDOWN1","dropdown1");
            
            if ($packageType->allowReg()) {
                //build options for domain registration
                $this->tpl->assign("OPTIONSTEP1ID", 1);
                $this->tpl->assign("OPTIONSTEP1VALUE", 0);
                $this->tpl->assign("DOMAINACTION", $this->lang('Register Domain'));
                $this->tpl->assign("DOMAINACTIONDESCRIPTION", $this->lang('The domain will be registered on your behalf and directed to your new account with %s. Your domain registration will automatically renew.', $this->settings->get('Company Name')));
                $this->tpl->parse("trash", ".registration_option");
            }
            if ($packageType->allowTransfer()) {
                $this->tpl->assign("OPTIONSTEP1ID", 2);
                $this->tpl->assign("OPTIONSTEP1VALUE", 1);
                $this->tpl->assign("DOMAINACTION", $this->lang('Transfer My Domain To You'));
                $this->tpl->assign("DOMAINACTIONDESCRIPTION", $this->lang('Select this option if you would like to make %s your registrar for your existing domain.', $this->settings->get('Company Name')));
                $this->tpl->parse("trash", ".registration_option");
            }

            if ($packageType->allowSelfManage()) {
                $this->tpl->assign("OPTIONSTEP1ID",3);
                $this->tpl->assign("OPTIONSTEP1VALUE",2);
                $this->tpl->assign("DOMAINACTION", $this->lang('Self-Management (no cost)'));
                $this->tpl->assign("DOMAINACTIONDESCRIPTION", $this->lang('Select this option if you prefer to handle your registration manually. %s can establish service with any Top-Level Domain (TLD), international or domestic.', $this->settings->get('Company Name')));
                $this->tpl->parse("trash", ".registration_option");
            }
            //only show registration option if not showing subdomain option and one of the registration options is available
            if ($packageType->allowReg() || $packageType->allowTransfer() || $packageType->allowSelfManage()) {
                $this->tpl->assign("SEARCHFUNCTION","CheckDomain");
                $this->tpl->assign("SUBMITFUNCTION","SubmitStep2");
                $this->tpl->parse('trash', 'domainoptions');
                $this->tpl->parse("SIGNUPBLOCKCONTENT","signup_1_dropdown");
                $this->tpl->parse("SIGNUPBLOCKCONTENT", ".signup_1_options");
            } else if (!$packageType->isSub()) {
                $this->tpl->assign("SEARCHFUNCTION","SubmitStep2b");
                $this->tpl->parse('trash', 'domainoptions');
                $this->tpl->parse("SIGNUPBLOCKCONTENT","signup_1_dropdown");
            }
            
            $this->tpl->parse("SIGNUP_1_DOMAINNAMEFORM","signup_1_domainnameform");
            $this->tpl->FastPrint("SIGNUP_1_DOMAINNAMEFORM");
        }
        $this->tpl->FastPrint("FOOTER");
    }
}

?>
