// JavaScript Document
function characterSetEncodings(lang) {
    //lang is the target language
    //From greek to latin
    if (lang!="GR") {
        this.a1 = new Array('\u0386' , "A");
        this.a2 = new Array('\u0388' , "E");
        this.a3 = new Array('\u0389' , "H");
        this.a4 = new Array('\u038A' , "I");
        this.a5 = new Array('\u038C' , "O");
        this.a6 = new Array('\u038E' , "Y");
        this.a7 = new Array('\u038F' , "O");
        this.a8 = new Array('\u0390' , "i");
        this.a9 = new Array('\u0391' , "A");
        this.a10 = new Array('\u0392' , "B");
        this.a11 = new Array('\u0393' , "G");
        this.a12 = new Array('\u0394' , "D");
        this.a13 = new Array('\u0395' , "E");
        this.a14 = new Array('\u0396' , "Z");
        this.a15 = new Array('\u0397' , "H");
        this.a16 = new Array('\u0398' , "T");
        this.a17 = new Array('\u0399' , "I");
        this.a18 = new Array('\u039A' , "K");
        this.a19 = new Array('\u039B' , "L");
        this.a20 = new Array('\u039C' , "M");
        this.a21 = new Array('\u039D' , "N");
        this.a22 = new Array('\u039E' , "Z");
        this.a23 = new Array('\u039F' , "O");
        this.a24 = new Array('\u03A0' , "P");
        this.a25 = new Array('\u03A1' , "R");
        this.a26 = new Array('\u03A3' , "S");
        this.a27 = new Array('\u03A4' , "T");
        this.a28 = new Array('\u03A5' , "Y");
        this.a29 = new Array('\u03A6' , "F");
        this.a30 = new Array('\u03A7' , "X");
        this.a31 = new Array('\u03A8' , "J");
        this.a32 = new Array('\u03A9' , "O");
        this.a33 = new Array('\u03AA' , "I");
        this.a34 = new Array('\u03AB' , "Y");
        this.a35 = new Array('\u03AC' , "a");
        this.a36 = new Array('\u03AD' , "e");
        this.a37 = new Array('\u03AE' , "h");
        this.a38 = new Array('\u03AF' , "i");
        this.a39 = new Array('\u03B0' , "y");
        this.a40 = new Array('\u03B1' , "a");
        this.a41 = new Array('\u03B2' , "b");
        this.a42 = new Array('\u03B3' , "g");
        this.a43 = new Array('\u03B4' , "d");
        this.a44 = new Array('\u03B5' , "e");
        this.a45 = new Array('\u03B6' , "z");
        this.a46 = new Array('\u03B7' , "h");
        this.a47 = new Array('\u03B8' , "t");
        this.a48 = new Array('\u03B9' , "i");
        this.a49 = new Array('\u03BA' , "k");
        this.a50 = new Array('\u03BB' , "l");
        this.a51 = new Array('\u03BC' , "m");
        this.a52 = new Array('\u03BD' , "n");
        this.a53 = new Array('\u03BE' , "j");
        this.a54 = new Array('\u03BF' , "o");
        this.a55 = new Array('\u03C0' , "p");
        this.a56 = new Array('\u03C1' , "r");
        this.a57 = new Array('\u03C2' , "s");
        this.a58 = new Array('\u03C3' , "s");
        this.a59 = new Array('\u03C4' , "t");
        this.a60 = new Array('\u03C5' , "y");
        this.a61 = new Array('\u03C6' , "f");
        this.a62 = new Array('\u03C7' , "x");
        this.a63 = new Array('\u03C8' , "p");
        this.a64 = new Array('\u03C9' , "o");
        this.a65 = new Array('\u03CA' , "i");
        this.a66 = new Array('\u03CB' , "y");
        this.a67 = new Array('\u03CC' , "o");
        this.a68 = new Array('\u03CD' , "y");
        this.a69 = new Array('\u03CE' , "o");
    }
    else {
        //From latin to greek
        this.a1 = new Array('A' , '\u0391');
        this.a2 = new Array('B' , '\u0392');
        this.a3 = new Array('C' , '\u039A');
        this.a4 = new Array('D' , '\u0394');
        this.a5 = new Array('E' , '\u0395');
        this.a6 = new Array('F' , '\u03A6');
        this.a7 = new Array('G' , '\u0393');
        this.a8 = new Array('H' , '\u0397');
        this.a9 = new Array('I' , '\u0399');
        this.a10 = new Array('J' , '\u0399');
        this.a11 = new Array('K' , '\u039A');
        this.a12 = new Array('L' , '\u039B');
        this.a13 = new Array('M' , '\u039C');
        this.a14 = new Array('N' , '\u039D');
        this.a15 = new Array('O' , '\u039F');
        this.a16 = new Array('P' , '\u03A0');
        this.a17 = new Array('Q' , '\u039A');
        this.a18 = new Array('R' , '\u03A1');
        this.a19 = new Array('S' , '\u03A3');
        this.a20 = new Array('T' , '\u03A4');
        this.a21 = new Array('U' , '\u03A5');
        this.a22 = new Array('V' , '\u0392');
        this.a23 = new Array('W' , '\u0392');
        this.a24 = new Array('X' , '\u03A7');
        this.a25 = new Array('Y' , '\u03A5');
        this.a26 = new Array('Z' , '\u0396');
        this.a27 = new Array('a' , '\u03B1');
        this.a28 = new Array('b' , '\u03B2');
        this.a29 = new Array('c' , '\u03BA');
        this.a30 = new Array('d' , '\u03B4');
        this.a31 = new Array('e' , '\u03B5');
        this.a32 = new Array('f' , '\u03C6');
        this.a33 = new Array('g' , '\u03B3');
        this.a34 = new Array('h' , '\u03B7');
        this.a35 = new Array('i' , '\u03B9');
        this.a36 = new Array('j' , '\u03B9');
        this.a37 = new Array('k' , '\u03BA');
        this.a38 = new Array('l' , '\u03BB');
        this.a39 = new Array('m' , '\u03BC');
        this.a40 = new Array('n' , '\u03BD');
        this.a41 = new Array('o' , '\u03BF');
        this.a42 = new Array('p' , '\u03C0');
        this.a43 = new Array('q' , '\u03BA');
        this.a44 = new Array('r' , '\u03C1');
        this.a45 = new Array('s' , '\u03C3');
        this.a46 = new Array('t' , '\u03C4');
        this.a47 = new Array('u' , '\u03C5');
        this.a48 = new Array('v' , '\u03B2');
        this.a49 = new Array('w' , '\u03B2');		
        this.a50 = new Array('x' , '\u03C7');
        this.a51 = new Array('y' , '\u03C5');
        this.a52 = new Array('z' , '\u03B6');
    }
}

