var xmlHttp;
var xmlDoc;


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


function showcomarques(str) {

if (str=="AD") {

     document.getElementById('paisbloc').style.display = "block";

     document.forms[0].comarca.disabled = true;
     document.forms[0].comarca.style.display = "none";
     document.getElementById('label_comarca').style.display = "none";
     document.getElementById('label_municipi').style.display = "block";
     document.getElementById('label_municipi').innerHTML= "Parròquia";
     document.forms[0].comarca.options[0].name = "Andorra";
     document.forms[0].comarca.options[0].value = "andorra";
     document.forms[0].municipi.disabled = false;
     document.forms[0].municipi.style.display = "block";
     showmunicipis('andorra');

}

else if (str=="AL") {

     document.getElementById('paisbloc').style.display = "block";

     document.forms[0].comarca.disabled = true;
     document.forms[0].comarca.style.display = "none";
     document.forms[0].municipi.disabled = true;
     document.forms[0].municipi.style.display = "none";
     document.getElementById('label_comarca').style.display = "none";
     document.getElementById('label_municipi').style.display = "none";
     document.forms[0].comarca.options[0].name = "l'Alguer";
     document.forms[0].comarca.options[0].value = "lalguer";
     document.forms[0].municipi.options[0].name = "l'Alguer";
     document.forms[0].municipi.options[0].value = "lalguer";

     var munivalue = "lalguer";



}


else if (str=="CR") {

     document.getElementById('paisbloc').style.display = "block";

     document.forms[0].comarca.disabled = true;
     document.forms[0].comarca.style.display = "none";
     document.getElementById('label_comarca').style.display = "none";
     document.getElementById('label_municipi').style.display = "block";
     document.getElementById('label_municipi').innerHTML= "Nucli";
     document.forms[0].comarca.options[0].name = "el Carxe";
     document.forms[0].comarca.options[0].value = "el_carxe";
     document.forms[0].municipi.disabled = false;
     document.forms[0].municipi.style.display = "block";

     var comarcavalue = "el_carxe";
     showmunicipis('el_carxe');

}

else if ((str=="CT") || (str=="CN") || (str=="PV") || (str=="FJ") || (str=="BA")) {

     document.getElementById('paisbloc').style.display = "block";

     document.getElementById('label_comarca').style.display = "block";

     if (str=="BA") {
     	document.getElementById('label_comarca').innerHTML= "Illa";
     }

     else {
     	document.getElementById('label_comarca').innerHTML= "Comarca";
     }

     document.getElementById('label_municipi').innerHTML= "Municipi";
     document.forms[0].comarca.disabled = false;
     document.forms[0].comarca.style.display = "block";
     document.getElementById('label_municipi').style.display = "block";
     document.forms[0].municipi.disabled = false;
     document.forms[0].municipi.style.display = "block";


 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("El navegador no funciona amb l'HTTP Request")
  return
  }
 var url="/getcomarca.php"
 url=url+"?territori="+str
 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=processaXML
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)

}

else {

     document.getElementById('paisbloc').style.display = "block";


     document.forms[0].comarca.name = "";
     document.forms[0].comarca.value = "";
     document.getElementById('label_comarca').style.display = "block";
     document.forms[0].comarca.disabled = false;
     document.forms[0].comarca.style.display = "block";
     document.getElementById('label_municipi').style.display = "block";
     document.forms[0].municipi.disabled = false;
     document.forms[0].municipi.style.display = "block";


 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("El navegador no funciona amb l'HTTP Request")
  return
  }
 var url="/getcomarca.php"
 url=url+"?territori="+str
 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=processaXML
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)
 }

}


function showcomarques2(str) {


 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("El navegador no funciona amb l'HTTP Request")
  return
  }
 var url="/getcomarca.php"
 url=url+"?territori="+str
 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=processaXML
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)


}


function activamuni() {


	if (document.forms[0].noumuni.checked) {

		document.getElementById('selectbloc').style.display = "none";

		document.forms[0].territori.disabled = true;
		document.forms[0].territori.style.display = "none";
		document.forms[0].comarca.disabled = true;
		document.forms[0].comarca.style.display = "none";
		document.forms[0].municipi.disabled = true;
		document.forms[0].municipi.style.display = "none";


	}

	else {

		document.getElementById('selectbloc').style.display = "none";

    	document.forms[0].territori.disabled = false;
    	document.forms[0].territori.style.display = "block";
    	document.forms[0].comarca.disabled = false;
    	document.forms[0].comarca.style.display = "block";
    	document.forms[0].municipi.disabled = false;
    	document.forms[0].municipi.style.display = "block";


	}

}

