// Liens externes --------------------------------------------------------------

function openSite(strLocationURL) {
  var siteWindow = null;
  siteWindow = window.open(strLocationURL);
}

function checkExternalLinks() {
  var objLinks = document.getElementsByTagName("a");
  for (i=0; i < objLinks.length; i++) {
    if (objLinks[i].href.length > 0) {
      if (objLinks[i].href.indexOf(document.domain) == -1 && objLinks[i].href.indexOf("javascript:") == -1 && objLinks[i].href.indexOf("mailto:") == -1) {
      objLinks[i].href = "javascript: openSite('" + objLinks[i].href + "');";
      }
    }
  }
}

try {
  window.addEventListener("load", checkExternalLinks, true);
}
catch(ex) {
  window.attachEvent("onload", checkExternalLinks);
}



// Vérification Email ----------------------------------------------------------

function validMail() {
  var champ = document.getElementById('email');
  if(champ.value.indexOf('www') >=0 || champ.value.indexOf('@')<0 || champ.value.indexOf('.')<0 || champ.value == '') {
    alert("Veuillez saisir une adresse email valide !");
		champ.focus();
		champ.style.color='#fff';
    champ.style.background='#ff6666';
    return false;
  } else {
    return true;
  }
}



// CSS Browser Selector --------------------------------------------------------

var css_browser_selector = function() {
  var ua=navigator.userAgent.toLowerCase(),is=function(t){
    return ua.indexOf(t) != -1;
  },
  h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;
}();

// Function Ajax 

var http_request = false;
function makePOSTRequest(url, parameters) {
	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;
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function alertContents() {
	if (http_request.readyState == 4) {
		//alert(http_request.status);
		 if (http_request.status == 200) {
				alert(http_request.responseText);
				if(http_request.responseText != 'Code incorrect'){
					document.getElementById('nom').value = '';
					document.getElementById('email_ami').value = '';
					document.getElementById('destinataire').value = '';
					document.getElementById('code').value = '';
					document.getElementById('message').value = '';
					reloadcaptcha();
				}
		 } else {
				alert('There was a problem with the request.');
		 }
	}
}


function sendFriend(){
var nom = document.getElementById('nom').value;
var email = document.getElementById('email_ami').value;
var destinataire = document.getElementById('destinataire').value;
var code = document.getElementById('code').value;
var message = document.getElementById('message').value;	
var poststr = 'nom='+nom+'&email='+email+'&destinataire='+destinataire+'&code='+code+'&message='+message;
if(poststr != '-1'){
	makePOSTRequest('html/envoi_ami/envoi_message.php', poststr);
}

}

// API GoogleMAP ---------------------------------------------------------------

window.onload = function() {
  if (window.onLoad) {
    onLoad();
    var base = new GIcon();
    base.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
    base.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    base.iconSize = new GSize(12, 20);
    base.shadowSize = new GSize(22, 20);
    base.iconAnchor = new GPoint(6, 20);
    base.infoWindowAnchor = new GPoint(5, 1);
    var bleu = new GIcon(base);
 }
}