// set variables for viewport height and width
var viewportwidth;
var viewportheight;

// set variable for prettyphoto height and width
var prettyPhotoHeight;
var prettyPhotoWidth;

// set the dimensions for the childs prettyphoto
var childPrettyPhotoHeight;
var childPrettyPhotoWidth;

// set variable for structure blocks
var iStructureBlocks;

/**
* This function will count the number of blocks
*/
function countBlocks() {
    // open the ajax call for counting blocks
    xmlHttp.open('GET', '/ajax_commands/count_structure_blocks.php', false);
    xmlHttp.send(null);

    // set the structure blocks
    return Math.abs(xmlHttp.responseText);
}

// initialise the count of structure blocks
iStructureBlocks = countBlocks();

/**
* This function will resize the elements inside the control panel
*/
function resizeElements() {
    // the more standard compliant browsers (mozilla, netscape, opera, IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
        viewportwidth = window.innerWidth;
    }

    // IE6 in standards compliant mode (withh a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
        viewportwidth = document.documentElement.clientWidth;
        viewportheight = document.documentElement.clientHeight;
    }

    // older versions of IE
    else {
        viewportheight = document.getElementsByTagName('body')[0].clientHeight;
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
    }

    // set the height of the content area
    document.getElementById('content').style.height = viewportheight - 80 + 'px';

    // set the pretty photo height value
    prettyPhotoHeight = viewportheight - 160;

    // set width of the content area
    document.getElementById('content').style.width = viewportwidth - 20 + 'px';

    // set the pretty photo width
    prettyPhotoWidth = viewportwidth - 80;

    // set the child dimensions
    childPrettyPhotoHeight = prettyPhotoHeight - 100;
    childPrettyPhotoWidth = prettyPhotoWidth - 50;

    // set an object for the building iframe
    var oBuildingIframe = document.getElementById('building_iframe');

    // if the building iframe object is found, set it's height
    if (oBuildingIframe) {
        oBuildingIframe.style.height = viewportheight - 137 + 'px';
        oBuildingIframe.style.width = viewportwidth - 70 + 'px';
    }

    // if the add page link is shown
    if (document.getElementById('add_page_link')) {
        // replace the url
        document.getElementById('add_page_link').href = document.getElementById('add_page_link').href.replace('height=600', 'height=' + prettyPhotoHeight);
        document.getElementById('add_page_link').href = document.getElementById('add_page_link').href.replace('width=800', 'width=' + prettyPhotoWidth);
    }
}

function getFrameWindow(frameId) {
    var frame = document.getElementById(frameId);

    var result = null;
    if (frame.contentDocument) {
        // For NS6
        result = frame.contentDocument.window || frame.contentDocument.defaultView || frame.contentWindow;
    } else if (frame.contentWindow) {
        // For IE5.5 and IE6
        result = frame.contentWindow;
    } else if (frame.document) {
        // For IE5
        if (frame.document.window) {
            result = frame.document.window;
        }
        else {
            result = frame.document;
        }
    }
    return result;
}


/**
* This function will be called once the building iframe has loaded
*/
function reloadBuildingLinks() {
    oBuildingFrame = getFrameWindow('building_iframe');

    for (a = 1; a <= iStructureBlocks; a++) {
        oLinkObject = oBuildingFrame.document.getElementById('block_' + a + '_config_link');

        if (oLinkObject) {
            sLinkUrl = oLinkObject.href;
            sLinkUrl = sLinkUrl.replace('width=600', 'width=' + childPrettyPhotoWidth);
            sLinkUrl = sLinkUrl.replace('height=400', 'height=' + childPrettyPhotoHeight);

            oLinkObject.href = sLinkUrl;
        }
    }

    if (oBuildingFrame.document.location != 'about:blank') {
        // clean the url for the ajax
        sAjaxUrl = oBuildingFrame.document.location.href.replace('http://' + document.location.hostname, '');
        sAjaxUrl = sAjaxUrl.replace('?building=true', '');

        // call the ajax to find out the structure content
        xmlHttp.open('GET', '/ajax_commands/get_page_structure_elements.php?url=' + sAjaxUrl, false);
        xmlHttp.send(null);

        // split the response into elements
        aElements = xmlHttp.responseText.split(',');

        // for all elements
        for (a = 0; a < aElements.length; a++) {
            if (aElements[a] != '') {
                // get the url of the control
                sUrl = oBuildingFrame.document.getElementById('structure_' + aElements[a] + '_edit').href;

                // replace height and width
                sUrl = sUrl.replace('width=600', 'width=' + childPrettyPhotoWidth);
                sUrl = sUrl.replace('height=400', 'height=' + childPrettyPhotoHeight);

                // replace the url
                oBuildingFrame.document.getElementById('structure_' + aElements[a] + '_edit').href = sUrl;
            }
        }
    }
}

