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)
{
    var keyCode = 0;
    
    if (e && e.which)
        keyCode = e.which;
    else if(e && e.keyCode)
        keyCode = e.keyCode;
    
    if(keyCode == 13) {
		addMatchWord();
    }
}

function addMatchWord() {
	var matchword = document.getElementById('txtMatchWord').value.toLowerCase().replace(/ /g, '');
  
    // force input lower case, no space
    document.getElementById('txtMatchWord').value = matchword;
  
    if(matchword == "") return;

	PageMethods.AddMatchWord(userid, matchword, languageid, callback_addMatchWord);
}

function callback_addMatchWord(res)
{
    var matchword = document.getElementById('txtMatchWord').value;
  
    var retVallue = res.split(",");     //res format: returnValue + "," + tagID
  
    var oUL;
    var oNewNode;
    if ((retVallue[0] == "1")||(retVallue[0] == "3"))   // 1=whitelist,3=personal tag, add to selflist
    {
		oUL = document.getElementById('ulSelfWords');
		
		oNewNode ='<li id=\"' + retVallue[1] + '\">';    
		oNewNode += "<img src=\"" + imgpath + "profile/process/3478_xIcon.gif\" alt=\"Remove this matchWord\" class=\"icon\" id='id" + retVallue[1] + "'" 
			+ " onClick=\"removeMatchWord('" + retVallue[1] + "');\" onMouseOver=\"hoverMatchWord('" + retVallue[1] + "');\" onMouseOut=\"blurMatchWord('" + retVallue[1] 
			+ "');\"/>&nbsp;<a href=\"/search/search.aspx?EXEC=go&TP=MWD&TAG=" + matchword + "\">" + matchword + "</a>"
			+ '</li>';
		oUL.innerHTML = oNewNode + oUL.innerHTML;
    }
    else if (retVallue[0] == "2" ) // 2=bad tag
    {    
        if (document.getElementById('divBadWords').style.display == 'none')
        {
	        document.getElementById('divBadWords').style.display = 'inline';
        }

        oUL = document.getElementById('ulBadWords');

        oNewNode = document.createElement("LI");
        oNewNode.id = retVallue[1];

        oNewNode.innerHTML = "<span>" + matchword + "</span>";

        oUL.appendChild(oNewNode); 
    }
    else if (retVallue[0] == "0")    // 0=unreviewed, add to pending list
    {
		if (document.getElementById('divPendingLable').style.display == 'none')
		{
			document.getElementById('divPendingLable').style.display = 'inline';
		}
		
		oUL = document.getElementById('ulPendingWords');

		oNewNode = '<li id=\"' + retVallue[1] + '\">' ;
	  	
		oNewNode += "<img src=\"" + imgpath + "profile/process/3478_xIcon.gif\" alt=\"Remove this matchWord\" class=\"icon\" id='id" + retVallue[1] + "'" 
			+ " onClick=\"removeMatchWord('" + retVallue[1] + "');\" onMouseOver=\"hoverMatchWord('" + retVallue[1] + "');\" onMouseOut=\"blurMatchWord('" + retVallue[1] 
			+ "');\"/>&nbsp;<span>" + matchword + "</span>"
			+ '</li>';
		oUL.innerHTML = oNewNode + oUL.innerHTML;
    }
    else if (retVallue[0] == "5")    // 5=over 50 words limitation
    {
        alert("We allow 50 MatchWords to be added to your profile.\r\n\r\n" +
	    	 "You can delete an already added word to make space for additional MatchWords to be added.");
    }
  
    document.getElementById('txtMatchWord').value = "";
    document.getElementById('txtMatchWord').focus();
}


function showMatchWordsHelp(show) {
	document.getElementById('matchWordsHelp').style.display = (show) ? '' : 'none';
}