function getDoc(XMLHttpRequestObject) {
	var doc;
	if (document.implementation && document.implementation.createDocument){
		doc = XMLHttpRequestObject.responseXML;
		//In case to be the internet explorer
	}
	else if (window.ActiveXObject){
		//Create a xml tag in run time
		var testandoAppend = document.createElement('xml');
		//Put the requester.responseText in the innerHTML of the xml tag
		testandoAppend.setAttribute('innerHTML',XMLHttpRequestObject.responseText);
		//Set the xml tag's id to _formjAjaxRetornoXML
		testandoAppend.setAttribute('id','_formjAjaxRetornoXML');
		//Add the created tag to the page context
		document.body.appendChild(testandoAppend);
		//Just for check put the xmlhttp.responseXML in the innerHTML of the tag
		document.getElementById('_formjAjaxRetornoXML').innerHTML = XMLHttpRequestObject.responseText;
		//Now we can get the xml tag and put it on a var
		doc = document.getElementById('_formjAjaxRetornoXML');
		//So we have a valid xml we can remove the xml tag
		document.body.removeChild(document.getElementById('_formjAjaxRetornoXML'));
	}
	return doc;
}

function processaXML(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {


		xmlDoc=getDoc(xmlHttp);

		var terri = xmlDoc.getElementsByTagName('territori').length;

		document.forms[0].comarca.options.length = 0;
		document.forms[0].municipi.options.length = 0;



		var box3 = document.forms[0].municipi;
		box3.options[0]= new Option('Seleccioneu una comarca/illa...', '0');


		var ter = new Array();

		for(var i=0;i<terri;i++){

			ter.push(xmlDoc.getElementsByTagName('territori')[i].getAttribute('nom'));

			var terricom = xmlDoc.getElementsByTagName('territori')[i].getElementsByTagName('comarca').length;



			var box2 = document.forms[0].comarca;
			box2.options[0] = new Option('Seleccioneu-ne una...', '0');



			for (var y=1; y<terricom+1;y++) {

				var coma = xmlDoc.getElementsByTagName('territori')[i].getElementsByTagName('comarca')[y-1].getAttribute('nom');
				var valor = xmlDoc.getElementsByTagName('territori')[i].getElementsByTagName('comarca')[y-1].getAttribute('sem');
				var sem = xmlDoc.getElementsByTagName('territori')[i].getElementsByTagName('comarca')[y-1].getAttribute('sem');
				box2.options[y] = new Option(coma, valor);


			}

		}

	}

}


function showmunicipis(str)
 {
 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("El navegador no funciona amb l'HTTP Request")
  return
  }

 var url="/getmunicipi.php"
 url=url+"?comarca="+str
 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=processaXML2
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)
 }

function processaXML2(){


if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {


	xmlDoc=getDoc(xmlHttp);

	var comarca = xmlDoc.getElementsByTagName('comarca').length;


	document.forms[0].municipi.options.length = 0;

	var comar = new Array();

	var box2 = document.forms[0].municipi;
	box2.options[0] = new Option('Seleccioneu-ne un...', '0');


	for(var i=0;i<comarca;i++){

		comar.push(xmlDoc.getElementsByTagName('comarca')[i].getAttribute('nom'));

		var communi = xmlDoc.getElementsByTagName('comarca')[i].getElementsByTagName('municipi').length;


			for (var y=1; y<communi+1;y++) {

				var muni = xmlDoc.getElementsByTagName('comarca')[i].getElementsByTagName('municipi')[y-1].getAttribute('nom');
				var valor = xmlDoc.getElementsByTagName('comarca')[i].getElementsByTagName('municipi')[y-1].getAttribute('sem');
				var sem = xmlDoc.getElementsByTagName('comarca')[i].getElementsByTagName('municipi')[y-1].getAttribute('sem');
				box2.options[y] = new Option(muni, valor);


			}


		}


	}

}

function go_there(value, terri, model, fixa) {

	var corresp = new Array();
	corresp['AD'] = "andorra";
	corresp['AL'] = "lalguer";
	corresp['CN'] = "catalunya_nord";
	corresp['CT'] = "catalunya";
	corresp['CR'] = "el_carxe";
	corresp['FJ'] = "la_franja";
	corresp['BA'] = "illes_balears";
	corresp['PV'] = "pais_valencia";


	if (terri=='territori') {

		if (terri=='territori' && (value=='PC' || value=='0') ) {

			document.getElementById('button_counter').innerHTML = "<img src='getbutton.php?pais=1&type="+model+"' id='button' />";
		}

		else if (terri=='territori' && value=='MO') {

			document.getElementById('button_counter').innerHTML = "<img src='getbutton.php?type="+model+"' id='button' />";
		}

		else {

			document.getElementById('button_counter').innerHTML = "<img src='getbutton.php?"+terri+"="+corresp[value]+"&type="+model+" ' id='button' />";
		}

	}

	else {

	document.getElementById('button_counter').innerHTML = "<img src='getbutton.php?"+terri+"="+value+"&type="+model+" ' id='button' />";


 	}

	putcode(fixa);


}

