﻿var AnsiToUTF_8_Array = new Array(
"a7",    //0x00A7 - Armenian Section Sign             
"d689",  //0x0589 - Armenian Full Stop (Verjaket)     
"29",    //0x0029 - Armenian Right Parenthesis        
"28",    //0x0028 - Armenian Left Parenthesis         
"d59a",  //0x055A - Armenian Right Quotation Mark     
"d599",  //0x0559 - Armenian Left Quotation Mark      
"d687",  //0x0587 - Armenian Ligature "ew"            
"2e",    //0x002E - Armenian Dot (Mijaket)            
"d59d",  //0x055D - Armenian Separation Mark (But)    
"2c",    //0x002C - Armenian Comma                    
"2d",    //0x002D - Armenian EN Dash                  
"2d",    //0x002D - Armenian Hyphen (Yentamna)        
"2e",    //0x002E - Armenian Ellipsis (...)           
"d59c",  //0x055C - Armenian Exclamation Mark (Amanak)
"d59b",  //0x055B - Armenian Accent (Shesht)          
"d59e",  //0x055E - Armenian Question Mark (Paruyk)   
"d4b1",  //0x0531 - Armenian Capital Letter [ayb] 
"d5a1",  //0x0561 - Armenian Small Letter [ayb]   
"d4b2",  //0x0532 - Armenian Capital Letter [ben] 
"d5a2",  //0x0562 - Armenian Small Letter [ben]  
"d4b3",  //0x0533 - Armenian Capital Letter [gim]
"d5a3",  //0x0563 - Armenian Small Letter [gim]  
"d4b4",  //0x0534 - Armenian Capital Letter [da] 
"d5a4",  //0x0564 - Armenian Small Letter [da] 
"d4b5",  //0x0535 - Armenian Capital Letter [yech]
"d5a5",  //0x0565 - Armenian Small Letter [yech]  
"d4b6",  //0x0536 - Armenian Capital Letter [za]  
"d5a6",  //0x0566 - Armenian Small Letter [za]    
"d4b7",  //0x0537 - Armenian Capital Letter [e]   
"d5a7",  //0x0567 - Armenian Small Letter [e]     
"d4b8",  //0x0538 - Armenian Capital Letter [at]  
"d5a8",  //0x0568 - Armenian Small Letter [at]    
"d4b9",  //0x0539 - Armenian Capital Letter [to]  
"d5a9",  //0x0569 - Armenian Small Letter [to]    
"d4ba",  //0x053A - Armenian Capital Letter [zhe] 
"d5aa",  //0x056A - Armenian Small Letter [zhe]   
"d4bb",  //0x053B - Armenian Capital Letter [ini] 
"d5ab",  //0x056B - Armenian Small Letter [ini]   
"d4bc",  //0x053C - Armenian Capital Letter [lyun]
"d5ac",  //0x056C - Armenian Small Letter [lyun]  
"d4bd",  //0x053D - Armenian Capital Letter [khe] 
"d5ad",  //0x056D - Armenian Small Letter [khe]   
"d4be",  //0x053E - Armenian Capital Letter [tsa] 
"d5ae",  //0x056E - Armenian Small Letter [tsa]   
"d4bf",  //0x053F - Armenian Capital Letter [ken] 
"d5af",  //0x056F - Armenian Small Letter [ken]   
"d580",  //0x0540 - Armenian Capital Letter [ho]  
"d5b0",  //0x0570 - Armenian Small Letter [ho]    
"d581",  //0x0541 - Armenian Capital Letter [dza] 
"d5b1",  //0x0571 - Armenian Small Letter [dza]   
"d582",  //0x0542 - Armenian Capital Letter [ghat] 
"d5b2",  //0x0572 - Armenian Small Letter [ghat]  
"d583",  //0x0543 - Armenian Capital Letter [tche] 
"d5b3",  //0x0573 - Armenian Small Letter [tche]  
"d584",  //0x0544 - Armenian Capital Letter [men] 
"d5b4",  //0x0574 - Armenian Small Letter [men]   
"d585",  //0x0545 - Armenian Capital Letter [hi]  
"d5b5",  //0x0575 - Armenian Small Letter [hi]    
"d586",  //0x0546 - Armenian Capital Letter [nu]  
"d5b6",  //0x0576 - Armenian Small Letter [nu]    
"d587",  //0x0547 - Armenian Capital Letter [sha] 
"d5b7",  //0x0577 - Armenian Small Letter [sha]   
"d588",  //0x0548 - Armenian Capital Letter [vo]  
"d5b8",  //0x0578 - Armenian Small Letter [vo]    
"d589",  //0x0549 - Armenian Capital Letter [cha]  
"d5b9",  //0x0579 - Armenian Small Letter [cha]   
"d58a",  //0x054A - Armenian Capital Letter [pe]  
"d5ba",  //0x057A - Armenian Small Letter [pe]  
"d58b",  //0x054B - Armenian Capital Letter [je]  
"d5bb",  //0x057B - Armenian Small Letter [je]    
"d58c",  //0x054C - Armenian Capital Letter [ra]  
"d5bc",  //0x057C - Armenian Small Letter [ra]    
"d58d",  //0x054D - Armenian Capital Letter [se]  
"d5bd",  //0x057D - Armenian Small Letter [se]    
"d58e",  //0x054E - Armenian Capital Letter [vev] 
"d5be",  //0x057E - Armenian Small Letter [vev]   
"d58f",  //0x054F - Armenian Capital Letter [tyun] 
"d5bf",  //0x057F - Armenian Small Letter [tyun]  
"d590",  //0x0550 - Armenian Capital Letter [re]  
"d680",  //0x0580 - Armenian Small Letter [re]    
"d591",  //0x0551 - Armenian Capital Letter [tso] 
"d681",  //0x0581 - Armenian Small Letter [tso]   
"d592",  //0x0552 - Armenian Capital Letter [vyun] 
"d682",  //0x0582 - Armenian Small Letter [vyun]  
"d593",  //0x0553 - Armenian Capital Letter [pyur] 
"d683",  //0x0583 - Armenian Small Letter [pyur]  
"d594",  //0x0554 - Armenian Capital Letter [ke]  
"d684",  //0x0584 - Armenian Small Letter [ke]    
"d595",  //0x0555 - Armenian Capital Letter [o]   
"d685",  //0x0585 - Armenian Small Letter [o]     
"d596",  //0x0556 - Armenian Capital Letter [fe]  
"d686",  //0x0586 - Armenian Small Letter [fe]    
"27"     //0x0027 - Armenian Apostrophe
);        

