/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function addInfoPopup(content) {
    //close_div
    if (!$('infoPopup-overlay')){
        bod             = document.getElementsByTagName('body')[0];
	overlay         = document.createElement('div');
	overlay.id      = 'infoPopup-overlay';
        bod.appendChild(overlay);
    } else {
        overlay = $('infoPopup-overlay');
        overlay.className = '';
    }

    if (!$('infopopup')){
        raf             = document.createElement('div');
        raf.id          = 'infopopup';
        raf.className 	= 'timetable_tnt';
        raf.innerHTML	= content;
        overlay.appendChild(raf);

        close_div             = document.createElement('div');
        close_div.id          = 'close_tnt';
        close_div.className 	= 'close_tnt';
        close_div.innerHTML	= '<a onclick="$(\'infoPopup-overlay\').className=\'close_div\'; return false;" href="#">&nbsp;</a>';
        raf.appendChild(close_div);

    } else {
        raf = $('infopopup');
        raf.innerHTML	= content;
        close_div             = document.createElement('div');
        close_div.id          = 'close_tnt';
        close_div.className 	= 'close_tnt';
        close_div.innerHTML	= '<a onclick="$(\'infoPopup-overlay\').className=\'close_div\'; return false;" href="#">&nbsp;</a>';
        raf.appendChild(close_div);
    }
    //background:#fff; width:250px; height:130px; z-index:999999; position:absolute; top:400px; left:600px; font-size:10px;
    //alert(content);

    
    var elt         = $('infoPopup-overlay');
    // retrieve required dimensions
    var eltDims     = elt.getDimensions();
    var browserDims = document.body.getDimensions();

    // calculate the center of the page using the browser and element dimensions
    var y  = (browserDims.height - eltDims.height) / 2;
    var x = (browserDims.width - eltDims.width) / 2;

    // set the style of the element so it is centered
    var styles = { position : 'absolute',
            top      : y + 'px',
            left     : x + 'px' };
    elt.setStyle(styles);
    //elt.scrollTo();
}


