﻿// ===================================================================
// GoogleTranslation.js - Google AJAX Translation functions
// ===================================================================
var itemId = document.getElementById("ItemIdLabel").innerHTML;

// Translates item title and description, and inserts Google brand
function translate() {

    document.getElementById("googleBrand").innerHTML = '';
    google.language.getBranding("googleBrand");
    var translated = document.getElementById("googleTranslationResultLabel");
    google.language.translate(GetTextToTranslate(), translateFromLanguage, translateToLanguage, function(result) {
        if (!result.error) {
            if (result.translation) {
                translated.innerHTML = result.translation;
            }
        }
        else {
            translated.innerHTML = "An error in the Google Translation has occured. Please try again.";
        }
    });    
}

// Determine the start up view of specific item based on the users previous visit (cookies)
function startTranslate() {
    if (HasCookie() == "true") {    
        enableTranslation();
    }
    else {
        disableTranslation();
    }
}
        
// Enables display of Google translation.
function enableTranslation() {
    document.getElementById("GoogleTransDiv").style.display = "block";
    document.getElementById("showTranslationDiv").style.display = "none";
    document.getElementById("hideTranslationDiv").style.display = "block";
    SetCookie("true");

    // Loads the google translation version configured in webconfig
    google.load("language", googleTranslationVersion, { "callback": translate });
}

// Disable display of Google translation.
function disableTranslation() {
    document.getElementById("GoogleTransDiv").style.display = "none";
    document.getElementById("showTranslationDiv").style.display = "block";
    document.getElementById("hideTranslationDiv").style.display = "none";
    SetCookie("false");
}

// Writes a cookie to the users browser with Google translation choices.
function SetCookie(valueBoolean) {
    var expiredate = new Date();
    expiredate.setDate(expiredate.getDate() + 1);
    document.cookie = "GoogleTrans" + itemId + "=" + valueBoolean + ";expires=" + expiredate.toGMTString();
}

// Checks if a specific item has a Google translation cookie.
function HasCookie() {
    if (document.cookie.indexOf("GoogleTrans" + itemId) != -1) {
        cookiestart = document.cookie.indexOf("GoogleTrans" + itemId) + ("GoogleTrans" + itemId).length + 1;
        cookieend = document.cookie.indexOf(';', cookiestart - 1);
        return (document.cookie.substring(cookiestart, cookieend));
    } else { return "false"; }
}

// Retrieve title and description of item in original language for translation.
function GetTextToTranslate(){
    var text
    text = "<b>"
    text = text + document.getElementById('ItemTitleLabel').innerHTML;
    text = text + "</b><br/>";
    text = text + document.getElementById('ItemDescription').innerHTML;
return text;
}