function twoEncodings(str) {
    var isGr = false;
    var isLatin = false;
    for (var i = 0 ; i < str.length; i++) {
        if (str.charCodeAt(i)>=902 && str.charCodeAt(i)<=974) {
            isGr = true;
        }
        if (str.charCodeAt(i)>=65 && str.charCodeAt(i)<=97 || str.charCodeAt(i)>=97 && str.charCodeAt(i)<=122) {
            isLatin = true;
        }
        if (isGr && isLatin)
            return true;
    }
    return false;
}

function convertEncoding(lang, val) {
		
    if (val.value!=null && val.value!="") {
        var tokens = val.value.split(" ");
        var correctedString = "";
        for (var tok = 0 ; tok < tokens.length ; tok++){
            if (twoEncodings(tokens[tok])) {
                conversions = new characterSetEncodings(lang);
                for (var i = 0 ; i < tokens[tok].length; i++) {
                    tempchar = tokens[tok].charAt(i);
                    for (x in conversions) {
                        var field = conversions[x][0];
                        var newField = conversions[x][1];
                        if (tempchar==field) {
                            tokens[tok] = tokens[tok].replace(field, newField);
                        }
                    }
                }
                if (correctedString!="")
                    correctedString = correctedString + " " + tokens[tok];
                else
                    correctedString = tokens[tok];
            }
            else {
                if (correctedString!="")
                    correctedString = correctedString + " " + tokens[tok];
                else
                    correctedString = tokens[tok];
            }
        }

					if ( correctedString.charAt(correctedString.length-1) == '\u03C2'){
						
						correctedString = correctedString.substring(0, correctedString.length-1)+'\u03C3';
						
					}
        val.value = correctedString.toUpperCase();
    }
}

function GREncoding(str) {     
    for (var i = 0 ; i < str.length; i++) {
        if (str.charCodeAt(i)>=902 && str.charCodeAt(i)<=974) {
            return true;            
        }
    }
    return false;
}