/**
* This function will change the page inside the structure builder
*/
function changeStructurePage(sUrl) {
    // create an object for the building iframe
    var oBuildingIframe = document.getElementById('building_iframe');

    // create an object variable to hold the page selector
    oPageSelector = document.getElementById('page_selector');

    // change the SRC of the building iframe
    oBuildingIframe.src = sUrl;

    // if we have a url
    if (sUrl != '') {
        // set a cleaned url
        sCleanedUrl = sUrl.replace('?building=true', '');

        // set the add block link to be shown
        document.getElementById('add_block_link').style.display = '';

        // set the url of the prettyphoto
        document.getElementById('add_block_link').href = '/control_panel/page_details/add_block.php?url=' + sCleanedUrl + '&iframe=true&width=' + prettyPhotoWidth + '&height=' + prettyPhotoHeight;

        // set the parameters link to be shown
        document.getElementById('parameters_link').style.display = '';

        // set the url of the prettyphoto
        document.getElementById('parameters_link').href = '/control_panel/page_details/parameters.php?url=' + sCleanedUrl + '&iframe=true&width=' + prettyPhotoWidth + '&height=' + prettyPhotoHeight;

        // set the SEO link to be shown
        document.getElementById('seo_link').style.display = '';

        // set the url of the pretty photo
        document.getElementById('seo_link').href = '/control_panel/page_details/seo.php?url=' + sCleanedUrl + '&iframe=true&width=' + prettyPhotoWidth + '&height=' + prettyPhotoHeight;

        // set the delete block link to be shown
        document.getElementById('delete_block_link').style.display = '';

        // set the url of the pretty photo
        document.getElementById('delete_block_link').href = '/control_panel/page_details/delete_block.php?url=' + sCleanedUrl + '&iframe=true&width=' + prettyPhotoWidth + '&height=' + prettyPhotoHeight;

        // set the delete link to be shown
        document.getElementById('delete_page_link').style.display = '';

        // set the url of the prettyphoto
        document.getElementById('delete_page_link').href = '/control_panel/page_details/delete_page.php?url=' + sCleanedUrl + '&iframe=true&width=' + prettyPhotoWidth + '&height=' + prettyPhotoHeight;


    }

    // if no url, set all as hidden, and empty the urls
    else {
        document.getElementById('add_block_link').href = '#';
        document.getElementById('add_block_link').style.display = 'none';

        document.getElementById('parameters_link').href = '#';
        document.getElementById('parameters_link').style.display = 'none';

        document.getElementById('seo_link').href = '#';
        document.getElementById('seo_link').style.display = 'none';

        document.getElementById('delete_block_link').href = '#';
        document.getElementById('delete_block_link').style.display = 'none';

        document.getElementById('delete_page_link').style.display = 'none';
        document.getElementById('delete_page_link').href = '#';
    }
}









function validateLoginForm(form) {
    if (form.username.value == '') {
        getErrorMessage('login.UsernameEmpty');
        form.username.focus();
        return false;
    }

    if (form.password.value == '') {
        getErrorMessage('login.PasswordEmpty');
        form.password.focus();
        return false;
    }

    return true;
}

