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o_о\'_p_q_r_s_сh_t_u_ұ\'_v_w_x_y_z_е\'_йu_йa_ЦH_ГH_НG_СH_ЙU_ЙA_А`_Е`_І`_О`_Ұ`_а`_е`_і`_о`_ұ`_#_ь#_|').split('_'); 
var lat_char3=('A_A\'_B_C_Ch_D_E_F_G_Gh_İ\'_I_İ\"_J_K_Q_L_M_N_Ng_O_O\'_P_R_S_Sh_T_U_U\'_V_W_X_Y_Z_E\'_Yu_Ya_a_a\'_b_c_ch_cH_d_e_f_g_gh_gH_h_i\'_i_i\"_j_k_l_m_n_ng_nG_o_o\'_p_q_r_s_sh_t_u_u\'_v_w_x_y_z_e\'_yu_ya_CH_GH_NG_SH_YU_YA_A`_E`_İ`_O`_U`_a`_e`_i`_o`_u`_ь_ъ_щ').split('_'); 
var arab_char=('ا_Ә_ب_Ц_Ч_د_ة_ف_ض_غ_ع_І_ي_ج_ك_ق_ل_م_ن_ث_و_Ө_ص_ر_س_ش_ت_ذ_Ү_أ_ؤ_خ_ي_ز_Э_Ю_Я_а_ә_б_ц_ч_ч_д_е_ф_گ_ғ_ғ_һ_ы_ئ_и_ж_ک_л_м_н_ң_нг_о_ө_پ_қ_р_с_ш_т_ұ_ү_в_у_ح_й_з_э_ю_я_Ч_Ғ_Ң_Ш_Ю_Я_،_#_##_|').split('_');

//0 - english, 1 - cyrillic (standart)
var language = 1;
//============================================================================================

//---------------------------------------------------------------------------------------------

function ConvertAll(option)
{
	range=document.selection.createRange();
	if(range==null || range.text==null || range.parentElement==null || range.parentElement().type!="textarea")
	{	
		document.converter.input_box.value=Convert(document.converter.input_box.value,option);	
		document.converter.input_box.focus();
		document.converter.input_box.select();
	}
	else
	{	range.text=Convert(range.text,option); range.collapse();range.select();}		
	return 0;
}

//---------------------------------------------------------------------------------------------

function ConvertInRealTime()
{
	//the entered character
        var code = event.keyCode;
	txt=String.fromCharCode(code); 
	//make selection
	cursor_pos_selection = document.selection.createRange();
	//delete something selected before
	cursor_pos_selection.text="";
	//get the previous character
        cursor_pos_selection.moveStart("character",-1);
	pretxt = cursor_pos_selection.text;
	if (pretxt.length>1) {pretxt="";}
	event.keyCode = 0;
	
	if (pretxt!="I") result=Convert(pretxt+txt,language);
	else result=pretxt+txt;
	//delete the previous symbol if it is exists (if the entered symbol is not the first one)
	if (pretxt!="") { cursor_pos_selection.select(); cursor_pos_selection.collapse();}
	with(document.selection.createRange()) {text = result; collapse(); select()}
	return;

}


//---------------------------------------------------------------------------------------------

