<?php

include("postget.php");
include_once("common.php");
$pagetitle = lang("_SIGNUP");
if ($config[120][1] == 1) {
    go2('signup2.php');
}


if ($config[74][1] == 0 OR isset($_COOKIE['hd_status'])) {
    messagetop(_ERROR_403, '', 'index.php', 0);
}

if ($_REQUEST[send] == "" . _SEND . "") {

    if ($config[79][1] == 1) {
        session_start();
        include_once 'securimage/securimage.php';
        $securimage = new Securimage();
    }
    $query2 = "SELECT email FROM hd_clients WHERE email='$_POST[Email]'";
    $result2 = mysql_query($query2, $db_link);
    $row2 = mysql_fetch_row($result2);
    $estiloerror = "error";
    $errors = '';
    if ($_POST['First'] == '') {
        $errors.= "<br>" . ucfirst(_FIRST_NAME) . ": " . _NOT_SPECIFIED . "";
        $estilofirst = $estiloerror;
    }
    if ($_POST['Last'] == '') {
        $errors.= "<br>" . ucfirst(_LAST_NAME) . ": " . _NOT_SPECIFIED . "";
        $estilolast = $estiloerror;
    }
    if ($_POST['City'] == '') {
        $errors.= "<br>" . ucfirst(_CITY) . ": " . _NOT_SPECIFIED . "";
        $estilocity = $estiloerror;
    }
    if ($_POST['Address'] == '') {
        $errors.= "<br>" . ucfirst(_ADDRESS) . ": " . _NOT_SPECIFIED . "";
        $estiloaddress = $estiloerror;
    }
    if ($_POST['Phone'] == '') {
        $errors.= "<br>" . ucfirst(_PHONE) . ": " . _NOT_SPECIFIED . "";
        $estilophone = $estiloerror;
    }
    if (strlen($_POST['password']) < 6) {
        $errors.= "<br>" . ucfirst(_PASSWORD) . ": " . _TOO_SHORT . "";
        $estilopassword = $estiloerror;
    }

    if ($_POST['password'] <> $_POST['password2']) {
        $errors.= "<br>" . ucfirst(_THE_PASSWORDS_ENTERED_DO_NOT_MATCH) . "";
        $estilopassword = $estiloerror;
    }
    if (!preg_match('/' . $regex4mail . '/i', $_POST['Email'])) {
        $errors.= "<br>" . ucfirst(_INVALID_EMAIL) . "";
        $estiloemail = $estiloerror;
    }

    if ($_POST['Email'] == $row2[0] AND $_POST['Email'] <> '') {
        $errors.= "<br>" . ucfirst(_ALREADY_EXIST_A_CLIENT_REGISTERED_WITH_THIS_MAIL_ADDRESS) . "";
        $estiloemail = $estiloerror;
    }


    if ($_POST['Country'] == '230'
            AND ($config[45][1] == 'Documento' OR $config[45][1] == 'Cedula de identidad')) {
        if (!checkciuy($_POST['Personalized_5'])) {
            $errors.= "<br>El número de Cédula de Identidad es INCORRECTO";
            $estilocedula = $estiloerror;
        }
    }

    if (antispam('Email', $_POST['Email'])) {
        $errors.= antispam('Email', $_POST['Email']);
        $estiloemail = $estiloerror;
    }

    if ($config[79][1] == 1) {
        if ($securimage->check($_POST['captcha_code']) == false) {
            $errors.= "<br>" . ucfirst(_SECURITY_CODE) . " " . _WRONG . "";
            $estilocaptcha = $estiloerror;
        }
    }
}