function checkFormChangePassword(form) {
    if (form.username.value == '') {
        getErrorMessage('ChangePassword.UsernameEmpty');
        form.username.focus();
        return false;
    }

    if (form.old_password.value == '') {
        getErrorMessage('ChangePassword.OldPasswordEmpty');
        form.old_password.focus();
        return false;
    }

    if (form.new_password.value == '') {
        getErrorMessage('ChangePassword.NewPasswordEmpty');
        form.new_password.focus();
        return false;
    }

    if (form.new_password_confirm.value == '') {
        getErrorMessage('ChangePassword.NewPasswordConfirmEmpty');
        form.new_password_confirm.focus();
        return false;
    }

    if (form.old_password.value == form.new_password.value) {
        getErrorMessage('ChangePassword.BothPasswordSame');
        form.old_password.value = '';
        form.new_password.value = '';
        form.new_password_confirm.value = '';
        form.old_password.focus();
        return false;
    }

    if (form.new_password.value != form.new_password_confirm.value) {
        getErrorMessage('ChangePassword.PasswordConfirmMismatch');
        form.new_password.value = '';
        form.new_password_confirm.value = '';
        form.new_password.focus();
        return false;
    }
}

function checkRegistrationForm(form) {
    if (form.username.value == '') {
        getErrorMessage('Registration.LoginEmpty');
        form.username.focus();
        return false;
    }

    if (form.email.value == '') {
        getErrorMessage('Registration.EmailEmpty');
        form.email.focus();
        return false;
    }

    if (form.password.value == '') {
        getErrorMessage('Registration.PasswordEmpty');
        form.password.focus();
        return false;
    }

    if (form.confirm_password.value == '') {
        getErrorMessage('Registration.PasswordConfirmationEmpty');
        form.confirm_password.focus();
        return false;
    }

    if (form.password.value != form.confirm_password.value) {
        getErrorMessage('Registration.PasswordMismatch');
        form.password.value = '';
        form.confirm_password.value = '';
        form.password.focus();
        return false;
    }

    if (checkCaptcha()) {
        if (form.code.value == '') {
            getErrorMessage('Captcha.empty');
            form.code.focus();
            return false;
        }
    }
}

function checkDeleteAccountForm(form) {
    if (form.password.value == '') {
        getErrorMessage('DeleteAccount.PasswordEmpty');
        form.password.focus();
        return false;
    }
}

function checkLockRemovalForm(form) {
    if (form.email.value == '') {
        getErrorMessage('LockRemoval.EmailEmpty');
        form.email.focus();
        return false;
    }

    if (form.username.value == '') {
        getErrorMessage('LockRemoval.UsernameEmpty');
        form.username.focus();
        return false;
    }

    if (form.password.value == '') {
        getErrorMessage('LockRemoval.PasswordEmpty');
        form.password.focus();
        return false;
    }

    if (form.message.value == '') {
        getErrorMessage('LockRemoval.MessageEmpty');
        form.message.focus();
        return false;
    }
}

function validate_add_file(form) {
    if (form.container_block.selectedIndex == 0) {
        getErrorMessage('TemplateFileBlock.NoBlockSelected');
        form.container_block.focus();
        return false;
    }

    if (form.include_file_path.selectedIndex == 0) {
        getErrorMessage('TemplateFileBlock.NoFileSelected');
        form.include_file_path.focus();
        return false;
    }
}

/*
function validate_add_dynamic_list(form) {
if (form.container_block.selectedIndex == 0) {
getErrorMessage('TemplateDynamicBlock.NoBlockSelected');
form.container_block.focus();
return false;
}
}
*/

/*
function validate_add_text_block(form) {
if (form.container_block.selectedIndex == 0) {
getErrorMessage('TemplateTextBlock.NoBlockSelected');
form.container_block.focus();
return false;
}

if (form.text_block_id.selectedIndex == 0) {
getErrorMessage('TemplateTextBlock.NoTextSelected');
form.text_block_id.focus();
return false;
}
}
*/

