
/*
function toDoOnLoad(fn) {
  
  obj = this.document;
  
  if (obj.addEventListener){
    obj.addEventListener("load", fn, false);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("onload", fn);
    return r;
  } else {
    document.onload = fn;
  }
}
*/

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be attached");
  }
}

function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}






//GO1.1
///////////////////////////////////////
//
//  Generic onload by Brothercake
//  http://www.brothercake.com/
//
///////////////////////////////////////


function toDoOnLoad(fn) {

  //setup onload function
  if(typeof window.addEventListener != 'undefined') {
    //.. gecko, safari, konqueror and standard
    window.addEventListener('load', fn, false);
  } else if(typeof document.addEventListener != 'undefined') {
    //.. opera 7
    document.addEventListener('load', fn, false);
  } else if(typeof window.attachEvent != 'undefined') {
    //.. win/ie
    window.attachEvent('onload', fn);
    
  } else {
    //.. mac/ie5 and anything else that gets this far
    //if there's an existing onload function
    if(typeof window.onload == 'function') {
      //store it
      var onloadexisting = onload;
      var onloadfnonload = fn;
      //add new onload handler
      window.onload = function() {
        //call existing onload function
        onloadexisting();
        
        //call generic onload function
        //generic();
        onloadfnonload();
      };
    } else {
      //setup onload function
      window.onload = fn;
    }
  }
}


function dhtmlLoadScript(url) {
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);
}

function EcrireCookie(nom, valeur)
{
var argv=EcrireCookie.arguments;
var argc=EcrireCookie.arguments.length;
var expires=(argc > 2) ? argv[2] : null;
var path=(argc > 3) ? argv[3] : null;
var domain=(argc > 4) ? argv[4] : null;
var secure=(argc > 5) ? argv[5] : false;
document.cookie=nom+"="+escape(valeur)+
((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
((path==null) ? "" : ("; path="+path))+
((domain==null) ? "" : ("; domain="+domain))+
((secure==true) ? "; secure" : "");
}

function getCookieVal(offset)
{
var endstr=document.cookie.indexOf (";", offset);
if (endstr==-1) endstr=document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom)
{
var arg=nom+"=";
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
while (i<clen)
{
var j=i+alen;
if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
i=document.cookie.indexOf(" ",i)+1;
if (i==0) break;

}
return null;
}
function EffaceCookie(nom, dir)
{
date=new Date;
date.setFullYear(date.getFullYear()-1);
EcrireCookie(nom,null,date, dir);
}

/**
*
*  WebToolKit Base64 encode / decode component
*  Compiled by Justas Vinevicius <justas.vinevicius(at)gmail.com>
*  Original code by Tyler Akins <fidian(at)rumkin.com>
*
*  Dependencies:
*  WebToolKit UTF-8 encode / decode component for correct UTF-8 handling
*
**/

/**
*
*  // Usage
*  // extended String object:
*  str = '1234abc'
*  encoded = str.base64encode();
*  decoded = encoded.base64decode();
*
*  // Usage
*  // Standalone object:
*  str = '1234abc'
*  encoded = WebToolKit.base64.encode(str);
*  decoded = WebToolKit.base64.decode(encoded);
*
**/

if (typeof(WebToolKit) == "undefined") {
	var WebToolKit = {};
};

WebToolKit.base64 = {

	keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		if (typeof(String.prototype.utf8encode) !== "undefined") {
			input = input.utf8encode();
		}

		do {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output + this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) +
			this.keyStr.charAt(enc3) + this.keyStr.charAt(enc4);
		} while (i < input.length);

		return output;
	},

	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		do {
			enc1 = this.keyStr.indexOf(input.charAt(i++));
			enc2 = this.keyStr.indexOf(input.charAt(i++));
			enc3 = this.keyStr.indexOf(input.charAt(i++));
			enc4 = this.keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
		} while (i < input.length);

		if (typeof(String.prototype.utf8decode) !== "undefined") {
			return output.utf8decode();
		} else {
			return output;
		}
	}

};

if (typeof(String.prototype.base64encode) == "undefined") {
	String.prototype.base64encode = function () {
		return WebToolKit.base64.encode(this);
	};
};

if (typeof(String.prototype.base64decode) == "undefined") {
	String.prototype.base64decode = function () {
		return WebToolKit.base64.decode(this);
	};
};


/**
*
*  WebToolKit UTF-8 encode / decode component
*  Compiled by Justas Vinevicius <justas.vinevicius(at)gmail.com>
*  Original code by Tobias Kieslich <tobias(at)justdreams.de>
*
**/

/**
*
*  // Usage
*  // extended String object:
*  str = '???123'
*  encoded = str.utf8encode();
*  decoded = encoded.utf8decode();
*
*  // Usage
*  // Standalone object:
*  str = '???123'
*  encoded = WebToolKit.utf8.encode(str);
*  decoded = WebToolKit.utf8.decode(encoded);
*
**/

if (typeof(WebToolKit) == "undefined") {
	var WebToolKit = {};
};

WebToolKit.utf8 = {

	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;
	},

	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;
	}

};

