<?php
include("postget.php");
include_once("common.php");
$pagetitle = _SIGNUP;

if ($config[120][1] == 0) {go2('signup.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 = "class='formfielderror'";
	$errors = '';
	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')
	AND $_POST['Personalized_5']<>'' ) {
		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."") {

	$PromotionalMailing=1;
	$fechahora = time()+$config[31][1]*3600;
	$reseller = 0;
	$ClientID = generate_id('hd_clients','clientid',$config[101][1]);

	$query="INSERT INTO hd_clients (clientid, country, state, email, password, start, personalized_1, personalized_2, personalized_3, personalized_4, personalized_5, preflang, promotionalmailing) ";

	$query.="VALUES(
	'$ClientID',
	'".mysql_real_escape_string($_POST['Country'])."',
	'".mysql_real_escape_string($_POST['State'])."',
	'".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(_EMAIL).":</b></td>";
		$bodymail.="<td style='border: 1px solid gray;'>".$Email."</td></tr>";
		$bodymail.="</table>";

		mail2admin($subject,$bodymail) ;

		go2('welcome.php');
	}





}else{





	include("head_template_guest.php");
	
    echo '<div class="container">
        <div class="row">';
    
	print errors($errors); $errors = '';
	print("<table class='table table-bordered'>");

	print("<form action='signup2.php' method='POST'>");


	print("<table class='table'>
            <tr><td>");

	print("<table class='table'>");


	print("<tr>");
	print("<td nowrap><b>".ucfirst(_COUNTRY).":</b></td>");
	print("<td>");
	print("<select $estilocountry 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]);
	}
	print("</td>");
	print("</tr>");

	print("<tr>");
	print("<td nowrap><b>".ucfirst(_STATE).":</b></td>");
	print("<td>");
	print("<select $estilostate 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);
	//			print ("<option value=''>** ".ucfirst(_SELECT_AN_OPTION)." **</option>");
	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>");

	print("</td>");
	print("</tr>");



	print("<tr> ");
	print("<td nowrap><b>".ucfirst(_EMAIL)."</b>:</td>");
	print("<td><input $estiloemail type='text' name='Email' size='40' value='".$_POST['Email']."'></td>");
	print("</tr>");

	
	$pass0 = $_POST['password'];
	print("<tr>");
	print("<td nowrap><b>".ucfirst(_PASSWORD).":</b></td>");
	print("<td><input $estilopassword type='password' name='password' value = '".$pass0."' size='40'></td>");
	print("</tr>");

	$pass0 = $_POST['password2'];
	print("<tr>");
	print("<td nowrap><b>".ucfirst(_RE_ENTER_PASSWORD).":</b></td>");
	print("<td><input $estilopassword type='password' name='password2' value = '".$pass0."' size='40'></td>");
	print("</tr>");

	print("</table>");

	print("</td><td valign='top'>");

	print("<table class='table'>");


	if ($config[41][1]) {
		print("<tr> ");
		print("<td>". $config[41][1].":</td>");
		print("<td><input type='text' name='Personalized_1' size='50' value='".$_POST['Personalized_1']."'></td>");
		print("</tr>");
	}
	if ($config[42][1]) {
		print("<tr> ");
		print("<td>". $config[42][1].":</td>");
		print("<td><input type='text' name='Personalized_2' size='50' value='".$_POST['Personalized_2']."'></td>");
		print("</tr>");
	}
	if ($config[43][1]) {
		print("<tr> ");
		print("<td>". $config[43][1].":</td>");
		print("<td><input type='text' name='Personalized_3' size='50' value='".$_POST['Personalized_3']."'></td>");
		print("</tr>");
	}
	if ($config[44][1]) {
		print("<tr> ");
		print("<td>". $config[44][1].":</td>");
		print("<td><input type='text' name='Personalized_4' size='50' value='".$_POST['Personalized_4']."'></td>");
		print("</tr>");
	}
	if ($config[45][1]) {
		print("<tr> ");
		print("<td>". $config[45][1].":</td>");
		print("<td><input $estilocedula type='text' name='Personalized_5' size='50' value='".$_POST['Personalized_5']."'>");
		if ($_POST['Country']=='230'
		AND ($config[45][1]=='Documento' OR $config[45][1]=='Cedula de identidad')
		AND $_POST['Personalized_5']<>'' ) {
			if (checkciuy($_POST['Personalized_5'])) {
				echo " <img class='sprites green_status' src='images/transparent.gif'";
			}else{
				echo " <img class='sprites red_status' src='images/transparent.gif'";
			}
		}
		print("</td>");
		print("</tr>");
	}


	if ($config[79][1]==1) {
		print("
		<tr>
				<td align='left'><b>".ucfirst(_TYPE_THE_FOLLOWING_CODE)."</b><br>
				<img id='captcha' src='securimage/securimage_show.php' alt='CAPTCHA Image' />
				</td>
				<td>
				<input $estilocaptcha type='text' name='captcha_code' size='20' maxlength='20' />
				<a href='#' onclick=\"document.getElementById('captcha').src = 'securimage/securimage_show.php?' + Math.random(); return false\">".ucfirst(_RELOAD_IMAGE)."</a>
				</td>
		</tr>");
	}
        
        
	print("<tr>");
	print("<td>
	<input type='hidden' name='Preferredlang' value='".$currentlang."'>
        </td><td>
	<input type='submit' name='send' value='"._SEND."' class='btn'>");
	print(" <input type='reset' name='Reset' value='"._RESET."' class='btn'></td>");
	print("</tr>");
        
	print("</table>");

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

	print("</form>");


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