// if we have jQuery
if (window.jQuery) {
    // initiate when document is ready
    window.jQuery(document).ready(function() {
        // declare the function to drag faq articles
        window.jQuery.fn.DragArticles = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable("serialize");
                    window.jQuery.post('/ajax_commands/change_faq_order.php', order);
                }
            });
            window.jQuery("#list_sortable").disableSelection();
        }

        window.jQuery.fn.DragTopNavigation = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable("serialize");
                    window.jQuery.post('/ajax_commands/change_topmenu_order.php', order);
                }
            });
            window.jQuery("#list_sortable").disableSelection();
        }

        window.jQuery.fn.DragDynamicList = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable("serialize");
                    window.jQuery.post('/ajax_commands/change_dynamic_list_order.php', order);
                }
            });
            window.jQuery("#list_sortable").disableSelection();
        }

        window.jQuery.fn.DragPhotoGalleries = function() {
            window.jQuery('#list_sortable').sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable("serialize");
                    window.jQuery.post('/ajax_commands/change_gallery_order.php', order);
                }
            });
            window.jQuery("#list_sortable").disableSelection();
        }

        window.jQuery.fn.DragProductGalleries = function() {
            window.jQuery('#list_sortable').sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable("serialize");
                    window.jQuery.post('/ajax_commands/change_product_gallery_order.php', order);
                }
            });
            window.jQuery("#list_sortable").disableSelection();
        }

        window.jQuery.fn.DragProductCategories = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable('serialize');
                    window.jQuery.post('/ajax_commands/change_category_order.php', order);
                }
            });
            window.jQuery("#list_sortable").disableSelection();
        }

        window.jQuery.fn.DragProducts = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable('serialize');
                    window.jQuery.post('/ajax_commands/change_products_order.php', order);
                }
            });
            window.jQuery("#list_sortable").disableSelection();
        }

        window.jQuery.fn.DragPromoBox = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable('serialize');
                    window.jQuery.post('/ajax_commands/change_promo_order.php', order);
                }
            });
            window.jQuery("#list_sortable").disableSelection();
        }

        window.jQuery.fn.DragVideos = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable('serialize');
                    window.jQuery.post('/ajax_commands/change_video_order.php', order);
                }
            });
        }

        window.jQuery.fn.DragRSS = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable('serialize');
                    window.jQuery.post('/ajax_commands/change_rss_order.php', order);
                }
            });
        }

        window.jQuery.fn.dragPhotos = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable('serialize');
                    window.jQuery.post('/ajax_commands/change_photo_order.php', order);
                }
            });
        }

        window.jQuery.fn.dragColors = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable('serialize');
                    window.jQuery.post('/ajax_commands/change_color_order.php', order);
                }
            });
        }

        window.jQuery.fn.dragMats = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable('serialize');
                    window.jQuery.post('/ajax_commands/change_mats_order.php', order);
                }
            });
        }

        window.jQuery.fn.dragExpertCategories = function() {
            window.jQuery("#list_sortable").sortable({
                cursor: 'move',
                opacity: 0.6,
                update: function() {
                    var order = window.jQuery(this).sortable('serialize');
                    window.jQuery.post('/ajax_commands/change_expert_categories_order.php', order);
                }
            });
        }

        window.jQuery(function() {
            if (document.location.href.indexOf('/control_panel/faqs/change_order.php') != -1) {
                window.jQuery("#drag_content").DragArticles();
            }
            else if (document.location.href.indexOf('/control_panel/top_navigation/change_order.php') != -1) {
                window.jQuery("#drag_top_navigation").DragTopNavigation();
            }
            else if (document.location.href.indexOf('/control_panel/dynamic_folding_list/change_order.php') != -1) {
                window.jQuery("#drag_dynamic_list").DragDynamicList();
            }
            else if (document.location.href.indexOf('/control_panel/site_galleries/change_order.php') != -1) {
                window.jQuery("#drag_gallery").DragPhotoGalleries();
            }
            else if (document.location.href.indexOf('/control_panel/product_categories/change_order.php') != -1) {
                window.jQuery("#drag_product_categories").DragProductCategories();
            }
            else if (document.location.href.indexOf('/control_panel/products/change_order.php') != -1) {
                window.jQuery('#drag_products').DragProducts();
            }
            else if (document.location.href.indexOf('/control_panel/promobox_admin/index.php') != -1) {
                window.jQuery("#drag_promobox").DragPromoBox();
            }
            else if (document.location.href.indexOf('control_panel/product_gallery_images/change_order.php') != -1) {
                window.jQuery("#drag_promobox").DragProductGalleries();
            }
            else if (document.location.href.indexOf('control_panel/videos/change_order.php') != -1) {
                window.jQuery("#drag_videos").DragVideos();
            }
            else if (document.location.href.indexOf('/control_panel/rss_news/change_order.php') != -1) {
                window.jQuery("#drag_rss").DragRSS();
            }
            else if (document.location.href.indexOf('/control_panel/site_gallery_images/change_order.php') != -1) {
                window.jQuery("#drag_photo").dragPhotos();
            }
            else if (document.location.href.indexOf('/control_panel/available_colors/change_order.php') != -1) {
                window.jQuery("#drag_colors").dragColors();
            }
            else if (document.location.href.indexOf('/control_panel/mats/change_order.php') != -1) {
                window.jQuery("#drag_mats").dragMats();
            }
            else if (document.location.href.indexOf('/control_panel/expert_corner_categories/change_order.php') != -1) {
                window.jQuery("#drag_expert_categories").dragExpertCategories();
            }
        });
    });
}

