var pageWidth=0;

i1=new Image;
i1.src='images/structure/tram.jpg';
i2=new Image;
i2.src='images/structure/categ_bg.png';
i3=new Image;
i3.src='images/structure/cadres/btnCadre1Over.jpg';
i4=new Image;
i4.src='images/structure/cadres/btnCadre2Over.jpg';
i5=new Image;
i5.src='images/structure/cadres/btnCadre3Over.jpg';
i20=new Image;
i20='images/structureInt/tramGenerale.jpg';

function montreoverlay() {
  if (w3c||ie){
	overlay = document.all ? document.all["overlay_general"] : document.getElementById ? document.getElementById("overlay_general") : ""
	overlay.innerHTML = ""; // fixe le code HTML dans l'overlay balise (div)
    overlay.style.visibility = "visible"; // modification du style
	document.body.scroll="no";
  }
}
function montreoverlay360() {
  if (w3c||ie){
	overlay = document.all ? document.all["overlay_general"] : document.getElementById ? document.getElementById("overlay_general") : ""
	overlay.innerHTML = ""; // fixe le code HTML dans l'overlay balise (div)
    overlay.style.visibility = "visible"; // modification du style
	document.body.scroll="no";
  }
}


function affiche_overlay_windowimage(productname, productdescription,web, productplus, productimg){
	// creation de l'overlay et affichage de l'image
	montreoverlay360();
	// creation de la fenêtre
	montrefenetreimage(productname, productdescription,web, productplus, productimg);


}


function montrefenetreimage(productname, productdescription,web, productplus, productimg) {
  if (w3c||ie){
    //affichage de la fenetre
	my_window_back = document.all ? document.all['windowback'] : document.getElementById ? document.getElementById('windowback') : ""
    my_window_back.style.visibility = "visible";
	my_window = document.all ? document.all['window'] : document.getElementById ? document.getElementById('window') : ""
    my_window.style.visibility = "visible";

	// affichage du corps de la fenêtre (balise iframe)
    my_window = document.all ? document.all['contempwindow'] : document.getElementById ? document.getElementById('contempwindow') : ""
    my_window.style.visibility = "visible";

	contenuformulaire = "<a href='#' onclick='cachetout360();return false;'><img align='right' src='../../images/closelabel.gif' style='margin-right:15px;'></a><br>"+"<span style='font-family:Arial;font-size:11px;color:#646464;'><b>"+ productname +"</b><br />"+ productdescription +"<br /><div class='btnProd'><a href='"+ web +"' target='_blank' class='lienFil' style='font-size:12px;padding-top:5px;'>"+ productplus +"</a></div><br /><a href='#' onclick='cachetout360();return false;'>"+ productimg +"</a><br><br></span>";
	my_window.innerHTML=contenuformulaire;

	
  }
}

function affiche_overlay_windowimageproperty(property, image, image2, image3, image4, image5, link){
	// creation de l'overlay et affichage de l'image
	montreoverlay360();
	// creation de la fenêtre
	montrefenetreimageproperty(property, image, image2, image3, image4, image5, link);


}


