var xmlHttp = null;
var lastDivShowed = null;
var IE = null;
var FF = null

function DistribuidorDescriptor()
{
	var idDiv = "";
	var nombre = "";
	var telefono = "";
	var email = "";
	var web = "";
	var direccion = "";
}

function ArtistaDescriptor()
{
	var nombre = "";
	var grupo = "";
	var web = "";
	var imagen = "";
}

function crearHTMLCapa(distribuidorDesc)
{
	var emailHTMLText = "";
	var webHTMLText = "";
	if (distribuidorDesc.email == "ND")
		emailHTMLText = "<td align=\"left\" class=\"texto\">No Disponible</td>";
	else
		emailHTMLText = "<td align=\"left\"><a class=\"enlace\" href=\"mailto:" + distribuidorDesc.email + "\">" + distribuidorDesc.email +"</a></td>";

	if (distribuidorDesc.web == "ND")
		webHTMLText = "<td align=\"left\" class=\"texto\">No Disponible</td>";
	else
		webHTMLText = "<td align=\"left\"><a class=\"enlace\" href=\"#\" onclick=\"javascript:window.open('" + distribuidorDesc.web + "','new','');\">" + distribuidorDesc.web + "</a></td>";

	var htmlText =
	"<div style=\"visibility:hidden;position:absolute;\" id=\"" + distribuidorDesc.idDiv + "\">" +
		"<table>" +
			"<TBODY>" +
				"<tr>" +
					"<td class=\"textoNegrita\">Tel&eacute;fono:</td>" +
				"</tr>" +
				"<tr>" +
					"<td class=\"texto\">" + distribuidorDesc.telefono + "</td>" +
				"</tr>" +
				"<tr>" +
					"<td  class=\"textoNegrita\">E-Mail: </td>" +
				"</tr>" +
				"<tr>" +
					emailHTMLText +
				"</tr>" +
				"<tr>" +
					"<td class=\"textoNegrita\">Web: </td>" +
				"</tr>" +
				"<tr>" +
					webHTMLText +
				"</tr>" +
				"<tr>" +
					"<td class=\"textoNegrita\">Direcci&oacute;n:</td>" +
				"</tr>" +
				"<tr>" +
					"<td class=\"texto\">" + distribuidorDesc.direccion +"</td>" +
				"</tr>" +
			"</TBODY>" +
		"</table>" +
	"</div>";
	return htmlText;
}

function establecerTituloProvincia(titulo)
{
	document.getElementById("tituloTablaProvincias").innerHTML = titulo;
}

function createDistribuidorDescriptor(xmlDoc, i)
{
	var distribuidorDesc = new DistribuidorDescriptor();
	distribuidorDesc.idDiv = "id" + i;
	distribuidorDesc.nombre = xmlDoc.getElementsByTagName("nombre")[i].firstChild.nodeValue;
	distribuidorDesc.telefono = xmlDoc.getElementsByTagName("telefono")[i].firstChild.nodeValue;
	distribuidorDesc.email = xmlDoc.getElementsByTagName("e-mail")[i].firstChild.nodeValue;
	distribuidorDesc.web = xmlDoc.getElementsByTagName("web")[i].firstChild.nodeValue;
	distribuidorDesc.direccion = xmlDoc.getElementsByTagName("direccion")[i].firstChild.nodeValue;
	return distribuidorDesc;
}

function createArtistaDescriptor(xmlDoc, i)
{
	var distribuidorDesc = new ArtistaDescriptor();
	distribuidorDesc.nombre = xmlDoc.getElementsByTagName("nombre")[i].firstChild.nodeValue;
	distribuidorDesc.grupo = xmlDoc.getElementsByTagName("grupo")[i].firstChild.nodeValue;
	distribuidorDesc.web = xmlDoc.getElementsByTagName("web")[i].firstChild.nodeValue;
	distribuidorDesc.imagen = xmlDoc.getElementsByTagName("imagen")[i].firstChild.nodeValue;
	return distribuidorDesc;
}

