var lastLoaded = document.location.href;

function getParentHref(elem){
	while(elem){
		if(elem && (elem.tagName == "A" || elem.tagName == "AREA") && elem.getAttribute("href")){
			return elem.getAttribute("href");
		}
		elem = elem.parentNode;
	}
}

function loadContent(evt){
	if(!document.addEventListener){
		evt  = window.event;
		evt.target = evt.srcElement;
		evt.returnValue = false;
	} else {
		evt.preventDefault();
	}
/*
	if(evt.target.tagName != "A" && evt.target.tagName != "AREA"){
		src = evt.target.parentNode.getAttribute("href");
	} else {
		src = evt.target.getAttribute("href");
	}
*/
	src = getParentHref(evt.target);
	lastLoaded = src;
	loadContentStr(src);

	return false;
}

function form2Ajax(){
	formAr = document.getElementsByTagName("form");
	for(i=0; formAr[i]; i++){
		if(document.addEventListener){
			formAr[i].addEventListener("submit", submitForm, false);
		} else {
			formAr[i].attachEvent("onsubmit", submitForm); 
		}
	}
	if(formAr.length > 1){
		setAllRestraints(); 
		signupFrmAutoValidate();
	}
	
}

function loadContentStr(src){
	var xmlHttpReq = false;
	if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if(!src.match(/\?/)){
		src += "?ajax=1";
	} else {
		src += "&ajax=1";
	}

	xmlHttpReq.open('GET', src+"?ajax=1", true);
	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() { 
		if (xmlHttpReq.readyState == 4) {
			if(src.match("logout.php")){ doLogout(); } 
			if(xmlHttpReq.responseXML) {
				if( xmlHttpReq.responseXML.getElementsByTagName("data")[0]){
					document.getElementById("contentZone").innerHTML = xmlHttpReq.responseXML.getElementsByTagName("data")[0].firstChild.nodeValue; 
					replaceAnchors();
				}
				if(xmlHttpReq.responseXML.getElementsByTagName("blink")[0] && xmlHttpReq.responseXML.getElementsByTagName("blink")[0].firstChild && document.getElementsByName("blinker").length==2){
					if(xmlHttpReq.responseXML.getElementsByTagName("blink")[0].firstChild.nodeValue == "1"){
						document.getElementsByName("blinker")[0].style.display = "";
						document.getElementsByName("blinker")[1].style.display = "";
					} else {
						document.getElementsByName("blinker")[0].style.display = "none";
						document.getElementsByName("blinker")[1].style.display = "none";
					}
				}
			}
		}
	}
	xmlHttpReq.send(null); 

	return false;
}

function replaceAnchors(){		
	aAr   = document.getElementsByTagName("a");
	mapAr = document.getElementsByTagName("area");
		
	for(i=0; aAr[i]; i++){
		//if(aAr[i].getAttribute("class") && !aAr[i].getAttribute("class").match("stdLink") ){
			if(document.addEventListener){	
				if(!aAr[i].getAttribute("href").match("/") && (aAr[i].getAttribute("href").match(".html") || aAr[i].getAttribute("href").match(".php"))  ){
					//aAr[i].removeEventListener("click", loadContent, false);
					aAr[i].addEventListener("click", loadContent, false);
				}
			} else {
				if(aAr[i].getAttribute("href").match("localhost") || aAr[i].getAttribute("href").match("jonathan") || aAr[i].getAttribute("href").match("homardavendre.ca") || aAr[i].getAttribute("href").match("homardavendre.com") && (aAr[i].getAttribute("href").match(".html") || aAr[i].getAttribute("href").match(".php"))  ){
					if(aAr[i].getAttribute("href").match(".html") || aAr[i].getAttribute("href").match(".php")){
						aAr[i].detachEvent("onclick", loadContent);
						aAr[i].attachEvent("onclick", loadContent);
					}
				}
			}
		//}
	}

	for(i=0; mapAr[i]; i++){
		if(document.addEventListener){	
			if(!mapAr[i].getAttribute("href").match("/") && (mapAr[i].getAttribute("href").match(".html") || mapAr[i].getAttribute("href").match(".php"))  ){
				//mapAr[i].removeEventListener("click", loadContent, false);
				mapAr[i].addEventListener("click", loadContent, false);
			}
		} else {
			if(mapAr[i].getAttribute("href").match("localhost") || mapAr[i].getAttribute("href").match("jonathan") || mapAr[i].getAttribute("href").match("percenautic.com") || mapAr[i].getAttribute("href").match("percenautic.ca") && (mapAr[i].getAttribute("href").match(".html") || mapAr[i].getAttribute("href").match(".php"))  ){
				mapAr[i].detachEvent("onclick", loadContent);
				mapAr[i].attachEvent("onclick", loadContent);
			}
		}
	}


	form2Ajax();

}

function submitForm(evt){		// We'll always use POST
	if(!document.addEventListener){
		evt  = window.event;
		evt.target = evt.srcElement;
	} else {
		evt.preventDefault();
	}

	query = "";
	for(i=0; evt.target.elements[i]; i++){
		query += "&"+evt.target.elements[i].getAttribute("name")+"="+escape(evt.target.elements[i].value);
	}

	var xmlHttpReq = false;
	if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}


	xmlHttpReq.open('POST', evt.target.action+"?ajax=1", true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() { 
		if (xmlHttpReq.readyState == 4) {
			if(evt.target.getAttribute("name") == "loginForm" || evt.target.getAttribute("name") == "pageLoginForm"){
				if(xmlHttpReq.responseXML.getElementsByTagName("logged")[0].firstChild.nodeValue == "O"){
					evt.target.reset();
					showLogged();
					if(xmlHttpReq.responseXML.getElementsByTagName("blink")[0] && xmlHttpReq.responseXML.getElementsByTagName("blink")[0].firstChild && document.getElementsByName("blinker").length==2){
						if(xmlHttpReq.responseXML.getElementsByTagName("blink")[0].firstChild.nodeValue == "1"){
							document.getElementsByName("blinker")[0].style.display = "";
							document.getElementsByName("blinker")[1].style.display = "";
						} else {
							document.getElementsByName("blinker")[0].style.display = "none";
							document.getElementsByName("blinker")[1].style.display = "none";
						}
					}
				} else {
					document.getElementById("contentZone").innerHTML = xmlHttpReq.responseXML.getElementsByTagName("logged")[0].firstChild.nodeValue;	
				}
			} else {
				if(xmlHttpReq.responseXML){
					document.getElementById("contentZone").innerHTML = xmlHttpReq.responseXML.getElementsByTagName("data")[0].firstChild.nodeValue;		
				}
			}
			form2Ajax();
		}
	}
	
    xmlHttpReq.send(query);

	return false;

}

function showLogged(){		
	document.getElementById("loginBox").style.display  = "none";
	document.getElementById("logoutBox").style.display = "";
	loadContentStr(lastLoaded);
}

function doLogout(){
	document.getElementById("loginBox").style.display  = "";
	document.getElementById("logoutBox").style.display = "none";
}

function prodDetail(which){
	document.location.href = "prodDetail.php?which="+which;
}



function fixFlash(){
  if (document.getElementsByTagName) {
      var objs = document.getElementsByTagName("object");
      for (i=0; i<objs.length; i++) {
        objs[i].outerHTML = objs[i].outerHTML;
      }
   }
}

if(!document.addEventListener){
	window.attachEvent("onload", fixFlash);
}


