window.addEvent('domready', function(){
    // var tokens = ['Afghanistan', 'Aland Islands', 'Albania', 'Algeria', 'American Samoa', 'Andorra', 'Angola', 'Anguilla', 'Antarctica', 'Antigua And Barbuda', 'Argentina', 'Armenia', 'Aruba', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bermuda', 'Bhutan', 'Bolivia', 'Bosnia And Herzegovina', 'Botswana', 'Bouvet Island', 'Brazil', 'British Indian Ocean Territory', 'Brunei Darussalam', 'Bulgaria', 'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde', 'Cayman Islands', 'Central African Republic', 'Chad', 'Chile', 'China', 'Christmas Island', 'Cocos (Keeling) Islands', 'Colombia', 'Comoros', 'Congo', 'Congo, The Democratic Republic Of The', 'Cook Islands', 'Costa Rica', 'Cote D\'ivoire', 'Croatia', 'Cuba', 'Cyprus', 'Czech Republic', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 'Falkland Islands (Malvinas)', 'Faroe Islands', 'Fiji', 'Finland', 'France', 'French Guiana', 'French Polynesia', 'French Southern Territories', 'Gabon', 'Gambia', 'Georgia', 'Germany', 'Ghana', 'Gibraltar', 'Greece', 'Greenland', 'Grenada', 'Guadeloupe', 'Guam', 'Guatemala', 'Guernsey', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Heard Island And Mcdonald Islands', 'Holy See (Vatican City State)', 'Honduras', 'Hong Kong', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran, Islamic Republic Of', 'Iraq', 'Ireland', 'Isle Of Man', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jersey', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', 'Korea, Democratic People\'s Republic Of', 'Korea, Republic Of', 'Kuwait', 'Kyrgyzstan', 'Lao People\'s Democratic Republic', 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libyan Arab Jamahiriya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Macao', 'Macedonia, The Former Yugoslav Republic Of', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands', 'Martinique', 'Mauritania', 'Mauritius', 'Mayotte', 'Mexico', 'Micronesia, Federated States Of', 'Moldova, Republic Of', 'Monaco', 'Mongolia', 'Montenegro', 'Montserrat', 'Morocco', 'Mozambique', 'Myanmar', 'Namibia', 'Nauru', 'Nepal', 'Netherlands', 'Netherlands Antilles', 'New Caledonia', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Niue', 'Norfolk Island', 'Northern Mariana Islands', 'Norway', 'Oman', 'Pakistan', 'Palau', 'Palestinian Territory, Occupied', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Pitcairn', 'Poland', 'Portugal', 'Puerto Rico', 'Qatar', 'Reunion', 'Romania', 'Russian Federation', 'Rwanda', 'Saint Helena', 'Saint Kitts And Nevis', 'Saint Lucia', 'Saint Pierre And Miquelon', 'Saint Vincent And The Grenadines', 'Samoa', 'San Marino', 'Sao Tome And Principe', 'Saudi Arabia', 'Senegal', 'Serbia', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia', 'Slovenia', 'Solomon Islands', 'Somalia', 'South Africa', 'South Georgia And The South Sandwich Islands', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname', 'Svalbard And Jan Mayen', 'Swaziland', 'Sweden', 'Switzerland', 'Syrian Arab Republic', 'Taiwan, Province Of China', 'Tajikistan', 'Tanzania, United Republic Of', 'Thailand', 'Timor-Leste', 'Togo', 'Tokelau', 'Tonga', 'Trinidad And Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Turks And Caicos Islands', 'Tuvalu', 'Uganda', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'United States Minor Outlying Islands', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Venezuela', 'Viet Nam', 'Virgin Islands, British', 'Virgin Islands, U.S.', 'Wallis And Futuna', 'Western Sahara', 'Yemen', 'Zambia', 'Zimbabwe'];
    /*
    new Autocompleter.Local('enzym_input_search', tokens, {
        'minLength': 1, // We wait for at least one character
        'selectMode': 'type-ahead', // Instant completion
        'overflow': true // Overflow for more entries
    });
    */
    
    if ($('wikiboy_topbar')!=null) {
        $('wikiboy_topbar').setStyles({opacity:'0.2'});
        $('wikiboy_topbar').addEvent('mouseover',function(event){$('wikiboy_topbar').fade(1)});
        $('wikiboy_topbar').addEvent('mouseout',function(event){$('wikiboy_topbar').fade(0.2)});
    }
    if ($('enzym_input_search')!=null) {
        new Autocompleter.Request.JSON('enzym_input_search', 'index.php?file=ajax&min=1&request=page&keyring='+ring, {
            'postVar': 'search',
            'minLength': 1, // We wait for at least one character
            'selectMode': 'type-ahead', // Instant completion
            'overflow': true // Overflow for more entries
        });
    }
});

