
//En local
//var pathXML = 'http://192.168.0.6/Elancyl_PL/pdv/recherche_xml.php';

//En ligne
var pathXML = 'recherche_xml.php';

var xhrSiege = null;
var xhrRegion = null;
var xhrVille = null;

var mode_get = false;
var regionGet = '';
var villeGet = '';

function getHR()
{
	var xhr = null; 
	 
	if (window.XMLHttpRequest) // Firefox et autres
	{ 
	   xhr = new XMLHttpRequest();
	} 
	else if(window.ActiveXObject) // Internet Explorer 
	{ 
	   try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP"); 
		}
		catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
	}
	else // XMLHttpRequest non supporté par le navigateur 
	{ 
	   alert("Votre navigateur est trop ancien. Veuillez le mettre &agrave; jour pour continuer la visite."); 
	}
	
	return xhr;
}

function go( c )
{
	if ( !c.data.replace( /\s/g, '') ) {
		c.parentNode.removeChild(c);
	}
}


function clean( d )
{
	var bal = d.getElementsByTagName('*');

	for ( i=0; i < bal.length; i++ )
	{
		a = bal[i].previousSibling;
		
		if ( a && a.nodeType == 3 ) {
			go(a);
		}
		
		b = bal[i].nextSibling;
		
		if ( b && b.nodeType == 3 ) {
			go(b);
		}
	}
	
	return d;
}

function getXML( reponseXML, tagName )
{
	var reponse = clean( reponseXML );
	var xml = reponse.getElementsByTagName( tagName );
	
	return xml;
}

function display( obj, v ) {
	obj.style.display = v;
}

function disabled( obj, v ) {
	obj.disabled = v;
}




function qSiege()
{
	if ( xhrSiege.readyState == 4 )
	{
		
		if ( xhrSiege.status == 200 )
		{
			var xml = getXML( xhrSiege.responseXML, 'resultat' );
			
			if ( xml.length > 0 )
			{
				window.document.getElementById('siegeCoordonnees').innerHTML = xml.item(0).firstChild.data;
				//alert(xml.item(0).firstChild.data);
				display( window.document.getElementById('siege'), 'block' );
				display( window.document.getElementById('regionBlk'), 'none' );
				display( window.document.getElementById('villeBlk'), 'none' );
				display( window.document.getElementById('pdvBtRecherche'), 'none' );
				display( window.document.getElementById('pdvBtRecherche2'), 'block' );
				
				//disabled( window.document.getElementById('pays'), true );
			}
			else {
				window.document.getElementById('siegeCoordonnees').innerHTML = '';
				//alert( 'Aucun siege !' );
				display( window.document.getElementById('siege'), 'none' );
				display( window.document.getElementById('regionBlk'), 'none' );
				display( window.document.getElementById('villeBlk'), 'none' );
				display( window.document.getElementById('pdvBtRecherche'), 'none' );
				display( window.document.getElementById('pdvBtRecherche2'), 'none' );
			}
		}
	}
}

function selectElement( obj, v )
{

	for ( var i = 0; i < obj.length; i++ )
	{
		if ( obj[i].value == v ) {
			obj[i].selected = true;
		}
	}
}

function ucfirst( value ){
	return value.substring(0, 1).toUpperCase() + value.substring(1, value.length).toLowerCase();	
}

function fillSelect( xml, selectObj )
{
	var xml = getXML( xml, 'select' );
	for ( var i = (selectObj.length - 1); i >= 1; i-- ) {
		selectObj.remove(i);
	}
	//alert( "length : " + xml.length );
	for ( i = 0; i < xml.length; i++ ) {
		//alert( xml.item(i).firstChild.data );
		//selectObj.options[i+1] = new Option( xml.item(i).firstChild.data, xml.item(i).firstChild.data.toLowerCase() );
		selectObj.options[i+1] = new Option( ucfirst ( stringReplace(xml.item(i).firstChild.data) ), xml.item(i).firstChild.nextSibling.data );	
	}
}

