function imprimirError(str){
	alert(str);
}

function setDefault(){
	document.busqueda.apellido.focus();
}

function cambiarTab(id) {
document.getElementById('clearflag').value="true";
 if(id=="radio2"){
   document.getElementById('inpArea').value="011";
   document.getElementById('inpPrefijo').value="4348";
   document.getElementById('inpSufijo').value="8000";
   document.getElementById('idCampo').value="";
   document.getElementById('idCalle').value="";
   document.getElementById('idAltura').value="";
   document.getElementById(id).style.display = "block";
   document.getElementById('provloc').style.display = "none";
   document.getElementById('radio1').style.display = "none";
   document.getElementById('radio3').style.display = "none";
   document.busqueda.action = 'BuscarTelefonica.action';
 }
 else if(id=="radio1"){
   document.getElementById('idCampo').value="Apellido y Nombre / Empresa";
   document.getElementById('idLocalidad').value="Localidad";
   document.getElementById('inpArea').value="";
   document.getElementById('inpPrefijo').value="";
   document.getElementById('inpSufijo').value="";
   document.getElementById('idCalle').value="";
   document.getElementById('idAltura').value="";
   document.getElementById(id).style.display = "block";
   document.getElementById('provloc').style.display = "block";
   document.getElementById('radio2').style.display = "none";
   document.getElementById('radio3').style.display = "none";
   document.busqueda.action = 'BuscarTelefonica.action';
 }else if(id=="radio3"){
   document.getElementById('idCalle').value="Calle";
   document.getElementById('idAltura').value="Altura";
   document.getElementById('inpArea').value="";
   document.getElementById('inpPrefijo').value="";
   document.getElementById('inpSufijo').value="";
   document.getElementById('idCampo').value="";
   document.getElementById('idLocalidad').value="Localidad";
   document.getElementById(id).style.display = "block";
   document.getElementById('provloc').style.display = "block";
   document.getElementById('radio1').style.display = "none";
   document.getElementById('radio2').style.display = "none";
   document.busqueda.action = 'BuscarTelefonicaInvertida.action';
 }
}

function recargarTab() {
	var i; 
    for (i=0; i < document.busqueda.tipoBusqueda.length; i++){ 
       if (document.busqueda.tipoBusqueda[i].checked) 
          break; 
    } 
    var id = document.busqueda.tipoBusqueda[i].value;
     
 if(id=="radio2"){
   document.getElementById(id).style.display = "block";
   document.getElementById('provloc').style.display = "none";
   document.getElementById('radio1').style.display = "none";
   document.getElementById('radio3').style.display = "none";
   document.busqueda.action = 'BuscarTelefonica.action';
 }
 else if(id=="radio1"){
   document.getElementById(id).style.display = "block";
   document.getElementById('provloc').style.display = "block";
   document.getElementById('radio2').style.display = "none";
   document.getElementById('radio3').style.display = "none";
   document.busqueda.action = 'BuscarTelefonica.action';
 }else if(id=="radio3"){
   document.getElementById(id).style.display = "block";
   document.getElementById('provloc').style.display = "block";
   document.getElementById('radio1').style.display = "none";
   document.getElementById('radio2').style.display = "none";
   document.busqueda.action = 'BuscarTelefonicaInvertida.action';
 }
 showHideLoc();
}


function limpiarCampo(id) {
  document.getElementById(id).value="";
}

function limpiarCampoTelefono() {
 if(document.getElementById('clearflag').value=="true"){
  document.getElementById('inpArea').value="";
  document.getElementById('inpPrefijo').value="";
  document.getElementById('inpSufijo').value="";
  document.getElementById('clearflag').value="false";
 }
}

function limpiarCampoCondicional(id,cond) {
 if(document.getElementById(id).value==cond){
  	document.getElementById(id).value="";
 }
}

function showHideLoc(){
	if(document.busqueda.provinciasId.options[document.busqueda.provinciasId.selectedIndex].value == '4'){	
		document.busqueda.idLocalidad.style.visibility = 'hidden';
		document.busqueda.idLocalidad.value = 'Localidad';
	}else{
		document.busqueda.idLocalidad.style.visibility = 'visible';
	}
}