////////////////////////////////////////////////////////////////////////////////////////        
function hex2bin(hexdata) {
  var strdata = '';
  for (var i=0; i< hexdata.length; i += 2) {
     bindata += String.fromCharCode(parseInt(hexdata.substr(i,2),16));
  }  
  return bindata;
}
////////////////////////////////////////////////////////////////////////////////////////        
function ArmAnsiToUTF8(ansistr)
{
        var length = ansistr.length;
        var new_str = "";
        for(var si = 0; si < length; si = si+1)
        {
                if(ansistr.charAt(si) >= 0xA2 && ansistr.charAt(si) <= 0xFE){
                        new_str += hex2bin(AnsiToUTF_8_Array[ansistr.charAt(si) - 0xA2]); 
                }
                else{
                        new_str += ansistr.charAt(si);
                }
        }
                
        return new_str;
}
////////////////////////////////////////////////////////////////////////////////////////        
function UnicoedeToANSI(ftext)
{
//  	var ArmUniCode =  new Array("0x0531","0x0532","0x0533","0x0534","0x0535","0x0536","0x0537","0x0538","0x0539","0x053A","0x053B","0x053C","0x053D","0x053E","0x053F","0x0540","0x0541","0x0542","0x0543","0x0544","0x0545","0x0546","0x0547","0x0548","0x0549","0x054A","0x054B","0x054C","0x054D","0x054E","0x054F","0x0550","0x0551","0x0552","0x0553","0x0554","0x0555","0x0556","0x0557","0x0558","0x0559","0x055A","0x055B","0x055C","0x055D","0x055E","0x055F","0x0560","0x0561","0x0562","0x0563","0x0564","0x0565","0x0566","0x0567","0x0568","0x0569","0x056A","0x056B","0x056C","0x056D","0x056E","0x056F","0x0570","0x0571","0x0572","0x0573","0x0574","0x0575","0x0576","0x0577","0x0578","0x0579","0x057A","0x057B","0x057C","0x057D","0x057E","0x057F","0x0580","0x0581","0x0582","0x0583","0x0584","0x0585","0x0586","0x0587","0x0588","0x0589");
        var ArmAnsiCode = new Array(    0xB2,    0xB4,    0xB6,    0xB8,    0xBA,    0xBC,    0xBE,    0xC0,    0xC2,    0xC4,    0xC6,    0xC8,    0xCA,    0xCC,    0xCE,    0xD0,    0xD2,    0xD4,    0xD6,    0xD8,    0xDA,    0xDC,    0xDE,    0xE0,    0xE2,    0xE4,    0xE6,    0xE8,    0xEA,    0xEC,    0xEE,    0xF0,    0xF2,    0xF4,    0xF6,    0xF8,    0xFA,    0xFC,    0x20     ,0x20    ,0xA7,    0xA6,    0xB0,    0xAF,    0xAA,    0xB1,    0xB1,   0x20,    0xB3,    0xB5,    0xB7,    0xB9,    0xBB,    0xBD,    0xBF,    0xC1,    0xC3,    0xC5,    0xC7,    0xC9,    0xCB,    0xCD,    0xCF,    0xD1,    0xD3,    0xD5,    0xD7,    0xD9,    0xDB,    0xDD,    0xDF,    0xE1,    0xE3,    0xE5,    0xE7,    0xE9,    0xEB,    0xED,    0xEF,    0xF1,    0xF3,    0xF5,    0xF7,    0xF9,    0xFB,    0xFD,    0xA8,    0x20,   0x3a);
        var vyu = String.fromCharCode(0xf5);
        var ye_capital = String.fromCharCode(0xba);
        var ye_small = String.fromCharCode(0xbb);
        var Courier_AM_yev = String.fromCharCode(0xa2);
        var yev = String.fromCharCode(0xa8);
        var winword_g = String.fromCharCode(0x2022);
        var ansi_g = String.fromCharCode(0xb7);
        
        var sAnsiText = "";
        var current_char = '';
        var next_char = '';
        var charcode = 0;
        var length = ftext.length;
		var index = 0, nextindex = 0;
        for(index = 0; index < length; ++index)
        {
                current_char = ftext.charAt(index);
                if (current_char == winword_g) {
                        current_char = ansi_g;
                }
                else{
                        charcode = current_char.charCodeAt(current_char,0);
                        if(charcode >= 0x0531 && charcode <= 0x0556  ||  charcode >= 0x0559 && charcode <= 0x0589 )
                        {
                                current_char = String.fromCharCode(ArmAnsiCode[charcode-0x0531]);
                        }
                }
                
				if(charcode == String.fromCharCode(0x2024)){
					current_char = '.';
				}
				else
                if(current_char == Courier_AM_yev)// in 'Courier AM' ev code is a2, but in LatArm A8
                {
                        current_char == yev;
                }
				else
                {
                if(current_char == vyu && index != 0) // for ye+vyu construction // 0xf5 - vyu
                        if(sAnsiText.charAt(sAnsiText.length-1) == ye_small)
                        {
                                sAnsiText = sAnsiText.substr(0,sAnsiText.length-1);
                                current_char = yev;
                        }
                }
                sAnsiText += current_char;
        }
        return sAnsiText;
}
////////////////////////////////////////////////////////////////////////////////////////        
        function change_image(oname, imgsrc)
        {
                document.getElementById(oname).src = imgsrc;
                return true;
        }
