var telecharge = {},f,xmlhttp;


$().ready(function() { go(); });

function go() {
	if (typeof Custom!="undefined" && Custom) Custom.init();
	if (document.getElementById('mapjs')) {
		if (typeof setMap!="undefined" && setMap) setMap();
		if (typeof onLoadmap!="undefined" && onLoadmap) {
			if (document.all && !window.opera) setTimeout("onLoadmap()", 250);
			else onLoadmap();
		}
	}
}

function get(theUrl) {
        var xmlHttp = null;
        xmlHttp = new XMLHttpRequest();
        xmlHttp.open( "GET", theUrl, false );
        xmlHttp.send( null );
        return true;
}

function post(theUrl,params) {
        var xmlHttp = null;
        xmlHttp = new XMLHttpRequest();
        xmlHttp.open("POST", theUrl, false );
	var p =  null;
    	for(var key in params) {
	        p = p + key + "=" + params[key] + "&";
    	}
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHttp.send(p);
	return true;
}

function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default, if not specified.

    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
}

function getData(select1,select2) {
	
	if (!document.getElementsByTagName || !document.body.innerHTML || !window.XMLHttpRequest ) { return; }

	cleanSelects(select2,select1);

	/* Set up the request */
	var xmlhttp =  new XMLHttpRequest();
	xmlhttp.open('POST', 'XMLLocation.php', true);
	

	/* The callback function */
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp && xmlhttp.readyState && xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200)
			{
				populate(xmlhttp.responseXML, select2, select1);
				if (typeof Custom!="undefined" && Custom) Custom.init();
			}
			else
			{
				getData(select1,select2);
			}
			xmlhttp = null;
		}
	}
	/* Send the POST request */
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(select1.name + '=' + escape(select1.value));

	/* Add temporary feedback that the request has been sent */
  	document.getElementById(select1.name + '-working').style.visibility = 'visible';
}

function cleanSelects(select2,select1)
{

	if (document.getElementById(select2))
	{
		container = document.getElementById(select2 + '-container');
		container.innerHTML = '';
	}
	if (document.getElementById('municipalite'))
	{
		container = document.getElementById('municipalite-container');
		container.innerHTML = '';
	}
  	document.getElementById(select1.name + '-working').style.visibility = 'hidden';
}


function populate(responseXML, select2, select1)
{
	cleanSelects(select2,select1);
        var list = responseXML.getElementsByTagName(select2);

        if (list && list.length && list.length > 0) {

		var newImg = document.createElement('img');
		newImg.id = select2 + '-working';
		newImg.setAttribute('src','images/working.gif');
		newImg.setAttribute('class','hidden');

		var newBox = document.createElement('select');
		newBox.name = select2;
		newBox.id = select2;
		newBox.setAttribute('class','styled');
		newBox.setAttribute('className','styled');
		
		if (select2 == 'mrc') {	
			newBox.onchange = function() {
				if (select1.form.name) id = select1.form.name;
				if (select1.form.id) id = 'forms.' + select1.form.id;
				getData(eval('document.' + id + '.' + select2) ,'municipalite');
			}
		}

		var newSpan = document.createElement('span');
		newSpan.id = select2 + '-working-container';

		var newDiv1 = document.createElement('div');
		newDiv1.id = select2 + '-element';

		var newDiv2 = document.createElement('div');
		newDiv2.id = select2 + '-label';

		var newLabel = document.createElement('label');
		newLabel.setAttribute('for',select2);
		newLabel.setAttribute('class','optional');
		newLabel.setAttribute('className','optional');

		var newTxt;
		if (select2 == 'mrc') {
			newTxt = document.createTextNode('Choisir une MRC');
			newBox.options[0] = new Option('--- MRC ---','');

		}
		else { 
			newTxt = document.createTextNode('Choisir une municipalité');
			newBox.options[0] = new Option('--- Municipalité ---','');
		}

		newLabel.appendChild(newTxt);

		newDiv2.appendChild(newLabel);

		for (var i = 1; i < list.length+1; i++) {
			var t = list[i-1];
			var v = t.getAttribute('id'); 
			newBox.options[i] = new Option(t.firstChild.nodeValue,v);
		}

		newDiv1.appendChild(newBox);
		newSpan.appendChild(newImg);
		newDiv1.appendChild(newSpan);

		container = document.getElementById(select2 + '-container');
		container.appendChild(newDiv2);
		container.appendChild(newDiv1);
  	
		document.getElementById(select2 + '-working').style.visibility = 'hidden';
	}
}

function goToLocation(localisation) {

	params = '';
        if (localisation.region && localisation.region.value) params = '?region=' + localisation.region.value;
        if (localisation.mrc && localisation.mrc.value) params = '?mrc=' + localisation.mrc.value;
        if (localisation.municipalite && localisation.municipalite.value) params = '?municipalite=' + localisation.municipalite.value;

	document.location.href = localisation.action + params;
}

function setLocation(region,mrc,municipalite) {
	var params = '';
	if (region) params = params + '?region=' + region;
	if (mrc) params = params + (region ? '&' : '' ) + 'mrc=' + mrc;
	if (municipalite) params = params + ( (region | mrc) ? '&' : '' ) + 'municipalite=' + municipalite;

	if (municipalite && document.getElementById('municipalite-working')) document.getElementById('municipalite-working').style.visibility = 'visible';
	else if (mrc && document.getElementById('mrc-working')) document.getElementById('mrc-working').style.visibility = 'visible';
	else if (region && document.getElementById('region-working')) document.getElementById('region-working').style.visibility = 'visible';

	var objT = YAHOO.util.Get.script("js/menu.js.php" + params, {
		onSuccess : function(o) {
			var menujs = document.getElementById('menujs'); 
			menujs.innerHTML = menu;
			Custom.init();
			/*
			if (municipalite && document.getElementById('municipalite-working')) document.getElementById('municipalite-working').style.visibility = 'visible';
			else if (mrc && document.getElementById('mrc-working')) document.getElementById('mrc-working').style.visibility = 'visible';
			else if (region && document.getElementById('region-working')) document.getElementById('region-working').style.visibility = 'visible';
			*/
		}
	});


	var objT = YAHOO.util.Get.script("js/mapj.js.php" + params, {
		onSuccess : function(o) {
			setMap();
	                if (document.all && !window.opera) setTimeout("onLoadmap()", 250);
			else onLoadmap();
			if (municipalite && document.getElementById('municipalite-working')) document.getElementById('municipalite-working').style.visibility = 'hidden';
			else if (mrc && document.getElementById('mrc-working')) document.getElementById('mrc-working').style.visibility = 'hidden';
			else if (region && document.getElementById('region-working')) document.getElementById('region-working').style.visibility = 'hidden';
		}
	});


	// jquery
	/*
	$.get("js/mapj.js.php", params, function(data) {
			var script = document.createElement('script');
			script.type = 'text/javascript';
			script.text = data;
			$('head').append(script);
			
			setMap();

	}, 'script');
	*/
}

function setMap() {
	var mapjs = document.getElementById('mapjs');
	var sidebarjs = document.getElementById('sidebarjs');
	mapjs.innerHTML = '<div id="map" style="width: 600px; height: 600px; position:relative;"></div>';
	sidebarjs.innerHTML = '<div id="sidebar_map" class="sidebar_map"><ul id="sidebar-liste"></ul></div>';
}