function montrefenetreimageproperty(property, image, image2, image3, image4, image5, link) {
  if (w3c||ie){
    //affichage de la fenetre
	my_window_back = document.all ? document.all['windowback'] : document.getElementById ? document.getElementById('windowback') : ""
    my_window_back.style.visibility = "visible";
	my_window = document.all ? document.all['window'] : document.getElementById ? document.getElementById('window') : ""
    my_window.style.visibility = "visible";

	// affichage du corps de la fenêtre (balise iframe)
    my_window = document.all ? document.all['contempwindow'] : document.getElementById ? document.getElementById('contempwindow') : ""
    my_window.style.visibility = "visible";

	contenuformulaire = "<a href='#' onclick='cachetout360();return false;'><img align='right' src='../../images/closelabel.gif' style='margin-right:15px;'></a><br /><span style='font-family:Arial;font-size:11px;color:#646464;margin-left:5px;margin-top:5px;'>" + 
  property + "</span><div class='btnProd'><a href='"+ link +"' target='_blank' class='lienFil' style='font-size:11px;'> GO </a></div><br />"
  + "<div style='width:500px;text-align:center;'><a href='#' onclick='cachetout360();return false;'><img width='0' id='currentimage' align='middle' src='"+ image +"' onload=\"jsOnload(375,this,'"+ image + "')\"></a></div><br />"
  + "<a href='#' onclick=\"document.getElementById('currentimage').src='" + image + "';return false;\"><img width='0' align='middle' style='margin:5px;' src='"+ image +"' onload=\"jsOnload(80,this,'"+ image + "')\"></a>"
  + "<a href='#' onclick=\"document.getElementById('currentimage').src='" + image2 + "';return false;\"><img width='0' align='middle' style='margin:5px;' src='"+ image2 +"' onload=\"jsOnload(80,this,'"+ image2 + "')\"></a>"
  + "<a href='#' onclick=\"document.getElementById('currentimage').src='" + image3 + "';return false;\"><img width='0' align='middle' style='margin:5px;' src='"+ image3 +"' onload=\"jsOnload(80,this,'"+ image3 + "')\"></a>"
  + "<a href='#' onclick=\"document.getElementById('currentimage').src='" + image4 + "';return false;\"><img width='0' align='middle' style='margin:5px;' src='"+ image4 +"' onload=\"jsOnload(80,this,'"+ image4 + "')\"></a>"
  + "<a href='#' onclick=\"document.getElementById('currentimage').src='" + image5 + "';return false;\"><img width='0' align='middle' style='margin:5px;' src='"+ image5 +"' onload=\"jsOnload(80,this,'"+ image5 + "')\"></a>"
  ;
	my_window.innerHTML=contenuformulaire;

	
  }
}


function cachetout360() {
	if (w3c||ie){

	// masque la fenetre (balise div [window] )
	my_window = parent.document.getElementById('window');
	my_window.style.visibility = "hidden";
	my_window_back = parent.document.getElementById('windowback');
	my_window_back.style.visibility = "hidden";
	// masque le contenu (balise iframe [contempwindow])
	my_window = parent.document.getElementById('contempwindow');
	my_window.style.visibility = "hidden";

	// masque l'overlay (balise div [overlay])
	my_window = document.all ? parent.document.all['overlay_general'] : parent.document.getElementById ? parent.document.getElementById('overlay_general') : ""
	my_window.style.visibility = "hidden";
	document.body.scroll="yes";
	}


}

function isMail(field){
  var email = document.getElementById(field).value;
	return isStringMail(email);
}
function isStringMail(str){
  var verif=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]{2,}[.][a-zA-Z]{2,6}$/
	if(verif.exec(str)==null){ return false; }else{ return true; }
}

function estVide(field)
{
	var champs = document.getElementById(field).value;
	champs = champs.replace(/ /g,'');// enleve les espaces
	champs = champs.replace(/([\n])/g,''); // enleve les retours a la ligne
	champs = champs.replace(/([\r])/g,''); // enleve les retours a la ligne
	champs = champs.replace(/([\t])/g,''); // enleve les tabulations
	if (champs=='') {
		return true;
	} else {
		return false;
	}
}

function IsNumeric(sText)
//vérifie si la chaine envoyée est de type numérique
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


