<?php



//FUNCIONES 

function iniciaSesion( $idempleado, $idTipo, $nombreUsuario, $db)

{

    //limpiar		 

  	$_SESSION = array();

  	//establcer a nuevo valor

    $_SESSION['SESidempleado'] = $idempleado;

    $_SESSION['SESidTipo'] = $idTipo;

  	$_SESSION['SESusuario'] = $nombreUsuario;

	$_SESSION['db'] = $db;



		

		if( DEBUG )

		{

				echo "<br>SESION INICIADA -> idempleado ($idempleado), idTipo ($idTipo), usuario ($nombreUsuario) <br>";

				$info = session_get_cookie_params();

				echo "Session lifetime " .$info['lifetime'] . "<br>";

		}

}



function iniciaSesionCliente( $idcliente, $nombreEmpresa )

{

  //limpiar		 

  $_SESSION = array();

  //establcer a nuevo valor

  $_SESSION['SESidcliente'] = $idcliente;

  $_SESSION['SESnomEmpresa'] = $nombreEmpresa;

		

  if( DEBUG )

  {

      echo "<br>SESION CLIENTE INICIADA -> idcliente ($idcliente), empresa ($nombreEmpresa) <br>";

      $info = session_get_cookie_params();

      echo "Session lifetime " .$info['lifetime'] . "<br>";

      }

}



function terminaSesion(  )

{

    //limpiar		 

  	$_SESSION = array();

  	

		//limpiar cookie

    if( isset($_COOKIE[session_name()]) )

		    setcookie(session_name(), '', time() - 3*60 * 60, '/');

								

		session_destroy();

}



function validaSesion()

{

 		if( DEBUG )

		{

    		if( isset($_SESSION['SESusuario']) )

				{

						echo "<br>SESION YA INICIADA -> idempleado (". $_SESSION['SESidempleado'] ."), idTipo (".$_SESSION['SESidTipo']."), usuario (".$_SESSION['SESusuario'].") <br>";

						$info = session_get_cookie_params();

						echo "Session lifetime " .$info['lifetime'] . "<br>";

				}

    		else

    				echo "<br/>SESION NO INICIADA <br/>";

		}

		

    if( isset($_SESSION['SESusuario']) )

				return TRUE;

		else

				return FALSE;	

}





function validaSesionCliente()

{



    if( DEBUG )

    {

        if( isset($_SESSION['SESnomEmpresa']) )

        {

             echo "<br>SESION CLIENTE YA INICIADA -> idcliente (". $_SESSION['SESidcliente'] ."), empresa (".$_SESSION['SESnomEmpresa'].") <br>";

             $info = session_get_cookie_params();

             echo "Session lifetime " .$info['lifetime'] . "<br>";

        }

        else

             echo "<br/>SESION CLIENTE NO INICIADA <br/>";

    }



    if( isset($_SESSION['SESnomEmpresa']) )

        return TRUE;

    else

        return FALSE;	

}



//PARA DEBUG

function debugImprimeError($errno, $errstr, $errfile, $errline)

{

 		print("<br/><font color='red' >Error en PHP:</font> [$errno] [$errstr] at $errline in $errfile.<br/>");

		if( $errno != 8 )

				die();

}



function debugInicializa()

{

    if( DEBUG )

    {

     		 error_reporting( E_ALL );

    		 ini_set( "display_errors", "1");

				 set_error_handler( "debugImprimeError" );

    }

    else

    {

     		 error_reporting( 0 );

    		 ini_set( "display_errors", "0");

				 restore_error_handler();

    }

}





function printdata($arraypos,$llave)

{

$cadena="";

global $link;

     

	foreach($arraypos as $key => $value)

	{

	if($llave)

		$cadena.="$key,";

	else

	{

	 	if(!is_numeric($value))

		$cadena.="'" . mysql_real_escape_string($value,$link) ."',";

		else

		$cadena.= mysql_real_escape_string($value,$link).",";

	}

	}

	return substr($cadena,0,strlen($cadena)-1);	

}



function getData($tabla,$condicion,$arraydata, $orden ="")

{

 global $link;

	// llave =0 es VALOR a insertar

	//llave = 1 es POSICION a inseretar

	$query = "select " .printdata($arraydata,'1')." from $tabla";

	if( $condicion > "" )

			$query .= " where $condicion";



	if( $orden > "" )

			$query .= " order by $orden";

	

	$result=mysql_query($query, $link);

	

	if( DEBUG )

	{

			echo "<br/>" .$query . "<br/>";

			if( !$result )

					echo "<br/>" . mysql_error() . "<br/>";		

	}

	

	return $result;

}



function insertdata ($tabla,$arraydata)