/*
 * Maximize the page
 */
var maximize = 0;
function fullscreen() {
    var menu1 = $('enzym_menu_1');
    var menu2 = $('enzym_menu_2');
    var content = $('enzym_page');
    if (!maximize) {
        menu1.fade(0);
        menu2.fade(0);
        var morph3 = new Fx.Morph(content);
        morph3.start({
            'left': 0,
            'right': 0,
            'padding': 0,
            'margin':0
        });
    }
    else {
        menu1.fade(1);
        menu2.fade(1);
        content.morph('#enzym_page');
    }
    maximize = !maximize;
}
function check(box,value) {
    var i=0;
    while (document.getElementById(box+"["+i+"]"))
    {
        // document.getElementById(box+"["+i+"]").checked = !document.getElementById(box+"["+i+"]").checked;
        document.getElementById(box+"["+i+"]").checked = value ;
        i=i+1;
    }
}
/*
 * Count the character from a textarea
 */
function countCharacter(id,display,maxChars) {
    var textArea = $(id);
    textArea.addEvents({
        focus: function() {
            this.focused = true;
        },
        blur: function() {
            this.focused = false;
        },
        keyup: function(e) {
            if (textArea.focused)
                checkmax();
        }
    });
    function checkmax() {  
        var chars = textArea.get("value").trim().length;
        if (chars+1 >= maxChars) {
            chars = maxChars;
            $(display).set("html", "<strong>limit reached!</strong>");
            // remove surplus chars
            textArea.set("value", textArea.get("value").substring(0,maxChars));
        }
        else {
            $(display).set("html", "");
        }
        // inform how much left
        $(display).set("text", chars);
    }
    checkmax();
}
function calendar(id,month,year,keyring) {
    var myrequest = new Request.HTML({
        url: 'index.php',
        method: 'get',
        noCache: true,
        onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
            $(id).empty();
            $(id).set('html',responseHTML);
            $$('#'+id+' table')[0].setStyles({
                opacity: '0'
            });
            $$('#'+id+' table')[0].fade(1);
        }
    });
    myrequest.get(
    {
        'min': 1,
        'file': 'date_pickers',
        'calmonth':month,
        'calyear':year,
        'keyring':keyring
    }
    );
}
function verifTitle(title)
{
    if(title != '')
    {
        if((title.length<3) || (title.length>30))
        {
            $('pseudobox').innerHTML='<img src="cross.png" alt="cross" />';
        }
        else
        {
            var text = file('veriftitle.php?namePage='+escape(title));
            var verifimg = (text == 2)?'tick':'cross' ;
            $('pseudobox').innerHTML='<img src="'+verifimg+'.png" alt="tick" />';
        }
    }
}
function getPage(param,id)
{
    this.id = id;
    var myrequest = new Request.HTML({
        url: 'index.php',
        method: 'get',
        noCache: true,
        onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
            $('enzym_txt').setStyles({
                opacity: '0'
            });
            $('enzym_txt').empty();

            // replace id from the url
            var modify;
            var search = /id=([0-9]*)/gi;
            var change = "id="+id;
            var element = $$('a');
            for (var cpt=0;cpt<element.length;cpt++) {
                modify = element[cpt].href;
                modify = modify.replace(search, change);
                element[cpt].href = modify;
            }

            $('enzym_txt').set('html',responseHTML);

            if ($('comment_hidden')!=null)
                $('comment_hidden').style.display = 'block';

            $('enzym_txt').fade(1);
        }
    });
    myrequest.get(
        '?request=1&keyring='+ring+'&page='+param
        );
    return false;
}
function getFile(title,param)
{
    if (param==null) param='';
    var myrequest = new Request.HTML({
        url: 'index.php',
        method: 'get',
        noCache: true,
        onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
            $('enzym_txt').setStyles({
                opacity: '0'
            });
            $('enzym_txt').empty();
            $('enzym_txt').set('html',responseHTML);
            $('enzym_txt').fade(1);
        }
    });
    myrequest.get(
        '?min=1&file='+title+'&id='+this.id+'&'+param
        );
    return false;
}
function insertTags(tagOpen, tagClose, sampleText)
{
    var txtarea;
    if (document.editform) {
        txtarea = document.editform.message;
    } else {
        // some alternate form? take the first one we can find
        var areas = document.getElementsByTagName('textarea');
        txtarea = areas[0];
    }

    // IE
    if (document.selection  && !is_gecko) {
        var theSelection = document.selection.createRange().text;
        if (!theSelection) {
            theSelection=sampleText;
        }
        txtarea.focus();
        if (theSelection.charAt(theSelection.length - 1) == " ") { // exclude ending space char, if any
            theSelection = theSelection.substring(0, theSelection.length - 1);
            document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
        } else {
            document.selection.createRange().text = tagOpen + theSelection + tagClose;
        }

    // Mozilla
    } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
        var replaced = false;
        var startPos = txtarea.selectionStart;
        var endPos = txtarea.selectionEnd;
        if (endPos-startPos) {
            replaced = true;
        }
        var scrollTop = txtarea.scrollTop;
        var myText = (txtarea.value).substring(startPos, endPos);
        if (!myText) {
            myText=sampleText;
        }
        var subst;
        if (myText.charAt(myText.length - 1) == " ") { // exclude ending space char, if any
            subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
        } else {
            subst = tagOpen + myText + tagClose;
        }
        txtarea.value = txtarea.value.substring(0, startPos) + subst +
        txtarea.value.substring(endPos, txtarea.value.length);
        txtarea.focus();
        //set new selection
        if (replaced) {
            var cPos = startPos+(tagOpen.length+myText.length+tagClose.length);
            txtarea.selectionStart = cPos;
            txtarea.selectionEnd = cPos;
        } else {
            txtarea.selectionStart = startPos+tagOpen.length;
            txtarea.selectionEnd = startPos+tagOpen.length+myText.length;
        }
        txtarea.scrollTop = scrollTop;

    // All other browsers get no toolbar.
    // There was previously support for a crippled "help"
    // bar, but that caused more problems than it solved.
    }
    // reposition cursor if possible
    if (txtarea.createTextRange) {
        txtarea.caretPos = document.selection.createRange().duplicate();
    }
}
function changeCss(position) {
    var value = parseInt(readCookie("style"));
    value = (position==2) ? value+1 : value-1 ;
    if (position==1) value=10;
    if ((value>7) && (value<14))
    {
        document.getElementsByTagName("body")[0].style.fontSize = value+'pt';
        createCookie("style", value, 365);
    }
}

