<?php
include("postget.php");
include_once("common.php");
$pagetitle = _DATA_OF_THE_CLIENT;
checklevel(1,$_REQUEST['clientid']);



if ($_REQUEST[send]==""._EDIT."") {
	$estiloerror = "class='formfielderror'";
	$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 ($_POST['password']=='') {$errors.= "<br>".ucfirst(_PASSWORD).": "._NOT_SPECIFIED.""; $estilopassword=$estiloerror;}
	if (!preg_match ('/'.$regex4mail.'/i', $_POST['Email'])) {$errors.= "<br>".ucfirst(_INVALID_EMAIL).""; $estiloemail=$estiloerror;}

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

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

	mysql_query("INSERT INTO hd_clients_history SELECT *  FROM hd_clients WHERE clientid='".$_SESSION['hd_clientid']."'",$db_link);

	$now = time()+$config[31][1]*3600;

	$thisclient=1;

	$_POST['First'] = ucwords(strtolower($_POST['First']));
	$_POST['Last'] = ucwords(strtolower($_POST['Last']));


	if($reseller=="on"){$reseller=1;}else{$reseller=0;}
	if($_POST['PromotionalMailing']=="on"){$PromotionalMailing=1;}else{$PromotionalMailing=0;}

	$query="UPDATE hd_clients SET ";
	$query.="first='".mysql_real_escape_string($_POST['First'])."', ";
	$query.="last='".mysql_real_escape_string($_POST['Last'])."', ";
	$query.="company='".mysql_real_escape_string($_POST['Company'])."', ";
	if ($Country){
		$query.="country='".mysql_real_escape_string($_POST['Country'])."', ";
	}
	if ($State){
		$query.="state='".mysql_real_escape_string($_POST['State'])."', ";
	}
	$query.="city='".mysql_real_escape_string($_POST['City'])."', ";
	$query.="address='".mysql_real_escape_string($_POST['Address'])."', ";
	$query.="phone='".mysql_real_escape_string($_POST['Phone'])."', ";
	
if ($config[56][1]==0){
	$query.="email='".mysql_real_escape_string($_POST['Email'])."', ";
	$query.="password='".mysql_real_escape_string(encrypt($_POST['password']))."', ";
}
	$query.="zip='".mysql_real_escape_string($_POST['Zip'])."', ";
	$query.="clientalias='".mysql_real_escape_string($_POST['ClientAlias'])."', ";
	$query.="personalized_1='".mysql_real_escape_string($_POST['Personalized_1'])."', ";
	$query.="personalized_2='".mysql_real_escape_string($_POST['Personalized_2'])."', ";
	$query.="personalized_3='".mysql_real_escape_string($_POST['Personalized_3'])."', ";
	$query.="personalized_4='".mysql_real_escape_string($_POST['Personalized_4'])."', ";
	$query.="personalized_5='".mysql_real_escape_string($_POST['Personalized_5'])."', ";
	$query.="comments='".strip_tags(nl2br($_POST['comments']))."', ";

	$query.="modified='$now', ";

	$query.="preflang='$Preferredlang', ";

	$query.="promotionalmailing = '$PromotionalMailing', ";
	
	$query = substr ($query, 0, - 2);   // quitar la ultima coma y el espacio

	$query.=" WHERE clientid='".$_SESSION['hd_clientid']."'";

	if(!mysql_query($query,$db_link))	{
		print("$query<br>");
		print("".mysql_error().""); exit;
	}else{
		logevent(""._CLIENT." ".$_SESSION['hd_clientid']." "._EDITED."",$_SESSION['hd_clientid'],$_SESSION['hd_idc']);
		if(secure(4)){
			go2('clientdata2.php?clientid='.$_SESSION['hd_clientid']);
		}else{
			go2('clientdata2.php');
		}
		exit();
	}






}else{






	$thisclient=1;

	include("head_template.php");

	$q="SELECT * FROM hd_clients WHERE clientid=".$_SESSION['hd_clientid']."";
	$result=mysql_query($q,$db_link);
	$row = mysql_fetch_array($result);

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

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


	if($_SESSION['hd_accesslvl']==1) {
		$readonly=" readonly style='border: 1px solid red; background: #FFB380;'";
		$disabled = " disabled ";
		$color=" style='border: 1px solid green; background: #BFFFBF;'";
	}else{
		$readonly=" style='border: 1px solid red; background: #00CC00;'";
		$disabled = " style='border: 1px solid red; background: #00CC00;'";
		$color=" style='border: 1px solid green; background: #BFFFBF;'";
	}

	$valores = query("SELECT valor FROM hd_config WHERE ref='8'",1);
	$aValores = explode(' ',$valores[0]);


	print("<form method='POST'>");

	print("<table width='100%' border='0'><tr><td width='50%' valign='top'>");

	print("<table cellspacing='4' cellpadding='2' border='0' width='100%'>");

	print("<tr>");
	print("<td nowrap><b>".ucfirst(_FIRST_NAME).":</b></td>");
	print("<td><input $estilofirst type='text' name='First' size='50' value='".$row[first]."' ");
	print((in_array('first:on',$aValores)? $color : $readonly ));
	print("></td></tr>");

	print("<tr>");
	print("<td nowrap><b>".ucfirst(_LAST_NAME).":</b></td>");
	print("<td><input $estilolast type='text' name='Last' size='50' value='".$row[last]."' ");
	print((in_array('last:on',$aValores)? $color : $readonly ));
	print("></td></tr>");

	print("<tr>");
	print("<td nowrap>".ucfirst(_COMPANY).":</td>");
	print("<td><input type='text' name='Company' size='50' value='".$row[company]."' ");
	print((in_array('company:on',$aValores)? $color : $readonly ));
	print("></td></tr>");

	print("<tr>");
	print("<td nowrap><b>".ucfirst(_COUNTRY).":</b></td>");
	print("<td>");
	print("<select name='Country' onChange='this.form.submit()'");
	print((in_array('country:on',$aValores)? $color : $disabled ));
	print(">");
	$query="SELECT * FROM hd_countries ORDER BY country";
	$countries=mysql_query($query,$db_link);
	while($rowC=mysql_fetch_array($countries)) {
		if ($_POST['Country']) {
			print ("<option value='".$rowC[id]."' ".sele($_POST['Country'],$rowC[id]).">".$rowC[country]." (".$rowC[countrycode].")</option>");
		}else{
			print ("<option value='".$rowC[id]."' ".sele($row[country],$rowC[id]).">".$rowC[country]." (".$rowC[countrycode].")</option>");
		}
	};
	print("</select> ");
	if ($_POST['Country']) {
		bandera2($_POST['Country']);
	}else{
		bandera2($row[country]);
	}
	print("</td>");
	print("</tr>");



	print("<tr>");
	print("<td nowrap><b>".ucfirst(_STATE).":</b></td>");
	print("<td>");
	print("<select name='State' ");
	print((in_array('state:on',$aValores)? $color : $disabled ));
	print(">");
	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='".$row[country]."' ORDER BY state";
	}
	$states=mysql_query($query,$db_link);
	while($rowS=mysql_fetch_array($states)) {
		if ($_POST['State']) {
			print ("<option value='".$rowS[id]."' ".sele($_POST['State'],$rowS[id]).">".$rowS[state]."</option>");
		}else{
			print ("<option value='".$rowS[id]."' ".sele($row[state],$rowS[id]).">".$rowS[state]."</option>");
		}
	};
	print("</select>");

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



	print("<tr>");
	print("<td nowrap><b>".ucfirst(_CITY).":</b></td>");
	print("<td><input $estilocity type='text' name='City' size='50' value='".$row[city]."' ");
	print((in_array('city:on',$aValores)? $color : $readonly ));
	print("></td></tr>");


	print("<tr>");
	print("<td nowrap><b>".ucfirst(_ADDRESS).":</b></td>");
	print("<td><input $estiloaddress type='text' name='Address' size='50' value='".$row[address]."' ");
	print((in_array('address:on',$aValores)? $color : $readonly ));
	print("></td></tr>");

	print("<tr>");
	print("<td nowrap><b>".ucfirst(_PHONE).":</b></td>");
	print("<td>");
	if ($_POST['Country']) {
		print("<input type='text' name='ccode' size='3' value='(".showphonecode($_POST['Country']).")' ");
		print("readonly style='border: 1px solid green; background: #CCCCCC;'>");
	}else{
		print("<input type='text' name='ccode' size='3' value='(".showphonecode($row[country]).")' ");
		print("readonly style='border: 1px solid green; background: #CCCCCC;'>");
	}
	print(" <input $estilophone type='text' name='Phone' size='40' value='".$row[phone]."' ");
	print((in_array('phone:on',$aValores)? $color : $readonly ));
	print("></td></tr>");

	print("<tr>");
	print("<td nowrap><b>".ucfirst(_EMAIL).":</b></td>");
	print("<td><input $estiloemail type='text' name='Email' size='50' value='".$row[email]."' ");
	print((in_array('email:on',$aValores)? $color : $readonly ));
	print("></td></tr>");

	print("<tr>");
	print("<td nowrap>".ucfirst(_ZIP_CODE).":</td>");
	print("<td><input type='text' name='Zip' size='50' value='".$row[zip]."' ");
	print((in_array('zip:on',$aValores)? $color : $readonly ));
	print("></td></tr>");

	print("<tr>");
	print("<td nowrap>".ucfirst(_ALIAS).":</td>");
	print("<td><input type='text' name='ClientAlias' size='50' value='".$row[clientalias]."' ");
	print((in_array('clientalias:on',$aValores)? $color : $readonly ));
	print("></td></tr>");

	print("</table>");




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


	print("<table cellspacing='4' cellpadding='2' border='0' width='100%'>");


	print("<tr>");
	print("<td nowrap><b>".ucfirst(_PASSWORD).":</b></td>");
	print("<td><input $estilopassword class='password' type='password' name='password' size='50' value='".decrypt($row[password])."' ");
	print((in_array('password:on',$aValores)? $color : $readonly ));
	print("></td></tr>");


	if ($config[41][1]) {
		print("<tr> ");
		print("<td>". $config[41][1].":</td>");
		print("<td><input type='text' name='Personalized_1' size='35' value='".$row[personalized_1]."' ");
		print((in_array('personalized_1:on',$aValores)? $color : $readonly ));
		print("></td></tr>");
	}
	if ($config[42][1]) {
		print("<tr> ");
		print("<td>". $config[42][1].":</td>");
		print("<td><input type='text' name='Personalized_2' size='35' value='".$row[personalized_2]."' ");
		print((in_array('personalized_2:on',$aValores)? $color : $readonly ));
		print("></td></tr>");
	}
	if ($config[43][1]) {
		print("<tr> ");
		print("<td>". $config[43][1].":</td>");
		print("<td><input type='text' name='Personalized_3' size='35' value='".$row[personalized_3]."' ");
		print((in_array('personalized_3:on',$aValores)? $color : $readonly ));
		print("></td></tr>");
	}
	if ($config[44][1]) {
		print("<tr> ");
		print("<td>". $config[44][1].":</td>");
		print("<td><input type='text' name='Personalized_4' size='35' value='".$row[personalized_4]."' ");
		print((in_array('personalized_4:on',$aValores)? $color : $readonly ));
		print("></td></tr>");
	}
	if ($config[45][1]) {
		print("<tr> ");
		print("<td>". $config[45][1].":</td>");
		print("<td><input $estilocedula type='text' name='Personalized_5' size='35' value='".$row[personalized_5]."' ");
		print((in_array('personalized_5:on',$aValores)? $color : $readonly ));
		print(">");
		if ($row[country]=='230'
		AND ($config[45][1]=='Documento' OR $config[45][1]=='Cedula de identidad')
		AND $row[personalized_5]<>'' ) {
			if (checkciuy($row[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></tr>");
	}


	print("<tr>");
	print("<td nowrap>".ucfirst(_PREFERRED_LANGUAGE).":</td>");
	print("<td>");
	print("<select name='Preferredlang' ");
	print($color);
	print(">");

	$query="SHOW COLUMNS FROM hd_languages";
	$preflangs=mysql_query($query,$db_link);
	while($rowpf=mysql_fetch_assoc($preflangs)) {
		if ($rowpf[Field]<>'original'){
			print ("<option value='".$rowpf[Field]."' ".sele($row['preflang'],$rowpf[Field]).">".langname($rowpf[Field])." (".$rowpf[Field].")</option>");
		}
	};
	print("</select> ");
	print("</td>");
	print("</tr>");


	print("<tr>");
	print("<td nowrap><b>".ucfirst(_I_WANT_TO_RECEIVE_PROMOTIONAL_MAILINGS)."</b></td>");
	if($row['promotionalmailing']){
		print("<td><input type='checkbox' name='PromotionalMailing' checked></td>");
	}else{
		print("<td><input type='checkbox' name='PromotionalMailing'></td>");
	}
	print("</tr>");



	if ($_SESSION['hd_accesslvl']>1) {
		$status = substr($row['comments'],0,1);
		if ($status == '0' OR $status == '1' OR $status == '2') {
			$comenzaren = 1;
		}else{
			$comenzaren = 0;
		}

		print("<tr> ");
		print("<td>"._INTERNAL_NOTE."</b><br>("._NOT_VISIBLE_TO_THE_CLIENT."):</td>");
		print("<td><textarea name='comments' cols='40' rows='8'");
		print((in_array('comments:on',$aValores)? $color : $readonly ));
		print(">".substr($row['comments'],$comenzaren)."</textarea>");
		print("</td></tr>");
	}

	print("<tr>");
	print("<td><input type='submit' name='send' value='"._EDIT."'>&nbsp;&nbsp;&nbsp;");
	print("<input type='reset' name='Reset' value='"._RESET."' class='btnformyellow'></td>");
	print("</tr>");

	print("</table>");

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


	print("</form>");

	if ($_SESSION['hd_accesslvl']>1) {
		print("<table cellpadding='3' cellspacing='14' border='0'>");
		print("<tr>");
		print("<td $color>".ucfirst(_EDITABLE_FIELDS_BY_THE_CLIENT)." <a href='preferences.php?option=4'><img class='sprites edit' src='images/transparent.gif' border='0' title='".ucfirst(_CHANGE)."'></a></td>");
		print("<td $readonly>".ucfirst(_NON_EDITABLE_FIELDS_BY_THE_CLIENT)." <a href='preferences.php?option=4'><img class='sprites edit' src='images/transparent.gif' border='0' title='".ucfirst(_CHANGE)."'></a></td>");
		print("</tr>");
		print("</table>");
	}

echo '
</div>
</div>
';

	include("foot_template.php");
}
?>