
// »ç¿ëÀÚ°¡ ÀÔ·ÂÇÑ ÁúÀÇ¾î¸¦ Dasen21 ÁúÀÇ¾î·Î º¯È¯ (Æ¯Á¤ÇÊµå °Ë»ö)
function ConvDasenQuery(query, field)
{
	var str;
	var str1, str2, str3;

	// str = StrDelStr( query, "  ");

	str = query;

	str1 = ReplaceOpSymbol1( str );
	str2 = ReplaceOpSymbol2( str1 , field );
	str3 = '(' + str2 + ')';
	return str3;

}

// »ç¿ëÀÚ°¡ ÀÔ·ÂÇÑ ÁúÀÇ¾î¸¦ Dasen21 Ç×¸ñº° ÁúÀÇ (ÀüÃ¼ÇÊµå °Ë»ö)
function ConvDasenQuery2(query)
{
	var str;
	var str1, str2, str3;

	str = trim(query);

	str1 = ReplaceOpSymbol1( str );
	str2 = ReplaceOpSymbol3( str1 );
	//str3 = '(' + str2 + ')';
	return str2;
}

function ReplaceOpSymbol1(value )
{
	var i, str = value;
	var ret_str = "";
	SymbolTable = ['+ ',' +',' + ','& ',' &',' & ','| ',' |',' | ','! ',' !',' ! ','* ',' *',' * ',' '];
	OpNameTable = ['+','+','+','&','&','&','|','|','|','!','!','!','*','*','*',' '];

	for( i = 0; i < str.length ; i++ ) {

		if( str.charAt(i) != ' ' ) {
			ret_str += str.charAt(i);
		}
		else {
			if( str.charAt(i-1) != ' ' && i - 1 >= 0) {
				ret_str += str.charAt(i);
			}
		}

	}


	for(i=0; i<SymbolTable.length; i++){
		ret_str =  Replace1(ret_str, SymbolTable[i], OpNameTable[i] );
	}

	return ret_str;
}

// Á¶°Ç½Ä¿¡ ¿¬»ê ¹®ÀÚ¸¦ Dasen21 ¿£Áø ÁúÀÇ¾î ¿¬»êÀ¸·Î ¼öÁ¤ (Æ¯Á¤ÇÊµå °Ë»ö)
function ReplaceOpSymbol2(value, field )
{
	var i, str = value;
	SymbolTable = [' ', '&', '+', '|', '!', '*'];
	OpNameTable = [' AND ', ' AND ', ' OR ', ' OR ', ' NOT ', '*'];

	for(i=0; i<SymbolTable.length; i++){
		str = Replace2(str, SymbolTable[i], OpNameTable[i], field );
	}

	str = field + ':' + str;

	return str;
}

// Á¶°Ç½Ä¿¡ ¿¬»ê ¹®ÀÚ¸¦ Dasen21¿£Áø ÁúÀÇ¾î ¿¬»êÀ¸·Î ¼öÁ¤ (ÀüÃ¼ÇÊµå °Ë»ö)
function ReplaceOpSymbol3(value)
{
	var i, str = value;
	SymbolTable = [' ', '&', '+', '|', '!', '*'];
	OpNameTable = [' AND ', ' AND ', ' OR ', ' OR ', ' NOT ', '*'];

	for(i=0; i<SymbolTable.length; i++){
		str = Replace1( str, SymbolTable[i], OpNameTable[i] );
	}

	return str;
}

function Replace1(string, src, dst )
{
	var str = string, words = "";

	if(string.indexOf(src) != -1){
		words = str.split(src);

		str = words[0];

		for(i=1; i<words.length; i++){
			str += dst + words[i];
		}
	}
	return str;
}

// string¿¡ src ¹®ÀÚ¸¦ dst·Ñ º¯È¯ (Æ¯Á¤ÇÊµå °Ë»ö)
function Replace2(string, src, dst, field )
{
	var str = string, words = "";

	if(string.indexOf(src) != -1 && src != '*'){
		words = str.split(src);

		str = words[0];

		for(i=1; i<words.length; i++){
			str += dst + field + ':' + words[i];
		}
	}
	return str;
}

