//<![CDATA[
var currentTab = 'tab1';
function showTab(name) {
    if( $('reviewProductLink') ) {
        $('reviewProductLink').style.display = name == 'tab3' ? 'none' : '';
    }
    if(($(currentTab + 'A')) != null){
        $(currentTab + 'A').className = '';
        $(currentTab).style.display = 'none';
    }
    $(name).style.display = "block";
    $(name + 'A').className = 'activeTab';
    currentTab = name;
}
function showReviewTab(){
    showTab('tab3');
}
function emailToFriend(id, isAdvancedGoogleAnalyticsActive) {
    var email = $('email');
    var name = $('name');
    var yourEmail = $('yourEmail');
    var message = $('message');

    $('friendEmailError').hide();
    $('yourNameError').hide();
    $('yourEmailError').hide();
    $('messageError').hide();
    $('globalErrorEmailToFriend').hide();

    email.removeClassName('validationError');
    name.removeClassName('validationError');
    yourEmail.removeClassName('validationError');
    message.removeClassName('validationError');

    var error = false;
    if (trim(email.value) == '') {
        $('friendEmailError').innerHTML = t('Friend\'s Email cannot be empty');
        $('friendEmailError').show();
        email.addClassName('validationError');
        error = true;
    } else if (!email_regex.test(email.value)) {
        $('friendEmailError').innerHTML = t('You must provide a valid e-mail address, please try again.');
        $('friendEmailError').show();
        email.addClassName('validationError');
        error = true;
    }
    if (trim(yourEmail.value) == '') {
        $('yourEmailError').innerHTML = t('Your Email cannot be empty');
        $('yourEmailError').show();
        yourEmail.addClassName('validationError');
        error = true;
    } else if (!email_regex.test(yourEmail.value)) {
        $('yourEmailError').innerHTML = t('You must provide a valid e-mail address, please try again.');
        $('yourEmailError').show();
        yourEmail.addClassName('validationError');
        error = true;
    }

    if (trim(name.value) == 'Enter Your Name' || trim(name.value) == '') {
        $('yourNameError').show();
        name.addClassName('validationError');
        error = true;
    }
    if (trim(message.value) == 'Enter Your Message' || trim(message.value) == '') {
        $('messageError').show();
        message.addClassName('validationError');
        error = true;
    }
    if(validateHTMLTags(name.value) || validateHTMLTags(message.value)){
        alert(t('Chars < and > are not allowed in email to friend'));
        enableFrontendButtons();
        return false;
    }

    if (error) {
        $('globalErrorEmailToFriend').show();
        enableFrontendButtons();
        return false;
    }

    ajaxProceeding = true;
    $('emailToFriendTable').style.display = 'none';
    $('loadingEmailToFriend').style.display = 'block';
    ajax.post(
        '/product/emailToFriend',
        {id: id, name: $F('name'), email: $F('email'), yourEmail: $F('yourEmail'), message: $F('message'), location: document.location.href},
        function(message) {
            ajaxProceeding = false;
            if(!message) {
                alert(t('Your email has been sent successfully'));
                enableFrontendButtons();
                addGAEventEmailToFriend(isAdvancedGoogleAnalyticsActive);
                return;
            }
            alert(t('Message cannot be sent'));
            enableFrontendButtons();
        },
        function() {
            ajaxProceeding = false;
            alert(t('Message cannot be sent'));
            enableFrontendButtons();
        }
    );

    $('emailToFriendTable').style.display = 'block';
    $('loadingEmailToFriend').style.display = 'none';
}

function addGAEventEmailToFriend(isAdvancedGoogleAnalyticsActive) {
    if(isAdvancedGoogleAnalyticsActive && pageTracker != undefined) {
        try {
            pageTracker._trackEvent(t('Customer'), t('Email To Friend'));
        }catch(err){}
    }
}

function changeProduct(url) {
    var menu = document.getElementById('options');
    if (menu) {
        menu.style.display='none';
    }
    window.location.href = url;
    return false;
}

function handleDropDown(elementId) {
    $(elementId).toggle();
}
function addToWishListProducts(id) {
    ajaxProceeding = true;
    ajax.post(
        '/product/addToWishList/',
        {id: id},
        function(message) {
            ajaxProceeding = false;
            if(!message) {
                alert(t('Product has been added to your Wish List.'));
                $('productWishList').innerHTML = '<a href="/account/wishlist/">' + t('Is on your Wish List') + '</a>';
                enableFrontendButtons();
                return;
            }
            alert(t('Product cannot be added'));
            enableFrontendButtons();
        },
        function() {
            location.href = '/account/wishlist?redirectFromCmd=/account/addToWishList/' + id
        }
    );
}

var myLyteBoxWatermarkImage = null;
function showImage(id, isCallForPrice){
    if (typeof myLytebox == "undefined") {
        imageToShow = document.getElementById('imageLink_' + id);
        return false;
    }

    var el   = document.getElementById('imageLink_' + id);
    myLytebox.start(el);
    if (isCallForPrice) { document.getElementById('lbBuy').style.display = 'none'; }
}

var currentPic = 0;

function showBigPicture(picNumber){
    if(!$('image_' + picNumber)) {
        return;
    }
    $('image_' + currentPic).hide();
    $('image_' + picNumber).show();
    currentPic = picNumber;
}

function enlargePicture(isCallForPrice){
    showImage(currentPic, isCallForPrice);
}

function changeNumberOfPersonalisedMessages(count) {
    var messages = '';
    for(var i=1; i<=count; i++) {
        messages += '<label for="message_' + i + '">' + t('Personalised Message ' + i) + ':<span class="required">&nbsp;*&nbsp;</span></label><input type="text" id="message_' + i + '" name="message_' + i + '" maxlength="200" value="" /><br />';
    }
    $('personalisedMessageList').update(messages);
}
//]]>
