var turismo = {

 suscribe:function(dir)
 {  
    element = document.getElementById("FormsEditSuscribe");
    
      if ( this.validarEmail(element.value) )
      { 
        var callAjax=new callbackClass();
        callAjax.argument=["showSuscribeCartel"];
        arg = 'mail='+element.value;
       
        var transaction=ajaxClass.asyncRequest('php/suscribe.php',0,callAjax,arg);
       
      }
      else
         alert("El email no es valido");
    },
  
  validarEmail:function(valor) {
  
   if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor))
   {
    
    return true;
   } 
   else {
    
    return false;
   }

   },
  validateComment:function(form)
  {
       
       var input = form.getElementsByTagName('input');
       for(i=0;i<input.length;i++)
       {
        
        if (input[i].name == 'EFName' && input[i].value == '')
        {    /*alert('Debe ingresar un nombre');
             input[i].focus();
              return false;*/
              input[i].value='anónimo';
        }
        if (input[i].name == 'EFMail' && input[i].value!='')
            if ( !this.validarEmail(input[i].value))
             {
               alert('El email no es valido');
               input[i].focus();
               return false; 
             }
        if (input[i].name == 'captcha' && input[i].value=='')
        {
            alert('Debe ingresar el captcha');
            input[i].focus();
            return false; 
        }             
       }
       
       //todo: validar el texarea que no este vacio al enviar el comentario 
      
       var myEditor = nicEditors.findEditor('idMLComment');
      
        myEditor.saveContent();
        area = document.getElementById('idMLComment');
        
        if (area.value=='<font><br></font>' || area.value=="")
        {  alert('Debe ingresar un comentario');
          area.focus();
          return false;
        } 
        
       return true;
       
       
  }, 
  validateExperience:function(form)
  {
       
       var input = form.getElementsByTagName('input');
       for(i=0;i<input.length;i++)
       {
        
        if (input[i].name == 'FormsEditFieldName' && input[i].value == '')
        {    alert('Debe ingresar un nombre');
              input[i].focus();
              return false;
        }
        if (input[i].name == 'FormsEditFieldMail' && input[i].value!='')
            if ( !this.validarEmail(input[i].value))
             {
               alert('El email no es valido');
               input[i].focus();
               return false; 
             }
        if (input[i].name == 'captcha' && input[i].value=='')
        {
            alert('Debe ingresar el captcha');
            input[i].focus();
            return false; 
        }     
                    
       }
       
       region = document.getElementById('FormsComboBoxRegion');
        if (region.value == 0)
        {
         alert('Debe selecccionar una región');
         region.focus();
         return false;
        }
       //todo: validar el texarea que no este vacio al enviar el comentario 
        var myEditor = nicEditors.findEditor('FormsMultiLineExperiencia');
      
        myEditor.saveContent();
        area = document.getElementById('FormsMultiLineExperiencia');
        
        if (area.value=='<font><br></font>' || area.value=="")
        {  alert('Debe ingresar un comentario');
          area.focus();
          return false;
        } 
       return true;
       
       
  }, 
  validatePregunta:function(form)
  {
       
       var input = form.getElementsByTagName('input');
       for(i=0;i<input.length;i++)
       {
        
        if (input[i].name == 'FormsEditFieldName' && input[i].value == '')
        {    alert('Debe ingresar un nombre');
              input[i].focus();
              return false;
        }
        if (input[i].name == 'FormsEditFieldMail' && input[i].value!='')
            if ( !this.validarEmail(input[i].value))
             {
               alert('El email no es valido');
               input[i].focus();
               return false; 
             }
       
        if (input[i].name == 'FormsEditFieldTitle' && input[i].value=='')
         {   
               alert('Debe ingresar un titulo para la pregunta');
               input[i].focus();
               return false; 
             } 
        if (input[i].name == 'captcha' && input[i].value=='')
        {
            alert('Debe ingresar el captcha');
            input[i].focus();
            return false; 
        }       
                    
       }
       
       region = document.getElementById('FormsComboBoxRegion');
        if (region.value == 0)
        {
         alert('Debe selecccionar una región');
         region.focus();
         return false;
        }
       
       presup = document.getElementById('FormsComboBoxPresupuesto');
        if (presup.value == 0)
        {
         alert('Debe selecccionar un rango de presupuesto');
         presup.focus();
         return false;
        }
        
        //todo: validar las edades con checkbox
       //todo: validar el texarea que no este vacio al enviar el comentario 
       
       var myEditor = nicEditors.findEditor('FormsMultiLinePregunta');
      
        myEditor.saveContent();
        area = document.getElementById('FormsMultiLinePregunta');
        
        if (area.value=='<font><br></font>' || area.value=="")
        {  alert('Debe ingresar un comentario');
          area.focus();
          return false;
        } 
       return true;
       
       
  }, 
  validateTip:function(form)
  {
       
       var input = form.getElementsByTagName('input');
       for(i=0;i<input.length;i++)
       {
        
        if (input[i].name == 'FormsEditFieldName' && input[i].value == '')
        {    alert('Debe ingresar un nombre');
              input[i].focus();
              return false;
        }
        if (input[i].name == 'FormsEditFieldMail' && input[i].value!='')
            if ( !this.validarEmail(input[i].value))
             {
               alert('El email no es valido');
               input[i].focus();
               return false; 
             }
                          
       }
       
       region = document.getElementById('FormsComboBoxRegion');
        if (region.value == 0)
        {
         alert('Debe selecccionar una región');
         region.focus();
         return false;
        }
        
        edad = document.getElementById('FormsComboBoxEdad');
        if (edad.value == 0)
        {
         alert('Debe selecccionar un rango de edad');
         edad.focus();
         return false;
        }
        
        presup = document.getElementById('FormsComboBoxPresupuesto');
        if (presup.value == 0)
        {
         alert('Debe selecccionar un rango de presupuesto');
         presup.focus();
         return false;
        }
       
       //todo: validar el texarea que no este vacio al enviar el comentario
       var myEditor = nicEditors.findEditor('idMLComment');
      
        myEditor.saveContent();
        area = document.getElementById('FormsMultiLine4');
        
        if (area.value=='<font><br></font>' || area.value=="")
        {  alert('Debe ingresar un comentario');
          area.focus();
          return false;
        }  
       
       return true;
       
       
  }, 
  iLikeComment:function(commentId,noteId,dir){
     var callAjax=new callbackClass();
     callAjax.argument=["addLikeComment",commentId];
     
     arg = 'commentId='+commentId;
     if (!this.readCookie('vote'+noteId+commentId))
     {  this.createCookie('vote'+noteId+commentId,'S',7);
        var transaction=ajaxClass.asyncRequest(dir,0,callAjax,arg);
     }
     else
        alert('Ya ha votado anteriormente'); 
     
  } ,
   iLikeNote:function(noteId,dir){
     
     var callAjax=new callbackClass();
     callAjax.argument=["addLikeNote",noteId];
     
     arg = 'noteId='+noteId;
     if (!this.readCookie('note'+noteId))
     {  this.createCookie('note'+noteId,'S',7);
        var transaction=ajaxClass.asyncRequest(dir,0,callAjax,arg);
     }
     else
        alert('Ya ha votado anteriormente');  
  } ,
  iLikePregunta:function(preguntaId,dir){
  var callAjax=new callbackClass();
     callAjax.argument=["addLikePregunta",preguntaId];
     
     arg = 'id='+preguntaId;
     if (!this.readCookie('pregunta'+preguntaId))
     {  this.createCookie('pregunta'+preguntaId,'S',7);
        var transaction=ajaxClass.asyncRequest(dir,0,callAjax,arg);
     }
     else
        alert('Ya ha votado anteriormente');
  },
  showSuscribeCartel:function(oajax){
   
    if (oajax.responseText == "ok")
    {    alert('Gracias por sucribirse.');
         element = document.getElementById("FormsEditSuscribe");
         element.value='';
    }
    else
        alert('Intente suscribirse nuevamente más tarde'); 
  },
  showExperience:function(e){
    window.location="experiencias.html?region="+e.value;
  
  },
  
  showPreguntas:function(e,element){
    //window.location="tips.html?region="+e.value;
   
    var urlEnd = document.URL.indexOf('?');
    var values = new Array();
    var names;
   
    if (urlEnd != -1)
    {
     var region = document.URL.indexOf(element);
     if (region != -1) //se ha indicado una region previamente
     {
       var reemplazo = element + "=" + e.value;
       patron = "/"+element+"=[0-9]*/";
       if (element == 'region')
         window.location = document.URL.replace(/region=[0-9]*/,reemplazo);
       if (element == 'presupuesto')
         window.location = document.URL.replace(/presupuesto=[0-9]*/,reemplazo);
       if (element == 'edad')
         window.location = document.URL.replace(/edad=[0-9]*/,reemplazo);    
     }
     else //se han pasado parametros pero no region
       window.location=document.URL+"&"+element+"="+e.value;
    }
    else //no hay parametros pasados en la url
     window.location=document.URL+"?"+element+"="+e.value;
  
  },
  
  showNote:function(e){
    
    var urlEnd = document.URL.indexOf('?');
    var values = new Array();
    var names;
   
    if (urlEnd != -1)
    {
     var region = document.URL.indexOf('region');
     if (region != -1) //se ha indicado una region previamente
     {
       var reemplazo="region="+e.value;
       window.location = document.URL.replace(/region=[0-9]*/,reemplazo);  
     }
     else //se han pasado parametros pero no region
       window.location=document.URL+"&region="+e.value;
    }
    else //no hay parametros pasados en la url
     window.location=document.URL+"?region="+e.value;
  
  },
  addLikeComment:function(oajax){
     
     element = document.getElementById('comment_'+oajax.argument[1]);
     element.innerHTML = oajax.responseText;
     
  },
  addLikeNote:function(oajax){
     
     element = document.getElementById('vote_note');
     element.innerHTML = oajax.responseText+' Votos';
     
     alert('Gracias por su votación');
     
  },
  addLikePregunta:function(oajax){
     
     element = document.getElementById('like_pregunta');
     element.innerHTML = oajax.responseText;
     
     alert('Gracias por su votación');
     
  },
  createCookie:function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
},

 readCookie:function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
},

 eraseCookie:function(name) {
	createCookie(name,"",-1);
},

 showSendNote:function(noteid){
  var newdiv = document.getElementById('sendNote');
  if (newdiv == null)
  {
  
  var newdiv = document.createElement('div');
  
  newdiv.setAttribute('id','sendNote');
  newdiv.style.width = "200px";
  newdiv.style.height = "180px";
  
  
  newdiv.style.background = "#fff";
  newdiv.style.border = "1px solid #000";
  newdiv.innerHTML = '<form id=\"formSendNote\" method=\"post\" >'; 
  newdiv.innerHTML += '<label for=\"name_from\"> Tu Nombre</label> <br/>';
  newdiv.innerHTML += '<input type=\"text\" name="name_from" id="name_from"  value=\"\"/>';
  newdiv.innerHTML += '<br/> <br/> <label for=\"email_to\"> Mail Destino</label> <br/>';
  newdiv.innerHTML += '<input type=\"text\" name="email_to" id="email_to"  value=\"\"/>';
  newdiv.innerHTML += '<br/> <br/> <input class=\"sendButtonMail\" type=\"button\" name="submit"  value=\"Enviar\" onclick=\"turismo.sendNoteMail('+noteid+');\" />';
  newdiv.innerHTML += '</form>';
  mainDivElement = document.getElementById("note_div_inf");
  mainDivElement.appendChild(newdiv);
  
 }
 
 },
sendNoteMail:function(noteid){
    
      var element = document.getElementById("email_to");
    
      if ( this.validarEmail(element.value) )
      { 
        var callAjax=new callbackClass();
        callAjax.argument=["showSendNoteCartel"];
        arg = 'noteid='+noteid+'&email_to='+element.value+'&name_from='+document.getElementById("name_from").value;
       
        var transaction=ajaxClass.asyncRequest('php/send.php',0,callAjax,arg);
       
      }
      else
         alert("El email no es valido");
         

},
showSendNoteCartel:function(o)
{
 var d = document.getElementById('note_div_inf');
 var olddiv = document.getElementById('sendNote');
 d.removeChild(olddiv);
 alert('Se ha enviado la nota');
 
}

   
};