////////////////////////////////////////////////////////////////////////////////////////        
        function myalert( value ) 
        {
                alert(value);
        }
////////////////////////////////////////////////////////////////////////////////////////        
        function GetScanText(PostText)
        {
                var max_word_count = 110;
                var use_length = 0;// text length which can be translate
                var last_sent_end = 0;// last sentence end index
                var last_word_start = 0;// last sentence first letter index
                var word_count = 0;// word count in all text
                var sent_word_count = 0;// word count in sentence

                var often_unions = new Array("and","or","that","because","but","when","When","who","which","If","if","than","և","ու","կամ","որ","որովհետև","բայց","երբ","Երբ","որը","Եթե","եթե","ապա");
                var often_unions_size = often_unions.length;

                var unions_count = 0; 
                var is_prev_coma = false;
                
                var word_coefficient = 1.4;// in socund
                var union_coefficient = 4;// in second
                
                trans_timeout = 0;
                var ci;
                for(ci = 1; ci < PostText.length; ci++)
                {
                        
					if (  PostText.charAt(ci) == ' ' 
					|| PostText.charAt(ci) == '.'
					|| PostText.charAt(ci) == ':'
					|| PostText.charAt(ci) == ','
					|| PostText.charAt(ci) == ';'
					|| PostText.charAt(ci) == '\r'
					|| PostText.charAt(ci) == '\n'
					|| PostText.charAt(ci) == '\t')
                    {
                                        
                        if ( PostText.charAt(ci-1) != ' ' 
                        && PostText.charAt(ci-1) != '.'
                        && PostText.charAt(ci-1) != ':'
                        && PostText.charAt(ci-1) != ','
						&& PostText.charAt(ci-1) != ';'
                        && PostText.charAt(ci-1) != '\r'
						&& PostText.charAt(ci-1) != '\n'
                        && PostText.charAt(ci-1) != '\t')
                        {
                        word_count++;
                        sent_word_count++;
                                                
                        // determin union words and count it
							last_word = ArmAnsiToUTF8( PostText.substr(last_word_start, ci - last_word_start) );
							var ui;
							for( ui = 0; ui < often_unions_size; ui++)
									if (often_unions[ui] == last_word)
									{
											if( !is_prev_coma )// if previus element is not a union or coma
													unions_count++;
											break;
									}
									
							if (ui == often_unions_size) is_prev_coma = false;// may be ", and then". this must be count one union

                        }
                
                        last_word_start = ci+1;
						// store is previus element a comma.  it will be used when shan unions
						if(PostText.charAt(ci) == ',' 
						&& PostText.charAt(ci-1) != '.'
                        && PostText.charAt(ci-1) != ':'
                        && PostText.charAt(ci-1) != ','
                        && PostText.charAt(ci-1) != ';'
                        && PostText.charAt(ci-1) != '\r'
                        && PostText.charAt(ci-1) != '\n')
						{
							if( !is_prev_coma )// if previus element is not a union or coma
									unions_count++;
							is_prev_coma = true;
						}
                
                        if( (PostText.charAt(ci) == "."
							| PostText.charAt(ci) == ":"
							| PostText.charAt(ci) == "\r"
							| PostText.charAt(ci) == "\n") 
                        && 
							(PostText.charAt(ci-1) != "."
                        && PostText.charAt(ci-1) != ":"
						&& PostText.charAt(ci-1) != "\r"
                        && PostText.charAt(ci-1) != "\n") )
						{
							// aproximatly count time length of translate
							if(unions_count != 0)
									trans_timeout += (sent_word_count*word_coefficient)*(unions_count*word_coefficient);
							else
									trans_timeout += (sent_word_count*word_coefficient);
                                                        
							last_sent_end = ci;
							sent_word_count = 0;
                            unions_count = 0;
                        }
                
                        if(word_count > max_word_count)
							break;
					}
                }
                
                if (  PostText.charAt(ci-1) != ' ' 
					&& PostText.charAt(ci-1) != '.'
					&& PostText.charAt(ci-1) != ':'
					&& PostText.charAt(ci-1) != ','
					&& PostText.charAt(ci-1) != ';'
					&& PostText.charAt(ci-1) != '\r'
					&& PostText.charAt(ci-1) != '\n'
					&& PostText.charAt(ci-1) != '\t') sent_word_count++;
                        
//                if(unions_count != 0)
//                        trans_timeout += (sent_word_count*word_coefficient)*(unions_count*word_coefficient);
//                else
//                        trans_timeout += (sent_word_count*word_coefficient);
//
//                trans_timeout += 20;// navsjaki sluchi
//
//                if (trans_timeout > 180)// petq chi shat spasel
//                        trans_timeout = 180;
//                else
//                if (trans_timeout < 30)// navsjaki sluchi
//                        trans_timeout = 30;
//                        
//                trans_timeout = Math.round(trans_timeout);
//                trans_timeout = trans_timeout + 20;
//        
//                if (trans_timeout > 180)// petq chi shat spasel
//                    trans_timeout = 180;
//
                if(word_count > max_word_count)
                {
					if(last_sent_end == 0)
					{
						if(window.document.getElementById("face_language").value == "armenian")
							alert("Նախադասությունը պատունակում է " + max_word_count + "-ից ավելի բառեր: Այս նախադասությունը թարգմանության չի ուղարկվի"); 
						else	
							alert("The sentence contain more than " + max_word_count + " words. We don\'t translate the text, which contain more than "+  max_word_count+ " words"); 
						return "";
					}
                    else{
						if(window.document.getElementById("face_language").value == "armenian")
							alert("Տեքստը պարունակում է "+ max_word_count +"-ից ավելի բառեր. Թարգմանության կուղարկվի տեքստի միայն մի հատվածը:");
						else
							alert("The Text contain more than "+ max_word_count +" words. We translate only firs part of the text, which contain less than "+ max_word_count +" words");
					}
        
					use_length = last_sent_end+1;
					PostText = PostText.substr(0, use_length);
                }  
                
                return PostText;
        }