function setTabActive(sTab) {
    if (sTab != 'new_text' && sTab != 'dynamic_folding_list_list' && sTab != 'add_dynamic_list') {
        document.getElementById(sTab + '_link').style.fontWeight = 'bold';
        document.getElementById(sTab + '_link').style.fontStyle = 'italic';
        document.getElementById(sTab + '_link').style.backgroundColor = '#C0C0C0';
        document.getElementById(sTab + '_link').style.color = '#FFFFFF';
        document.getElementById(sTab + '_cell').style.backgroundColor = '#C0C0C0';
    }
    document.getElementById(sTab + '_section').style.display = '';
}

function setInactiveTab(sTab) {
    if (sTab != 'new_text' && sTab != 'dynamic_folding_list_list' && sTab != 'add_dynamic_list') {
        document.getElementById(sTab + '_link').style.fontWeight = '';
        document.getElementById(sTab + '_link').style.fontStyle = '';
        document.getElementById(sTab + '_link').style.backgroundColor = '';
        document.getElementById(sTab + '_link').style.color = '';
        document.getElementById(sTab + '_link').style.backgroundColor = '';
    }
    document.getElementById(sTab + '_section').style.display = 'none';
}

function TabMenuShowAuxiliary(parent, child) {
    var p = document.getElementById(parent);
    var c = document.getElementById(child);

    var top = (c["at_position"] == "y") ? p.offsetHeight + 2 : 0;
    var left = (c["at_position"] == "x") ? p.offsetWidth + 2 : 0;

    c.style.position = "absolute";
    c.style.top = top + 'px';
    c.style.left = left + 'px';
    c.style.visibility = "visible";
}

function TabMenuShow() {
    var p = document.getElementById(this["at_parent"]);
    var c = document.getElementById(this["at_child"]);

    TabMenuShowAuxiliary(p.id, c.id);
    clearTimeout(c["at_timeout"]);
}

function TabMenuHide() {
    var p = document.getElementById(this["at_parent"]);
    var c = document.getElementById(this["at_child"]);
    c["at_timeout"] = setTimeout("document.getElementById('" + c.id + "').style.visibility = 'hidden'", 1000);
}

function TabMenuClick() {
    var p = document.getElementById(this["at_parent"]);
    var c = document.getElementById(this["at_child"]);

    if (c.style.visibility != "visible") {
        TabMenuShowAuxiliary(p.id, c.id);
    }
    else {
        c.style.visibility = "hidden";
    }
    return false;
}

// PARAMETERS:
// parent   - id of the parent html element
// child    - id of the child  html element that should be droped down
// showtype - "click" = drop down child html element on mouse click
//            "hover" = drop down child html element on mouse over
// position - "x" = display the child html element to the right
//            "y" = display the child html element below
// cursor   - omit to use default cursor or specify CSS cursor name

function TabMenuAttach(parent, child, showtype, position, cursor) {
    var p = document.getElementById(parent);
    var c = document.getElementById(child);

    p["at_parent"] = p.id;
    c["at_parent"] = p.id;
    p["at_child"] = c.id;
    c["at_child"] = c.id;
    p["at_position"] = position;
    c["at_position"] = position;
    c.style.position = "absolute";
    c.style.visibility = "hidden";

    if (cursor != undefined) {
        p.style.cursor = cursor;
    }

    switch (showtype) {
        case "click":
            p.onclick = TabMenuClick;
            p.onmouseout = TabMenuHide;
            c.onmouseover = TabMenuShow;
            c.onmouseout = TabMenuHide;
            break;

        case "hover":
            p.onmouseover = TabMenuShow;
            p.onmouseout = TabMenuHide;
            c.onmouseover = TabMenuShow;
            c.onmouseout = TabMenuHide;
            break;
    }
}

