//libreria de funciones ajax 

//var URL = 'http://fractalsys.dyndns.org/sistemas/Ideas_Inteligentes/nianu/www/';
//var URL = 'http://fractalsys/sistemas/Ideas_Inteligentes/nianu/www/';
//var URL = 'http://www.nianu.com/';

function ajaxValidarCampos( ) {
		
	if(allNotChecks($("form_sitios"))){
		alert("Debe seleccionar algun sitio!!");
		return false;			     
		 } 
	   
	if(allNotChecks($("form_horizontales"))&&
		allNotChecks($("form_verticales")) &&
		allNotChecks($("form_otros"))
		){
		alert("Debe seleccionar algun tipo de banner!");
		return false;
	}
	//sino tomo los valores de los campos y mando los parametros a mostrar_banners.php
	var parametros = '?sitios='+getSitios($("form_sitios"))+'&banners='+getBanners($("form_horizontales"),$("form_verticales"),$("form_otros"))+'&rporpage='+$("sel_res_por_pagina").value+'&orden='+$("orden_res").value+'&geoip='+$("geo_ip").value+'&mercado='+$("mercado").value+'&metodo_cobro='+$("metodo").value+'&tour='+$("tour").value;
	window.location.href="mostrar_banners.php"+parametros;	
}


function validarRecomendacion(){
	
	if($("nombre").value==""){
		alert("Debes indicar tu nombre para recomendar este aviso a un amigo");
		return false;
		}
	if($("email").value==""){
		alert("Debes indicar el email de tu amigo");
		return false;
		}	
	if(!EsEmail($("email").value)){
		alert("El email que has ingresado para recomendar este aviso, no parece valido, por favor corrijelo.");
		return false;
		}
	
	return true;
	}



function validar() {
		
	if(allNotChecks($("form_banners"))){
		alert("Debe seleccionar algun banner para la rotacion!!");
		return false;			     
		 } 
	   
	if($("nombre_rotacion").value == ""){
		alert("Debe indicar algun nombre para la rotacion!!");
		return false;
	     }
	ajaxGuardarRotacion();	 
	return true;
	}

function EsEmail(w_email) {
	//si le pones comillas a test se caga
	var test = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var emailReg = new RegExp(test);

	return emailReg.test(w_email);
}


function ajaxValidarMail(){
	
	if($("descripcion1").value == ""){
	  alert("Debe escribir algun mensaje para enviar por mail");
	  return false;
	}
	if($("email").value == ""){
	  alert("Debe indicar una casilla de mail");
	return false;	
	}
	if($("nombre").value == ""){
	  alert("Debes indicar tu nombre");
	return false;
	}
	if(EsEmail($("email").value) == ""){
	  alert("El mail ingresado no parece valido, por favor, corrijelo");
	return false;
	}
	return true;
}


function ajaxCambioFoto(urrl, id){
	var url = urrl+'procesa_ajax.php';
	var params = 'opc=cambia_foto&id='+id+'&url='+urrl;
    var ajax = new Ajax.Request( url, {
                                      method:"get",
    								  parameters: params,
    								  onComplete: procesarRespuestaAjaxCambioFoto                                    
									}
      );
	}


function procesarRespuestaAjaxCambioFoto( resp ){

	$("foto_grande").innerHTML = resp.responseText;
	
	}
	
function buscarSubCategoria(id){
	
	var url = 'procesa_ajax.php';
	var params = 'opc=inyecta_subcategorias&id='+id;
    var ajax = new Ajax.Request( url, {
                                      method:"get",
    								  parameters: params,
    								  onComplete: procesarRespuestaInyectaCategoria                                    
									}
      );
	}


function procesarRespuestaInyectaCategoria( resp ){

	$("subcategoria").innerHTML = resp.responseText;
	
	}
	
	
function ajaxBuscarDetallesSubCategoria(id){
	var url = 'procesa_ajax.php';
	var params = 'opc=inyecta_caracteristicas&id='+id;
    var ajax = new Ajax.Request( url, {
                                      method:"get",
    								  parameters: params,
    								  onComplete: procesarRespuestaInyectaCaracteristicas                                    
									}
      );
	
	}
	
function procesarRespuestaInyectaCaracteristicas( resp ){

	$("caracteristicas").innerHTML = resp.responseText;
	
	}
	