function submitForm(num,name,pseudo,gender,eula,mail,email)
{
var num=num;
var name=name;
var pseudo=pseudo;
var gender=gender;
var eula=eula;
var mail=mail;
var email=email;

	if (num==1)
	{	
		if (isMail('ADR_EMAIL'))
		{
			mail1 = document.Membre1.elements['ADR_EMAIL'].value;
			mail2 = document.Membre1.elements['ADR_EMAIL2'].value;
			if (mail1 == mail2)
			{
				Nom = document.Membre1.elements['NOM'].value;
				Prenom = document.Membre1.elements['PRENOM'].value;
				pseudo = document.Membre1.elements['ADR_EMAIL'].value;

				if ((Nom == "") || (Prenom ==""))
				{
					alert(name);
					document.Membre1.elements['NOM'].focus();
					document.Membre1.elements['NOM'].style.borderColor="red";
					return false;
				}
				else
				{
					if (pseudo == "" || pseudo.length<4)
					{
					alert(pseudo);
					document.Membre1.elements['LOGIN'].focus();
					document.Membre1.elements['LOGIN'].style.borderColor="red";
					}
					else
					{
					if (document.Membre1.elements['GENRE'].value == "")
					{
						// Reponse à la Civilité => erreur
						alert(gender);
						document.Membre1.elements['GENRE'].focus();
						document.Membre1.elements['GENRE'].style.borderColor="red";
						return false;
					}
					else
					{
						if (document.Membre1.elements['ACCEPTE_CONDITIONS']) {
							if (document.Membre1.elements['ACCEPTE_CONDITIONS'].checked == true)
							{
								return true;
							}
							else
							{
								alert(eula);
								document.Membre1.elements['ACCEPTE_CONDITIONS'].focus();
								document.Membre1.elements['ACCEPTE_CONDITIONS'].style.borderColor="red";
								return false;
							}								
						}
						else								
						{
							return true;
						}
					}
				}
			}
			}
			else
			{
				alert(mail);
				document.Membre1.elements['ADR_EMAIL2'].focus();
				document.Membre1.elements['ADR_EMAIL2'].style.borderColor="red";
				return false;
			}
		}
		else
		{
			// Format mail incorrect
			alert(email);
			document.Membre1.elements['ADR_EMAIL'].focus();
			document.Membre1.elements['ADR_EMAIL'].style.borderColor="red";
			verrou = false ;
			return false;
		}
	}
	if (num==2)
	{
		if (IsNumeric(document.getElementById('ad_cp').value)==true && document.getElementById('ad_cp').value.length==5)
				{
					if (IsNumeric(document.getElementById('ad_nb').value)==true)
						{
							if (IsNumeric(document.getElementById('rooms').value)==true && document.getElementById('rooms').value>0)
								{
									if (IsNumeric(document.getElementById('sq').value)==true && document.getElementById('sq').value>10)
										{
											//if(document.getElementById('ad_status').value!=0)
												//{
													if(document.getElementById('ad_type').value!=0)
														{
															return true;
														}
													else{
														alert(email);
														document.myhome.elements['ad_type'].focus();
														document.myhome.elements['ad_type'].style.borderColor="red";
														return false;
													}
												//}
											//else{
											//	alert(mail);
											//	document.myhome.elements['ad_status'].focus();
											//	document.myhome.elements['ad_status'].style.borderColor="red";
											//	return false;
											//}
										}
										else{
											alert(eula);
											document.myhome.elements['sq'].focus();
											document.myhome.elements['sq'].style.borderColor="red";
											return false;
										}
								}
							else{
								alert(gender);
								document.myhome.elements['rooms'].focus();
								document.myhome.elements['rooms'].style.borderColor="red";
								return false;
							}
						}
					else{
						alert(pseudo);
						document.myhome.elements['ad_nb'].focus();
						document.myhome.elements['ad_nb'].style.borderColor="red";
						return false;
					}
				}
				else{
					alert(name);
					document.myhome.elements['ad_cp'].focus();
					document.myhome.elements['ad_cp'].style.borderColor="red";
					return false;
				}
	}
}

function envoi(num,name,pseudo,gender,eula,mail,email)
{
	var test=submitForm(num,name,pseudo,gender,eula,mail,email);
	if (test == true)
	{
		document.Membre1.submit();
	}
}

function envoimyhome(num,name,pseudo,gender,eula,mail,email)
{
	
	var test=submitForm(num,name,pseudo,gender,eula,mail,email);
	if (test == true)
	{
		document.myhome.submit();
	}
}

