var _matchword = "";
function addSimilar(word)
{
	if(word!=null)
	{
		_matchword = word.toLowerCase().replace(/ /g, '');
		addMatchWord();
	}
}


function hoverMatchWord(icon) {
    var rowElem = document.getElementById(icon);

	rowElem.className = rowElem.className + " hover";
}

function blurMatchWord(icon) {
    var rowElem = document.getElementById(icon);
  
	rowElem.className = rowElem.className.replace("hover", "");
}

function removeMatchWord(icon) {
    var rowElem = document.getElementById(icon);
  
	rowElem.innerHTML = '';
	rowElem.id = '';
	rowElem.style.display = "none";

	PageMethods.RemoveMatchWord(userid, icon,remove_callBack);
}

function remove_callBack(res) {
	//do nothing.. just need a callback sink
}

function onKeyPress(e) {
// only working fine in IE
//    if(event.keyCode == 13) {
//        addMatchWord();
//		document.forms[0].onsubmit = cancelSubmit;
//    }
    var keyCode = 0;
    
    if (e && e.which)
        keyCode = e.which;
    else if(e && e.keyCode)
        keyCode = e.keyCode;
    
    if(keyCode == 13) {
        addMatchWord();
		document.forms[0].onsubmit = cancelSubmit;
		return false;
    }
    return true;
}

function cancelSubmit() {
    return false; // cancel event
}

function addMatchWord() {
    if (_matchword == "") {
        //check value of textMatchWord
        _matchword = document.getElementById('txtMatchWord').value.toLowerCase().replace(/ /g, '');;
    }

    if(_matchword == "") return;

	PageMethods.AddMatchWord(userid, _matchword, languageid, callback_addMatchWord);
}

function callback_addMatchWord(res) {
    var retVallue = res.split(",");     //res format: returnValue + "," + tagID
  
    var oTable;
    var oNewNode;
    if ((retVallue[0] == "1")||(retVallue[0] == "3"))   // 1=whitelist,3=personal tag, add to selflist
    {
        oTable = document.getElementById('ulSelfWords');

        oTable.innerHTML += "<div  style=\"margin:5px 0 5px 0;\" id='" + retVallue[1] +"'>" 
            + "<img src=\"" + imgPathWords + "profile/process/3478_xIcon.gif\" class=\"icon\" id='" + retVallue[1] + "'" 
            + " onClick=\"removeMatchWord('" + retVallue[1] + "');\" onMouseOver=\"hoverMatchWord('" + retVallue[1] + "');\" onMouseOut=\"blurMatchWord('" + retVallue[1] 
            + "');\"/>&nbsp;<a href=\"/search/search.aspx?EXEC=go&TAG=" + _matchword + "\">" + _matchword + "</a></div>";
    }
    else if (retVallue[0] == "2" ) // 2=bad tag
    {    
        alert("The word " + _matchword + " is not a valid MatchWord.");
    }
    else if (retVallue[0] == "0")    // 0=unreviewed, add to pending list
    {

        if (document.getElementById('awaitWords').style.display == 'none')
        {
            document.getElementById('awaitWords').style.display = 'inline';
        }

        oPending = document.getElementById('awaitWords');

        oPending.innerHTML += "<div style=\"margin:5px 0 5px 0; border:1px solid;\" id='" + retVallue[1] + "'>" 
            + "<img src=\"" + imgPathWords + "profile/process/3478_xIcon.gif\" class=\"icon\" id='" + retVallue[1] + "'" 
            + " onClick=\"removeMatchWord('" + retVallue[1] + "');\" onMouseOver=\"hoverMatchWord('" + retVallue[1] + "');\" onMouseOut=\"blurMatchWord('" + retVallue[1] 
            + "');\"/>&nbsp;<a href=\"/search/search.aspx?EXEC=go&TAG=" + _matchword + "\">" + _matchword + "</a></div>";
    }
    else if (retVallue[0] == "5")    // 5=over 50 words limitation
    {
        alert("You have reached your maximum MatchWords limit of 50 words.  Please delete a word from your list before adding another.");
    }

    _matchword="";
    document.getElementById('txtMatchWord').value = "";
    document.getElementById('txtMatchWord').focus();
    document.forms[0].onsubmit = "";
}

function showMatchWordsHelp(show) {
	document.getElementById('matchWordsHelp').style.display = (show) ? '' : 'none';
}