function cargarProvincia(codigoProvincia)
{
	if (cargarProvincia != "")
	{
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("La versión del navegador no permite la correcta visualización de la pagina.Actualice su navegador")
			return;
		}
		var urlPath = "../xml/distribuidores/" + codigoProvincia + ".xml?param=" + Math.round(100*Math.random());
		xmlHttp.onreadystatechange=stateChangedProvincias;
		xmlHttp.open("GET",urlPath,true);
		xmlHttp.send(null);
	}
}

function establecerMensajeEspera()
{
	var texto = "Descargando datos, espere por favor...";

	if (IE)
		actualizarHTMLInicialEspanaIE(texto);
	else
		actualizarHTMLInicialEspanaFF(texto);
}

function establecerMensajeNoHayArtistas()
{
	var texto = "No hay artistas registrados";
	actualizarHTMLInicialArtistas(texto);
}

function establecerTextoInicialEspana()
{
	var texto = "Seleccione una provincia para ver los distribuidores";
	if (IE)
		establecerTextoInicialEspanaIE(texto);
	else
		establecerTextoInicialEspanaFF(texto);
}

function stateChangedProvincias()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		ocultarLastDivShowed();
		xmlText = xmlHttp.responseText;
		if (IE)
			tratarProvinciasIE(xmlText);
		else
			tratarProvinciasFF(xmlText);
	}
}

function GetXmlHttpObject()
{
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}



function establecerEstiloMapa()
{
	var className = "portugalDefault";
	if (IE)
		className = getClassNameEstiloMapaIE();

	document.getElementById("divPortugal").className = "portugalDefault";
}

function establecerTituloProvincia(titulo)
{
	document.getElementById("tituloTablaProvincias").innerHTML = titulo;
}

function seleccionProvincia(provincia)
{
	if (provincia != "")
	{
		var numeroProvincias = 52;

		for (i = 1 ; i < numeroProvincias+1 ; i++)
		{
			if (document.getElementById("provincias").options[i].value == provincia)
			{
			  document.getElementById("provincias").selectedIndex = i;
			  break;
		 	}
		}
		establecerMensajeEspera();
		cargarProvincia(provincia);
		establecerTituloProvincia(document.getElementById("provincias").options[i].text);
	}
}

function ocultarCapa(divId)
{
	if (divId != null)
		document.getElementById(divId).style.visibility = "hidden";

}

function gestionarCapa(divId)
{
	if (lastDivShowed != null)
		document.getElementById(lastDivShowed).style.visibility = "hidden";

	if (document.getElementById(divId).style.visibility == "hidden")
		document.getElementById(divId).style.visibility = "visible";
	lastDivShowed = divId;
}

function ocultarLastDivShowed()
{
	if (lastDivShowed != null)
		document.getElementById(lastDivShowed).style.visibility = "hidden";
}

function verEspana()
{
	if (IE)
		actualizarHTMLInicialEspanaIE("");
	document.getElementById("divPortugal").style.visibility = "hidden";
	document.getElementById("divEspana").style.visibility = "visible";
	ocultarCapa(lastDivShowed);
	establecerTextoInicialEspana();
}

function verPortugal()
{
	document.getElementById("divEspana").style.visibility = "hidden";
	document.getElementById("divPortugal").style.visibility = "visible";
	ocultarCapa(lastDivShowed);
	establecerMensajeEspera();
	establecerTituloProvincia("Portugal");
	cargarProvincia('portugal');
}

function initPageDistribuidores()
{
	initPage();
	establecerEstiloMapa();
	establecerTextoInicialEspana();
}


function initPage()
{
	IE = (navigator.appName == "Microsoft Internet Explorer");
	FF = (navigator.appName == "Netscape")
}
function setImageURL()
{
	var path = "../images/artists/" + URLParam('imageName');
	var textHTML = "<img src='" + path + "' />";
	document.getElementById("artistImage").innerHTML = textHTML;
}


function URLParam(strParamName)
{
  var strReturn = "";
  var strHref = window.location.href;
  if (strHref.indexOf(strParamName+'=') > -1)
  {
    strHref = strHref.substr(strHref.indexOf(strParamName+'='));

    if (strHref.indexOf('&') > -1)
    	strHref = strHref.substr(0, strHref.indexOf('&'));
    var aQueryString = strHref.split("=");
    strReturn = aQueryString[1];
  }
  return strReturn;
}