function getSize() {

if( window.innerHeight && window.scrollMaxY ) {
pageWidth = window.innerWidth + window.scrollMaxX;
pageHeight = window.innerHeight + window.scrollMaxY;
}
else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
{
pageWidth = document.body.scrollWidth;
pageHeight = document.body.scrollHeight;
}
else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
{ pageWidth = document.body.offsetWidth + document.body.offsetLeft; pageHeight = document.body.offsetHeight + document.body.offsetTop; }


  return pageHeight;
}

function similar(productid){
var xhr_object = null;   
  
if(window.XMLHttpRequest) // Firefox   
   xhr_object = new XMLHttpRequest();   
else if(window.ActiveXObject) // Internet Explorer   
   xhr_object = new ActiveXObject("Microsoft.XMLHTTP");   
else { // XMLHttpRequest non supporté par le navigateur   
   alert("XMLHTTPRequest objects not supported by browser, sorry");   
   return;   
 }   
   
document.getElementById('ajaxcontent').innerHTML="<div style='text-align:center;margin-top:150px;'><img src='../../images/ajax-loader.gif'></div>";
		 montreoverlay();	my_window_back = document.all ? document.all['windowback'] : document.getElementById ? document.getElementById('windowback') : ""
    my_window_back.style.visibility = "visible";
    
    my_window = document.all ? document.all['similarproducts'] : document.getElementById ? document.getElementById('similarproducts') : ""
    my_window.style.visibility = "visible";

xhr_object.open("GET", "../../js/ajax.php?similarproducts&productid=" + productid, false);   
xhr_object.send(null);   
	if(xhr_object.readyState == 4) {
		document.getElementById('ajaxcontent').innerHTML=xhr_object.responseText;
	}
}


function cachetout(popup) {
	if (w3c||ie){
	// masque la fenetre (balise div [window] )
	my_window = document.getElementById(popup);
	my_window.style.visibility = "hidden";
	my_window_back = document.getElementById('windowback');
	my_window_back.style.visibility = "hidden";
	// masque le contenu (balise iframe [contempwindow])
	my_window = document.getElementById('contempwindow');
	my_window.style.visibility = "hidden";

	// masque l'overlay (balise div [overlay])
	if(!document.getElementById('overlay')){
	   my_window = document.all ? document.all['overlay_general'] : document.getElementById ? document.getElementById('overlay_general') : ""}
	else{
      my_window = document.all ? document.all['overlay'] : document.getElementById ? document.getElementById('overlay') : ""}
	   my_window.style.visibility = "hidden";
	document.body.scroll="yes";
	}

}


function comment(productid,type,note,id){

    var xhr_object = null;   
      
    if(window.XMLHttpRequest) // Firefox   
       xhr_object = new XMLHttpRequest();   
    else if(window.ActiveXObject) // Internet Explorer   
       xhr_object = new ActiveXObject("Microsoft.XMLHTTP");   
    else { // XMLHttpRequest non supporté par le navigateur   
       alert(" XMLHTTPRequest objects not supported by browser, sorry !");   
       return;   
     }   
       
    document.getElementById('commentpopup').innerHTML="<div style='text-align:center;margin-top:150px;'><img src='../../images/ajax-loader.gif'></div>";
    		 montreoverlay();	my_window_back = document.all ? document.all['windowback'] : document.getElementById ? document.getElementById('windowback') : ""
        my_window_back.style.visibility = "visible";
        
        my_window = document.all ? document.all['commentpopup'] : document.getElementById ? document.getElementById('commentpopup') : ""
        my_window.style.visibility = "visible";
    xhr_object.open("GET", "../../js/ajax.php?comment&note=" + note + "&id=" + productid + "&type=" + type + "&starid='" + id + "'", false);   
    
    xhr_object.send(null);   
    	if(xhr_object.readyState == 4) {
    		document.getElementById('commentpopup').innerHTML=xhr_object.responseText;
    	}
   
    averagevalue=document.getElementById('avg').value;
    comments=document.getElementById('nbofstarbox').value;
    usernoteinput=document.getElementById('usernote').value;
    trackinput=document.getElementById('track').value;
    if(trackinput>0){finalvalue=usernoteinput;}else{finalvalue=note;}
    if(type=="view"){
      new Starbox("ratingglobal", averagevalue, {overlay: 'big.png', color:'#8bc21e', hoverColor:'#7fb11b', locked: true});
      for(i=0;i<comments;i++){
        currentstarbox="name"+i;
        currentstarboxname=document.getElementById(currentstarbox).value;
        currentvalue="input"+i;
        starvalue=document.getElementById(currentvalue).value;
        new Starbox(currentstarboxname, starvalue, {color:'#8bc21e', hoverColor:'#7fb11b',locked: true});
    }
    }else{
      new Starbox("userrating", finalvalue, {overlay: 'big.png', locked: false, rerate: true, color:'#8bc21e', hoverColor:'#7fb11b', onRate:function(element,memo){document.getElementById('newnote').value=memo.rated;}});
    }
    
}