/**
* This function will validate the add page form
*/
function check_add_page_form(form) {
    // get the multiple language switch
    sMultipleLanguage = document.getElementById('multiple_language').innerHTML;

    // if it's on
    if (sMultipleLanguage == 'on') {
        // if we do not have a page details id present
        if (form.page_details_id.value == '') {
            if (!form.language[0].checked &
                !form.language[1].checked) {
                getErrorMessage('AddPage.NoLanguageSelected');
                form.language[0].focus();
                return false;
            }
        }
    }

    // if the page url is not filled
    if (form.url.value == '') {
        getErrorMessage('AddPage.NoUrlSpecified');
        form.url.focus();
        return false;
    }

    // if we have a file name
    else {
        // check for periods
        if (form.url.value.indexOf('.') != -1) {
            getErrorMessage('AddPage.UrlNoDots');
            form.url.focus();
            return false;
        }
    }

    // if the checkbox for adding to navigation is checked
    if (form.add_to_navigation.checked) {
        // if the french language is checked
        if (form.language[0].checked) {
            // if the link text for french version is not provided
            if (form.link_text_french.value == '') {
                getErrorMessage('AddPage.FrenchTextLinkNotProvided');
                form.link_text_french.focus();
                return false;
            }
        }

        // if it's english selected
        else if (form.language[1].checked) {
            // if the english text is not provided
            if (form.link_text_english.value == '') {
                getErrorMessage('AddPage.EnglishTextLinkNotProvided');
                form.link_text_english.focus();
                return false;
            }
        }
    }

    // if the form title is not provided
    if (form.page_title.value == '') {
        getErrorMessage('AddPage.TitleNotProvided');
        form.page_title.focus();
        return false;
    }
    return true;
}

function check_contact_form(form) {
    if (form.prenom.value == '') {
        getErrorMessage('ContactForm.FirstNameEmpty');
        form.prenom.focus();
        return false;
    }

    if (form.nom.value == '') {
        getErrorMessage('ContactForm.NameEmpty');
        form.nom.focus();
        return false;
    }

    if (form.email.value == '') {
        getErrorMessage('ContactForm.EmailEmpty');
        form.email.focus();
        return false;
    }

    if (form.telephone1.value == '' || form.telephone2.value == '') {
        getErrorMessage('ContactForm.PhoneIncomplete');
        form.telephone1.focus();
        return false;
    }

    if (form.sujet.value == '') {
        getErrorMessage('ContactForm.EmptySubject');
        form.sujet.focus();
        return false;
    }

    if (form.message.value == '') {
        getErrorMessage('ContactForm.EmptyMessage');
        form.message.focus();
        return false;
    }

    if (form.code.value == '') {
        getErrorMessage('ContactForm.CaptchaEmpty');
        form.code.focus();
        return false;
    }
}

/**
* This function will toggle the navigation display when a multi language section is present
*/
function toggleNavigationDisplay(sCheckbox) {
    // get the form container
    var form = document.getElementById('page_form');

    // if we are checked
    if (sCheckbox.checked) {
        // if neither language are checked
        if (!form.language[0].checked &
		    !form.language[1].checked) {
            getErrorMessage('AddPage.NoLanguageSelected');
            form.language[0].focus();
            sCheckbox.checked = false;
            return false;
        }

        // if we do have a checked language
        else {
            // if the french is checked
            if (form.language[0].checked) {
                // set the french name to be displayed
                document.getElementById('link_text_french_row').style.display = '';

                // enable the component
                form.link_text_french.disabled = false;
            }

            // if the english is checked
            else if (form.language[1].checked) {
                // set the english name to be displayed
                document.getElementById('link_text_english_row').style.display = '';

                // enable the component
                form.link_text_english.disabled = false;
            }
        }
    }

    // if we are not checked
    else {
        // hide both 
        document.getElementById('link_text_french_row').style.display = 'none';
        form.link_text_french.disabled = true;
        document.getElementById('link_text_english_row').style.display = 'none';
        form.link_text_english.disabled = true;
    }
}

