function resizeElements() {
    var viewportwidth;
    var viewportheight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with 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 {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
    document.getElementById('leftcol').style.height = viewportheight - (31 + 46) + 'px';
    document.getElementById('content').style.height = viewportheight - (31 + 46) + 'px';
    document.getElementById('content').style.width = viewportwidth - 230 + 'px';

    if (document.getElementById('search_frame')) {
        document.getElementById('search_frame').style.height = viewportheight - 150 + 'px';
    }
}

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(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();
            }
        });
    });
}

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 base_price = document.add_cart_form.base_price.value;
    var form_length = document.add_cart_form.length;
    var new_price = Math.abs(base_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) {
                new_price += Math.abs(document.add_cart_form.elements[a].value);
            }
            else {
                new_price -= 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';
    }
    else {
        document.getElementById('new_price').innerHTML = '';
        document.getElementById('original_price').style.textDecoration = '';
    }
}

/**
* 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;
}