function getRandomPassword(length,extraChars,min,id)
{
    var charSet = "";
    charSet += "0123456789";
    charSet += "abcdefghijklmnopqrstuvwxyz";
    charSet += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if (extraChars == true)
        charSet += "`~!@#$%^&*()-_=+[{]}\\|;:',<.>/? ";
    var password = "";
    while (securePassword(password,id)<min)
    {
        password = "";
        for (var i = 0; i < length; i++)
            password += charSet.charAt(Math.floor(Math.random() * (charSet.length)));
    }
    return password;
}
function writediv(nb,id)
{
    var maxNb = 7;
    var color = 'blue';
    nb = nb/maxNb*100;
    if (nb<20)      color = 'red';
    else if (nb<40) color = 'orange';
    else if (nb<60) color = 'yellow';
    else if (nb<80) color = 'green';
    $(id).style.backgroundColor = color;
    $(id).style.width = nb +'%';
}
function securePassword(title,id)
{
    var cptPass = 0;
    if (title != '')
    {
        cptPass  = (title.length>3) + (title.length>7) + (title.length>15);
        cptPass += (title.match(/[^a-zA-Z0-9]+/))?1:0;
        cptPass += (title.match(/[a-z]+/))?1:0;
        cptPass += (title.match(/[A-Z]+/))?1:0;
        cptPass += (title.match(/[0-9]+/))?1:0;
        writediv(cptPass,id);
        return cptPass;
    }
    return 0;
}