/*Search engine*/

function doOnKeyPress(e,f){
  if (e.keyCode == 13 || e.keyCode == 10){
    doOnSubmit(f);
    return false;
  }else{
    return true;
  }	
}

function getCheckedValue(field){
  if(field.length){
    for(var i=0;i<field.length;i++){
      if(field[i].checked)return field[i].value;	
    }	
  }else{
      return field.value;
  }
}

function doOnSubmit(f){
  var context = (f.context? getCheckedValue(f.context) : "");	
  var query = f.q.value;
	
  if(query.length>1){	
    if ((context=="video")||(context=="image")){            
      self.location.href = "http://www2.yatv.com/jsp/JYatvSearchResults.jsp?s=15177&mode=text&results=5&format="+context+"&query="+query;
    }else if ((context=="cartelera")){        
      self.location.href = "http://www2.yatv.com/jsp/JYatvBillboardSearch.jsp?s=15420&mode=text&type=title&results=5000&title=on&query="+query;
    }else if ((context=="web-news")){      
      self.location.href = "http://buscar.ya.com/fast-cgi/tsc?type=all&PortalLanguage=es&UserLanguage=es&language=&mandant=toi&device=html&iocr=yes&search_type=new&context=web-news&q=" +query;
    }else if((context=="C")){
      self.location.href = "http://www.finanzas.com/Buscador.asp?SE="+context+"&TE="+query;
    }else if((context=="empresas")){
    	//self.location.href = "http://www.finanzas.com/empresas/buscador.asp?cadena="+query;
    	f.action = "http://www.finanzas.com/empresas/buscador.asp";
    	f.cadena.value=escapeChars(query);
        f.submit();    
    }else{
      self.location.href = "http://busca.ya.com/search?buscar="+query+"&origen=header&destino=web";	
	}
  }
}
var replaceChars=[["á","a"],["é","e"],["í","i"],["ó","o"],["ú","u"],["Á","A"],["É","E"],["Í","I"],["Ó","O"],["Ú","U"],["ñ","n"],["Ñ","N"]];
function escapeChars(s){
  var length = s.length;
  var output = '';
  for(var i=0;i<length;i++){
    output +=searchChar(s.charAt(i));    	
  }
  return output;
}

function searchChar(c){
  var length = replaceChars.length;
  for(var i=0;i<length;i++){
    if(replaceChars[i][0]==c){
      return replaceChars[i][1];
    } 		
  }
  return c;
}



/*End Search engine*/