function getRelaiDetails(url_site, postcode, town, relais){
    //http://www.tnt.fr/public/b2c/relaisColis/load.do?cp=".$postcode."&commune=".$communes->VILLE
    //http://amillet.developpez.com/tutoriels/javascript/ajax-prototype/
    //alert(relais);

    new Ajax.Request(
            /*'http://www.tnt.fr/public/b2c/relaisColis/load.do',*/
            url_site+'tnt_proxy/proxy.php?url=http://www.tnt.fr/public/b2c/relaisColis/load.do?cp='+postcode+'%26commune='+town,
            {
                    method: 'get',
                    parameters: {cp: postcode, commune: town},
                    onComplete: function(xhr) {
                        //alert(xhr.responseText);
                        var nodeR = xhr.responseXML;
                        //alert('ici');
                        var nodesPR = nodeR.firstChild.getElementsByTagName('POINT_RELAIS');
                        //alert('la');

                       
                        var output = '';
                        var outputHTML = '';

                        for (i=0 ; i<nodesPR.length ; i++)
                        {
                            //alert('coucou');
                            var nodePR = nodesPR[i];
                            var attributes = nodePR.attributes;
                            //var id = attributes.getNamedItem('id').nodeValue;

                            var nodeCdeR = nodePR.getElementsByTagName('CODE_RELAIS')[0];
                            var cdeR = nodeCdeR.firstChild.nodeValue;
                            //alert(cdeR +' == '+ relais);

                            if (cdeR == relais){
                                var nodeNom = nodePR.getElementsByTagName('NOM_RELAIS')[0];
                                var nom = nodeNom.firstChild.nodeValue;
                                
                                var lundiAM = "";
                                var lundiPM = "";
                                var nodeLAM = nodePR.getElementsByTagName('LUNDI_AM')[0];
                                if (nodeLAM.firstChild)
                                    lundiAM = nodeLAM.firstChild.nodeValue;
                                var nodeLPM = nodePR.getElementsByTagName('LUNDI_PM')[0];
                                if (nodeLPM.firstChild)
                                    lundiPM = nodeLPM.firstChild.nodeValue;

                                

                                var mardiAM = "";
                                var mardiPM = "";
                                var nodeMA = nodePR.getElementsByTagName('MARDI_AM')[0];
                                if (nodeMA.firstChild)
                                    mardiAM = nodeMA.firstChild.nodeValue;
                                var nodeMP = nodePR.getElementsByTagName('MARDI_PM')[0];
                                if (nodeMP.firstChild)
                                    mardiPM = nodeMP.firstChild.nodeValue;

                                var mercrediAM = "";
                                var mercrediPM = "";
                                var nodeMEA = nodePR.getElementsByTagName('MERCREDI_AM')[0];
                                if (nodeMEA.firstChild)
                                    mercrediAM = nodeMEA.firstChild.nodeValue;
                                var nodeMEP = nodePR.getElementsByTagName('MERCREDI_PM')[0];
                                if (nodeMEP.firstChild)
                                    mercrediPM = nodeMEP.firstChild.nodeValue;

                                var jeudiAM = "";
                                var jeudiPM = "";
                                var nodeJA = nodePR.getElementsByTagName('JEUDI_AM')[0];
                                if (nodeJA.firstChild)
                                    jeudiAM = nodeJA.firstChild.nodeValue;
                                var nodeJP = nodePR.getElementsByTagName('JEUDI_PM')[0];
                                if (nodeJP.firstChild)
                                    jeudiPM = nodeJP.firstChild.nodeValue;

                                
                                var vendrediAM = "";
                                var vendrediPM = "";
                                var nodeVA = nodePR.getElementsByTagName('VENDREDI_AM')[0];
                                if (nodeVA.firstChild)
                                    vendrediAM = nodeVA.firstChild.nodeValue;
                                var nodeVP = nodePR.getElementsByTagName('VENDREDI_PM')[0];
                                if (nodeVP.firstChild)
                                    vendrediPM = nodeVP.firstChild.nodeValue;

                                
                                var samediAM = "";
                                var samediPM = "";
                                var nodeSA = nodePR.getElementsByTagName('SAMEDI_AM')[0];
                                if (nodeSA.firstChild)
                                    samediAM = nodeSA.firstChild.nodeValue;
                                var nodeSP = nodePR.getElementsByTagName('SAMEDI_PM')[0];
                                if (nodeSP.firstChild)
                                    samediPM = nodeSP.firstChild.nodeValue;

                                
                                var dimancheAM = "";
                                var dimanchePM = "";

                                //nodePR.getElementsByTagName('DIMANCHE_AM').length

                                if (nodePR.getElementsByTagName('DIMANCHE_AM')){
                                    var nodeDA = nodePR.getElementsByTagName('DIMANCHE_AM')[0];
                                    if (nodeDA.firstChild)
                                        dimancheAM = nodeDA.firstChild.nodeValue;
                                }
                                
                                if (nodePR.getElementsByTagName('DIMANCHE_PM')){
                                    var nodeDP = nodePR.getElementsByTagName('DIMANCHE_PM')[0];
                                    if (nodeDP.firstChild)
                                        dimanchePM = nodeDP.firstChild.nodeValue;
                                }
  

                                //alert(dimanchePM);

                                outputHTML += '<ul class="schedule_tnt"><li><strong>RELAIS : '+nom.toUpperCase()+ "</strong></li>";
                                outputHTML += '<li>Lundi : '+lundiAM+' / '+lundiPM+ '</li>';
                                outputHTML += '<li>Mardi : '+mardiAM+' / '+mardiPM+'</li>';
                                outputHTML += '<li>Mercredi : '+mercrediAM+' / '+mercrediPM+'</li>';
                                outputHTML += '<li>Jeudi : '+jeudiAM+' / '+jeudiPM+'</li>';
                                outputHTML += '<li>Vendredi : '+vendrediAM+' / '+vendrediPM+'</li>';
                                outputHTML += '<li>Samedi : '+samediAM+' / '+samediPM+'</li>';
                                outputHTML += '<li>Dimanche : '+dimancheAM+' / '+dimanchePM+'</li>';
                                outputHTML += '</ul>';
                                
                                output += 'RELAIS : '+nom.toUpperCase()+ "\n";
                                output += 'Lundi : '+lundiAM+' / '+lundiPM+ "\n";
                                output += 'Mardi : '+mardiAM+' / '+mardiPM+"\n";
                                output += 'Mercredi : '+mercrediAM+' / '+mercrediPM+"\n";
                                output += 'Jeudi : '+jeudiAM+' / '+jeudiPM+"\n";
                                output += 'Vendredi : '+vendrediAM+' / '+vendrediPM+"\n";
                                output += 'Samedi : '+samediAM+' / '+samediPM+"\n";
                                output += 'Dimanche : '+dimancheAM+' / '+dimanchePM+"\n";

                                //alert(output);


                            }
                        }
                        //alert(output);
                        //Dialog.alert(output, {width:300, height:100, okLabel: "close", ok:function(win) {debug("validate alert panel"); return true;}});
                        addInfoPopup(outputHTML);
                    },
                    onSuccess: function() {
                        //alert('Requête terminée avec succès.');
                    },
                    onFailure: function() { alert('Requête échouée.') }
            }
    );
    //alert('test');
}
