function day1offline(containerTagId){
    // If firstname and age are filled we display the lesson

    if( validateFirstnameAndAge() ){
        var containerTag = document.getElementById(containerTagId);
        if (! containerTag)
            return

        fixFlashBlock();

        // Display content
        containerTag.style.display = 'block';
        containerTag.style.overflow = 'visible';

        // Make sure we scroll down to the tag id 'day1beforestarting' to make the questions visible
        window.scroll(0, getOffsetTop('day1beforestarting'));
    }
}


function day1online(containerTagId, valueTagId, textToReplace){
    // If firstname and age are filled, we replace textToReplace by value of valueTagId and display the lesson

    if( validateFirstnameAndAge() ){

        // Go to the end of the page
        window.scroll(0, document.body.scrollHeight);
        var slot = document.getElementById('loading-image');
        if(slot)
            slot.style.display = 'block';

        // Use setTimeout with 0 delay to execute replaceValueAndFixFlashBlockForDay1 asynchronious
        setTimeout('replaceValueAndFixFlashBlockForDay1("' + containerTagId + '", "' + valueTagId + '", "' + textToReplace + '")', 0);

    }
}


function replaceValueAndFixFlashBlockForDay1(containerTagId, valueTagId, textToReplace){
    getValueAndReplace(containerTagId, valueTagId, textToReplace);

    var containerTag = document.getElementById(containerTagId);
    if (! containerTag)
        return

    fixFlashBlock();

    // Display content
    containerTag.style.display = 'block';
    containerTag.style.overflow = 'visible';

    // Make sure we scroll down to the tag id 'day1beforestarting' to make the questions visible
    window.scroll(0, getOffsetTop('day1beforestarting'));

    // Add tooltip events
    addEventForTooltips();

    var slot = document.getElementById('loading-image');
    if(slot)
        slot.style.display = 'none';

}


function getOffsetTop(id){
    // Get offset Top for this id (offsetTop works fine on Firefox but not on IE...)
    // Note: we can also get offset Left with the same method.
    var obj = document.getElementById(id);

    if (! obj)
        return

    var topOffset = obj.offsetTop;
    var parent    = obj.offsetParent;
    while(parent) 
    {
        topOffset += parent.offsetTop;
        parent     = parent.offsetParent;
    }

    return topOffset
}


function fixFlashBlock() {
   // Fix flash block and IE: When we want to display a hide tag, 
   // the flash doesn't work, we MUST re-excecute the javascript code.

    // Get all javascript tags
    var tags = document.getElementsByTagName('script');
    
    for(var index = 0; index < tags.length; index++){
        tag = tags[index];
        html = tag.innerHTML;

        // If this script generate a swf object for mini tv we excecute it.
        if (html.indexOf('var so = new SWFObject("/richmorning/flash/player-mini.swf?"') != -1)
            eval(html);  
        }
}


function hiddenTag(tagId){
    var tag = document.getElementById(tagId);
    if (! tag)
        return

    // Display content
    tag.style.display = 'none';
    
}


function validateFirstnameAndAge(){
    // Return bool
    // check tag 'id_firstname' and 'id_age'
    // if empty put a red border on the empty tag

    var firstname = document.getElementById('id_firstname');
    var age       = document.getElementById('id_age');

    ok = true;

    if (! firstname || ! firstname.value){
       firstname.style.border = '2px solid red';
       ok = false;
    }else
       firstname.style.border = '1px solid gray';

    if (! age || ! age.value || ! parseInt(age.value)){
       age.style.border = '2px solid red';
       ok = false;
     }else
       age.style.border = '1px solid gray';

    return ok;
}


function getValueAndReplace(containerTagId, valueTagId, textToReplace){
    // cointainerTagId: The container to find value and where we replace them.
    // valueTagId : this tag must be a input tag. It 's the value to put instead of textToReplace
    // textToReplace: the text to replace by the value of valuieTagId (NOT USE FIX IT)

    var valueTag = document.getElementById(valueTagId);
    if( ! valueTag )
        return
    var value = valueTag.value;

    var containerTag = document.getElementById(containerTagId);
    if (! containerTag)
        return

    // Capitalized firstname
    value = transformFirstname(value);
    valueTag.value = value; 

    var htmlContent = containerTag.innerHTML.replace(/{{firstname}}/g, value);
    containerTag.innerHTML = htmlContent;

}


function dayoneSubmit(form){

    if (! form.parent_cerise || ! form.parent_cerise.value){
        form.parent_cerise.style.border = '2px solid red';
        return false;
    }
}

function transformFirstname(firstname){
   // Return str
   //
   // firstname: str
   // Return word with the first char in uppercase and all chars after '-' also.
   // ex1: jean-bon -> Jean-Bon
   // ex2: JEaN-bON -> Jean-Bon

   firstname = firstname.toLowerCase();
   firstnames = firstname.split('-');

   for(var i=0;i<firstnames.length;i++)
       firstnames[i] = firstCharToUpperCase(firstnames[i]);

   return firstnames.join('-');
}


function firstCharToUpperCase(word){
   // Return str
   //
   // word: str
   // Return word with the first char in uppercase
   // ex: toto -> Toto

   if (!word)
       return ''
   return word.charAt(0).toUpperCase() + word.substring(1);
}


/*  Other method to replace all occurences of a given string in html text
    var SearchIndex = 0;
    var newString = "";
    while (containerTag.innerHTML.indexOf(textToReplace,SearchIndex) != -1) {			
    	newString += containerTag.innerHTML.substring(SearchIndex,containerTag.innerHTML.indexOf(textToReplace,SearchIndex));
    	newString += document.getElementById(valueTagId).value;
    	SearchIndex = (containerTag.innerHTML.indexOf(textToReplace,SearchIndex) + textToReplace.length); 		
    	}
    newString += containerTag.innerHTML.substring(SearchIndex,containerTag.innerHTML.length);		
    containerTag.innerHTML = newString;
*/
