// hide all images
function hide_all() {
    // look through each of the images
    for (a = 1; a <= 3; a++) {
        // set the display to none
        document.getElementById('rotate_' + a).style.display = 'none';
    }
}

// show the event image
function show_event(iEvent) {
    // if we are looking for event 4, set to zero
    if (iEvent == 4) {
        iEvent = 1;
    }

    // if we are looking at event zero, set precedent to three
    if (iEvent == 1) {
        iPrecedent = 3;
    }
    // else set precedent to the number before
    else {
        iPrecedent = iEvent - 1;
    }

    // hide all events
    hide_all();

    // set the image to be shown
    document.getElementById('rotate_' + iEvent).style.display = '';

    // change opacity
    opacity('rotate_' + iPrecedent, 100, 0, 2000);

    // set the preceding image to be hidden
    document.getElementById('rotate_' + iPrecedent).style.display = 'none';

    // set the current image to be shown
    document.getElementById('rotate_' + iEvent).style.display = '';

    // change opacity
    opacity('rotate_' + iEvent, 0, 100, 2000);

    // increment event
    iEvent++;

    // repeat after 7 seconds.
    setTimeout('show_event(' + iEvent + ')', 7000);
}


function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    }
    else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 


function changeFaqOrder(iFaqId, iNewOrder, bIsBefore) {
    if (ready_to_accept()) {
        xmlHttp.open('GET', '/ajax_commands/change_faq_order.php?faqs_id=' + iFaqId + '&display_order=' + iNewOrder + '&is_before=' + bIsBefore);
        xmlHttp.onreadystatechange = function myFunction() {
            if (ready_to_process()) {
                aList = xmlHttp.responseText.split('|');

                for (a = 0; a < aList.length; a++) {
                    aRecord = aList[a].split('-');

                    document.getElementById('row_' + aRecord[0]).innerHTML = aRecord[1];
                }
            }
        }
        xmlHttp.send(null);
    }
    else {
        setTimeout('changeFaqOrder(' + iFaqId + ', ' + iNewOrder + ')', 250);
    }
}

/**
* Thsi function will retreive the error message of a javascript form
*/
function getErrorMessage(sErrorKey) {
    // if we are ready to accept
    if (ready_to_accept()) {
        // open the feed
        xmlHttp.open('GET', '/ajax_commands/get_error_message.php?error_key=' + sErrorKey);

        // when status changes
        xmlHttp.onreadystatechange = function myFunction() {
            // if we are ready to process
            if (ready_to_process()) {
                // return the response
                alert(xmlHttp.responseText);
            }
        }

        // send the feed
        xmlHttp.send(null);
    }

    // if we are not ready
    else {
        // try again in 250 MS
        setTimeout('getErrorMessage("' + sErrorKey + '")', 250);
    }
}

/**
* This function will detect if we are using captcha or not
*/
function checkCaptcha() {
    // if we are ready to accept
    if (ready_to_accept()) {
        // open the feed
        xmlHttp.open('GET', '/ajax_commands/check_captcha.php');

        // when status changes
        xmlHttp.onreadystatechange = function myFunction() {
            // if we are ready to process
            if (ready_to_process()) {
                // return the response
                if (xmlHttp.responseText == 'yes') {
                    return true;
                }
                else {
                    return false;
                }
            }
        }

        // send the feed
        xmlHttp.send(null);
    }

    // if not ready
    else {
        // repeat after 250ms
        setTimeout('checkCaptcha()', 250);
    }
}

/**
* Get google maps company
*/
function getGoogleMapsCompany() {
    // if we are ready to accept
    if (ready_to_accept()) {
        // open the feed
        xmlHttp.open('GET', '/ajax_commands/get_google_maps_company.php');

        // when status change
        xmlHttp.onreadystatechange = function myFunction() {
            // if we are ready to process
            if (ready_to_process()) {
                document.getElementById('google_maps_company').innerHTML = xmlHttp.responseText;
                return true;
            }
        }

        // send the feed
        xmlHttp.send(null);
    }

    // if not ready
    else {
        // repeat after 250ms
        setTimeout('getGoogleMapsCompany()', 250);
    }
}

/**
* This will get the google maps city
*/
function getGoogleMapsCity() {
    // if we are ready to accept
    if (ready_to_accept()) {
        // open the feed
        xmlHttp.open('GET', '/ajax_commands/get_google_maps_city.php');

        // when status change
        xmlHttp.onreadystatechange = function myFunction() {
            // if ready to process
            if (ready_to_process()) {
                // set the city
                document.getElementById('google_maps_city').innerHTML = xmlHttp.responseText;
            }
        }

        // send the feed
        xmlHttp.send(null);
    }

    // if not ready
    else {
        // repeat after 250ms
        setTimeout('getGoogleMapsCity()', 250);
    }
}