function addcomment(type,note,webcomment,userid,productid,starid,nbeval,avgnote,oldnote){

    var xhr_object = null;   
     note=note*1; 
    if(window.XMLHttpRequest) // Firefox   
       xhr_object = new XMLHttpRequest();   
    else if(window.ActiveXObject) // Internet Explorer   
       xhr_object = new ActiveXObject("Microsoft.XMLHTTP");   
    else { // XMLHttpRequest non supporté par le navigateur   
       alert("XMLHTTPRequest objects not supported by browser, sorry");   
       return;   
     }   
     document.getElementById('loadingwhileproductsave').innerHTML="<img src='../../images/ajax-loader.gif'>";
    //alert("type=" + type + " // nbeval="+nbeval+" // avgnote=" + avgnote + " // note=" + note);
	if(type=="save"){newnote=(nbeval*avgnote+note)/(nbeval+1);}
	if(type=="delete"){if(nbeval==1){newnote=(nbeval*avgnote-oldnote);}else{newnote=(nbeval*avgnote-oldnote)/(nbeval-1);}}
	if(type=="update"){newnote=(nbeval*avgnote+note-oldnote)/(nbeval);}//alert(newnote);	

     xhr_object.open("GET", "../../js/ajax.php?addcomment&note=" + note + "&productid=" + productid + "&type=" + type + "&userid=" + userid + "&webcomment=" + webcomment + "&nbeval=" + nbeval + "&averagenote=" + avgnote + "&oldnote=" + oldnote, false);   
    
    xhr_object.send(null);   
    	if(xhr_object.readyState == 4) {
	  if(starid!="tobedefined"){
    		  new Starbox(starid, newnote, {className:'dotted', color:'#8bc21e', hoverColor:'#7fb11b', duration: 0.6, rerate : true, onRate:function(element,memo){comment(productid,'note', memo.rated, starid);}}); 
    		  cachetout('commentpopup');
	  }
	  else{window.location.reload();}
    	}
    	else{alert("Please retry...");}
    
} 

/*var imgTesting = new Image();

function CreateDelegate(contextObject, delegateMethod)
{
    return function()
    {
        return delegateMethod.apply(contextObject, arguments);
    }
}

function imgTesting_onload()
{
    alert(this.width + " by " + this.height);
}


imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
imgTesting.src = 'yourimage.jpg';*/