function showbutton (model, fixa) {

	var imag = document.getElementById('button').getAttribute('src');
	var imagArray = imag.split("type");

	document.getElementById('button_counter').innerHTML = "<img src='"+imagArray[0]+"type="+model+" ' id='button' />";

	putcode(fixa);

}

function showcodebutton (fixa) {

	putcode(fixa);

}

function putcode(fixa) {

	var imag = document.getElementById('button').getAttribute('src');

	var imagArray1 = imag.split("?");
	var imagArray2 = imagArray1[1].split("&");
	var imagArray3 = imagArray2[0].split("=");

	var terri = imagArray3[0];
	var vterri = imagArray3[1];

	var istyle = fixaho(fixa);


	if (terri == 'type') {

		document.getElementById('button_code').innerHTML = "<textarea cols=\"100\" rows=\"5\">"+
"&lt;a href='http://www.estatpropi.cat'&gt;\n"+
"&lt;img src='http://www.estatpropi.cat/"+imag+"' style='"+istyle+"' /&gt;\n"+
"&lt;/a&gt;</textarea>";

	}

	else if (terri == 'pais') {

		document.getElementById('button_code').innerHTML = "<textarea cols=\"100\" rows=\"5\">"+
"&lt;a href='http://www.estatpropi.cat'&gt;\n"+
"&lt;img src='http://www.estatpropi.cat/"+imag+"' style='"+istyle+"' /&gt;\n"+
"&lt;/a&gt;</textarea>";

	}

	else {

		document.getElementById('button_code').innerHTML = "<textarea cols=\"100\" rows=\"5\">"+
"&lt;a href='http://www.estatpropi.cat/"+terri+"/"+vterri+"'&gt;\n"+
"&lt;img src='http://www.estatpropi.cat/"+imag+"' style='"+istyle+"' /&gt;\n"+
"&lt;/a&gt;</textarea>";

	}
}

function fixaho(fixa) {

	if (fixa =='up_left') {

		var istyle="float:left;position:absolute;position:fixed;top:0;left:0;border:0;display:block;text-indent:-999em;text-decoration:none;"

	}

	else if (fixa =='up_right') {

		var istyle="float:right;position:absolute;position:fixed;top:0;right:0;border:0;display:block;text-indent:-999em;text-decoration:none;"

	}

	else if (fixa =='down_left') {

		var istyle="float:left;position:absolute;position:fixed;bottom:0;left:0;border:0;display:block;text-indent:-999em;text-decoration:none;"

	}

	else if (fixa =='down_right') {

		var istyle="float:right;position:absolute;position:fixed;bottom:0;right:0;border:0;display:block;text-indent:-999em;text-decoration:none;"

	}

	else {

		var istyle="";

	}

	return(istyle);

}

function showcodebutton2 (fixa2) {

        putcode2(fixa2);

}

function putcode2(fixa) {

        var imag = document.getElementById('button2').getAttribute('src');

        var istyle = fixaho(fixa);

        document.getElementById('button_code2').innerHTML = "<textarea cols=\"100\" rows=\"5\">"+
"&lt;a href='http://www.estatpropi.cat/som_una_nacio_volem_estat_propi'&gt;\n"+
"&lt;img src='http://www.estatpropi.cat"+imag+"' style='"+istyle+"' /&gt;\n"+
"&lt;/a&gt;</textarea>";


}


function showcodebutton3 (fixa3) {

        putcode3(fixa3);

}

function putcode3(fixa) {

        var imag = document.getElementById('button3').getAttribute('src');

        var istyle = fixaho(fixa);

        document.getElementById('button_code3').innerHTML = "<textarea cols=\"100\" rows=\"5\">"+
"&lt;a href='http://www.estatpropi.cat/som_una_nacio_volem_estat_propi'&gt;\n"+
"&lt;img src='http://www.estatpropi.cat"+imag+"' style='"+istyle+"' /&gt;\n"+
"&lt;/a&gt;</textarea>";


}

function showcodebutton_arenys (fixa) {

        putcode_arenys(fixa);

}

function putcode_arenys(fixa) {

        var imag = document.getElementById('button_arenys').getAttribute('src');

        var istyle = fixaho(fixa);

        document.getElementById('button_code_arenys').innerHTML = "<textarea cols=\"100\" rows=\"5\">"+
"&lt;a href='http://www.estatpropi.cat/arenys_de_munt_comenca_a_decidir'&gt;\n"+
"&lt;img src='http://www.estatpropi.cat"+imag+"' style='"+istyle+"' /&gt;\n"+
"&lt;/a&gt;</textarea>";


}