// Lstr¿¡¼­ SstrÀ» Á¦°Å ÇÑ String À» ¹ÝÈ¯
function StrDelStr(Lstr, Sstr)
{
	var str = "";
	var index = Lstr.indexOf(Sstr);

	if(index == -1)
		str = Lstr;
	else{
		str = Lstr.substring(0, index) + Lstr.substring(index+Sstr.length, Lstr.length);
		str = StrDelStr(str, Sstr);
	}
	return str;
}

function CheckQuery(qry){
    var tmp_qry = qry;

    if(hasSpecial(tmp_qry)){
        var firstChar = getNextChar(tmp_qry);
        /*if((firstChar == '+')||(firstChar == '*')){*/
        if((firstChar == '+')){
            alert('\'+\'³ª \'*\' ¾Õ¿¡´Â ´Ù¸¥ Á¶°ÇÀÌ ÀÖ¾î¾ß ÇÕ´Ï´Ù.\n\nQuery must not start with an operator such as \'+\' or \'*\'');
            return false;
        }
/*
        else if(firstChar =='*'){
            alert('¾ÕÀý´ÜÀÚ´Â »ç¿ëÇÒ ¼ö ¾ø½À´Ï´Ù.\n\nLeft truncation is not available.');
            return false;
        }
*/

        var lastChar = getBeforeChar(tmp_qry);
        //if((lastChar == '+')||(lastChar == '*')){
        if((lastChar == '+')){
            alert('\'+\'³ª \'*\'µÚ¿¡´Â ´Ù¸¥ Á¶°ÇÀÌ ÀÖ¾î¾ß ÇÕ´Ï´Ù.\n\nQuery must not end with an operator such as \'+\' or \'*\'');
            return false;
        }
        return true;
    }
    else return false;
}

function hasSpecial(qry){
    var specialchar = '@?#$%^\\{}[]\'";';
    for(var i=0 ; i < qry.length;i++){
	NextChar = '';
//	NextChar = getNextChar(qry.substring(i+1));
	NextChar = qry.charAt(i+1);
	currentChar = qry.charAt(i);

        if (currentChar=='+'){
        	if (NextChar=='+' || NextChar == '&' || NextChar=='|' ){
                	alert('\''+currentChar+'+\',\''+currentChar+'*\',\''+currentChar+'*\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \''+currentChar+'+\',\''+currentChar+'*\',\''+currentChar+'*\'');
                	return false;
		}
        }
        else if (qry.charAt(i)=='*'){
            if((NextChar=='*')){
                alert('\'**\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \'**\'');
                return false;
            }

            var tmpBefore = getBeforeChar(qry.substr(0,i));
            if((NextChar=='+')&& (isOperator(tmpBefore) || (tmpBefore == ' '))){
                alert('\'*+\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \'*+\'');
                return false;
            }
            else if(isOperator(tmpBefore)){
                    alert('¾ÕÀý´ÜÀÚ´Â »ç¿ëÇÒ ¼ö ¾ø½À´Ï´Ù.\n\nLeft truncation is not available.');
                    return false;
            }
        }

        if ((currentChar=='&')){
        	if (NextChar=='+' || NextChar == '&' || NextChar=='|' ){
                	alert('\''+currentChar+'+\',\''+currentChar+'*\',\''+currentChar+'*\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \''+currentChar+'+\',\''+currentChar+'*\',\''+currentChar+'*\'');
                	return false;
		}
        }
        else if (qry.charAt(i)=='*'){
            if((NextChar=='*')){
                alert('\'**\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \'**\'');
                return false;
            }

            var tmpBefore = getBeforeChar(qry.substr(0,i));
            if((NextChar=='&')&& (isOperator(tmpBefore) || (tmpBefore == ' '))){
                alert('\'*&\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \'*&\'');
                return false;
            }
            else if(isOperator(tmpBefore)){
                    alert('¾ÕÀý´ÜÀÚ´Â »ç¿ëÇÒ ¼ö ¾ø½À´Ï´Ù.\n\nLeft truncation is not available.');
                    return false;
            }
        }

        if ((currentChar=='|')){
        	if (NextChar=='+' || NextChar == '&' || NextChar=='|' ){
                	alert('\''+currentChar+'+\',\''+currentChar+'*\',\''+currentChar+'*\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \''+currentChar+'+\',\''+currentChar+'*\',\''+currentChar+'*\'');
                	return false;
		}
        }
        else if (qry.charAt(i)=='*'){
            if((NextChar=='*')){
                alert('\'**\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \'**\'');
                return false;
            }

            var tmpBefore = getBeforeChar(qry.substr(0,i));
            if((NextChar=='&')&& (isOperator(tmpBefore) || (tmpBefore == ' '))){
                alert('\'*&\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \'*&\'');
                return false;
            }
            else if(isOperator(tmpBefore)){
                    alert('¾ÕÀý´ÜÀÚ´Â »ç¿ëÇÒ ¼ö ¾ø½À´Ï´Ù.\n\nLeft truncation is not available.');
                    return false;
            }
        }

        if (currentChar=='!') {
		// NextChar = qry.charAt(i+1);
        	if (NextChar=='+' || NextChar == '&' || NextChar=='|' || NextChar == '!' ){
                	alert('\''+currentChar+'+\',\''+currentChar+'*\',\''+currentChar+'*\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \''+currentChar+'+\',\''+currentChar+'*\',\''+currentChar+'*\'');
                	return false;
		}
        }
        else if (qry.charAt(i)=='*'){
            if((NextChar=='*')){
                alert('\'**\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \'**\'');
                return false;
            }

            var tmpBefore = getBeforeChar(qry.substr(0,i));
            if((NextChar=='&')&& (isOperator(tmpBefore) || (tmpBefore == ' '))){
                alert('\'*&\'¿Í °°Àº ¿¬»êÀÚÀÇ Áßº¹Àº Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.\n\nUsing the operator in sequence is not allowed such as \'*&\'');
                return false;
            }
            else if(isOperator(tmpBefore)){
                    alert('¾ÕÀý´ÜÀÚ´Â »ç¿ëÇÒ ¼ö ¾ø½À´Ï´Ù.\n\nLeft truncation is not available.');
                    return false;
            }
        }


        for(var j=0;j<specialchar.length;j++){
            if(qry.charAt(i)==specialchar.charAt(j)){
                alert('Áö¿øÇÏÁö ¾Ê´Â Æ¯¼ö¹®ÀÚ¸¦ »ç¿ëÇÏ¼Ì½À´Ï´Ù.\n\nInvalid character is used.');
                return false;
            }
        }

    }

     return true;
}