////////////////////////////////////////////////////////////////////////////////////////        
		function isWord(str)
		{
			if(str.length == 0)
				return false;
			var i = 0;
			for(i=0; i < str.length; ++i)
			{
				if(str[i] == " " ||
				  str[i] == "." ||
				  str[i] == ":" ||
				  str[i] == ";" ||
				  str[i] == "\r" ||
				  str[i] == "\n" ||
				  str[i] == "\t" ||
				  str[i] == "?" ||
				  str[i] == "`" ||
				  str[i] == "-"
				)
				continue;
				
				break;
			}
			var j = str.length;
			for(j=str.length-1; j > i; --j)
			{
				if(str[j] == " " ||
				  str[j] == "." ||
				  str[j] == ":" ||
				  str[j] == ";" ||
				  str[j] == "\r" ||
				  str[j] == "\n" ||
				  str[j] == "\t" ||
				  str[j] == "?" ||
				  str[j] == "`" ||
				  str[j] == "-"
				)
				continue;
				
				break;
			}
			var ret = true;
			for(k=i+1; k < j; ++k)
			{
				if(str[k] == " " ||
				  str[k] == "." ||
				  str[k] == ";" ||
				  str[k] == ":" ||
				  str[k] == "\r" ||
				  str[k] == "\n" ||
				  str[k] == "\t" ||
				  str[k] == "?" ||
				  str[k] == "`" ||
				  str[k] == "-"
				){
					ret = false;
					break;
				}
			}
			
			return ret;
		}
        function TranslateText()
        {
            if (window.document.getElementById("SourceFildID").value == "")
						{
								 Clear();
				                window.document.getElementById("SourceFildID").focus();
								return false;
						}
						else
						{      
							var iswrd = isWord(window.document.getElementById("SourceFildID").value);
							var short_text = GetScanText(UnicoedeToANSI(window.document.getElementById("SourceFildID").value));
				            if(short_text == "")
							{
                				Clear();
								window.document.getElementById("SourceFildID").focus();
								return false;
				            }

//							if(iswrd == false){
								document.getElementById("ToTransID").value = short_text;
								document.getElementById("TransTextFormID").action = "../sentence.php";
								document.getElementById("TransTextFormID").target = "DestForm";
								document.getElementById("TransTextFormID").submit(); 
//							}
//							else{
//								TranslateWord();
//							}
							return true;
							
            }
        }
        function Clear()
        {
			document.getElementById("ToTransID").value = "";
			document.getElementById("TransTextFormID").action = "../empty.php";
			document.getElementById("TransTextFormID").target = "DestForm";
			document.getElementById("TransTextFormID").submit(); 
        }