function ajaxBuscarModelo( id ){
	var url = 'procesa_ajax.php';
	var params = 'opc=inyecta_modelos&id='+id;
    var ajax = new Ajax.Request( url, {
                                      method:"get",
    								  parameters: params,
    								  onComplete: procesarRespuestaInyectaModelos                                    
									}
      );
	
	}	
	
function procesarRespuestaInyectaModelos( resp ){

	$("modelo").innerHTML = resp.responseText;
	
	}
	
	
function cambiarTipo(valor){
	$("tipo_persona").innerHTML = valor;
	}
	
function contarImpresion(id){
	var url = 'procesa_ajax.php';
	var params = 'opc=cuenta_impresion&id='+id;
    var ajax = new Ajax.Request( url, {
                                      method:"get",
    								  parameters: params
    								 
									}
      );
	
	}	
	
function ajaxMostrarAviso(){
	$("mensaje_eliminacion").innerHTML='Esperamos que hayas tenido &eacute;xito con tu anuncio. Para eliminarlo, simplemente escribe la contrase&ntilde;a que utilizaste al insertar tu anuncio.';
	$("mensaje_eliminacion").style.display = 'table-row';
	}
	
function ajaxOcultarAviso(){
	$("mensaje_eliminacion").style.display = 'none';
	}
	
function ajaxValidarEdEl(){
	if($("pass").value == ""){
		alert("Debes indicar una constrasena");
		return false;
		}
	return true;
	}	
	
function ajaxEnviaContrasena(){
	
	$("envia_pass").innerHTML = '<form action="" method="post" enctype="application/x-www-form-urlencoded" name="pass" target="_self">Email: <input id="email" size="15" maxlength="55" name="email" type="text" class="form">&nbsp;&nbsp;<input id="submit" value="Enviar Password" onClick="return ajaxValidarEmail();" name="env_pass" type="submit" class="form"></form>';
	
	}	
	
function verTelefono(tel,opc){
	if(opc=="0")
		$("telefono").innerHTML = '<span class="rojo"><strong>'+tel+'</strong></span>';
	else
	  	$("telefono").innerHTML = '<span class="rojo"><strong>El anunciante eligi&oacute; no mostrar su tel&eacute;fono</strong></span>';
	}
	
	
function ajaxValidarEmail(){
	
	if(!EsEmail($("email").value) || $("email").value == ""){
		alert('El email ingresado no parece valido, corrijelo');
		return false;
		}
	return true;
	}	
	
	
function buscarCaracteristicas(id){
	$("opciones_caracteristicas").style.display = 'none';
	var url = 'procesa_ajax.php';
	var params = 'opc=caracteristicas_vigilante&id='+id;
    var ajax = new Ajax.Request( url, {
                                      method:"get",
    								  parameters: params,
    								 onComplete: procesarRespuestaBuscarCaracteristicas
									}
      );
	
	}	
	
function procesarRespuestaBuscarCaracteristicas( resp ){
	
	$("opciones_caracteristicas").style.display = 'table-row';
	$("opciones_caracteristicas").innerHTML=resp.responseText;
	}
	
	
function validarConsulta(){
	
	
	
	if($("nombre").value==""){
		alert("Debes indicar tu nombre.  El mismo que indicaste en el anuncio");
		return false;
	}
	
	if($("email").value==""){
		alert("Debes indicar un email.  Este campo es obligatorio.");
		return false;
	}
	
	if(!EsEmail($("email").value)){
		alert("El email ingresado no parece valido, por favor, corrijelo.");
		return false;
	}
	
	if($("texto").value==""){
		alert("Debes indicar la descripcion del problema.");
		return false;
	}
	
	if($("tipoerror").value=="imgnofunc" && $("imagen").value==""){
		alert("Debes seleccionar la imagen a enviar");
		return false;
	}
	
	return true;
	
	
	}	
	
function validarErrorTecnico(){
	
	if($("nombre").value==""){
		alert("Debes indicar tu nombre.  El mismo que indicaste en el anuncio");
		return false;
	}
	
	if($("email").value==""){
		alert("Debes indicar un email.  Este campo es obligatorio.");
		return false;
	}
	
	if(!EsEmail($("email").value)){
		alert("El email ingresado no parece valido, por favor, corrijelo.");
		return false;
	}
	
	if($("texto").value==""){
		alert("Debes indicar la descripcion del problema.");
		return false;
	}
	
	return true;

	
	
	}
	
	