{

 global $link;

	// llave =0 es VALOR a insertar

	//llave = 1 es POSICION a inseretar

	$query="insert into $tabla (" .printdata($arraydata,'1').") values (" .printdata($arraydata,'0').")";

	

	if( DEBUG )

	{

	 		//echo "<br/>" . print_r($arraydata) ."<br/>";

			//echo "<br/>" . printdata($arraydata,'1') . "<br/>";

			//echo "<br/>" . printdata($arraydata,'0') . "<br/>";

			

			echo "<br/>" . $query . "<br/>";

	}

			

	return mysql_query($query,$link);

}



function updateData($tabla,$condicion,$arraydata)

{

 	global $link;

	$query="update $tabla set ";

	foreach($arraydata as $key => $value)

	{

		$query.="$key=";

		if(!is_numeric($value))

		$query.="'" . mysql_real_escape_string($value,$link) ."',";

		else

		$query.= mysql_real_escape_string($value,$link).",";

	}

	$query=substr($query,0,strlen($query)-1);

	$query.=" where $condicion";

	

	if( DEBUG )

			echo "<br/>" .$query . "<br/>";

			

	return mysql_query($query,$link);

}



function deleteData($tabla,$condicion)

{

  global $link;

	

	$query="delete from $tabla where $condicion";

	



	if( DEBUG )

			echo "<br/>" .$query . "<br/>";

			

	return mysql_query($query,$link);

}



//

function showTable( $titulo, $arreglo, $result )

{

 	 $tamano = count($arreglo);

	 $valores = array_values( $arreglo);

	 $llaves = array_keys( $arreglo);

	 

	 $width = 100 / ($tamano +1 );

 	 echo "<table width='90%' border='1' cellpadding='3' cellspacing='0' align='center'> \n";

	 //header



	 echo "<tr>\n";

   for( $i =1; $i< $tamano; $i++ )

   {

  		echo "\t<th>" . $valores[$i] . "</th>\n";

   }

   echo "<th>&nbsp;</th><th>&nbsp;</th>";

   echo "</tr>\n";

	 

	 //body

   while( $row = mysql_fetch_row( $result) )

   {

   echo "<tr>\n";

  		 for( $i =1; $i< $tamano; $i++)

  		 {

  		 			echo "\t<td width='$width%'>$row[$i]</td>\n";

  		 }

		

  		 echo "\t<td width='$width%'><a href='" . strtolower( $titulo) . "Update.php?" . $llaves[0] . "=$row[0]'>Actualizar datos</a></td>\n";

  		 echo "\t<td width='$width%'><a href='javascript:alert($row[0]);'>Eliminar</a></td>\n";

   echo "</tr>\n";

   }

	 

	 echo "</table>\n";	 

}



function showTableSinLinks( $titulo, $arreglo, $result )

{

 	 $tamano = count($arreglo);

	 $valores = array_values( $arreglo);

	 $llaves = array_keys( $arreglo);

	 

	 $width = 100 / ($tamano +1 );

 	 echo "<table width='90%' border='1' cellpadding='3' cellspacing='0' align='center'> \n";

	 //header



	 echo "<tr>\n";

   for( $i =1; $i< $tamano; $i++ )

   {

  		echo "\t<th>" . $valores[$i] . "</th>\n";

   }

   //echo "<th>&nbsp;</th><th>&nbsp;</th>";

   echo "</tr>\n";

	 

	 //body

   while( $row = mysql_fetch_row( $result) )

   {

   echo "<tr>\n";

  		 for( $i =1; $i< $tamano; $i++)

  		 {

  		 			echo "\t<td width='$width%'>$row[$i]</td>\n";

  		 }

		

  		 //echo "\t<td width='$width%'><a href='" . strtolower( $titulo) . "Update.php?" . $llaves[0] . "=$row[0]'>Actualizar datos</a></td>\n";

  		 //echo "\t<td width='$width%'><a href='javascript:alert($row[0]);'>Eliminar</a></td>\n";

   echo "</tr>\n";

   }

	 

	 echo "</table>\n";	 

}





function buscaEquipoCliente( $tabla, $idcliente )

{

    global $link;



    $query="select count(*) from $tabla where idcliente = $idcliente";

    $result = mysql_query($query,$link);



    if( DEBUG )

        echo "<br/>" .$query . "<br/>";



    if (!$result)

        return -1;



    $row = mysql_fetch_row($result);

    return $row[0];

}



function NoNull($TestVar,$ValueIfNull)

{

  if (is_null($TestVar))

     return $ValueIfNull;

   else

     return $TestVar;

}



function sinValorEnviado( $arreg, $valor, $defualt =0)

{

    if( !array_search($valor, array_keys($arreg)))

        return array_merge($arreg, array( $valor => $defualt) );

    else

        return $arreg;

}

//require ('C:\wamp\www\Mappec\Admin\mail2.php');

//require ('/var/www/Mappec/Admin/mail2.php');

//require ('/var/www/Mappec/Admin/ventas/alarmas2.php');

?>