/**
* This will get the google maps address
*/
function getGoogleMapsAddress() {
    // if we are ready to accept
    if (ready_to_accept()) {
        // open the feed
        xmlHttp.open('GET', '/ajax_commands/get_google_maps_address.php');

        // when status change
        xmlHttp.onreadystatechange = function myFunction() {
            // if ready to process
            if (ready_to_process()) {
                // set the address
                document.getElementById('google_maps_address').innerHTML = xmlHttp.responseText;
            }
        }

        // send the feed
        xmlHttp.send(null);
    }

    // if not ready
    else {
        // repeat after 250ms
        setTimeout('getGoogleMapsAddress()', 250);
    }
}

/**
* This will get the google maps province
*/
function getGoogleMapsProvince() {
    // if ready to process
    if (ready_to_process()) {
        // open the feed
        xmlHttp.open('GET', '/ajax_commands/get_google_maps_province.php');

        //when status changes
        xmlHttp.onreadystatechange = function myFunction() {
            // if ready to process
            if (ready_to_process()) {
                // set the province
                document.getElementById('google_maps_province').innerHTML = xmlHttp.responseText;
            }
        }

        // send the feed
        xmlHttp.send(null);
    }

    // if not ready
    else {
        // repeat after 250ms
        setTimeout('getGoogleMapsProvince()', 250);
    }
}

/**
 * Thsi function will get the rotating image delay
 */
function getRotatingImageDelay() {
    xmlHttp.open('GET', '/ajax_commands/get_image_delay.php', false);
    xmlHttp.send(null);
    return xmlHttp.responseText;
}

/**
 * This function will get the transition duration
 */
function getRotatingTransitionDuration() {
    xmlHttp.open('GET', '/ajax_commands/get_transition_duration.php', false);
    xmlHttp.send(null);
    return xmlHttp.responseText;
}

/**
* This will get the google maps country
*/
function getGoogleMapsCountry() {
    // if ready to process
    if (ready_to_process()) {
        // open the feed
        xmlHttp.open('GET', '/ajax_commands/get_google_maps_country.php');

        // when status changes
        xmlHttp.onreadystatechange = function myFunction() {
            // if ready to process
            if (ready_to_process()) {
                // set the country
                document.getElementById('google_maps_country').innerHTML = xmlHttp.responseText;
            }
        }

        // send the feed
        xmlHttp.send(null);
    }

    // if not ready
    else {
        // repeat after 250ms
        setTimeout('getGoogleMapsCountry()', 250);
    }
}

/**
* Get google maps zipcode
*/
function getGoogleMapsZipcode() {
    // if ready to process
    if (ready_to_process()) {
        // open the feed
        xmlHttp.open('GET', '/ajax_commands/get_google_maps_zipcode.php');

        // as status changes
        xmlHttp.onreadystatechange = function myFunction() {
            // if ready to process
            if (ready_to_process()) {
                // set the zipcode
                document.getElementById('google_maps_zipcode').innerHTML = xmlHttp.responseText;
            }
        }

        // send the feed
        xmlHttp.send(null);
    }

    // if not ready
    else {
        // repeat after 250ms
        setTimeout('getGoogleMapsZipcode()', 250);
    }
}


/**
* This function will check if we are using multiple languages
*/
function checkMultipleLanguage() {
    // initiate a return variable
    var bReturn;

    // if we are ready to accept
    if (ready_to_accept()) {
        // open the feed
        xmlHttp.open('GET', '/ajax_commands/check_multiple_language.php');

        // when status changes
        xmlHttp.onreadystatechange = function myFunction() {
            // if we are ready to process
            if (ready_to_process()) {
                if (xmlHttp.responseText == 'true') {
                    alert(xmlHttp.responseText);

                    bReturn = 'true';
                }
                else {
                    bReturn = 'false';
                }

                return bReturn;
            }
        }

        // send the feed
        xmlHttp.send(null);
    }

    // if not ready
    else {
        // repeat after 250mms
        setTimeout('checkMultipleLanguage()', 250);
    }
}

/**
* This function will retreive the list of attribute choices
*/
function getAtributeChoices(iAttributeId) {
    if (ready_to_accept()) {
        xmlHttp.open('GET', '/ajax_commands/get_attribute_choices.php?product_attributes_id=' + iAttributeId);
        xmlHttp.onreadystatechange = function myFunction() {
            if (ready_to_process()) {
                droplist = document.price_configuration_form.attribute_choices_id;
                droplist.options.length = 1;

                aAttributeChoices = xmlHttp.responseText.split('|');

                if (aAttributeChoices.length != 0) {
                    for (a = 0; a < aAttributeChoices.length; a++) {
                        aChoice = aAttributeChoices[a].split('*');

                        var newOption = document.createElement('OPTION');
                        newOption.text = aChoice[1];
                        newOption.value = aChoice[0];
                        droplist.options.add(newOption);
                    }
                }
            }
        }
        xmlHttp.send(null);
    }
    else {
        setTimeout('getAttributeChoices(' + iAttributeId + ')', 250);
    }
}

