function lancer(fct) 
{
    if (window.addEventListener)
        window.addEventListener('load', fct, false);
    else
        window.attachEvent('onload', fct);
}

/*FUNC CLASS*/


function serialize( mixed_value ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // +   improved by: Dino
    // +   bugfixed by: Andrej Pavlovic
    // +   bugfixed by: Garagoth
    // +      input by: DtTvB (http://dt.in.th/2008-09-16.string-length-in-bytes.html)
    // +   bugfixed by: Russell Walker (http://www.nbill.co.uk/)
    // %          note: We feel the main purpose of this function should be to ease the transport of data between php & js
    // %          note: Aiming for PHP-compatibility, we have to translate objects to arrays
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
    // *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
    // *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'
 
    var _getType = function( inp ) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            /*
            if (type == "object") {
                var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
                if (objname == undefined) {
                    return;
                }
                objname[1] = serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);
            }
            */
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
        case "undefined": // Fall-through
        default: // if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
            val = "N";
            break;
    }
    if (type != "object" && type != "array") {
        val += ";";
    }
    return val;
}


function hasClass(element,value) {
    return element.className.match(new RegExp('(\\s|^)'+value+'(\\s|$)'));
}

function addClass(element, value) {
    if(!element.className) {
        element.className = value;
    } else {
        newClassName = element.className;
        newClassName+= " ";
        newClassName+= value;
        element.className = newClassName;
    }
}

function removeClass(element,value) {
    if (hasClass(element,value)) {
        var reg = new RegExp('(\\s|^)'+value+'(\\s|$)');
        element.className=element.className.replace(reg,' ');
    }
}


if (Prototype.Browser.IE) {
     Prototype.Browser.IEVersion = parseFloat(navigator.appVersion.split(';')[1].strip().split(' ')[1]);
     Prototype.Browser.IE6 = Prototype.Browser.IEVersion == 6;
     Prototype.Browser.IE7 = Prototype.Browser.IEVersion == 7;
}

if(Prototype.Browser.Gecko)                                          
    addClass(document.getElementsByTagName('html')[0], 'mozilla');   
else if(Prototype.Browser.IE6)                                   
    addClass(document.getElementsByTagName('html')[0], 'ie6');                                   
else if(Prototype.Browser.IE7)                                       
    addClass(document.getElementsByTagName('html')[0], 'ie7'); 

/*FUNC CLASS*/

/*FUNC EVENTS*/

function isset(  ) {  
    var a=arguments; var l=a.length; var i=0;    
    if (l==0) { 
        throw new Error('Empty isset'); 
    }                                            
    while (i!=l) {
        if (typeof(a[i])=='undefined' || a[i]===null) { 
            return false; 
        } else { 
            i++; 
        }
    }
    return true;
}

function is_object( mixed_var ){  
    if(mixed_var instanceof Array) {
        return false;
    } else {             
        return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
    }
}

function importScript(url){
    var tag = document.createElement("script");
    tag.type="text/javascript";
    tag.src = url;
    document.body.appendChild(tag);
}


/*ADDEVENT*/
function addEvent( obj, type, fn ) {
    if(is_object(obj))
    {
        if ( obj.attachEvent ) {
            obj['e'+type+fn] = fn;
            obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
            obj.attachEvent( 'on'+type, obj[type+fn] );
        } else
            obj.addEventListener( type, fn, false );
    }
}    

function removeEvent( obj, type, fn ) {
    if ( obj.detachEvent ) {
        obj.detachEvent( 'on'+type, obj[type+fn] );
        obj[type+fn] = null;
    } 
    else
        obj.removeEventListener( type, fn, false );
}
/*FUNC EVENTS*/


/*********************************************************** display subs **********************************************************/
function displaycriteres(the_sub)
{
    if ($(the_sub).style.display=="") {
        $(the_sub).style.display = "none";
        return
    }
    /*
    for (i=0;i<criteres_array.length;i++){
        var my_sub = $(criteres_array[i]);
        my_sub.style.display = "none";
    }
    */
    $(the_sub).style.display = "";
}

elem=document.getElementsByTagName('select');
var browserName=navigator.appName;

function voirSelect(v){    
    if(browserName=="Microsoft Internet Explorer")
    {  
        for(i=0;i<elem.length;i++)
        {
            elem[i].style.visibility=v;
        }
    }
}

var subs_array = new Array("fen_superpose_1","fen_superpose_2");
var visibil = 0;
function displaySubs(the_sub)
{
    if(visibil == 0)
    {                        
        voirSelect('hidden');
        visibil = 1;   
    }   
    else
    {
        voirSelect('visible');
        visibil = 0;   
    }
    
    if(navigator.appName=='Microsoft Internet Explorer') {
    
        var diff_pos;      
        pos_scroll = (navigator.appName == "Microsoft Internet Explorer") ? document.documentElement.scrollTop : window.pageYOffset; 
        $(the_sub).style.top = pos_scroll+'px';
        $(the_sub).style.position = 'absolute';
                         
        if($('id_body').style.overflowY != 'hidden')
            $('id_body').style.overflowY = 'hidden'; 
        else
            $('id_body').style.overflowY = 'auto'; 
    }                                                                   
    
    if ($(the_sub).style.display=="") {
        $(the_sub).style.display = "none";                
        
        return
    }
    /*
    for (i=0;i<subs_array.length;i++){
        var my_sub = $(subs_array[i]);
        my_sub.style.display = "none";
    }
    */
    $(the_sub).style.display = "";
}

/*********************************************************** display subs **********************************************************/
function executeAjax(url,div,pars,method)
{
    if(method == '')
        method = 'post';
    
    var myAjax = new Ajax.Updater(div, url, { method: method,
                                              parameters:pars, 
                                              asynchronous:true, 
                                              evalScripts : true
                                            });
    
    
}

function erase_square_euros(id,focus)
{
    
    if(id == 'model_constructeur_surface_maison_habitable1')
    {
        if(focus == 'focus' && $(id).value == 'en m²')
            $(id).value = '';
        else if($(id).value == '')
            $(id).value = 'en m²';
    }  
    
    if(id == 'model_constructeur_prix2')
    {
        
        if(focus == 'focus' && $(id).value == 'en €')
            $(id).value = '';
        else if($(id).value == '')
            $(id).value = 'en €';
    }  
}

function submitMoteur()
{
    if($('model_constructeur_departements_list').value == '')
    {
        alert('Veuillez séléctionner un département');
        return;
    }
        
    if($('model_constructeur_surface_maison_habitable1').value == 'en m²')
        $('model_constructeur_surface_maison_habitable1').value = '';
    
    
    if($('model_constructeur_prix2').value == 'en €')
        $('model_constructeur_prix2').value = '';
    
    document.form_model_maison.submit();
}