function switchLanguage(sLanguage) {
    var form = document.getElementById('page_form');

    if (form.add_to_navigation.checked) {
        switch (sLanguage) {
            case 'french':
                document.getElementById('link_text_french_row').style.display = '';
                form.link_text_french.disabled = false;

                document.getElementById('link_text_english_row').style.display = 'none';
                form.link_text_english.disabled = true;
                form.link_text_english.value = '';
                break;

            case 'english':
                document.getElementById('link_text_french_row').style.display = 'none';
                form.link_text_french.disabled = true;
                form.link_text_french.value = '';

                document.getElementById('link_text_english_row').style.display = '';
                form.link_text_english.disabled = false;
                break;
        }
    }
}

/**
* This function will enable / disable an attribute in the advanced products
*/
function enable_attribute(sCheckBox, iAttributeId) {
    // if we are ticked
    if (sCheckBox.checked) {
        // show the item
        document.getElementById('attribute_section_' + iAttributeId).style.display = '';

        // enable the field
        document.getElementById('attribute_field_' + iAttributeId).disabled = false;
    }

    // if not ticked
    else {
        // hide the item
        document.getElementById('attribute_section_' + iAttributeId).style.display = 'none';

        // ensure value is empty
        document.getElementById('attribute_field_' + iAttributeId).value = '';

        // disable the field
        document.getElementById('attribute_field_' + iAttributeId).disabled = true;
    }
}

/*
* This method will calculate the new product price 
*/
function calculate_new_product_price() {
    var iOldPrice = Math.abs(document.add_cart_form.old_price.value);
    var form_length = document.add_cart_form.length;
    var new_price = Math.abs(document.add_cart_form.base_price.value);
    var iDiscount = Math.abs(document.add_cart_form.discount.value);
    var base_price = new_price;

    for (a = 0; a < form_length; a++) {
        if (document.add_cart_form.elements[a].type == 'select-one') {
            if (document.add_cart_form.elements[a].value.indexOf('+') != -1) {
                if (document.add_cart_form.elements[a].value != '+0') {
                    iItemDiscount = Math.abs(document.add_cart_form.elements[a].value * iDiscount);


                    new_price += Math.abs(document.add_cart_form.elements[a].value) - iItemDiscount;
                    iOldPrice += Math.abs(document.add_cart_form.elements[a].value);
                }
            }
            else {
                iItemDiscount = Math.abs(document.add_cart_form.elements[a].value * iDiscount)
                new_price -= Math.abs(document.add_cart_form.elements[a].value) - iItemDiscount; ;
                iOldPrice -= Math.abs(document.add_cart_form.elements[a].value);
            }
        }
    }

    if (Math.abs(new_price) != Math.abs(base_price)) {
        document.getElementById('new_price').innerHTML = Math.abs(new_price).toFixed(2) + ' $';
        document.getElementById('original_price').style.textDecoration = 'line-through';
        document.getElementById('original_price').innerHTML = Math.abs(iOldPrice).toFixed(2) + ' $';
    }
    else {
        document.getElementById('new_price').innerHTML = Math.abs(new_price).toFixed(2) + ' $';
        document.getElementById('original_price').innerHTML = Math.abs(iOldPrice).toFixed(2) + ' $';
    }
}

/**
* This product will check the add to cart form
*/
function check_add_cart_form(sLanguage) {
    var form_length = document.add_cart_form.length;
    var bConfigurationFilled = true;

    for (a = 0; a < form_length; a++) {
        if (document.add_cart_form.elements[a].type == 'select-one') {
            if (document.add_cart_form.elements[a].selectedIndex == 0) {
                bConfigurationFilled = false;
                break;
            }
        }
    }

    if (!bConfigurationFilled) {
        switch (sLanguage) {
            case 'english':
                alert('Please fill out all of the products options before adding it into your shopping cart');
                return false;
                break;

            case 'french':
                alert('Veuillez selectionner toute les options du produit avant de le placer dans votre panier d\'achat');
                return false;
                break;
        }
    }

    if (Math.abs(document.add_cart_form.quantity.value) == 0) {
        switch (sLanguage) {
            case 'english':
                alert('Please select a quantity higher than zero');
                return false;
                break;

            case 'french':
                alert('Veuillez indiquer une quantité à ajouter au panier d\'achat');
                return false;
                break;
        }
    }

    return true;
}