/**
* This function will display the add new block container
*/
function showAddNewBlock(iPageId) {
    // if ready to accept
    if (ready_to_accept()) {
        // open the feed
        xmlHttp.open('GET', '/ajax_commands/show_new_text_block.php?page_details_id=' + iPageId);

        // when status changes
        xmlHttp.onreadystatechange = function myFunction() {
            // if ready to process
            if (ready_to_process()) {
                // get the response text and put into container
                document.getElementById('main_section').innerHTML = xmlHttp.responseText;

                // get the instance
                var instance = CKEDITOR.instances['new_block_text'];

                // if the instance exists
                if (instance) {
                    // remove it
                    CKEDITOR.remove(instance);
                }

                // replace the text area with a CKEditor instance
                CKEDITOR.replace('new_block_text',
                {
                    filebrowserBrowseUrl: "/kfm/"
                });
            }
        }

        // send the feed
        xmlHttp.send(null);
    }

    // repeat after 250ms
    else {
        setTimeout('showAddNewBlock(' + iPageId + ')', 250);
    }
}

/**
* This function will display the content container in the page
*/
function showPageContentBlock(iPageId) {
    // open the feed
    xmlHttp.open('GET', '/ajax_commands/show_page_container.php?page_details_id=' + iPageId, false);

    // send the feed
    xmlHttp.send(null);
    // get the response text and put into container
    document.getElementById('main_section').innerHTML = xmlHttp.responseText;

    // get the instance
    var instance = CKEDITOR.instances['page_content_text'];

    // if the instance exists
    if (instance) {
        // destroy it
        CKEDITOR.remove(instance);
    }

    // replace the textarea with a CKEditor instance
    CKEDITOR.replace('page_content_text',
                {
                    filebrowserBrowseUrl: "/kfm/"
                });

    // hide all
    hideAll();

    // set the background color
    document.getElementById('main_link').style.backgroundColor = '#C0C0C0';

}

/**
* This function will show a text container
*/
function showTextContainer(iPageId) {
    // if ready to accept
    if (ready_to_accept()) {
        // open the feed
        xmlHttp.open('GET', '/ajax_commands/show_text_block_container.php?page_details_id=' + iPageId);

        // when status changes
        xmlHttp.onreadystatechange = function myFunction() {
            // if ready to process
            if (ready_to_process()) {
                // get the response and put in main content
                document.getElementById('main_section').innerHTML = xmlHttp.responseText;

                // get the instance
                var instance = CKEDITOR.instances['text_block_' + iPageId];

                // if the instance exists
                if (instance) {
                    // destroy it
                    CKEDITOR.remove(instance);
                }

                // replace the textarea with a CKEDITOR instance
                CKEDITOR.replace('text_block_' + iPageId,
                {
                    filebrowserBrowseUrl: "/kfm/"
                });

                // hide all
                hideAll();

                // set the tab active
                setTabActive('text_block_' + iPageId);

                // display the main section
                document.getElementById('main_section').style.display = '';
            }
        }

        // send the feed
        xmlHttp.send(null);
    }

    // if not ready
    else {
        // retry after 250ms
        setTimeout('showTextContainer(' + iPageId + ')', 250);
    }
}
/**
* This function will reload the parent window's product gallery list to select the one we just created
*/
function reloadParentGalleryList(iGalleryId) {
    // open the call
    xmlHttp.open('GET', '/ajax_commands/refresh_product_galleries_list.php', false);

    // send the call
    xmlHttp.send(null);

    // set the droplist from the opener
    droplist = opener.document.product_form.image_gallery_id;
    // set the list length to one
    droplist.options.length = 1;
    // explode the list of galleries
    aGalleryChoices = xmlHttp.responseText.split('|');

    // if we have at least one choice avvailable
    if (aGalleryChoices.length != 0) {
        // for each choice available
        for (a = 0; a < aGalleryChoices.length; a++) {
            // explode the choice
            aChoice = aGalleryChoices[a].split('*');

            // create the new option
            var newOption = opener.document.createElement('OPTION');
            // set the option text
            newOption.text = aChoice[1];
            // set the option value
            newOption.value = aChoice[0];

            // if the choice is the one selected
            if (iGalleryId == aChoice[0]) {
                // set the option as selected
                newOption.selected = true;
            }

            // add the option into the list
            droplist.options.add(newOption);
        }
    }
}

// inscription infolettre
function inscription_infolettre() {
    // if ready to accept
    if (ready_to_accept()) {
        // build the command string
        sCommand = '/ajax_commands/inscription_infolettre.php?email=' + document.getElementById('infolettre_adresse').value;
        sCommand += '&first_name=' + document.getElementById('infolettre_prenom').value;
        sCommand += '&last_name=' + document.getElementById('infolettre_nom').value;

        // open XML feed
        xmlHttp.open('GET', sCommand);

        // when changes are occuring in status
        xmlHttp.onreadystatechange = function myFunction() {
            if (ready_to_process()) {
                if (document.getElementById('infolettre_division')) {
                    document.getElementById('infolettre_division').innerHTML = 'Inscription Effectuée avec succès';
                }
            }
        }

        // send the xml feed
        xmlHttp.send(null);
    }
    // if not ready
    else {
        // attempt after 250ms
        setTimeout('inscription_infolettre()', 250);
    }
}