function stringReplace(str) 
{
  	var strform = new Array('&#243;', '&#260;', '&#261;', '&#262;', '&#263;', '&#279;', '&#280;', '&#281;', '&#282;', '&#321;', '&#322;', '&#322;', '&#323;', '&#324;', '&#346;', '&#347;', '&#377;', '&#378;', '&#379;', '&#380;', '&oacute;', '&oacute;','&#246;', '&#220;', '&#199;','&#38;');
  	var strhtml = new Array('243', '260', '261', '262', '263', '279', '280', '281', '282', '321', '322', '322', '323', '324', '346', '347', '377', '378', '379', '380', '243', '243', '246', '220', '199', '38');
  	for (var i=0; i<strform.length; i++) {
     	str = str.replace(strform[i], String.fromCharCode(strhtml[i]));
  	}
  	return str;
} 

function qRegion()
{
	if ( xhrRegion.readyState == 4 )
	{
		if ( xhrRegion.status == 200 )
		{ 
			fillSelect( xhrRegion.responseXML, window.document.getElementById( 'region' ) );
			display( window.document.getElementById('regionBlk'), 'block' );

			if ( mode_get ) {
				
				selectElement( window.document.getElementById( 'region' ), regionGet );
				window.document.getElementById('region').onchange();
			}
		}
	}
}

function qPdv()
{
	if ( xhrVille.readyState == 4 )
	{
		if ( xhrVille.status == 200 )
		{
			fillSelect( xhrVille.responseXML, window.document.getElementById( 'ville' ) );
			display( window.document.getElementById('villeBlk'), 'block' );
			display( window.document.getElementById('pdvBtRecherche'), 'block' );
			
			if ( mode_get ) {
				selectElement( window.document.getElementById( 'ville' ), villeGet );
				mode_get = false;
			}
		}
	}
}

function qVille( paysIndex, region )
{
	var hr = getHR();
	
	paysIndex--;
	
	if ( hr != null )
	{
		if ( paysIndex >= 0 )
		{
			
			//var nomPays = pays[paysIndex].nom;
			
			//V2
			var idPays = pays[paysIndex].id;
			//
			
			hr.onreadystatechange = qPdv;
			
			xhrVille = hr;
			
			//hr.open( 'GET', pathXML + '?type=region&pays=' + escape( nomPays )  + '&region=' + escape( region ), true );
			
			//V2
			hr.open( 'GET', pathXML + '?type=region&pays=' + escape( idPays )  + '&region=' + escape( region ), true );
			//
			
			hr.send( null );
		}
	}
}

function qPays( index )
{ 
	index--;
	
	var hr = getHR();
	
	if ( hr != null )
	{
		if ( index >= 0 )
		{
			
			if ( pays[index].siege )
			{	
				
				hr.onreadystatechange = qSiege;
				
				xhrSiege = hr;

				//hr.open( 'GET', pathXML + '?type=siege&pays=' + escape( pays[index].nom ), true );
				
				//V2
				hr.open( 'GET', pathXML + '?type=siege&pays=' + escape( pays[index].id ), true );
				//
				
				hr.send( null );
			}
			else
			{
				
				display( window.document.getElementById('siege'), 'none' );
				display( window.document.getElementById('regionBlk'), 'none' );
				display( window.document.getElementById('villeBlk'), 'none' );
				display( window.document.getElementById('pdvBtRecherche2'), 'none' );
				
				hr.onreadystatechange = qRegion;
				
				xhrRegion = hr;

				//hr.open( 'GET', pathXML + '?type=pays&pays=' + escape( pays[index].nom ), true );
				
				//V2
				hr.open( 'GET', pathXML + '?type=pays&pays=' + escape( pays[index].id ), true );
				//
				
				hr.send( null ); 
			}
		}
	}
}

function nouvelleRecherche()
{
	display( window.document.getElementById('siege'), 'none' );
	display( window.document.getElementById('pdvBtRecherche'), 'none' );
	display( window.document.getElementById('pdvBtRecherche2'), 'none' );
	
	//disabled( window.document.getElementById('pays'), false );
}


function checkFormRecherchePDV(){

	if( form.ville.value != ""){
		form.submit();
	}

}