////////////////////////////////////////////////////////////////////////////////////////        
      function TranslateWord()
      {
            if (window.document.getElementById("SourceFildID").value == "")
						{
							Clear();
							window.document.getElementById("SourceFildID").focus();
							return false;
						}
						else
						{       
							var short_text = GetScanText(UnicoedeToANSI(window.document.getElementById("SourceFildID").value));
							if(short_text == "")
							{
								Clear();
								window.document.getElementById("SourceFildID").focus();
								return false;
							}

							document.getElementById("ToTransID").value = short_text;
							document.getElementById("trans_direction").value = document.getElementById("trans_direction_selID").value;
							document.getElementById("TransTextFormID").action = "../word.php";
							document.getElementById("TransTextFormID").target = "DestForm";
							document.getElementById("TransTextFormID").submit(); 
							return true;
            }
        }   
////////////////////////////////////////////////////////////////////////////////////////        
        function SpellChecker()
        {
			if (document.getElementById("SourceFildID").value == "")
			{
				document.getElementById("SourceFildID").focus();
			}
			else
			{
				document.getElementById("ToTransID").value = UnicoedeToANSI(document.getElementById("SourceFildID").value);
				document.getElementById("TransTextFormID").action = "../spellchecker.php";
				document.getElementById("TransTextFormID").target = "_blank";
				document.getElementById("TransTextFormID").submit(); 
			}
    }
		
		
		function trans_direction_change(select_object, div_object){
			if(select_object.value == "Armenian-WArmenian" ||
			   select_object.value == "WArmenian-Armenian"
			   )
			{
				div_object.style.display="block";
			}
			else
			{
				div_object.style.display="none";
			}
		}
		
////////////////////////////////////////////////////////////////////////////////////////        