function Convert(text, option)
{
//	0 - realtime Lat=>Cyr;
//	1 - realtime Lat=>QazLat;
//	2 - Lat=>Cyr;		
//	3 - Cyr=>QazLat;
//	4 - QazLat=>Cyr;
//	5 - Arab=>QazLat
//	6 - Lat=>QazLat;	
	var output_array,input_array;
	switch (option)
	{
		case 0: input_array=lat_char2;output_array=cyr_char;break;
		case 1: input_array=lat_char3;output_array=qazlat_char; break;
		case 2: input_array=lat_char;output_array=cyr_char; break;
		case 3: input_array=cyr_char;output_array=qazlat_char; break;
		case 4: input_array=qazlat_char;output_array=cyr_char; break;
		case 5: input_array=arab_char;output_array=qazlat_char; break;
		case 6: input_array=lat_char;output_array=qazlat_char; break;
		default: text="wrong option"; return text;
	}
	var conv_text=new String();
	var i,j,newsymb,newtext="";
	for(j=0;j<=text.length;j++)
	{
		if(option==1 || option==2 || option==0)
		{	
			newsymb=text.substr(j,2);
			for(i=0;i<output_array.length;i++)
				if(newsymb==input_array[i]) {j++; break;}
			if(i==output_array.length)//if it isnt a double symbol
				newsymb=newsymb.substr(0,1);
		}
		else	newsymb=text.substr(j,1); 
		//skipping html tags and bbcode
		if(newsymb=="["&&option!=0&&option!=1)
		{
			while ((conv_text[j]=text.substr(j,1))!="]"&&j<=text.length) {newtext=newtext+conv_text[j];j++;}
			newtext=newtext+"]";
			continue;
		}
		if(newsymb=="<"&&option!=0&&option!=1)
		{
			while ((conv_text[j]=text.substr(j,1))!=">"&&j<=text.length) {newtext=newtext+conv_text[j];j++;}
			newtext=newtext+">";
			continue;
		}

		//symbol change
		for(i=0; i<output_array.length; i++)
			if(newsymb==input_array[i]) {conv_text[j]=output_array[i];break;}
		if(i==output_array.length) conv_text[j]=newsymb;//if it isnt an alphabetic symbol, leave it as it is
		newtext=newtext+conv_text[j];
	}
	return newtext;
}

//---------------------------------------------------------------------------------------------

function AkeyIsDown()
{
	if (event.ctrlKey && event.altKey) {changelanguage();event.returnValue=false;}
	if(event.keyCode==27) {changelanguage(); event.returnValue=false;} 
return false;
}
	
//---------------------------------------------------------------------------------------------

function changelanguage()
{
	if (language==1) 
	{
		language=0; 
		document.converter.lang.value="Esc Cyr";  
		document.converter.input_box.focus();
		return;
	}
	if (language==0)
	{	language=1;
		document.converter.lang.value="Esc QazLat"; 
		document.converter.input_box.focus();
		return;
	}
}

//---------------------------------------------------------------------------------------------

function GetSelText()
{
	range = document.selection.createRange();
	if (range.text) document.converter.text_to_proccess.value=range.text;
	else document.converter.text_to_proccess.value=document.converter.input_box.value
	return;
}

//---------------------------------------------------------------------------------------------

function openHelp(option)
{
//	0 - framer;
//	1 - lang changing button;
	switch (option)
	{
		case 0: window.open("http://www.qaztranslit.com/help/help.html#1",null,"height=250,width=400,status=no,toolbar=no,menubar=no,resizeable=no"); return;
		case 1: window.open("http://www.qaztranslit.com/help/help.html#2",null,"height=250,width=400,status=no,toolbar=no,menubar=no,resizeable=no"); return;

		default: return;
	}
}

//---------------------------------------------------------------------------------------------

function goToTop() 
{  
	document.location.href = "http://qaztranslit.com/help/help.html#top";
	return;
}

//-------------------------------------------------------------------------
/*
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
 
//--------------------------------------------------------------------------

function hideDiv(idName,linkName1,linkName2)
{
	if (document.getElementById){ // DOM3 = IE5, NS6 
	document.getElementById(idName).style.display = 'none'; 
	document.getElementById(linkName1).style.display = 'none';
	document.getElementById(linkName2).style.display = 'block';
	} 
	else
	{ 
		if (document.layers){ // Netscape 4 
		document.hideshow.visibility = 'hidden'; 
		} 
		else { // IE 4 
		document.all.hideshow.style.visibility = 'hidden'; 
		} 
	} 
} 

function showDiv(idName,linkName1,linkName2)
{ 
	if (document.getElementById) { // DOM3 = IE5, NS6 
	document.getElementById(idName).style.display= 'block'; 
	document.getElementById(linkName1).style.display = 'none';
	document.getElementById(linkName2).style.display = 'block';
	} 
	else
	{ 
		if (document.layers) { // Netscape 4 
		document.hideshow.visibility = 'visible'; 
		} 
		else { // IE 4 
		document.all.hideshow.style.visibility = 'visible'; 
		} 
	} 
} 

//------------------------------------------------------------------------------------
/*
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

//------------------------------------------------------------------------------