function changeProvincia(){
	//Se comentda debido a que producto pide que no se muestre barrio cuando se selecciona CF
	if(false){
		if(document.busqueda.provinciasId.options[document.busqueda.provinciasId.selectedIndex].value == '4'){
			document.busqueda.localidadesId.style.visibility = 'visible';
			document.busqueda.localidadesId.style.width='100px';
			document.busqueda['localidad.descripcion'].style.visibility='hidden';
			document.busqueda['localidad.descripcion'].style.width='0px';
		}else{
			document.busqueda.localidadesId.style.visibility = 'hidden';
			document.busqueda.localidadesId.style.width='0px';
			document.busqueda['localidad.descripcion'].style.visibility='visible';
			document.busqueda['localidad.descripcion'].style.width='100px';
		}
	}
}

function selectProvincia(num){
	if(num != ''){
		newIndex = 0;
		for(i=0; i < document.busqueda.provinciasId.length; i++){
			if(document.busqueda.provinciasId.options[i].value == num)
				newIndex = i;
		}
		document.busqueda.provinciasId.selectedIndex = newIndex;
	}
}

function doHelp(ok){
	if(ok == 'false')
		return;
	
	var button = document.getElementById('txtBtnBuscar');
	if(button == null)
		return;

	button.click();
}

function onloadReadyBusquedaTelefonica(fromSiteMap, tab){
	recargarTab();
	var f = function() { doHelp( fromSiteMap ); };
	setTimeout(f, 1000);
}
/*
* Genera la url friendly dinamicamente
*/
function prepareSubmit() {
	
	var tipoBusqueda = '';

	var radios = document.getElementsByName('tipoBusqueda');
	if(radios == null)
		return;
	var radio;
	for(var i = 0; i < radios.length; i++)
	{
		radio = radios[i];
		if(radio.checked) {
			tipoBusqueda = radio.value;
			break;
		}
	}
	var provinciaId = document.getElementsByName('provinciasId');
	
	if(provinciaId == null || provinciaId[0] == null)
		return;
	
	var provinciaLocalidad = '';
	if((provinciaId[0].value != '') && (provinciaId[0].value != '0'))
		provinciaLocalidad = provinciaId[0][provinciaId[0].selectedIndex].text + '-' + provinciaId[0].value + '/';
	else
		provinciaLocalidad = 'TODAS-0'+ '/';
		
	var idLocalidad = document.getElementById('idLocalidad');
	
	if(idLocalidad == null)
		return;
	
	if(idLocalidad != null && idLocalidad.value != '') {
		provinciaLocalidad = provinciaLocalidad + idLocalidad.value + '/';
	}
	var newAction = "";
 	if(tipoBusqueda == "radio1"){

		//////////////////// MODIFICACIONES ////////////////////
		var loc = document.getElementById('idLocalidad').value;
		//var pro = document.getElementById('provinciasId').value;
		var pro = 1; 		
 		var provinciaId = document.getElementsByName('provinciasId');
 		if(provinciaId == null || provinciaId[0] == null  || provinciaId[0].value == 0 ){
 			pro = 0;
 		}

 		if( pro == 0 && loc.length ==0  ){
			document.busqueda.action = "";
 			alert("Debe especificar una provincia o localidad para realizar su búsqueda");
 			return false;		
		}
		//////////////////// MODIFICACIONES ////////////////////

		var nombre = document.getElementById('idCampo').value;
 		if(nombre.length < 3 || nombre.toLowerCase() == "apellido y nombre / empresa" || 
 				nombre == "apellido-nombre-empresa" || nombre.toLowerCase() == "bannerslaterales.js" ){
 			document.busqueda.action = "";
 			alert("Debe ingresar un 'Apellido' (minimo 3 caracteres)");
 			return false;
 		}else{
 			nombre = eliminarCaracteres(nombre);
 			newAction = '/busqueda-nombre/argentina/' + provinciaLocalidad + nombre;
 			document.busqueda.action = newAction.toUpperCase();
 			return true;
 		}
 	} else if(tipoBusqueda=="radio2"){
 		var area = document.getElementById('inpArea').value;
 		var prefijo = document.getElementById('inpPrefijo').value;
 		var sufijo = document.getElementById('inpSufijo').value;
 		
 		if(area.length == 0 || prefijo.length == 0 || sufijo.length == 0) {
 			document.busqueda.action = "";
			//////////////////// MODIFICACIONES ////////////////////
			alert("Debe ingresar un codigo de area,prefijo y sufijo");
			//////////////////// MODIFICACIONES ////////////////////
			return false;
 		}else{

 			if(	!validarEntero(area) || !validarEntero(prefijo) || !validarEntero(sufijo) ){
 				document.busqueda.action = "";
 				//////////////////// MODIFICACIONES ////////////////////
 				alert( "Para realizar la búsqueda debe ingresarse un número de teléfono válido");
 				//////////////////// MODIFICACIONES ////////////////////
 				return false;
 			}

 			document.busqueda.action = '/busqueda-telefono/argentina/' + eliminarCaracteres(area) + '-' + eliminarCaracteres(prefijo) + '-' + eliminarCaracteres(sufijo); 
 			return true;
 		}
 	} else if(tipoBusqueda=="radio3"){

		//////////////////// MODIFICACIONES ////////////////////
		var loc = document.getElementById('idLocalidad').value;
		//var pro = document.getElementById('provinciasId').value;
		var pro = 1; 		
 		var provinciaId = document.getElementsByName('provinciasId');
 		if(provinciaId == null || provinciaId[0] == null  || provinciaId[0].value == 0 ){
 			pro = 0;
 		}

		if( pro == 0 && loc.length ==0  ){
			document.busqueda.action = "";
 			alert("Debe especificar una provincia o localidad para realizar su búsqueda");
 			return;				
		}

		var altura = document.getElementById('idAltura').value;
		if( !validarEntero(altura) ){
			document.busqueda.action = "";
 			alert("Ingrese numeros en el campo altura para realizar su búsqueda");
 			return;
		}
		//////////////////// MODIFICACIONES ////////////////////

 		var calle = document.getElementById('idCalle').value;
 		//var altura = document.getElementById('idAltura').value;
 		if(calle.replace(/^\s+|\s+$/g,"").length == 0 || altura.replace(/^\s+|\s+$/g,"").length == 0 || calle.toLowerCase() == "calle" || altura.toLowerCase() == "altura"){
 			document.busqueda.action = "";
 			alert("Ingrese 'Calle' y 'Altura' para realizar su búsqueda");
 			return false;
 			
 		}else{

			//////////////////// MODIFICACIONES ////////////////////
			if(calle.length < 2){
 				document.busqueda.action = "";
 				alert("Debe ingresar un nombre de calle de al menos 2 caracteres");
 				return;
 			}
			//////////////////// MODIFICACIONES ////////////////////

// 			newAction = '/busqueda-direccion/argentina/' + provinciaLocalidad + eliminarCaracteres(calle) + '-' + eliminarCaracteres(altura);
// 	 		document.busqueda.action = newAction.toUpperCase();
// 	 		return true;
 			

 	 		
 	 		if(calle.length < 4 && document.busqueda.provinciasId.options[document.busqueda.provinciasId.selectedIndex].value == '0'){
 	 			
 				document.busqueda.action = "";
 				alert("Si la calle esta representada por tres o menos caracteres debe ingresar una provincia");
 				return false;
	 			 			
	 		} 	
 	 			
 				newAction = '/busqueda-direccion/argentina/' + provinciaLocalidad + eliminarCaracteres(calle) + '-' + eliminarCaracteres(altura);
	 	 		document.busqueda.action = newAction.toUpperCase();
	 	 		return true; 	
 		}
 	}
}

function validarEntero(altura){
	var num = parseInt(altura);	
	if (isNaN(num)){
		return false;
	}else {
		return true;
	}
}

function eliminarCaracteres(strOriginal)
{
	var retVal = strOriginal;
	//alert('retVal: ' + retVal);
	retVal = retVal.replace(/\./g, '');
	retVal = retVal.replace(/\:/g, '');
	retVal = retVal.replace(/\\/g, '');
	retVal = retVal.replace('/', '');
	retVal = retVal.replace(/\//g, '');
	retVal = retVal.replace(/\&/g, '');
	retVal = retVal.replace(/\?/g, '');
	retVal = retVal.replace(/#/g, '');
	retVal = retVal.replace(/%/g, '');
	//alert('retVal: ' + retVal);
	
	return retVal;
}