function trim(s){
	s = s.replace(/^\s*/,'').replace(/\s*$/, '');
	return s;
}

function getNextChar(str){
    for(var i=0;i<str.length;i++){
            if( (str.charAt(i) !=' ') && str.charAt(i) != '!') return str.charAt(i);
    }
    return ' ';
}

function getBeforeChar(str){
    var currentChar = '';
    for(var i=str.length-1 ;i>=0;i--){
            currentChar = str.charAt(i);
            if( (currentChar !=' ') && (currentChar != '!') && (currentChar.charCodeAt(0)!=10) && (currentChar.charCodeAt(0)!=13) ) return str.charAt(i);
    }
    return ' ';
}

function isOperator(op){
    checkOperation = '+*@';
    for(var i=0 ; i<checkOperation.length;i++)
        if(op == checkOperation.charAt(i)) return true;
    return false;
}

/* µµ¿ò¸» ÆäÀÌÁö È£Ãâ */
function GotoHelp()
{
	var targeturl="/HomepageKO/DataSearch/Help/help.html"
	newwin=window.open("","","scrollbars")

	if(document.all){
		newwin.moveTo(10,10)
		newwin.resizeTo(600, 600)
	}
	newwin.location=targeturl
}

/* °Ë»öÁß ¸Þ¼¼Áö Ã³¸® */
function _OpenStatus() {
        form = document.topsearch_form;
        x = 123;
        y = 34;
        form.waitBox.style.left = x;
        form.waitBox.style.top = y;
        form.waitBox.style.display = "";
}

function _OpenStatus2() {
        form = document.jangsi;
        x = 50;
        y = 105;
        form.waitBox.style.left = x;
        form.waitBox.style.top = y;
        form.waitBox.style.display = "";
}

function _CloseStatus() {
    if(top.statswin) {
            top.statswin.close();
    }
}

