﻿
function isInLine(str){
	if(encodeURI(str).length<LINE_MAX_CHAR)	return true;
	return false;
}

var LINE_MAX_CHAR = 15;//15 characters in a line


function cutToText(str)
{
	var temp			= "";
	var startIndex		= 0;
	var endIndex		= LINE_MAX_CHAR;
	
	while(true){
		temp = str.substring(startIndex,endIndex);//line length
		arrOrgText.push(temp + "\r\n");

		if (!isInLine(temp)){
			startIndex		+= LINE_MAX_CHAR;
			endIndex += LINE_MAX_CHAR;
			if(endIndex > str.length) endIndex = str.length ;
		}else{
			break;
		}
    }
	return arrOrgText;
}

function cutShortParagraphs(str)
{
	var tempParagraphs = str.split("\r\n");
	for(var i=0 ;i<tempParagraphs.length;i++)
	{
		if(!isInLine(tempParagraphs[i])){
            cutToText(tempParagraphs[i]);    
		}
		else
		    arrOrgText.push(tempParagraphs[i]);
	}
	return arrOrgText;
}



var arrOrgText; //放置原文数组

var pyText = "";
var pyedText = "";
var tLn	=0;
var pyPrama;//array
var iLn		=0;
var bNewTranslatedText = true;
var bRemovePy = false;
var bLabeling = false;
function pinyin(bShow){
    setHint("正在标注拼音 ...");
    if(bLabeling){ return;}
    var text= $('SelText').value;
    pyText = text;//save local variable,while cancel use it

    if(text == "" || text == undefined || text=="\n"){
     setHint("没有译文。");return;
    }
    
    
	arrOrgText = new Array();
	pyPrama	= new Array();
    iLn = 0;    
	pyPrama = cutShortParagraphs(text);
    tLn = pyPrama.length;
    pyedSentence = "";
    pyedLn = ""; 

    
    bRemovePy = false;
    bLabeling = true;
    getPY(pyPrama[iLn]);
    
}

function removePY(){
    if(pyText == "" || pyText == undefined || pyText=="\n"){
     setHint("您还没有标注拼音。");return;
    }
    else{
        if($('contentout').value == "") setHint("没有译文。");
        else     $('contentout').value=pyText;
    }
    setHint('取消了注音。');
    bRemovePy = true;bPY = false;
    }

function getPY(sLn){
    var passData;
    
    var isInput = ($('inputpy').checked) ? "0":"1";//affect speed code line
    passData= "Txt="+encodeURIComponent(sLn)
        +"&iq="+encodeURIComponent(isInput);
    //$('input').value = passData;
        
    var AJAX=crtAJAX();
    if (AJAX==null) {
            setHint("您的浏览器原因，不能翻译。");
            return false;
    }
    else{
        AJAX.onreadystatechange = function() {
          if (AJAX.readyState==4 || AJAX.readyState=="complete") {
            successedPY(AJAX);
            AJAX=null;//free
          }                               
        }
        AJAX.open("POST", "../svrLib/pyHandler.ashx"   , true);
        AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        AJAX.send(passData);
    }
}


var pyedSentence = "";
var pyedLn = ""; 
function continuePY()
{
    if(pyedLn == "" || pyedLn == undefined || pyedLn=="\n"){
        pyedSentence += "\r\n";}
    else{
        pyedSentence += pyedLn;
        }
    //$("proHint").innerHTML = "... " + iLn.toString() + ": " + tLn.toString();
	if(iLn < tLn){
	    getPY(pyPrama[iLn]);
	}
	else 
	{
	    bNewTranslatedText = false;
        $("contentout").value = pyedSentence;
        setHint("注音完成。") ;
        bPY = true;
        bLabeling = false;//finished flag
	}
}

function successedPY(aResquest){
    pyedLn = aResquest.responseText  + pyPrama[iLn] ;//included \r\n
    iLn++;
    continuePY();
}

function isInput(){

}


var bPY = false;
function newwin(){
    if(bPY){
        $("pyForm").pyTxt.value = pyText;
    }
    else{
        $("pyForm").pyTxt.value = $('SelText').value;}
    $("pyForm").iInput.value = $("inputpy").checked ? "0" : "1";
            
}