function jsOnload(size,image,src)
{
	var oImg = new Image(); 
	// Affectation du chemin de l'image a l'objet 
	oImg.src = src;
if(oImg.width==0){image.width=size;image.height=80*size/100;}else{
	// On recupere les tailles reelles 
	// dimensions maximales acceptées
	
	var maxHeight = 80*size/100;
	var maxWidth = size;
 	
	// calcul des ratios d'images
	var ratio = (oImg.width * 1.0 / oImg.height);
	var ratiotest = ((oImg.width * 1.0 / oImg.height) > 1) ? (oImg.width * 1.0 / maxWidth) : (oImg.height * 1.0 / maxHeight);
	

	// calcul des nouvelles valeurs
	if (ratio > 1) {
		var newWidth = size;
		var newHeight = maxWidth / ratio;
	}
	else{
		var newWidth = maxHeight * ratio;
		var newHeight = 80*size/100;
	}
 
	//var newWidth = 100;
	//var newHeight = 100 / ratio;

	
	// mise en place des nouvelles dimensions de l'image
	image.width = newWidth;
	image.height = newHeight;
	/*image.className = 'shownImage';*/
	}
}

function addfavorite(productid,type){

    var xhr_object = null;   
      
    if(window.XMLHttpRequest) // Firefox   
       xhr_object = new XMLHttpRequest();   
    else if(window.ActiveXObject) // Internet Explorer   
       xhr_object = new ActiveXObject("Microsoft.XMLHTTP");   
    else { // XMLHttpRequest non supporté par le navigateur   
       alert("XMLHTTPRequest objects not supported by browser, sorry");   
       return;   
     }   
       
    document.getElementById('commentpopup').innerHTML="<div style='text-align:center;margin-top:150px;'><img src='../../images/ajax-loader.gif'></div>";
    		 montreoverlay();	my_window_back = document.all ? document.all['windowback'] : document.getElementById ? document.getElementById('windowback') : ""
        my_window_back.style.visibility = "visible";
        
        my_window = document.all ? document.all['commentpopup'] : document.getElementById ? document.getElementById('commentpopup') : ""
        my_window.style.visibility = "visible";
    
    xhr_object.open("GET", "../../js/ajax.php?addfavorite=" + productid + "&type=" + type, false);   
    
    xhr_object.send(null);   
    	if(xhr_object.readyState == 4) {
    		cachetout('commentpopup');hideoverlay();document.getElementById("update").style.display='block';
    	}
    
} 

function addnewsletter(mailerror,host,submitok){
var email=document.getElementById('news').value;

if(isStringMail(email)==true){

   var xhr_object = null;   
      
    if(window.XMLHttpRequest) // Firefox   
       xhr_object = new XMLHttpRequest();   
    else if(window.ActiveXObject) // Internet Explorer   
       xhr_object = new ActiveXObject("Microsoft.XMLHTTP");   
    else { // XMLHttpRequest non supporté par le navigateur   
       alert("XMLHTTPRequest objects not supported by browser, sorry");   
       return;   
     }   
     
     xhr_object.open("GET", "http://" + host + "/js/ajax.php?newsletter=" + email, false);   
    
    xhr_object.send(null);   
    	if(xhr_object.readyState == 4) {
    		alert(submitok);
    	}
  
}

else{alert(mailerror);}

}

function nl2br (str, is_xhtml) {
var breakTag = '';
 
    breakTag = '<br />';
    if (typeof is_xhtml != 'undefined' && !is_xhtml) {
        breakTag = '<br>';
    }
 
    return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

function loadthumb(idproduct){

   var xhr_object = null;   
      
    if(window.XMLHttpRequest) // Firefox   
       xhr_object = new XMLHttpRequest();   
    else if(window.ActiveXObject) // Internet Explorer   
       xhr_object = new ActiveXObject("Microsoft.XMLHTTP");   
    else { // XMLHttpRequest non supporté par le navigateur   
       alert("XMLHTTPRequest objects not supported by browser, sorry");   
       return;   
     }   
     
     //alert("ajax.php?loadthumb=" + idproduct);
     
     xhr_object.open("GET", "ajax.php?loadthumb=" + idproduct, true);   
    
    xhr_object.send(null);   
    xhr_object.onreadystatechange = function() { 
    	if(xhr_object.readyState == 4) {
    		//alert(xhr_object.responseText);
        document.getElementById(idproduct).src=xhr_object.responseText;
    	}}
  
}