if ($errors == '' AND $_REQUEST[send] == "" . _SEND . "") {

    if ($_POST['PromotionalMailing'] == "on") {
        $PromotionalMailing = 1;
    } else {
        $PromotionalMailing = 0;
    }
    $fechahora = time() + $config[31][1] * 3600;
    $reseller = 0;
    $ClientID = generate_id('hd_clients', 'clientid', $config[101][1]);
    $_POST['First'] = ucwords(strtolower($_POST['First']));
    $_POST['Last'] = ucwords(strtolower($_POST['Last']));

    $query = "INSERT INTO hd_clients (clientid,first,last,company,address,city,state,zip,country,phone,email,password,start,personalized_1,personalized_2,personalized_3,personalized_4,personalized_5,preflang,promotionalmailing) ";

    $query.="VALUES(
	'$ClientID',
	'" . mysql_real_escape_string($_POST['First']) . "',
	'" . mysql_real_escape_string($_POST['Last']) . "',
	'" . mysql_real_escape_string($_POST['Company']) . "',
	'" . mysql_real_escape_string($_POST['Address']) . "',
	'" . mysql_real_escape_string($_POST['City']) . "',
	'" . mysql_real_escape_string($_POST['State']) . "',
	'" . mysql_real_escape_string($_POST['Zip']) . "',
	'" . mysql_real_escape_string($_POST['Country']) . "',
	'" . mysql_real_escape_string($_POST['Phone']) . "',
	'" . mysql_real_escape_string($_POST['Email']) . "',
	'" . mysql_real_escape_string(encrypt($_POST['password'])) . "',
	'$fechahora',
	'" . mysql_real_escape_string($_POST['Personalized_1']) . "',
	'" . mysql_real_escape_string($_POST['Personalized_2']) . "',
	'" . mysql_real_escape_string($_POST['Personalized_3']) . "',
	'" . mysql_real_escape_string($_POST['Personalized_4']) . "',
	'" . mysql_real_escape_string($_POST['Personalized_5']) . "',
	'$Preferredlang', 
	'$PromotionalMailing' 
	)";

    if (!mysql_query($query, $db_link)) {
        print("" . _CLIENT_NOT_ADDED . "");
    } else {
        $newclientid = $ClientID;

        logevent("" . _CLIENT . " $newclientid " . _CREATED . "", $newclientid, $newclientid);
        session_start();
        $_SESSION['newclientid'] = $newclientid;
        $_SESSION['hd_accesslvl'] = '1';
        $_SESSION['hd_clientid'] = $ClientID;
        $_SESSION['hd_idc'] = $ClientID;
        checklevel(1, $_SESSION['hd_clientid']);

        $subject = "" . _NEW_CLIENT . "";
        $bodymail = "<table border'0' cellspacing='4' cellpadding='4' width='80%'>";
        $bodymail.="<tr><td style='border: 2px solid blue;'><b>" . ucfirst(_FIRST_NAME) . ":</b></td>";
        $bodymail.="<td style='border: 1px solid gray;'>" . $First . "</td></tr>";
        $bodymail.="<tr><td style='border: 2px solid blue;'><b>" . ucfirst(_LAST_NAME) . ":</b></td>";
        $bodymail.="<td style='border: 1px solid gray;'>" . $Last . "</td></tr>";
        $bodymail.="<tr><td style='border: 2px solid blue;'><b>" . ucfirst(_EMAIL) . ":</b></td>";
        $bodymail.="<td style='border: 1px solid gray;'>" . $Email . "</td></tr>";
        $bodymail.="<tr><td style='border: 2px solid blue;'><b>" . ucfirst(_COMPANY) . ":</b></td>";
        $bodymail.="<td style='border: 1px solid gray;'>" . $Company . "</td></tr>";
        $bodymail.="<tr><td style='border: 2px solid blue;'><b>" . ucfirst(_ADDRESS) . ":</b></td>";
        $bodymail.="<td style='border: 1px solid gray;'>" . $Address . "</td></tr>";
        $bodymail.="<tr><td style='border: 2px solid blue;'><b>" . ucfirst(_PHONE) . ":</b></td>";
        $bodymail.="<td style='border: 1px solid gray;'>" . $Phone . "</td></tr>";
        $bodymail.="<tr><td style='border: 2px solid blue;'><b>" . ucfirst(_CITY) . ":</b></td>";
        $bodymail.="<td style='border: 1px solid gray;'>" . $City . "</td></tr>";
        $bodymail.="<tr><td style='border: 2px solid blue;'><b>" . ucfirst(_STATE) . ":</b></td>";
        $bodymail.="<td style='border: 1px solid gray;'>" . showstate($State) . "</td></tr>";
        $bodymail.="<tr><td style='border: 2px solid blue;'><b>" . ucfirst(_COUNTRY) . ":</b></td>";
        $bodymail.="<td style='border: 1px solid gray;'>" . showcountry($Country) . "</td></tr>";
        $bodymail.="</table>";

        mail2admin($subject, $bodymail);

        if ($_POST['question'] AND strpos($_POST['question'], " ") AND strlen($_POST['question']) > 10) {
            if (substr($config[64][1], 2, 1) == '1') {
                $form_subject = ucfirst(_QUESTION);
                $bodyticket = $_POST['question'];
                $packid = 0;
                $primerdepto = query("SELECT depid FROM hd_tickets_dep ORDER by orderby LIMIT 1", 1);
                include_once("ticketsfunc.php");
                openticket(
                        $_SESSION['hd_clientid'], $form_subject, $bodyticket, '1', 'medium', $packid, $primerdepto['depid'], '', '', '', ''
                );
            }
        }
        go2('welcome.php');
    }
} else {





    include("head_template_guest.php");

    echo '<div class="container">
        <div class="row">';

    print errors($errors);
    $errors = '';

    print("<form action='signup.php' method='POST' class='form-horizontal'>");

    print("<table class='table table-bordered'><tr><td width='50%' valign='top'>");

    echo '<div class="control-group '.$estilofirst.'">';
    echo '<label class="control-label" for="First">' . ucfirst(_FIRST_NAME) . '</label>';
    echo '<div class="controls">';
    print("<input type='text' name='First' id='First' value='" . $_POST['First'] . "'>");
    echo '</div>';
    echo '</div>';

    echo '<div class="control-group '.$estilolast.'">';
    echo '<label class="control-label" for="Last">' . ucfirst(_LAST_NAME) . '</label>';
    echo '<div class="controls">';
    print("<input type='text' name='Last' id='Last' value='" . $_POST['Last'] . "'>");
    echo '</div>';
    echo '</div>';

    echo '<div class="control-group">';
    echo '<label class="control-label" for="Company">' . ucfirst(_COMPANY) . '</label>';
    echo '<div class="controls">';
    print("<input type='text' name='Company' value='" . $_POST['Company'] . "'>");
    echo '</div>';
    echo '</div>';


    echo '<div class="control-group">';
    echo '<label class="control-label" for="Country">' . ucfirst(_COUNTRY) . '</label>';
    echo '<div class="controls">';
    print("<select name='Country' onChange='this.form.submit()'>");
    $query = "SELECT * FROM hd_countries ORDER BY country";
    $countries = mysql_query($query, $db_link);
    //			print ("<option value=''>** ".ucfirst(_SELECT_AN_OPTION)." **</option>");
    while ($row = mysql_fetch_array($countries)) {
        if ($_POST['Country']) {
            print ("<option value='" . $row[id] . "' " . sele($_POST['Country'], $row[id]) . ">" . $row[country] . " (" . $row[countrycode] . ")</option>");
        } else {
            print ("<option value='" . $row[id] . "' " . sele($config[54][1], $row[id]) . ">" . $row[country] . " (" . $row[countrycode] . ")</option>");
        }
    };
    print("</select> ");
    if ($_POST['Country']) {
        bandera2($_POST['Country']);
    } else {
        bandera2($config[54][1]);
    }
    echo '</div>';
    echo '</div>';

    echo '<div class="control-group">';
    echo '<label class="control-label" for="State">' . ucfirst(_STATE) . '</label>';
    echo '<div class="controls">';
    print("<select name='State'>");
    if ($_POST['Country']) {
        $query = "SELECT * FROM hd_countries_states WHERE country='" . $_POST['Country'] . "' ORDER BY state";
    } else {
        $query = "SELECT * FROM hd_countries_states WHERE country='" . $config[54][1] . "' ORDER BY state";
    }
    $states = mysql_query($query, $db_link);
    while ($row = mysql_fetch_array($states)) {
        if ($_POST['State']) {
            print ("<option value='" . $row[id] . "' " . sele($_POST['State'], $row[id]) . ">" . $row[state] . "</option>");
        } else {
            print ("<option value='" . $row[id] . "'>" . $row[state] . "</option>");
        }
    };
    print("</select>");
    echo '</div>';
    echo '</div>';

    echo '<div class="control-group '.$estilocity.'">';
    echo '<label class="control-label" for="City">' . ucfirst(_CITY) . '</label>';
    echo '<div class="controls">';
    print("<input type='text' name='City' value='" . $_POST['City'] . "'>");
    echo '</div>';
    echo '</div>';

    echo '<div class="control-group '.$estiloaddress.'">';
    echo '<label class="control-label" for="Address">' . ucfirst(_ADDRESS) . '</label>';
    echo '<div class="controls">';
    print("<input type='text' name='Address' value='" . $_POST['Address'] . "'>");
    echo '</div>';
    echo '</div>';

    echo '<div class="control-group">';
    echo '<label class="control-label" for="Phone">' . ucfirst(_PHONE) . '</label>';
    echo '<div class="controls">';
    if ($_POST['Country']) {
        print("<input type='text' name='ccode' value='(" . showphonecode($_POST['Country']) . ")' ");
        print("readonly style='border: 1px solid gray; background: #CCCCCC;'>");
    } else {
        print("<input type='text' name='ccode' value='(" . showphonecode($config[54][1]) . ")' ");
        print("readonly style='border: 1px solid gray; background: #CCCCCC;'>");
    }
    print(" <input type='text' name='Phone' value='" . $_POST['Phone'] . "'>");
    echo '</div>';
    echo '</div>';

    echo '<div class="control-group '.$estiloemail.'">';
    echo '<label class="control-label" for="Email">' . ucfirst(_EMAIL) . '</label>';
    echo '<div class="controls">';
    print("<input type='text' name='Email' value='" . $_POST['Email'] . "'>");
    echo '</div>';
    echo '</div>';

    echo '<div class="control-group">';
    echo '<label class="control-label" for="Zip">' . ucfirst(_ZIP_CODE) . '</label>';
    echo '<div class="controls">';
    print("<input type='text' name='Zip' value='" . $_POST['Zip'] . "'>");
    echo '</div>';
    echo '</div>';

    echo '</td>';
    echo '<td>';

    echo '<div class="control-group '.$estilopassword.'">';
    $pass0 = $_POST['password'];
    echo '<label class="control-label" for="password">' . ucfirst(_PASSWORD) . '</label>';
    echo '<div class="controls">';
    print("<input type='password' name='password' value = '" . $pass0 . "'>");
    echo '</div>';
    echo '</div>';

    echo '<div class="control-group '.$estilopassword.'">';
    $pass0 = $_POST['password2'];
    echo '<label class="control-label" for="password2">' . ucfirst(_RE_ENTER_PASSWORD) . '</label>';
    echo '<div class="controls">';
    print("<input type='password' name='password2' value = '" . $pass0 . "'>");
    echo '</div>';
    echo '</div>';

    if ($config[41][1]) {
        echo '<div class="control-group">';
        echo '<label class="control-label" for="Personalized_1">' . $config[41][1] . '</label>';
        echo '<div class="controls">';
        print("<input type='text' name='Personalized_1' value='" . $_POST['Personalized_1'] . "'>");
        echo '</div>';
        echo '</div>';
    }
    if ($config[42][1]) {
        echo '<div class="control-group">';
        echo '<label class="control-label" for="Personalized_2">' . $config[42][1] . '</label>';
        echo '<div class="controls">';
        print("<input type='text' name='Personalized_2' value='" . $_POST['Personalized_2'] . "'>");
        echo '</div>';
        echo '</div>';
    }
    if ($config[43][1]) {
        echo '<div class="control-group">';
        echo '<label class="control-label" for="Personalized_3">' . $config[43][1] . '</label>';
        echo '<div class="controls">';
        print("<input type='text' name='Personalized_3' value='" . $_POST['Personalized_3'] . "'>");
        echo '</div>';
        echo '</div>';
    }
    if ($config[44][1]) {
        echo '<div class="control-group">';
        echo '<label class="control-label" for="Personalized_4">' . $config[44][1] . '</label>';
        echo '<div class="controls">';
        print("<input type='text' name='Personalized_4' value='" . $_POST['Personalized_4'] . "'>");
        echo '</div>';
        echo '</div>';
    }
    if ($config[45][1]) {
        echo '<div class="control-group">';
        echo '<label class="control-label" for="Personalized_5">' . $config[45][1] . '</label>';
        echo '<div class="controls">';
        print("<input type='text' name='Personalized_5' value='" . $_POST['Personalized_5'] . "'>");
        echo '</div>';
        echo '</div>';
    }

    echo '<div class="control-group">';
    echo '<label class="control-label" for="Preferredlang">' . ucfirst(_PREFERRED_LANGUAGE) . '</label>';
    echo '<div class="controls">';
    print("<select name='Preferredlang''>");
    $query = "SHOW COLUMNS FROM hd_languages";
    $preflangs = mysql_query($query, $db_link);
    while ($rowpf = mysql_fetch_assoc($preflangs)) {
        if ($rowpf[Field] <> 'original') {
            if ($_POST['Preferredlang']) {
                print ("<option value='" . $rowpf[Field] . "' " . sele($_POST['Preferredlang'], $rowpf[Field]) . ">" . langname($rowpf[Field]) . " (" . $rowpf[Field] . ")</option>");
            } else {
                print ("<option value='" . $rowpf[Field] . "' " . sele($currentlang, $rowpf[Field]) . ">" . langname($rowpf[Field]) . " (" . $rowpf[Field] . ")</option>");
            }
        }
    };
    print("</select> ");
    echo '</div>';
    echo '</div>';

    if ($config[79][1] == 1) {
        echo '<div class="control-group">';
        echo '<label class="control-label" for="captcha_code">' . ucfirst(_TYPE_THE_FOLLOWING_CODE) . '</label>';
        echo '<div class="controls">';
        print("
                <img id='captcha' src='securimage/securimage_show.php' alt='CAPTCHA Image' />
                <input type='text' name='captcha_code' />
                <a href='#' onclick=\"document.getElementById('captcha').src = 'securimage/securimage_show.php?' + Math.random(); return false\"><br>" . ucfirst(_RELOAD_IMAGE) . "</a>
		");
        echo '</div>';
        echo '</div>';
    }

    echo '<div class="control-group">';
    echo '<label class="control-label" for="PromotionalMailing">' . ucfirst(_I_WANT_TO_RECEIVE_PROMOTIONAL_MAILINGS) . '</label>';
    echo '<div class="controls">';
    print("<input type='checkbox' name='PromotionalMailing' checked>");
    echo '</div>';
    echo '</div>';

    echo '<div class="control-group">';
    echo '<div class="controls">';
    print("<input type='submit' name='send' value='" . _SEND . "'>");
    echo '</div>';
    echo '</div>';

    echo '<div class="control-group">';
    echo '<div class="controls">';
    print("<input type='reset' name='Reset' value='" . _RESET . "'>");
    echo '</div>';
    echo '</div>';

    
    echo '</td>';
    echo '</tr>';
    print("</table>");

    print("</form>");


    echo '</div>';
    echo '</div>';
    include("foot_template.php");
}
?>