// global flag
var isIE = false;

// global request and XML document objects
var req;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}
function ajaxauto(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
     
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            
            req.open("GET", url, true);
            req.send();
        }
    }
}
// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            clearTopicList();
            buildTopicList();
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}

// invoked by "Category" select element change;
// loads chosen XML document, clears Topics select
// element, loads new items into Topics select element
function loadDoc(evt) {
    // equalize W3C/IE event models to get event object
    evt = (evt) ? evt : ((window.event) ? window.event : null);
    if (evt) {
        // equalize W3C/IE models to get event target reference
        var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if (elem) {
            try {
                if (elem.selectedIndex > 0) {
                    loadXMLDoc(elem.options[elem.selectedIndex].value);
                }	
            }
            catch(e) {
                var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
                alert("Unable to get XML data:\n" + msg);
                return;
            }
        }
    }
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}

// empty Topics select list content
function clearTopicList() {
    var select = document.getElementById("topics");
    while (select.length > 0) {
        select.remove(0);
    }
}

// add item to select element the less
// elegant, but compatible way.
function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}

// fill Topics select list with items from
// the current XML document
function buildTopicList() {
    var select = document.getElementById("topics");
    var items = req.responseXML.getElementsByTagName("item");
    // loop through <item> elements, and add each nested
    // <title> element to Topics select element
    for (var i = 0; i < items.length; i++) {
        appendToSelect(select, i,
            document.createTextNode(getElementTextNS("", "title", items[i], 0)));
    }
    // clear detail display
    document.getElementById("details").innerHTML = "";
}

// display details retrieved from XML document
function showDetail(evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : null);
    var item, content, div;
    if (evt) {
        var select = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if (select && select.options.length > 1) {
            // copy <content:encoded> element text for
            // the selected item
            item = req.responseXML.getElementsByTagName("item")[select.value];
            content = getElementTextNS("content", "encoded", item, 0);
            div = document.getElementById("details");
            div.innerHTML = "";
            // blast new HTML content into "details" <div>
            div.innerHTML = content;
        }
    }
}

function getcontent( url ) {
	getmenu(url);
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      
     // alert('ajax.php?menu=' + url);
      http_request.open('GET', 'ajax.php?menu=' + url, true);
      http_request.send(null);
      parent.location.hash = '' + url;
	
   }
   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('homecontent').innerHTML = result;            
         	
				/*var sURL = unescape(window.location.pathname);
				window.location.replace( sURL );
				alert('ici');
 				window.location.reload( false );*/

         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   function getcontentcode( url ) {
	getmenu(url);
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      var code =prompt("Entrer le code fourni ");
      http_request.onreadystatechange = alertContents;
      
     // alert('ajax.php?menu=' + url);
      http_request.open('GET', 'ajax.php?menu=' + url +"&code="+code, true);
      http_request.send(null);
      parent.location.hash = '' + url;
	
   }
   
  function getmenu(url )
   {
   	http_request2 = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request2 = new XMLHttpRequest();
         if (http_request2.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request2.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request2 = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request2 = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request2) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request2.onreadystatechange = alertContentsMenu;
      http_request2.open('GET', 'menu.php?menu='+url, true);

      http_request2.send(null);
      http_request3 = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request3 = new XMLHttpRequest();
         if (http_request3.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request3.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request3 = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request3 = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request3) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request3.onreadystatechange = alertContentsMenuTop;
      http_request3.open('GET', 'menutop.php?menu='+url, true);

      http_request3.send(null);
   	
   }

   
    function alertContentsMenu() {
    	

      if (http_request2.readyState == 4) {
         if (http_request2.status == 200) {
            ///alert(http_request.responseText);
            result = http_request2.responseText;
            document.getElementById('leftcol').innerHTML = result;            
         	
				

         } else {
            alert('There was a problem with the request.');
         }
      }
   }
    function alertContentsMenuTop() {
    	

      if (http_request3.readyState == 4) {
         if (http_request3.status == 200) {
            ///alert(http_request.responseText);
            result = http_request3.responseText;
            document.getElementById('menutop').innerHTML = result;            
         	
				

         } else {
            alert('There was a problem with the request.');
         }
      }
   }
	function gethash()
	{
		//alert('hash ' + parent.location.hash);
		if (parent.location.hash != "")
		{
			var url = parent.location.hash.replace("#","");
		getcontent(url);
		}
	}
	
	function VerifMail()
	{
	a = document.newsletter.newsletter.value;
	valide1 = false;
	
	for(var j=1;j<(a.length);j++){
		if(a.charAt(j)=='@'){
			if(j<(a.length-4)){
				for(var k=j;k<(a.length-2);k++){
					if(a.charAt(k)=='.') valide1=true;
				}
			}
		}
	}
	if(valide1==false) alert("Veuillez saisir une adresse email valide.");
	return valide1;
	}
	