function validarSugerencia(){
	
	if($("nombre").value==""){
		alert("Debes indicar tu nombre.");
		return false;
	}
	
	if($("email").value==""){
		alert("Debes indicar un email.  Este campo es obligatorio.");
		return false;
	}
	
	if(!EsEmail($("email").value)){
		alert("El email ingresado no parece valido, por favor, corrijelo.");
		return false;
	}
	
	if($("texto").value==""){
		alert("No olvides la sugerencia !!!");
		return false;
	}
	
	return true;

	
	
	}	
	
	
	
function validarAyudaTienda(){
	
	if($("nombre").value==""){
		alert("Debe indicar nombre de la tienda");
		return false;
	}
	
	if($("email").value==""){
		alert("Debe indicar un email.  Este campo es obligatorio.");
		return false;
	}
	
	if(!EsEmail($("email").value)){
		alert("El email ingresado no parece valido, por favor, corrijalo.");
		return false;
	}
	
	if($("texto").value==""){
		alert("Debes darnos una descripcion de la tienda");
		return false;
	}
	
	return true;

	
	
	}	
	
function validarDenuncia(){
	
	
	if($("url").value==""){
		alert("Debes indicar la URL");
		
		return false;
	}
	
	
	if(!EsUrl($("url").value)){
		alert("La URL ingresada no parece valida");
		return false;
	}
	
	
	if($("nombre").value==""){
		alert("Debes indicar tu nombre");
		return false;
	}
	
	if($("email").value==""){
		alert("Debes indicar un email.  Este campo es obligatorio.");
		return false;
	}
	
	if(!EsEmail($("email").value)){
		alert("El email ingresado no parece valido, por favor, corrijelo.");
		return false;
	}
	
	if($("texto").value==""){
		alert("Debes indicar la descripcion de la denuncia.");
		return false;
	}
	
	
	return true;
	
	}	
	function verificar1()
{

var imagen_ppal= $("imagen_ppal").value;

//var imagen_ppal="laimagen.exe"
var extension = (imagen_ppal.substring(imagen_ppal.lastIndexOf("."))).toLowerCase();

if ((extension=="")||(extension==".jpg")||(extension==".bmp")||(extension==".png")||(extension==".gif")) //Aqui escriben la extensión que desean aceptar

{
document.getElementByName.submit();

return true;
}
else
{

return false;
}
}
	
	
function revisarAnuncio(){
	if (!verificar1()){
		alert('Elija un archivo del tipo JPG,GIF,BMP,PNG');
		return false;
	}
	//revisar formatos y campos obligatorios....
	if($("nombre").value==""){
		alert("Debes indicar tu nombre");
		return false;
	}
	
	if($("email").value==""){
		alert("Debes indicar un email.  Este campo es obligatorio.");
		return false;
	}
	
	if(!EsEmail($("email").value)){
		alert("El email ingresado no parece valido, por favor, corrijelo.");
		return false;
	}
	
	if($("pass").value==""){
		alert("Debes indicar una password para el anuncio.");
		return false;
	}
	if($("repass").value==""){
		alert("Debes reingresar la password para confirmar la misma.");
		return false;
	}
	if($("pass").value!=$("repass").value){
		alert("La password y la confirmacion de la misma no coinciden, por favor corrijelo.");
		return false;
	}

	if($("telefono").value==""){
		alert("Debes indicar tu telefono, si no lo quieres mostrar en el anuncio, simplemente, chequea la opcion de ocultarlo");
		return false;
	}
	
	if($("cat").value=="0"){
		alert("Debes indicar categoria de producto, seleccionala de la lista");
		return false;
	}
	
	if($("subcateg").value=="0"){
		alert("Debes indicar una sub-categoria de producto, seleccionala de la lista");
		return false;
	}
	if($("titulo1").value==""){
		alert("Debes indicar el titulo del anuncio, es obligatorio.");
		return false;
	}
	if($("descripcion1").value==""){
		alert("Debes indicar la descripcion del anuncio, es obligatorio.");
		return false;
	}
	if($("precio1").value==""){
		alert("Debes indicar el precio del anuncio, es obligatorio.");
		return false;
	}
	if(!EsEntero($("precio1").value)){
		alert("El precio debe ser un valor entero, sin puntos, comas , ni guiones.");
		return false;
	}
	
	if(!codigo_verificacion($('verificacion').value)){ 
        alert("Ingrese Correctamente el Codigo de Verificacion.");
		return false;
	    }
		return true;
	
	
	
	}	

	
	
	
	function ajaxMasImagenes(){
		
		if($("adicional1").style.display != 'none' &&
		   $("adicional2").style.display != 'none' &&
		   $("adicional3").style.display != 'none')
		{
			alert("No puedes agregar mas imagenes extras.");
		    return false;
			
			
			}
		
		if($("adicional1").style.display == 'none' && $("imagen_ppal").value != ""){
		   $("adicional1").style.display = 'table-row';
		   return true;
		}
		else {
		 if($("adicional2").style.display == 'none' && $("imagen_a1").value != ""){
		    $("adicional2").style.display = 'table-row';
		    return true;
		 }
		 else{ 
		  if($("adicional3").style.display == 'none' && $("imagen_a2").value != ""){
		   	 $("adicional3").style.display = 'table-row';
		     return true;
		   }
		   else{ 
		  	alert("Por favor, indica primero la imagen principal y luego las imagenes extras en orden.");
		    return false;
		   }
		  }    
		 }
		
	}
	
	function ajaxAgregaImagenes(ppal, adicionales){
		
		if(ppal==0){
			$("foto_principal").style.display='table-row';			
			return true;
			}
		switch (adicionales) {
			case 0 : if($("adicional1").style.display=='none'){
						$("adicional1").style.display='table-row'; 
						return true;
					 }else 
					    if($("adicional2").style.display=='none'){
								$("adicional2").style.display='table-row'; 
								return true;
					 	}else 
					    	if($("adicional3").style.display=='none'){
								$("adicional3").style.display='table-row'; 
								return true;
					 		}else{
								
								alert("No puedes agregar mas imagenes"); 
								return false;
								
								}
					break;
			case 1 : if($("adicional2").style.display=='none'){
								$("adicional2").style.display='table-row'; 
								return true;
					 	}else 
					    	if($("adicional3").style.display=='none'){
								$("adicional3").style.display='table-row'; 
								return true;
					 		}else{
								
								alert("No puedes agregar mas imagenes"); 
								return false;
								
								}
						break;
			
			
			
			case 2 :    if($("adicional3").style.display=='none'){
								$("adicional3").style.display='table-row'; 
								return true;
					 		}else{
								
								alert("No puedes agregar mas imagenes"); 
								return false;
								
								}
						break;
			case 3 : alert("No puedes agregar mas imagenes"); return false; break;
		}
			
		
	}
