﻿/*
Hawlfraint Prifysgol Bangor 2009.
Copyright Bangor University 2009.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided 
that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions 
      and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
      and the following disclaimer in the documentation and/or other materials provided with the 
      distribution.
 
    * Neither the name of the Bangor University nor the names of its contributors may be used to endorse 
      or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/

var uiLanguage = "cy";
var textareaintro = false;

function initialiseUILanguage(arg) {

    rdoButton = document.getElementById("rbnCymraeg");
    rdoButton.checked = true;

    changeUIToCymraeg(arg);    
}

function changeUIToCymraeg(arg)
{
    uiLanguage = "cy";
    textareaintro = false;
    ret=org.egymraeg.cysill.ui.WebService.GetStrings(arg,"cy", OnUIComplete, OnUITimeout, OnUIError);                                                       
}

function changeUIToEnglish(arg)
{
    uiLanguage = "en";
    textareaintro = false;        
    ret=org.egymraeg.cysill.ui.WebService.GetStrings(arg,"en", OnUIComplete, OnUITimeout, OnUIError);                                                       
}

function OnUIComplete(arg)
{
    var json = eval('(' + arg + ')');
    var ui_strings = json.ui_strings;
    
    if (ui_strings.length > 0)
    {
        for(var i=0; i < ui_strings.length; i++)
        {
            if (ui_strings[i].ui_string_id.substring(0,3) == "btn") {
                var btnElement = document.getElementById(ui_strings[i].ui_string_id);
                if (btnElement!=null) {
                    btnElement.value = ui_strings[i].ui_string_value;
                }
            }
            
            if (ui_strings[i].ui_string_id.substring(0,3) == "txt"){
                var txtElement=document.getElementById(ui_strings[i].ui_string_id);
                if (txtElement!=null) {
                    txtElement.innerHTML = ui_strings[i].ui_string_value;
                }
            }
            
            if (ui_strings[i].ui_string_id.substring(0,4) == "href") {
                var hrefElement = document.getElementById(ui_strings[i].ui_string_id);
                if (hrefElement!=null){
                    hrefElement.href = ui_strings[i].ui_string_value;
                }
            }

            if (ui_strings[i].ui_string_id.substring(0, 3) == "img") {
                var imgElement = document.getElementById(ui_strings[i].ui_string_id);
                if (imgElement != null) {
                    imgElement.src = ui_strings[i].ui_string_value;
                }
            }
            
            if (ui_strings[i].ui_string_id.substring(0, 8) == "textarea") 
            {
                // are merged into one textarea called simply 'text'
                if (textintrodisplayed==true) {
                    var existingvalue=document.getElementById("text").value;
                    if (textareaintro==false) {
                        document.getElementById("text").value = ui_strings[i].ui_string_value;
                        textareaintro=true;
                    }
                    else {
                        document.getElementById("text").value = existingvalue + ui_strings[i].ui_string_value;
                    }
                }
            }
        }
    }
    
    setSelectionRange();
}

function OnUITimeout(arg) {
    loadingGif = document.getElementById("LoadingAnimation");
    loadingGif.style.display = 'none';

    alert('Timeout :' + arg);
}

function OnUIError(arg) {
    loadingGif = document.getElementById("LoadingAnimation");
    loadingGif.style.display = 'none';

    alert('Error :' + arg);
}

