function addToFavorites(url, titre) {	
	if (navigator.appName != 'Microsoft Internet Explorer' ) {
		window.sidebar.addPanel(titre,url,'');
	} else {
		window.external.AddFavorite(url, titre);
	}
}

function linkToPopup(){
	var link = document.getElementById("sendFriendLink");
	if(link){
		link.onclick=popup;
	}
}

function popup(){
	var url = this.href;    
	window.open(url, 'popup', 'directories=0,resizable=1,scrollbars=yes,location=0,menubar=0,status=0,toolbar=0,top=0,left=0,width=800,height=450');
	return ( false );
}
function loadNewPage(){
	var selector=document.getElementById("selector");
	var url = selector.options[selector.selectedIndex].value;
	location.href=url;
}

function initSelector(){
	var selector=document.getElementById("selector");
	selector.onchange=loadNewPage;
}

function initPopin(){
	var lienPopin = document.getElementById("lienPopin");
	var reponseFacture = document.getElementById("reponseFacture");
	if(lienPopin && reponseFacture){
		lienPopin.onclick=makePopin;
	}
}


function makePopin() {
	var newChild = document.createElement("div");
	newChild.id="bigWindow";
	newChild.onclick=removePopin;
	document.body.appendChild( newChild );
	//newChild.style.width = getViewPortDimension()[0] + "px";
	
	var reponseFacture = document.getElementById("reponseFacture");
	var content = reponseFacture.innerHTML;
	var popin = document.createElement("div");
	popin.id="popin";
	popin.innerHTML = content;
	document.body.appendChild( popin ); 
	
	
	var d = getViewPortDimension()[0] - 910;
	var val =Math.floor(d/2) ;
	if(val < 0) val = 0; 
	var p=document.getElementById("popin");
	p.style.left = val + "px";
	
	var lienPopin = document.getElementById("lienPopin");
	lienPopin.onclick="";
}
function getViewPortDimension(){
	 var viewportwidth;
	 var viewportheight;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
	      viewportwidth = window.innerWidth,
	      viewportheight = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined'
	     && typeof document.documentElement.clientWidth !=
	     'undefined' && document.documentElement.clientWidth != 0)
	 {
	       viewportwidth = document.documentElement.clientWidth,
	       viewportheight = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 
	 else
	 {
	       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	       viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
	 var r = new Array(viewportwidth, viewportheight );
	 return r ;
}
function removePopin(){
	  var d = d = document.body;
	  var olddiv = document.getElementById('popin');
	  d.removeChild(olddiv);
	  olddiv = document.getElementById('bigWindow');
	  d.removeChild(olddiv);
	  initPopin();
}



function stripSpaces(myString){
	var expr = new RegExp(/^\s*(.+)\s*$/i);
	expr.exec( myString );
	return RegExp.lastParen;
}





function initFormFocus(){
	var inputs = document.getElementsByTagName("input");
	var textareas = document.getElementsByTagName("textarea");
	var myCollection = new Array();
	for(var i=0 ; i< inputs.length ; i++){
		if( inputs[i].type.toLowerCase() == "text" ){
			myCollection.push(inputs[i]);
		}
	}
	for(var i=0 ; i< textareas.length ; i++){
			myCollection.push(textareas[i]);
	}
	for(var i=0 ; i< myCollection.length ; i++){
			myCollection[i].oldBg = myCollection[i].style.backgroundColor;
			myCollection[i].onblur=removeFocus;
			myCollection[i].onfocus=setFocus;
	}
}


var colorFocus = "#fffdaa";
function removeFocus(){
	this.style.backgroundColor = this.oldBg;
	this.className=this.className.replace("notValid","");
}
function setFocus(){
	this.style.backgroundColor = colorFocus;
}

function checkEmailRegex(email) {
   var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/i;
   return (reg.exec(email)!=null)
}
function overline(item){
	item.className = item.className + " notValid";
}


function checkFormByCSS(f){
	var returnValue=true;
	var e = f.elements;
	for(var i = 0 ; i < e.length ; i++){
		var item = e[i];
		item.className=item.className.replace("notValid", "");
		var notNull = item.className.match("checkNotNull");
		var email = item.className.match("checkMail");
		if	(notNull || email){
			var value;
			try{
				value = item.value;
				}catch(er){}
			if(value == undefined && item.innerHTML){
				value = item.innerHTML;
			}
			value = stripSpaces(value);
				if (
					notNull && value.match(/^\s*$/gi)
					|| email &&  !checkEmailRegex(value)
					)
					{
						overline(item);
						returnValue=false;
					}
			
			
			
		}
	}
	if(!returnValue) document.getElementById("alert").style.visibility="visible";
	return returnValue;
}









function init(){
	initSelector();
	initPopin();	
	initFormFocus();
	linkToPopup();
}
window.onload = init;