//inicio verificar fotos	
	
	function ajaxEliminaFoto(id, an, princ){
		
			if(princ == 1){
					alert("Al eliminar la foto principal, de existir mas fotos en el anuncio, se asignara como foto principal a la siguiente")
				}
		
			var url = 'procesa_ajax.php';
			var params = 'opc=borra_foto_anuncio&id='+id+'&an='+an+'&pr='+princ;
    		var ajax = new Ajax.Request( url, {
                                      method:"get",
    								  parameters: params,
    								  onComplete: procesarRespuestaEliminaFoto
									}
      );
	
	}
	
	function procesarRespuestaEliminaFoto( resp ){
		
		var datos = resp.responseText;
    	var array_datos = datos.split("##");
		$("foto_id_"+array_datos[0]).style.visibility = 'hidden';
		$("foto_id_link_"+array_datos[0]).style.visibility = 'hidden';
		window.location.reload();
		//$("mas_img").onClick='return ajaxAgregaImagenes('+array_datos[1]+','+array_datos[2]+')';
	 }
		
		
		function ajaxOtroCaptcha(){
			
			var url = 'procesa_ajax.php';
			var params = 'opc=cambia_captcha';
    		var ajax = new Ajax.Request( url, {
                                      method:"get",
    								  parameters: params,
    								  onComplete: procesarRespuestaOtroCaptcha
									}
     			 );
			
			
			
			}
			
			
			function procesarRespuestaOtroCaptcha( resp ){
		
				var datos = resp.responseText;
				var array_datos = datos.split("##");
				$("captcha").innerHTML= array_datos[1];
				$("valor_verificacion").value = array_datos[0];
				
			 }
	