function Remplace(expr,a,b) {
	if(expr!=null){
		var i=0
		while (i!=-1) {
			i=expr.indexOf(a,i);
			if (i>=0) {
				expr=expr.substring(0,i)+b+expr.substring(i+a.length);
				i+=b.length;
			}
		}
		return expr
	}
}

function trim(myString)
{
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
} 

function toStandardHTML(str){
	str=Remplace(str," />",">");
	str=Remplace(str,"<strong>","<b>");
	str=Remplace(str,"</strong>","</b>");
	str=Remplace(str,"<em>","<i>");
	str=Remplace(str,"</em>","</i>");
	return str;
}

var Utf8 = {
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}
}

function startUpload(){
	document.getElementById('f1_upload_process').style.visibility = 'visible';
	document.getElementById('f1_upload_form').style.visibility = 'hidden';
	return true;
}

function stopUpload(success){
	var result = '';
	if (success == 1){
		//result = '<span class="msg">Fichier envoyé !<\/span>';
	}
	else {
		//result = '<span class="emsg">Il y a eu une erreur au cours de l\'envois du fichier !<\/span>';
	}
	document.getElementById('f1_upload_process').style.visibility = 'hidden';
	document.getElementById('f1_upload_form').innerHTML = result + 'Fichier : <input name="myfile" type="file" id="PICTOGRAMME" size="30">';
	document.getElementById('f1_upload_form').style.visibility = 'visible';	
	return true;   
}

function RecupCoord(){
	var doChange=true;
	if (document.getElementById("LONGITUDE").value!="" && document.getElementById("LATITUDE").value!=""){
		doChange=confirm("Mettre a jour ?");
	}
	
	if (doChange==true){
		/*try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		} catch (e) {
			alert("ERROR");
		}*/
		var xhr_object = null;
		var ADRESSE= Remplace(document.getElementById("ADRESSE").value," ","+");
	 	var CP= Remplace(document.getElementById("CP").value," ","+");
		var VILLE= Remplace(document.getElementById("VILLE").value," ","+");
		var PAYS= Remplace(document.getElementById("PAYS").value," ","+");

		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("ERROR XMLHTTPRequest");
			return;
		}

		var data =encodeURI(ADRESSE+"+"+CP+"+"+VILLE+"+"+PAYS);
		xhr_object.open("GET", "../Admin/functions/RecupCoord.php?q="+data, true);

		xhr_object.onreadystatechange = function() {
			if(xhr_object.readyState == 4) {
				var result=xhr_object.responseText.split(",");
				if(parseInt(result[0])==200){
					document.getElementById("LATITUDE").value=result[2];
					document.getElementById("LONGITUDE").value=result[3];
					//alert("OK");
					//sendinfo(f);
					createlink2googlemap();
					if(parseInt(result[1])<8){
						alert("POSITION APPROXIMATIVE");
					}
				}else{
					alert("Adresse introuvable et/ou service indisponible pour le moment");
				}
			}
		}
		//Send the proper header information along with the request
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr_object.setRequestHeader("Content-length", data.length);
		xhr_object.setRequestHeader("Connection", "close");
 		xhr_object.send(data);
	}
}

var newWin = null;
function closeWin(){
	if (newWin != null){
		if(!newWin.closed)
			newWin.close();
	}
}

function popUp(strURL,strType,strHeight,strWidth,name) {
	closeWin();
	var strOptions="";
	if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
	if (strType=="consolewsb") strOptions="resizable=yes,scrollbars=yes, toolbar=no, location=no,menubar=no,directories=no,height="+strHeight+",width="+strWidth;
	if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
	//window.open(strURL, name, strOptions);
	newWin = window.open(strURL, name, strOptions);
	newWin.focus();
}

function popup_color_picker(opener_form,couleur){
	var width = 400;
	var height = 235;
	var url = "/color_picker.php?opener_form="+opener_form+"&couleur="+couleur+"&exemple=exemple"+couleur
	strOptions="resizable=no,scrollbars=no, toolbar=no, location=no,menubar=no,directories=no,height="+height+",width="+width;
	newWin =window.open(url, 'colorpicker', strOptions);
	newWin.focus();
}

function createlink2googlemap(){
	var URLtogo = Remplace(encodeURI("functions/RecupCoordFromMap.php?VILLE="+document.getElementById("VILLE").value+"&PAYS="+document.getElementById("PAYS").value+"&LATITUDE="+document.getElementById("LATITUDE").value.toString()+"&LONGITUDE="+document.getElementById("LONGITUDE").value.toString()),"'","\\'");
	document.getElementById("link2googlemap").setAttribute("onclick","popUp('"+URLtogo+"','consolewsb',550,850,'GMAP');");
}

function GetRadioValue(name){
	collection=document.getElementsByName(name);
	for (var x = 0; x < collection.length; x++){
		if(collection[x].checked){
			return collection[x].value;
		}
	}
}


function getnbchar(obj){
	return String(obj.value.length);
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength){
		obj.value=obj.value.substring(0,mlength)
	}
}
