<?php

ini_set('include_path','../../:../lib/:../../lib/:../tpl/ajax/');
require '../../config.inc';
require '../../urls.inc';
require '../../lib/phpmailer/class.phpmailer.php';

//ini_set('include_path','../../;../../lib/');
use_helper('UtilFunctions'); 

//obteniendo el carrito
//identifying section, module, language and parameters
$_POST['pais'] = 'peru';
$_POST['region'] = 'lima';
$_POST['lng'] = 'es';

$location = $_SESSION['location1'] = $_POST['pais'].'-'.$_POST['region'].'-'.$_POST['lng'];
//actualizar las urls
$_SESSION['lng'] = $lng;
$base_url = $_SESSION['base_url'] = $lng.'/'.$_POST['pais'].'/'.$_POST['region'].'/';
foreach($urls as $i=>$sections){
	foreach($sections as $j=>$modules){
		foreach($modules as $key => $idiomas){
			$urls[$i][$j][$key] = $base_url.$idiomas;
		}
	}
}


$lng ='es';

require '../../lng/'.$lng.'/'.$lng.'.lng';

if(isset($_POST)){

	
	if(!isset($_POST['operacion']))	exit("NOOOOO");
	

	
	switch ($_POST['operacion']){
		case "sendContactoRapido": 
				//-Envio automatico de mail para el usuario final
				
				
				$mailer = new PHPMailer();
				
				
				function cleanMessage($str){ return htmlentities(strip_tags($str),ENT_QUOTES,'UTF-8'); }
				
				
				$inputAreaFooter = $_POST['inputAreaFooter'];
				
				switch ($inputAreaFooter){
					case '1': 
							$fromName = "Portal Lansier - Recursos Humanos";
							$from = "icalderon@lansier.com";
						
							break;
							
					case '2': 
							$fromName = "Portal Lansier - Marketing";
							$from = "cflorez@lansier.com";
							
							break;
					case '3': 
							$fromName = "Portal Lansier- Ventas";
							$from = "yyesang@lansier.com";
							$mailer->AddAddress('panhuaman@lansier.com',MAIL_RECEIVER_NAME);
							$mailer->AddAddress('jarevalo@lansier.com',MAIL_RECEIVER_NAME);
							
							break;
					case '4': 
							$fromName = "Portal Lansier- Ventas Institucionales";
							$from = "lcastillo@lansier.com";
							$mailer->AddAddress('sarevalo@lansier.com',MAIL_RECEIVER_NAME);
							break;
							
					case '5': 
							$fromName = "Portal Lansier- Importaciones";
							$from = "ysilva@lansier.com";
							
							
							break;
				
					case '6': 
					        
							$fromName = "Portal Lansier- Libertad Visual";
							$from = "rlopez@lansier.com";
							$mailer->AddAddress('cflorez@lansier.com',MAIL_RECEIVER_NAME);
							
							break;
					
					case '7': 
					        
							$fromName = "Portal Lansier- Informacion sobre nuestros productos";
							$from = "oftalmologovirtual@lansier.com";
							$mailer->AddAddress('cflorez@laniser.com',MAIL_RECEIVER_NAME);
							
							break;
					
					case '8': 
					        
							$fromName = "Portal Lansier- Negocios Internacionales";
							$from = "agamboam@lansier.com";
							$mailer->AddAddress('sormeno@lansier.com',MAIL_RECEIVER_NAME);
							
							break;
					
					case '9': 
							$fromName = "Portal Lansier - FarmacoVigilancia";
							$from = "flopez@lansier.com";
						    $mailer->AddAddress('rorihuela@lansier.com',MAIL_RECEIVER_NAME);
							$mailer->AddAddress('flopez@lansier.com',MAIL_RECEIVER_NAME);
							
							break;
						
				}
				
				
				
				$from = "info@lansier.com";
				
				$mailer->From 		= $from;
				$mailer->FromName 	= "";
				$mailer->Subject 	= "Portal Lansier :: Contácto.";
				
				$body .= '<b>'._FOOT_CONT_NAME.'</b>: '.cleanMessage($_POST['inputNameFooter']).'<br/>';
				$body .= '<b>'._FOOT_CONT_EMAIL.'</b>: '.cleanMessage($_POST['inputEmailFooter']).'<br/>';
				$body .= '<b>'._FOOT_CONT_MENSAJE.'</b>: '.cleanMessage($_POST['inputMensaje']).'<br/>';
				$body .= '<hr/>Enviado:<b> '.date("F j, Y g:i a").'</b>';
				$replace = array('this'=>array('<br/>','<hr/>'),'whit'=>array("\n","\n----------------------------------------------\n"));
				$altbody = strip_tags(str_replace($replace['this'],$replace['whit'],$body));
				$mailer->AddAddress($from,MAIL_RECEIVER_NAME);
				$mailer->Body 				= $body;
				$mailer->AltBody 			= $altbody;
				
				
				$mailer->AddBCC('msotomayor@dataexe.com',MAIL_RECEIVER_NAME);
				
				
				if(!@$mailer->Send()) echo '0'; else 
				echo '1';
				
				
				break;
		case "sendContacto": 
				
				$servicio = $_POST['servicio'];
				switch ($servicio){
					case 's_bps': 
							$texto = "Servicio - BPS Outsourcing";
													
							break;
							
					case 's_informes_especiales': 
							$texto = "Servicio - Informes especiales";
							
							break;
					case 's_consultoria_tributaria': 
							$texto = "Servicio - Consultoría Tributaria Laboral y Jurídica";
							
							break;
					case 's_consultoria_negocios': 
							$texto = "Servicio - Consultoría de Negocios";
							break;
							
				}
				
				$mailer = new PHPMailer();
				
				
				function cleanMessage($str){ return htmlentities(strip_tags($str),ENT_QUOTES,'UTF-8'); }
				
				
				$from = "info@gisconsac.com";
				
				$mailer->From 		= $from;
				$mailer->FromName 	= "GISCON info";
				$mailer->Subject 	= "GISCON SAC :: Contácto :: ".$texto;
				
				$body .= '<b>'._FOOT_CONT_NAME.'</b>: '.cleanMessage($_POST['nombrecompleto']).'<br/>';
				$body .= '<b>'._FOOT_CONT_EMAIL.'</b>: '.cleanMessage($_POST['email']).'<br/>';
				$body .= '<b>'._CONTACTO_PHONE.'</b>: '.cleanMessage($_POST['telefono']).'<br/>';
				if($_POST['asunto']){
					switch ($servicio){
						case '1': $body .= '<b>Asunto</b>:Plan Empresario <br/>';break;
						case '2': $body .= '<b>Asunto</b>:Plan PYME <br/>';break;
						case '3': $body .= '<b>Asunto</b>:Plan Corporativo <br/>';break;
					}
				}
				$body .= '<b>'._FOOT_CONT_MENSAJE.'</b>: '.cleanMessage($_POST['mensaje']).'<br/>';
				$body .= '<hr/>Enviado:<b> '.date("F j, Y g:i a").'</b>';
				$replace = array('this'=>array('<br/>','<hr/>'),'whit'=>array("\n","\n----------------------------------------------\n"));
				$altbody = strip_tags(str_replace($replace['this'],$replace['whit'],$body));
		
				$mailer->Body 				= $body;
				$mailer->AltBody 			= $altbody;
				$mailer->AddAddress("info@gisconsac.com",MAIL_RECEIVER_NAME);
				$mailer->AddBCC('msotomayor@dataexe.com',MAIL_RECEIVER_NAME);
				
				
				if(!@$mailer->Send()) echo '0'; else 
				echo '1';	
				break;
		case "sendSugerencias": 
				$mailer = new PHPMailer();
				
				
				function cleanMessage($str){ return htmlentities(strip_tags($str),ENT_QUOTES,'UTF-8'); }
				
				
							
				$mailer->From 		= $from;
				$mailer->FromName 	= "";
				$mailer->Subject 	= "Portal Lansier :: Sugerencia.";
				
				$body .= '<b>'._FOOT_CONT_NAME.'</b>: '.cleanMessage($_POST['NombresSugerencia']).'<br/>';
				$body .= '<b>'._FOOT_CONT_EMAIL.'</b>: '.cleanMessage($_POST['EmailSugerencia']).'<br/>';
				$body .= '<b>'._CONTACTO_PHONE.'</b>: '.cleanMessage($_POST['TelefonoSugerencia']).'<br/>';
				$body .= '<b>'._CONTACTO_CIUDAD.'</b>: '.cleanMessage($_POST['CiudadSugerencia']).'<br/>';
				$body .= '<b>'._FOOT_CONT_MENSAJE.'</b>: '.cleanMessage($_POST['MensajeSugerencia']).'<br/>';
				if($_POST['BoletinSugerencia']) $body .= '<b>*</b>: '._CONT_MENSAJE_BOLETIN.'<br/>';
				$body .= '<hr/>Enviado:<b> '.date("F j, Y g:i a").'</b>';
				$replace = array('this'=>array('<br/>','<hr/>'),'whit'=>array("\n","\n----------------------------------------------\n"));
				$altbody = strip_tags(str_replace($replace['this'],$replace['whit'],$body));
		
				$mailer->Body 				= $body;
				$mailer->AltBody 			= $altbody;
				$mailer->AddAddress("oftalmologovirtual@lansier.com ",MAIL_RECEIVER_NAME);
				$mailer->AddAddress("info@lansier.com",MAIL_RECEIVER_NAME);
				$mailer->AddBCC('msotomayor@dataexe.com',MAIL_RECEIVER_NAME);
				
				
				if(!@$mailer->Send()) echo '0'; else 
				echo '1';		
				break;
		case "registrarCuenta": 
					
					
						$userDP = new MnuUserDP();
						if(empty($_POST['novedades'])) {
							$_POST['novedades']='0';
						} else {
							$_POST['novedades']='1';
						}
						$userDP->setUserNombres($_POST['nombres']);
						$userDP->setUserApellidos($_POST['apellidos']);
						$userDP->setUserName($_POST['nombres']." ".$_POST['apellidos']);
						$userDP->setDireccion($_POST['direccion']);
						$userDP->setUserSexo($_POST['sexo']);
						$temp = $_POST['nacimiento'];
						$temp=explode('/',$temp);
						if($temp[0] > 31){	
							$_POST['nacimiento']= $temp[2].'-'.$temp[1].'-'.$temp[0];
						}
						$userDP->setFechanacimiento($temp[2].'-'.$temp[1].'-'.$temp[0]);
						$userDP->setCreatedDate(date('Y-m-d h:i:s'));
						$userDP->setUserTelefono($_POST['telefono']);
						$userDP->setFlagnovedades($_POST['novedades']);
						$userDP->setPais($_POST['pais']);
						$userDP->setUserId($_POST['email']);
						$userDP->setUserPassword(md5($_POST['password']));
						$userDP->setRoleId('sys.visitante');//Visitante libre de portal
						
						
						if($userDP->checkMail()){
							$add_js = array(array('text'=>'var emailduplicado=true; var duplicado = "'._ALERTA_MAIL_DUPLICADO.'";	var errores = "'._ALERTA_FORM_ERRORES.'";'));
						} else {
							$add_js = array(array('text'=>'var emailduplicado=false; var duplicado = "'._ALERTA_MAIL_DUPLICADO.'";	var errores = "'._ALERTA_FORM_ERRORES.'";'));
							if($userDP->save()){
								$mail = $userDP->sendMail("../../lng/".$lng.'/mail_Registro.tpl',$_POST['password']);
								$_SESSION['user'] = array(
									'id' 			=> $_POST['email'],
									'nombres' 		=> $_POST['nombres'],
									'apellidos' 	=> $_POST['apellidos'],
									'direccion' 	=> $_POST['direccion'],
									'email' 		=> $_POST['email'],
									'sexo'			=> $_POST['sexo'],
									'novedades' 	=> $_POST['novedades'],
									'nacimiento' 	=> $_POST['nacimiento'],
									'pais' 			=> $_POST['pais'],
									'telefono' 		=> $_POST['telefono'],
									'registro'		=>1
								);
						
								//$user->insertEmail($db->insert_id,$_POST['novedades']);	
								echo '/'.$urls['usuario']['gracias'][$lng];
							}
						}
						//
							
					break;
						
		case "editarCuenta": 
						$userDP = new MnuUserDP();
						if(empty($_POST['novedades'])) {
							$_POST['novedades']='0';
						} else {
							$_POST['novedades']='1';
						}
						$userDP->setUserNombres($_POST['nombres']);
						$userDP->setUserApellidos($_POST['apellidos']);
						$userDP->setUserName($_POST['nombres']." ".$_POST['apellidos']);
						$userDP->setDireccion($_POST['direccion']);
						$userDP->setUserSexo($_POST['sexo']);
						$temp = $_POST['nacimiento'];
						$temp=explode('-',$temp);
						if($temp[0] > 31){	
							$_POST['nacimiento']= $temp[2].'-'.$temp[1].'-'.$temp[0];
						}
						$nacimiento = $temp[2].'-'.$temp[1].'-'.$temp[0];
						$userDP->setFechanacimiento($nacimiento);
						$userDP->setCreatedDate(date('Y-m-d h:i:s'));
						$userDP->setUserTelefono($_POST['telefono']);
						$userDP->setFlagnovedades($_POST['novedades']);
						$userDP->setPais($_POST['pais']);
						$userDP->setUserId($_POST['email']);
						if($userDP->update()){
							//$mail = $userDP->sendMail("../../lng/".$lng.'/mail_Registro.tpl',$_POST['password']);
							$_SESSION['user'] = array(
								'id' 			=> $_POST['email'],
								'nombres' 		=> $_POST['nombres'],
								'apellidos' 	=> $_POST['apellidos'],
								'direccion' 	=> $_POST['direccion'],
								'email' 		=> $_POST['email'],
								'sexo'			=> $_POST['sexo'],
								'novedades' 	=> $_POST['novedades'],
								'nacimiento' 	=> $_POST['nacimiento'],
								'pais' 			=> $_POST['pais'],
								'telefono' 		=> $_POST['telefono'],
								'registro'		=>1
							);
					
							//$user->insertEmail($db->insert_id,$_POST['novedades']);	
							echo '/'.$urls['usuario']['editargracias'][$lng];
						}
						
						break;
		case "validarMail": 
						$userDP = new MnuUserDP();
						$userDP->setUserId($_POST['mail']);
						if($userDP->checkMail()){
							echo "true";
						}else{
							echo "false";
						}
						break;
						
		case "deleteCart": 
						if(!isset($_POST['pidproducto']))	exit;
						$carrito = new carrito();
						$data = $_POST['pidproducto'];
						$carrito->removeItem($data);
						$carrito->synchronize();
						$carrito->keep();
						$raiz_images ="../../";
						include("../../tpl/_carritoItems.tpl");
						break;
						
		case "vaciarCart": 
						$carrito = new carrito();
						$carrito->emptyCart();
						$carrito->synchronize();
						$carrito->keep();
						$raiz_images ="../../";
						include("../../tpl/_carritoItems.tpl");
						break;	
		case "validarUsuario": 
						//if(isset($_POST['clave']) && is_password($_POST['clave']) && isset($_POST['email']) && is_mail($_POST['email'])){
							$oUser = new MnuUserFN();
							$oUser->setUserPassword(md5($_POST['clave']));
							$oUser->setUserId($_POST['email']);
							$a_o_usuarios = $oUser->select();
							
							if(is_array($a_o_usuarios)){
								echo "true";
							}else{
								echo "false";
							}
							
							break;	
						//}	
		case "sincronizar":
						$carrito = new carrito();
						$carrito->synchronize();
						$carrito->keep();
						$icono_carrito = true;
						break;				
						
		}
}
?>