<?php

require_once 'modules/admin/views/SignupView.php';
require_once 'classes/domain.php';

/**
* @package Admin
*/
class Signup_1_DomainNameForm extends SignupView
{
    var $jsLibs = array('newedge/javascript/prototype.php', 'javascript/common.php');

    function dispatch()
    {
        $bolAllowBack = false;

        $this->initialize();

         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->isRegDomain()) {
            //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->dispatch();
            return;
        } else {
            $this->templateInit();

            //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");
            }
            $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());

            if ($packageType->allowTransfer() || $packageType->allowSelfManage()) {
                $this->tpl->parse("ALLOWSELFMANAGETRANSFER", "allowselfmanagetransfer");
            }

            $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";
            $result = $this->db->query($query);
            $strPopUp = "";
            $jsArraySupportedDomains = "";
            $isOtherTLD = true;
            $i=0;
            while(list($tldname) = $result->fetch()) {
                $strPopUp .= " .".$tldname. ",";
                $jsArraySupportedDomains .= "'".$tldname."',";
                if (($dot === false && $i == 0) || ($dot !== false && $tldname == $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', DOMAIN_REGEXDOMAIN);
            $this->tpl->assign('REGEXSUBDOMAIN', DOMAIN_REGEXSUBDOMAIN);
            $this->tpl->assign("STRPOPUP", $strPopUp);
            $this->tpl->parse("SIGNUPCHECKDOMAIN", "signupCheckDomain");
            $this->tpl->FastPrint("SIGNUPCHECKDOMAIN");

            $this->showProgress($this->lang('Select domain.'),50);

            $this->tpl->assign("CLIENTTYPE",$this->clienttype);
            $this->tpl->assign("CURRENCY",@$_REQUEST['currency']);
            $this->tpl->assign("DOMAIN",$this->domain);
            $this->tpl->assign("PACKAGE", @$_REQUEST['package']);
            $this->tpl->assign("COUPONCODE", $this->couponcode);
            $this->tpl->assign("PAYMENTTERM",$this->paymentterm);
            
            $this->tpl->assign("SIZEOFBOX","70");
            $this->tpl->define_dynamic('searchBtn', 'boxsearch');
            $this->tpl->assign("INPUTVALUE","");
            $this->tpl->assign("INPUTNAME","subdomaincheck");

            if (!$packageType->isSub()) {
                $this->tpl->assign("SIGNUPBLOCKDESCRIPTION",$this->lang('Enter Your Domain Name.'));
                $this->tpl->parse("DROPDOWNONCHANGE",'dropdown_onchange');
                $this->tpl->parse("DROPDOWN1","dropdown1");
                $this->tpl->parse("SIGNUPBLOCKCONTENT","dropdown");
            } else {
                $this->tpl->assign("MAINDOMAIN",$packageType->getMainDomain());
                $this->tpl->assign("SIGNUPBLOCKDESCRIPTION",$this->lang("Enter Your Subdomain."));
                $this->tpl->parse("DROPDOWN1","dropdown2");
                $this->tpl->parse("SIGNUPBLOCKCONTENT","dropdown");
            }

            //only show search if we are allowing one of the registration methods
            //and we are not showing subdomain option
            if ((!$packageType->allowReg() && !$packageType->allowTransfer() && !$packageType->allowSelfManage()) || $packageType->isSub()) {
                $this->tpl->assign("WIDTHOFBOX","400");
                if($packageType->isSub()) {
                    $this->tpl->assign("SUBMITFUNCTION","SubmitStep2c");
                } else {
                    $this->tpl->assign("SUBMITFUNCTION","SubmitStep2b");
                }
                $this->tpl->assign("INPUTNAME","transfer");
                $this->tpl->assign("INPUTVALUE",2);
                $this->tpl->parse("FORM_INPUTS", "form_hidden_inputs");

                if ($_SESSION['AllowStep0']==true){
                    $this->tpl->assign("BACKFUNCTION","BackStepOne");
                    $this->tpl->assign("BACKINPUTNAME","back");
                    $this->tpl->assign("BACKINPUTVALUE","1");
                    $this->tpl->parse("BOXSUBMIT", "boxsubmitback");
                } else {
                     $this->tpl->parse("BOXSUBMIT", "boxsubmit");
                }
            } else {
                $this->tpl->assign("WIDTHOFBOX","326");
                $this->tpl->assign("SUBMITFUNCTION","CheckDomain");
                $this->tpl->subst('trash', 'searchBtn');
                $this->tpl->parse("BOXSUBMIT", "boxsearch");
            }

            $this->tpl->assign("SUBMITFUNCTION","SubmitStep2");
            $this->tpl->assign("SIZEOFBOX","200");
            $this->tpl->assign("WIDTHOFBOX","326");
            $this->tpl->assign("INPUTVALUE","");
            $this->tpl->assign("INPUTNAME","submitdomainplan");
            $this->tpl->assign("SIGNUPBLOCKDESCRIPTION",$this->lang('Choose Domain Plan'));

            $this->tpl->clear("SIGNUPBLOCKCONTENT");
            //Check to see if registration is allowed
            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("SIGNUPBLOCKCONTENT", ".options_step1");
            }
            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("SIGNUPBLOCKCONTENT", ".options_step1");
            }

            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("SIGNUPBLOCKCONTENT", ".options_step1");
            }


            if (@$_SESSION['AllowStep0'] == true) {
                $this->tpl->assign("BACKFUNCTION","BackStepOne");
                $this->tpl->assign("BACKINPUTNAME","back");
                $this->tpl->assign("BACKINPUTVALUE","1");
                $this->tpl->parse("BOXSUBMIT1", "boxsubmitback1");
            } else {
                 $this->tpl->parse("BOXSUBMIT1", "boxsubmit1");
            }
            if ($packageType->isSub()) {
                $this->tpl->parse("DIVDOMAINWINDOW1","divdomainwindow1");
            } else {
                $this->tpl->parse("DIVDOMAINWINDOW2","divdomainwindow2");
            }

            //only show registration option if not showing subdomain option and one of the registration options is available
            if (($packageType->allowReg() || $packageType->allowTransfer() || $packageType->allowSelfManage()) && !$packageType->isSub()) {
                $this->tpl->parse("DOMAINOPTIONS","domainoptions");
            }
            $this->tpl->parse("FORMSTEP1","formstep1");
            $this->tpl->FastPrint("FORMSTEP1");
        }

        $this->tpl->FastPrint("FOOTER");
    }
}

?>
