/************************* AJAX *******************************/

// Nettoie les caracteres speciaux des chaines passees en parametres dans une requete Ajax
function cleanParam(pValue) {
	var rOut = pValue;
	rOut = rOut.replace(new RegExp("\\?","g"),"%3F");
	rOut = rOut.replace(new RegExp("\\+","g"),"%2B");
	rOut = rOut.replace(new RegExp("\\+","g"),"%26");		
	return rOut;
}

// Tente d'envoyer une e-mail via une requete Ajax et renvoie une boite de dialogue javascript+ajax pour le resultat
function jsSendMail(pUrl, pForm, pContext, pRedirect) {	
	var eval;
	var context = '';
	var redirect;
	redirect = pRedirect || false;
	context = (pContext) ? '?format='+pContext : '';
	var myAjax = new Ajax.Request (
  	pUrl + context,
    {
    	method: 'post',
   		evalScripts: true,
     	parameters : pForm.serialize(true),
     	onLoading : function () {     		
     		new BDLoadingIndicator.base({pic:'ajax-loading.gif'});  			
     	},
     	onComplete: function transResult (response) {     		
     		BDLoadingIndicator.hideAll(); 
     		var TextResponse = response.responseText;
     		var JSONResponse = JSON.parse(TextResponse);    		
     		if (JSONResponse.success) {     			
	     		new BDMessageBox.base({ 				     			
					title:JSONResponse.title,
					message:JSONResponse.msg,
					width:280,
					icon:'confirm',
					effectDuration:0.2,
					closeAction: function() { pForm.reset(); BDAjaxWindow.hideAll(); }
	     		});
     		}
     		else {
     			new BDMessageBox.base({ 				     			
					title:JSONResponse.title,
					message:JSONResponse.msg,
					width:280,
					icon:'warning',
					effectDuration:0.2
	     		});
     		}     		     		
     	}					     	
     }					    
  );
  return false;
}

// Verifie qu'une chaine ne contient pas que des espaces
function isValidString(pString) {    		
	var r = true;
	var mString = pString.replace(new RegExp(' ','g'),'');    			    		    		
	return (mString.length > 0);  		
}


function validateSubmit(pForm,pScript) {					  									  		
  var mForm = $(pForm);				  
	var errors = new Array();
	var message = "";					
	
	for(i=0; i<(mForm.length); i++) {
		if ((typeof(mForm[i].alt)!='undefined')&&(mForm[i].alt!='')&&(!isValidString(mForm[i].value))) {
			errors.push('<strong>'+mForm[i].alt+'</strong>');
		}
	}
	
	// On verifie s'il y a des erreurs					
	if (errors.length > 0) {
		message+='Le(s) champs suivant(s) n\'ont pas &eacute;t&eacute; renseign&eacute;(s) correctement :';
		for(var i=0;i < errors.length;i++) {
			message+="<br />&nbsp;&nbsp;&nbsp;-&nbsp;"+errors[i];
		}
		message+='<br /><br />';
		new Mbox.base('mboxContainer',{ 
				externalControl : 'mboxFooterButton',
				title:'Attention',
				message:message,
				width:280,
				icon:'warning'
		});
	}
	else { 				
		var mUrl = pScript;		
	  jsSendMail(mUrl, "", mForm);
	}
}

function update_photo(pContainer, pPhoto, pUrl) {		
	var myAjax = new Ajax.Updater(
		$(pContainer),
	  	pUrl,
	    {		    	
	   		method: 'get',
	   		parameters : 'selected_photo='+pPhoto,
	   		onLoading : function () {     		
	     		//new BDLoadingIndicator.base({pic:'ajax-loading.gif'});  			
	     	},
	     	onComplete: function transResult (response) {     		
	     		//BDLoadingIndicator.hideAll();
	     	}
	   	}		
	);
}

function add_favorites(pUrl, pAnnonce) {		
	var myAjax = new Ajax.Updater(
		$('frame-favorites'),
	  	pUrl,
	    {		    	
	   		method: 'post',
	   		parameters : 'add_annonce='+pAnnonce,
	   		onLoading : function () {     		
	     		//new BDLoadingIndicator.base({pic:'ajax-loading.gif'});  			
	     	},
	     	onComplete: function transResult (response) {     		
	     		//BDLoadingIndicator.hideAll();
	     	}
	   	}		
	);
}

function del_favorites(pUrl, pAnnonce) {		
	var myAjax = new Ajax.Updater(
		$('frame-favorites'),
	  	pUrl,
	    {		    	
	   		method: 'post',
	   		parameters : 'del_annonce='+pAnnonce,
	   		onLoading : function () {     		
	     		//new BDLoadingIndicator.base({pic:'ajax-loading.gif'});  			
	     	},
	     	onComplete: function transResult (response) {     		
	     		//BDLoadingIndicator.hideAll();
	     	}
	   	}		
	);
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

//corrige bug ie6 affichage png
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
function correctImg(){
    if ((version >= 5.5) && (document.body.filters)){
     for(var i=0; i<document.images.length; i++){
     var img = document.images[i]
     var imgName = img.src.toUpperCase()
     if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
       var imgID = (img.id) ? "id='" + img.id + "' " : "";
       var imgClass = (img.className) ? "class='" + img.className + "' " : "";
       var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
       var imgStyle = "display:inline-block;" + img.style.cssText;
       if (img.align == "left") imgStyle = "float:left;" + imgStyle;
       if (img.align == "right") imgStyle = "float:right;" + imgStyle;
       if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
       var strNewHTML = "<span " + imgID + imgClass + imgTitle
       + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
       + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
       + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
       img.outerHTML = strNewHTML;
       i = i-1;
       }
      }
     }
 }

// IE only !! window.attachEvent("onload", externalLinks);
window.onload = externalLinks;
window.addEventListener("onload", correctImg, true);
