// JavaScript Document


//Function qui saisi la page 
function formHandler(form){
	var URL = document.form.RetailerCity.options[document.form.RetailerCity.selectedIndex].value;
	window.location.href = URL;
	}

//function qui saisi la variable passe dans le URL
function getParams() {
	var idx = document.URL.indexOf('?');
	var params = new Array();
	if (idx != -1) {
	var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
	for (var i=0; i<pairs.length; i++) {
	nameVal = pairs[i].split('=');
	params[nameVal[0]] = nameVal[1];
	   }
	}
	return params;
	}
	params = getParams();
	//variables qui selectionne la ville et le retail store
	RetailerCity = decodeURIComponent(params["RetailerCity"]);
	RetailerHead = unescape(params["H"]);
	RetailerLang = unescape(params["lang"]);
	
var xmlDoc;

// FONCTION QUI CHARGE LA PAGE XML EN MEMOIRE
function loadXmlDoc()
{	
  try
  {
	//POUR SAFARI
	if (jQuery.browser.safari) 
	{
		nodeIndexName = 1;
		nodeIndexAddress = 3;
		
 		req = new XMLHttpRequest();
		req.open("GET", xmlWifiFile, true);
		req.send(null);
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				xmlDoc = req.responseXML;
				getXmlData();
			}
		}
	}
	//POUR EXPLORER
	else if (window.ActiveXObject)
	{
		nodeIndexName = 0;
		nodeIndexAddress = 1;
		nodeIndexCity = 2;
		nodeIndexStatus = 3;
		
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
		xmlDoc.load(xmlWifiFile);
		getXmlData();
	}
	//POUR FIREFOX ET AUTRES
	else if (document.implementation && document.implementation.createDocument)	
	{
		nodeIndexName = 1;
		nodeIndexAddress = 3;
		
		xmlDoc = document.implementation.createDocument("","",null);
		xmlDoc.load(xmlWifiFile);
		xmlDoc.onload=getXmlData;
	} 
  }
  catch(err)
  {
	alert("XML data is unavailable");
  }	
}

function getXmlData()
{
    if (RetailerCity != "undefined") 
    {
		// cherche chaque <city> element nodes. 
		var nodeList = xmlDoc.getElementsByTagName("hotspotname");
		
	    // pour le Storefornt, bureau general, pour afficher les details du storefront et les details du bureau chef
	  	var store, office, col1, col2;
	  
	  	document.getElementById("WifiLocatorResultsBox").innerHTML = "";
	  
	  	if (RetailerLang == "en") {
		  	col1 = "Hotspots";
		  	col2 = "Addresses";
			col3 = "Google maps";
		}
	    else if (RetailerLang == "fr") {
		  	col1 = "Points d'acc&egrave;s";
		  	col2 = "Adresses";
			col3 = "Google maps";
		}
	
		// insere les images pour la section anglaise
		var html = "<table id='WifiLocatorResults' cellspacing='0' cellpadding='6' width='100%' class='table-orange-corner'>" +
				"<thead><tr>" +
				"  <td class='tc-nw'>&nbsp;</td>" +
				"  <td class='tc-n'><b>" + col1 + "</b></td>" +
				"  <td class='tc-n'><b>" + col2 + "</b></td>" +
				"  <td class='tc-n'><b>" + col3 + "</b></td>" +
				"  <td class='tc-ne'>&nbsp;</td>" +
				"</tr></thead>";
	  
	  //***********************************************************************
	  //AFFICHE LES POINTS D'ACCES
	  //***********************************************************************
	  var x;
	  var c = 1;
	  
	  // Loop a travers tout les elements du node <city>
	  for(i=0; i< nodeList.length; i++)
	  {	  
	      // Enregistre l'info de <store> parent node qui match avec le requete de l'element <city>
	      store = nodeList[i].parentNode;		
			  
		  // selectionne seulement le contenu de <city> element node ou id=1 et en fonction de la langue choisi.
	      if( (store.getAttribute("id")==RetailerHead) && (store.getAttribute("lang")==RetailerLang) )
	      {
	          // Ajout les information trouve pour chaque ville. 
        	  html += "<tr>" +
			          "  <td class='tc-w tc-bline'>&nbsp;</td>" +
			          "  <td class='tc-bline' style='width: 45%; padding-right: 5%'>" + store.childNodes[nodeIndexName].firstChild.nodeValue + "<br />" + store.childNodes[nodeIndexStatus].firstChild.nodeValue + "</td>" +
			          "  <td class='tc-bline'>" + store.childNodes[nodeIndexAddress].firstChild.nodeValue + "</td>" +
					  "  <td class='tc-bline'><img src=google_map_logo.gif width=50 height=0 border=0></td>" +
			          "  <td class='tc-e tc-bline'>&nbsp;</td>" +
			          "</tr>";
	      }
	  }
	  
	   
	   document.getElementById("WifiLocatorResultsBox").innerHTML = html;
    }
}

window.onload=loadXmlDoc;
