var ubi2swf = new Array(
	["150100","1"],
	["150000","1"],
	["150100","1"],
	["150200","9"],
	["150500","89"],
	["150600","49"],
	["150800","32"],
	["160000","118"],
	["010000","126"],
	["240000","134"],
	["200000","143"],
	["060000","151"],
	["220000","160"],
	["130000","168"],
	["250000","177"],
	["020000","186"],
	["100000","195"],
	["190000","204"],
	["170000","213"],
	["080000","222"],
	["090000","231"],
	["050000","240"],
	["110000","249"],
	["210000","259"],
	["030000","268"],
	["040000","278"],
	["180000","289"],
	["230000","300"],
	["140000","309"],
	["120000","319"]);
function getFlashMovieObject(movieName){
  if (window.document[movieName]){return window.document[movieName];}
  if (navigator.appName.indexOf("Microsoft Internet")==-1){
    if (document.embeds && document.embeds[movieName]){return document.embeds[movieName];}
  }else{return document.getElementById(movieName);}
}
function showByUbigeo(ubi){
	if(ubi==null){return;}
	showDepartamento(ubi.toString().substring(0,2));
	showProvincia(ubi.toString().substring(2,4));
	showDistrito(ubi.toString().substring(4,6));
	listarDistribuidores();
}
//["17","Cielos Rasos"]
function showCats(){
	if (cats.length<1){return;}
	for(var i=0;i<cats.length;i++){
		var newOption = document.createElement('option'); 
		newOption.value = cats[i][0];
		newOption.appendChild(document.createTextNode(cats[i][1].toString()));
		document.getElementById("lineasP").appendChild(newOption);
		newOption = null;
	}
}
//["46","040101","AREQUIPA","AREQUIPA","AREQUIPA","Marel EIRL","Av. Lima 137-B","054-450323","054-424611","mareleirl7@hotmail.com",""]
function showDepartamento(retVal){
	if (dist.length<1){return;}
	var html = "<select class=ubigeo name=ubigeoDep id=ubigeoDep size=1 onchange=\"showProvincia();showDistrito();listarDistribuidores();\">";
	html+="<option value=\"\">-- selec. el departamento --</option>";
	var cdep = "";
	for (var i=0;i<dist.length;i++){
		if(dist[i][1].toString().substring(0,2)!=cdep){
			cdep=dist[i][1].toString().substring(0,2);
			html+="<option "; if(retVal==cdep){html+="selected ";};html+="value=\""+cdep+"\">"+dist[i][2]+"</option>";
		}
	}
	html+="</select>";
	if(!document.getElementById("ubigeoDep_div")){document.write("<div id=\"ubigeoDep_div\"></div>");}
	document.getElementById("ubigeoDep_div").innerHTML=html;html=null;		
}
function showProvincia(retValue){
	if (dist.length<1){return;}
	if(retValue==null){retValue="";}
	var html = "<select class=ubigeo name=ubigeoPro id=ubigeoPro size=1 onchange=\"showDistrito();listarDistribuidores();\">";
	html+="<option value=\"\">-- selec.la provincia --</option>";
	var cdep = document.getElementById("ubigeoDep").value;
	var cpro = "";
	for (var i=0;i<dist.length;i++){
		if(dist[i][1].toString().substring(0,2)==cdep){
			if(dist[i][1].toString().substring(2,4)!=cpro){
				cpro=dist[i][1].toString().substring(2,4);
				html+="<option ";if(retValue==cpro){html+="selected ";};html+="value=\""+cpro+"\">"+dist[i][3]+"</option>";
			}
		}	
	}
	html+="</select>";
	if(!document.getElementById("ubigeoPro_div")){document.write("<div id=\"ubigeoPro_div\"></div>");}
	document.getElementById("ubigeoPro_div").innerHTML=html;html=null;		
}	
function showDistrito(retValue){
	if (dist.length<1){return;}
	var cdep = document.getElementById("ubigeoDep").value;
	var cpro = document.getElementById("ubigeoPro").value;
	var html = "<select class=ubigeo name=ubigeoDis id=ubigeoDis size=1 onchange=\"listarDistribuidores('d')\">";
	html+="<option value=\"\">-- selec. el distrito --</option>";
	var cdis = "";
	for (var i=0;i<dist.length;i++){
		if ((dist[i][1].toString().substring(0,2)==cdep)&&(dist[i][1].toString().substring(2,4)==cpro)){
			if (dist[i][1].toString().substring(4,6)!=cdis){
				cdis=dist[i][1].toString().substring(4,6);
				html+="<option ";if(retValue==cdis){html+="selected ";};html+="value=\""+cdis+"\">"+dist[i][4]+"</option>";
			}
		}	
	}
	html+="</select>";
	if(!document.getElementById("ubigeoDis_div")){document.write("<div id=\"ubigeoDis_div\"></div>");}
	document.getElementById("ubigeoDis_div").innerHTML=html;html=null;		
}
function updateMapa(ubi){
	if(!document.getElementById("mapa")){return;}
	var flashMovie=getFlashMovieObject("mapa");
	for(var i=0;i<ubi2swf.length;i++){
		if(ubi2swf[i][0]==ubi){
			flashMovie.GotoFrame(ubi2swf[i][1]);
			flashMovie.Play();
			break;
		} 	
	}
}
// Se puede listar por departamentos, provincias o distritos
//	0	1			2			3			4		5			6				7				8			9						10
//["46","040101","AREQUIPA","AREQUIPA","AREQUIPA","Marel EIRL","Av. Lima 137-B","054-450323","054-424611","mareleirl7@hotmail.com",""]
function listarDistribuidores(from){
	var cdep = document.getElementById("ubigeoDep").value;
	var cpro = document.getElementById("ubigeoPro").value;
	var cdis = document.getElementById("ubigeoDis").value;	
	var tcdep ; if (cdep==""){tcdep="00";}else{tcdep=cdep;} 
	var tcpro ; if (cpro==""){tcpro="00";}else{tcpro=cpro;}
	var tcdis ; if (cdis==""){tcdis="00";}else{tcdis=cdis;}
	updateMapa(tcdep.toString()+tcpro.toString()+tcdis.toString());
	var ccat = document.getElementById("lineasP").value;
	var showdep = false;
	if((from!=null)&&(cdis=="")){return;}
	var html=""
	var currentDep = cdep;
	var currentPro = cpro;
	var currentDis = cdis;
	var total = 0;
	for (var i=0;i<dist.length;i++){
		if	((dist[i][1].toString().substring(0,2)==cdep) && ((cpro=="")||(dist[i][1].toString().substring(2,4)==cpro))&& ((cdis=="")||(dist[i][1].toString().substring(4,6)==cdis)) ) {
			if ((ccat=="")||((","+dist[i][10]+",").indexOf(","+ccat+",")!=-1)){
				total+=1;
				html+="<div class=distribuidor_ficha>"
				if (!showdep){
					showdep = true;
					if (cdis!=""){
						html+= "<span class=titulo>Distribuidores en el distrito de " + dist[i][4]+":</span><br>"
					}else if (cpro==""){
						html+= "<span class=titulo>Distribuidores en el departamento de " + dist[i][2]+":</span><br>"
					}else{
						html+= "<span class=titulo>Distribuidores en el departamento de "+dist[i][2]+", provinica de "+dist[i][3]+":</span><br>"
					}
				}
				if (currentPro!=dist[i][1].toString().substring(2,4)){
					currentPro = dist[i][1].toString().substring(2,4);
					html+= "Provincia:&nbsp;<b>" + dist[i][3] + "</b></br>"
				}
				if (currentDis!=dist[i][1].toString().substring(4,6)){
					currentDis = dist[i][1].toString().substring(4,6);
					html+= "Distrito:&nbsp;<b>" + dist[i][4] + "</b></br>"
				}			
					
				html+="<div class=\"round_redtop_510\"></div>";
				html+="<div class=\"round_redmid_510\">";
						
				html+="<table cellpadding=1 cellspacing=0 border=0 width=510 style=\"table-layout:fixed;\">";
				html+="<col width=70><col width=5><col width=250><col width=70><col width=5><col width=110>"
				html+="<tr>";
				html+="	<td style=\"color:#222222;font-weight:bold;text-transform:uppercase;\" align=left valign=top colspan=6>"+dist[i][5]+"<br>"
				html+="</tr>"
				html+="<tr>";
				html+=" <td align=left valign=top><b>Dirección</b></td><td>:</td><td>"+dist[i][6]+"</td>"
				html+=" <td align=left valign=top><b>Teléfono</b></td><td>:</td><td>"+dist[i][7]+"</td>"
				html+="</tr>"
				html+="<tr>";
				if(dist[i][9]!=""){
					html+=" <td align=left valign=top><b>Email</b></td><td>:</td><td><a href=\"mailto:"+dist[i][9]+"\">"+dist[i][9]+"</a></td>"
				}else{
					html+=" <td align=left valign=top><b>Email</b></td><td>:</td><td>n/d.</td>"
				}
				html+=" <td align=left valign=top><b>Fax</b></td><td>:</td><td>"+dist[i][8]+"</td>"
				html+="</tr>"
				html+="</table>"
					
				html+="</div>";
				html+="<div class=\"round_redbot_510\"></div>";


				html+="</div>"
			}
		}
	}
	if (total<1){
		html+="<div class=alert>"
		if(ccat==""){
			html+="No hay ningún distribuidor en la ubicación seleccionada."
		}else{
			html+="No hay ningún distribuidor para la línea de productos solicitada en la ubicación seleccionada."
		}
		html+="</div>";
	}
	if(!document.getElementById("distribuidores_listado")){document.write("<div id=\"distribuidores_listado\"></div>");}
	document.getElementById("distribuidores_listado").innerHTML=html;html=null;	
}