if (typeof(String.prototype.utf8encode) == "undefined") {
	String.prototype.utf8encode = function () {
		return WebToolKit.utf8.encode(this);
	};
};

if (typeof(String.prototype.utf8decode) == "undefined") {
	String.prototype.utf8decode = function () {
		return WebToolKit.utf8.decode(this);
	};
};


/**
*
*  WebToolKit URL encode / decode component
*  Compiled by Justas Vinevicius <justas.vinevicius(at)gmail.com>
*  Original code by Tyler Akins <fidian(at)rumkin.com>
*
*  Dependencies:
*  WebToolKit UTF-8 encode / decode component for correct UTF-8 handling
*
**/

/**
*
*  // Usage
*  // extended String object:
*  str = 'index.php?a=1234&b=abc'
*  encoded = str.urlencode();
*  decoded = encoded.urldecode();
*
*  // Usage
*  // Standalone object:
*  str = 'index.php?a=1234&b=abc'
*  encoded = WebToolKit.url.encode(str);
*  decoded = WebToolKit.url.decode(encoded);
*
**/

if (typeof(WebToolKit) == "undefined") {
	var WebToolKit = {};
};

WebToolKit.url = {

	encode : function (string) {
		if (typeof(String.prototype.utf8encode) != "undefined") {
			return escape(string.utf8encode());
		} else {
			return escape(string);
		}
	},

	decode : function (string) {
		if (typeof(String.prototype.utf8decode) !== "undefined") {
			return unescape(string).utf8decode();
		} else {
			return unescape(string);
		}
	}

};

if (typeof(String.prototype.urlencode) == "undefined") {
	String.prototype.urlencode = function () {
		return WebToolKit.url.encode(this);
	};
};

if (typeof(String.prototype.urldecode) == "undefined") {
	String.prototype.urldecode = function () {
		return WebToolKit.url.decode(this);
	};
};


function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Merci de saisir une date au format jj/mm/aaaa.")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("La date n'est pas valide.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("La date n'est pas valide.")
		return false
	}
	if (strYear.length != 4 || year==0 ){
		alert("Merci de saisir une année sur 4 chiffres.")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Merci de saisir une date valide.")
		return false
	}
  
  return new Date(year,month,day);
}


function isEmail(email) {
  
  email = email.toString();
  email = email.replace(/(^\s*)|(\s*$)/g,'');
  
  r = new RegExp( "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", "i");
  
  if ( email.search( r ) == -1 ) {
    alert("Attention! L'adresse email n'est pas valide.");
    return false;
  }
  
  return true;

}

function connexion() {
  if(getuser() != "") {
    document.getElementById('connected').style.display="block";
    document.getElementById('connexion').style.display="none";
  }
  else {
    document.getElementById('connected').style.display="none";
    document.getElementById('connexion').style.display="block";
  }
}

function photoDelete(mess,id,insee) {
  path = 'http://fb66.fbtech.net/v7.communes.com/www/';
  if(confirm(mess))
  {
    document.location.href = 'photo_delete,'+id+','+insee+'.html';
  }
}
function connexionOnOff(){
  if(document.getElementById('connexion').getElementsByTagName('form')[0].style.display=='none'){
    document.getElementById('connexion').setAttribute("class","hover");
    document.getElementById('connexion').setAttribute("className","hover");
    document.getElementById('connexion').getElementsByTagName('form')[0].style.display='block';
  }else{
    document.getElementById('connexion').setAttribute("class","");
    document.getElementById('connexion').setAttribute("className","");
    document.getElementById('connexion').getElementsByTagName('form')[0].style.display='none';
  }
}

function lastPhotos(){
  document.getElementById('ul-derniers-ajouts').style.display = 'none';
  document.getElementById('top-villes').style.display = 'none';
  document.getElementById('last-photos').style.display = 'block';
}
function lastArticles(){
  document.getElementById('ul-derniers-ajouts').style.display = 'block';
  document.getElementById('top-villes').style.display = 'none';
  document.getElementById('last-photos').style.display = 'none';
}
function topVilles(){
  document.getElementById('ul-derniers-ajouts').style.display = 'none';
  document.getElementById('top-villes').style.display = 'block';
  document.getElementById('last-photos').style.display = 'none';
}

function hideShowVille(lettre){
  var v = document.getElementById('content').getElementsByTagName('ul');
  if(lettre!='toutes'){
    for(var i=0;i<v.length;i++){
      v[i].style.display = 'none';
    }
    if(document.getElementById(lettre)){
      document.getElementById(lettre).style.display = 'block';
    }
  }else{
    for(var i=0;i<v.length;i++){
      v[i].style.display = 'block';
    }
  }
}

function getMeteo(commune){
  new Ajax.Request('../../../mod/mod_meteo,'+commune+'.html',
  {
    method:'get',
    onSuccess: function(transport){
      var response = transport.responseText || "no response text";
      document.getElementById("meteo_ajax").innerHTML=response;
    },
    onFailure: function(){ alert('Something went wrong...') }
  });
}
