/*
	This file conatins the support functions for the searchresultappearance
	template.
*/
/**
 * Store the topic list in JS array, and use it for
 * re-populating the subtopic dropdown, based on topic selection
 */
var topLevelTopics = new Array();
var topics	= new Array();
var topic	= new Object();

var topicIndex = 0;
var operatorMap = new Array();
var operators = new Array();
var externalizedOperators = new Array();
var attrib = new Object();
var attribList = new Array();
var crTable;
var keywords = '';

var crList = new Array();
var cr = new Object();
var len = 0;
var numRows = 1;
var userInputList = new Array();

/*if(window.Event)
{
	window.captureEvents(Event.KEYDOWN);
	window.onkeydown=NSonKeyDown;
}*/

/*function checkEnter()
{
	if (window.event.keyCode==13)
	{
        alert(L10N_PRESS_GO);
        return false;
	}
}
*/

function onSearchHelp(country, language)
{
	var url="help/"+language+"/"+country+"/web/search_tips.htm";
	window.open(url,"Help");
}

function openURLinNewWindow(url)
{
	window.open(url);
}

function checkEnterForAdvancedSearch()
{
	if (window.event.keyCode==13)
	{
		if(numRows<2)
        		alert(L10N_ADD_CRITERIA_PROMPT);
        	else
        		alert(L10N_PRESS_GO1 + L10N_SEARCH_BUTTON + L10N_PRESS_GO2);
        return false;
	}
}


function populateTopLevelTopics()
{
	//alert('populateTopLeveTopics');
	topLevelTopics[topLevelTopics.length]	= topics;
	topics = new Array();
}
function populateTopicList(id, parentId, type, name, startingId, parentType)
{
	//alert('populateTopicList');
	topic		= new Object();
	topic.id 	= id;
	topic.parentId 	= parentId;
	//set for browse Topic
	topic.tType = type;
    if(startingId == 0) 
		topic.type = type;
	else
		topic.type = 0;
	topic.name = name;
	topic.startingId = startingId;
	topic.parentType = parentType;
	topics[topics.length]	= topic;
}
/**
 * Call back for topic selection in TOPIC dropdown
 * Based on the selected topic, populate sub-topic dropdown again
 * Start with selected topic, and populate all its children
 */
function onTopicChange(TOPIC, SUBTOPIC, formName)
{
        try
    	{
    	var selectedTopicIndex 	= document.all[formName].TOPIC.selectedIndex;
    	topicIndex = selectedTopicIndex;
	var subTopics;
	var currentTopic;
	if(topicIndex != 0)
	{
		subTopics = topLevelTopics[topicIndex-1];
		currentTopic = new Object();
		currentTopic = subTopics[0];
		document.all[formName].SUBTOPIC.options.length = subTopics.length;
		document.all[formName].SUBTOPIC.options[0].value = currentTopic.id;
		document.all[formName].SUBTOPIC.options[0].text = "All subtopics";
		for(i=1;i<subTopics.length;i++)
		{
			document.all[formName].SUBTOPIC.options[i].value = subTopics[i].id;
			document.all[formName].SUBTOPIC.options[i].text = subTopics[i].name;
		}
	}
	else
	{
		document.all[formName].SUBTOPIC.options.length = 1;
		document.all[formName].SUBTOPIC.options[0].value = -1;
		document.all[formName].SUBTOPIC.options[0].text = "All subtopics";
	}
	}
	catch(exception)
	{
		// Do nothing. This is added because if the method is called and the dropdowns do not exist
		// runtime errors will show up
	}

}

function setTopicAndSubtopic(TOPIC, SUBTOPIC, topicIndex, subtopicIndex, formName)
{

	subTopics = topLevelTopics[topicIndex];
	currentTopic = new Object();
	currentTopic = subTopics[0];
	try
	{
		document.all[formName].SUBTOPIC.options.length = subTopics.length;
		document.all[formName].SUBTOPIC.options[0].value = currentTopic.id;
		document.all[formName].SUBTOPIC.options[0].text = "All subtopics";
		for(i=1;i<subTopics.length;i++)
		{
			document.all[formName].SUBTOPIC.options[i].value = subTopics[i].id;
			document.all[formName].SUBTOPIC.options[i].text = subTopics[i].name;
		}
		document.all[formName].TOPIC.selectedIndex = topicIndex+1;
		document.all[formName].SUBTOPIC.selectedIndex = subtopicIndex;
	}
	catch(exception)
	{
		//Done to handle the case when the topic dropdown is disabled in the configuration
	}
}
function setTopicAndSubtopicUsingId(TOPIC, SUBTOPIC, topicId, subtopicId, formName)
{
	try
	{
	var topicIndex = -1; 
	var subtopicIndex = 0;
	var tmpLength = document.all[formName].TOPIC.options.length;
	for(i=0;i<tmpLength;i++)
	{
		tmpValue = document.all[formName].TOPIC.options[i].value;
		if(tmpValue == topicId)
		{	
			topicIndex = i - 1;
			break;
		}
	}
	
	subTopics = topLevelTopics[topicIndex];
	
	tmpLength = subTopics.length;
	for(i=0;i<tmpLength;i++)
	{
		tmpValue = subTopics[i].id;
		if(tmpValue == subtopicId)
		{
			subtopicIndex = i;
			break;
		}
	}
	
	//subTopics = topLevelTopics[topicIndex];
	currentTopic = new Object();
	currentTopic = subTopics[0];

		document.all[formName].SUBTOPIC.options.length = subTopics.length;
		document.all[formName].SUBTOPIC.options[0].value = currentTopic.id;
		document.all[formName].SUBTOPIC.options[0].text = "All subtopics";
		for(i=1;i<subTopics.length;i++)
		{
			document.all[formName].SUBTOPIC.options[i].value = subTopics[i].id;
			document.all[formName].SUBTOPIC.options[i].text = subTopics[i].name;
		}
		document.all[formName].TOPIC.selectedIndex = topicIndex+1;
		document.all[formName].SUBTOPIC.selectedIndex = subtopicIndex;
	}
	catch(exception)
	{
		//Done to handle the case when the topic dropdown is disabled in the configuration
	}
}

//this function submits the basic search form
function basicSearchSubmit(formAction)
{	
	if(verify())
	{
		//document.searchForm.action=formAction;
		document.searchForm.submit();
	}
}
//this function submits the advanced search form
function advancedSearchSubmit(formAction)
{
	if(go())
	{
		//document.searchForm.action=formAction;
		document.searchForm.submit();
	}
}
//this function verifies the user input in the basic search form.
var expandedStr = '';

function containsSpecialChars(str)
{
	if(str.indexOf('$')>-1 || str.indexOf('#')>-1 || str.indexOf('^')>-1 || str.indexOf('_')>-1)
	{
		return true;
	}
	return false;
}

function replaceSpecialChars(str)
{

	var retVal = str.replace(/[$#^_]/g,"");
	return retVal;
	

}

function verify()
{
	keywords = '';
	try
	{
		var selectedTopicIndex = document.searchForm.TOPIC.selectedIndex;
		var selectedSubTopicIndex = document.searchForm.SUBTOPIC.selectedIndex;
		document.searchForm.SIDE_LINK_TOPIC_ID.value = document.searchForm.TOPIC.options[selectedTopicIndex].value;
		document.searchForm.SIDE_LINK_SUB_TOPIC_ID.value = document.searchForm.SUBTOPIC.options[selectedSubTopicIndex].value;
	}
	catch(exception)
	{
		//Done to handle the case when the topic dropdown is disabled in the configuration
	}	
	
    var searchStr = document.searchForm.searchString.value;
	if(document.searchForm.TOPIC)
	{
		var selectedTopicIndex = document.searchForm.TOPIC.selectedIndex;
		var selectedSubTopicIndex = document.searchForm.SUBTOPIC.selectedIndex;
		if(selectedTopicIndex > 0)
		{
			var subTopics = topLevelTopics[selectedTopicIndex - 1];
			document.searchForm.subTopicType.value = subTopics[selectedSubTopicIndex].type;
			document.searchForm.TOPIC_NAME.value = subTopics[0].name;
			document.searchForm.SUBTOPIC_NAME.value = document.searchForm.SUBTOPIC.options[selectedSubTopicIndex].text
		}
		else
			document.searchForm.subTopicType.value = 0;
	}
	//var subTopicIndex = topicIndex + selectedSubTopicIndex;
	searchStr = replaceSpecialChars(searchStr);
	document.searchForm.searchString.value = searchStr;
	//if(containsSpecialChars(searchStr))
	//{
	//	alert("Special characters [$, #, ^, _] are not allowed.");
	//	return false;
	//}
	if(expandAndValidate(searchStr))
	{
		document.searchForm.BOOL_SEARCHSTRING.value = expandedStr;
		document.searchForm.KEYWORDS.value = keywords.replace(/ /g, "$");
		return true;
	}
	else
	{
		alert(L10N_INVALID_SEARCH_STRING);
		return false;
	}
    
    return true;
}

function addCriteria(searchButton, removeCr,formAction)
{
	if(attribList.length > 0)
	{
	var selectedAttributeIndex = document.searchForm.attributes.selectedIndex;
    var selectedOperatorIndex = document.searchForm.operators.selectedIndex;
    var selectedAndOrIndex = document.searchForm.andOr.selectedIndex;
		
    var selectedAttributeValue = document.searchForm.attributes.options[selectedAttributeIndex].value;
    var selectedAttributeText = document.searchForm.attributes.options[selectedAttributeIndex].text;
    var selectedOperatorValue = document.searchForm.operators.options[selectedOperatorIndex].value;
	var selectedOperatorText = document.searchForm.operators.options[selectedOperatorIndex].text;
	// Fix for CR# 14398 -- the value for operator < does not appear
	if(selectedOperatorText == '<')
		selectedOperatorText = '&lt;';
    var andOr = document.searchForm.andOr.options[selectedAndOrIndex].value;
    var val = document.searchForm.val.value;
    document.searchForm.val.value = "";
    
    if(val.length < 1)
    {
        alert(L10N_BLANK_SEARCH_STRING);
        return;
    }
	
	//if the attribute selected is of date type, then validate the input string
	if(selectedAttributeIndex > 0)
	{
//		var selectedAttrib = attribList[selectedAttributeIndex-1];
		var selectedAttrib = attribList[selectedAttributeIndex];
		if(selectedAttrib.operatorType == "date")
		{
			if(!dateValidate(val, true))
			{
				document.searchForm.val.value = val;			
				return false;
			}
			val = val.replace(/-/g,"/");
		}
		if(selectedAttrib.operatorType == "int")
		{
			if(isNaN(val))
			{
				return false;
			}
		}
	}
	//val = replaceSpecialChars(val);
	/*
	if(containsSpecialChars(val))
	{
		alert("Special characters [$, #, ^, _] are not allowed.");
		return false;
	}
	*/
    cr = new Object();
    cr.attribute = selectedAttributeValue;
    cr.operator = selectedOperatorValue;
    cr.andOr = andOr;
    cr.val = val;
	if(document.getElementById)
	{
		crTable = document.getElementById('criterionTable');
	}
	else if(document.all)
	{
		crTable = document.all["criterionTable"];
    }
	if(numRows>1)
        crTable.deleteRow(numRows-1);
    var newRow = crTable.insertRow(crTable.rows.length);
    numRows++;
    newRow.id = len;
    cell1 = newRow.insertCell(0);
    //cell1.outerHTML="class=\"SearchAdvancedRow2\"";
	//BEGIN style for cell 1
	cell1.style.paddingLeft="2px";
	cell1.style.paddingRight="2px";
	cell1.style.backgroundColor="#FFFFFF";
	cell1.style.borderLeftColor="#808080";
	cell1.style.borderLeftStyle="solid";
	cell1.style.borderLeftWidth="1px";
	cell1.style.borderBottomColor="#808080";
	cell1.style.borderBottomStyle="solid";
	cell1.style.borderBottomWidth="1px";
	cell1.style.fontSize="8pt";
	cell1.style.fontFamily="tahoma";
	cell1.style.width="25%";
	//END style for cell 1
	cell1.innerHTML = '';
	//cell1.innerHTML = "<input type='text' id ='criteria#"+len+'1'+"' value='"+selectedAttributeText+"' onkeydown='javascript:disableEdit(event)' onkeyup='javascript:reprint(event)' onmousedown='javascript:storeValue(event)' onfocus='javascript:storeValue(event)' style='width:100%'>";
	cell1.id = 'criteria#'+len+'1';
	cell1.value = selectedAttributeText;
	cell1.innerHTML = selectedAttributeText;
	cell2 = newRow.insertCell(1);
    //cell2.outerHTML="class=\"SearchAdvancedRow2\"";
	//BEGIN style for cell 2
	cell2.style.paddingLeft="2px";
	cell2.style.paddingRight="2px";
	cell2.style.backgroundColor="#FFFFFF";
	cell2.style.borderLeftColor="#808080";
	cell2.style.borderLeftStyle="solid";
	cell2.style.borderLeftWidth="1px";
	cell2.style.borderBottomColor="#808080";
	cell2.style.borderBottomStyle="solid";
	cell2.style.borderBottomWidth="1px";
	cell2.style.fontSize="8pt";
	cell2.style.fontFamily="tahoma";
	cell2.style.width="25%";
	//END style for cell 2
	cell2.innerHTML = '';
	//cell2.innerHTML = "<input type='text' id ='criteria#"+len+'2'+"' value='"+selectedOperatorText+"' onkeydown='javascript:disableEdit(event)' onkeyup='javascript:reprint(event)' onmousedown='javascript:storeValue(event)' onfocus='javascript:storeValue(event)'style='width:100%'>";
	cell2.id = 'criteria#'+len+'2';
	cell2.value = selectedOperatorText;
	cell2.innerHTML = selectedOperatorText;
	cell3 = newRow.insertCell(2);
	//cell3.outerHTML="class=\"SearchAdvancedRow2\"";
	//BEGIN style for cell 3
	cell3.style.paddingLeft="2px";
	cell3.style.paddingRight="2px";
	cell3.style.backgroundColor="#FFFFFF";
	cell3.style.borderLeftColor="#808080";
	cell3.style.borderLeftStyle="solid";
	cell3.style.borderLeftWidth="1px";
	cell3.style.borderBottomColor="#808080";
	cell3.style.borderBottomStyle="solid";
	cell3.style.borderBottomWidth="1px";
	cell3.style.fontSize="8pt";
	cell3.style.fontFamily="tahoma";
	cell3.style.width="25%";
	//END style for cell 3
	cell3.innerHTML = '';
	//cell3.innerHTML = "<input type='text' id ='criteria#"+len+'3'+"' value='"+val+"' onkeydown='javascript:disableEdit(event)' onkeyup='javascript:reprint(event)' onmousedown='javascript:storeValue(event)' onfocus='javascript:storeValue(event)' style='width:100%'>";
	cell3.id = 'criteria#'+len+'3';
	cell3.value = val;
	cell3.innerHTML = val;
	cell4 = newRow.insertCell(3);
	//cell4.outerHTML="class=\"SearchAdvancedRow2\"";
	//BEGIN style for cell 4
	cell4.style.paddingLeft="2px";
	cell4.style.paddingRight="2px";
	cell4.style.backgroundColor="#FFFFFF";
	cell4.style.borderLeftColor="#808080";
	cell4.style.borderLeftStyle="solid";
	cell4.style.borderLeftWidth="1px";
	cell4.style.borderBottomColor="#808080";
	cell4.style.borderBottomStyle="solid";
	cell4.style.borderBottomWidth="1px";
	cell4.style.fontSize="8pt";
	cell4.style.fontFamily="tahoma";
	cell4.style.width="9%";
	//END style for cell 4
	cell4.innerHTML = '';
	//cell4.innerHTML = "<input type='text' id ='criteria#"+len+'4'+"' value='"+ cr.andOr+"' onkeydown='javascript:disableEdit(event)' onkeyup='javascript:reprint(event)' onmousedown='javascript:storeValue(event)' onfocus='javascript:storeValue(event)' style='width:100%'>";
	cell4.id = 'criteria#'+len+'4';
	cell4.value = cr.andOr;
	cell4.innerHTML = cr.andOr;
	cell5 = newRow.insertCell(4);
	//cell5.outerHTML="class=\"SearchAdvancedR\"";
	//BEGIN style for cell 5
	cell5.style.paddingLeft="2px";
	cell5.style.paddingRight="2px";
	cell5.style.backgroundColor="#E9E9E6";
	cell5.style.borderLeftColor="#808080";
	cell5.style.borderLeftStyle="solid";
	cell5.style.borderLeftWidth="1px";
	cell5.style.borderRightColor="#808080";
	cell5.style.borderRightStyle="solid";
	cell5.style.borderRightWidth="1px";
	cell5.style.borderBottomColor="#808080";
	cell5.style.borderBottomStyle="solid";
	cell5.style.borderBottomWidth="1px";
	cell5.style.width="16%";
	//END style for cell 5
	cell5.innerHTML = '';
	cell5.innerHTML = "<button type='button' value='Remove' onClick='javascript:removeCriteria("+len+")' style='width:100%'>"+removeCr+"</button>";

	var newCriteriaInput = new Object();
	newCriteriaInput.selectedAttributeIndex = selectedAttributeIndex;
	newCriteriaInput.selectedOperatorIndex = selectedOperatorIndex;
	newCriteriaInput.selectedAndOrIndex = selectedAndOrIndex;
	newCriteriaInput.val = val;
	userInputList[len] = newCriteriaInput;

	crList[len++] = cr;


    var lastRow=crTable.insertRow(crTable.rows.length);
    if(numRows == 2)
        numRows++;
    var cell;
	for(i=0;i<3;i++)
	{
        cell = lastRow.insertCell(i);
		cell.style.width = "25%";
		cell.style.color="#FFFFFF";
		cell.style.paddingLeft="2px";
		cell.style.paddingRight="2px";
		cell.style.backgroundColor="#E9E9E6";
		cell.style.borderLeftColor="#808080";
		cell.style.borderLeftStyle="solid";
		cell.style.borderLeftWidth="1px";
		cell.style.borderBottomColor="#808080";
		cell.style.borderBottomStyle="solid";
		cell.style.borderBottomWidth="1px";
    }
	cell = lastRow.insertCell(3);
	cell.style.width = "9%";
	cell.style.color="#FFFFFF";
	cell.style.paddingLeft="2px";
	cell.style.paddingRight="2px";
	cell.style.backgroundColor="#E9E9E6";
	cell.style.borderLeftColor="#808080";
	cell.style.borderLeftStyle="solid";
	cell.style.borderLeftWidth="1px";
	cell.style.borderBottomColor="#808080";
	cell.style.borderBottomStyle="solid";
	cell.style.borderBottomWidth="1px";

	var newCell = lastRow.insertCell(4);
	newCell.style.paddingLeft="2px";
	newCell.style.paddingRight="2px";
	newCell.style.backgroundColor="#E9E9E6";
	newCell.style.borderLeftColor="#808080";
	newCell.style.borderLeftStyle="solid";
	newCell.style.borderLeftWidth="1px";
	newCell.style.borderRightColor="#808080";
	newCell.style.borderRightStyle="solid";
	newCell.style.borderRightWidth="1px";
	newCell.style.borderBottomColor="#808080";
	newCell.style.borderBottomStyle="solid";
	newCell.style.borderBottomWidth="1px";
	newCell.style.width="16%";
	newCell.innerHTML = "<button type='button' value='Go!' onClick=\"javascript:advancedSearchSubmit('" + formAction + "')\" style = 'width:100%'>"+searchButton+"!</button>";
	}
	else
	{
		alert(L10N_NO_ADV_SEARCH_ATTRIBS);
	}
}
function removeCriteria(ID)
{
    var toDelete=0;
    if(document.getElementById)
	{
		crTable = document.getElementById('criterionTable');
	}
	else if(document.all)
	{
		crTable = document.all["criterionTable"];
	}
	for(i=0;i<ID;i++)
    {
        if(crList[i] != "deleted")
            toDelete++;
    }
    crTable.deleteRow(toDelete+1);
    crList[ID] = "deleted";
    userInputList[ID] = "deleted";
    numRows = numRows - 1;
    if(numRows == 2)
    {
        crTable.deleteRow(1);
        numRows = numRows-1;
    }
}

function submitEscalationFormUsingDropDown()
{
	
	var selectedTopicIndex = document.escalation.TOPIC.selectedIndex;
	var selectedSubTopicIndex = document.escalation.SUBTOPIC.selectedIndex;
	//alert("selected topic index-- > " + selectedTopicIndex);
	//alert("selected Sub Topic index-- > " + selectedSubTopicIndex);
	//alert("document.sidelinks.CMD.value-- > " + document.sidelinks.CMD.value);
	if(selectedTopicIndex ==0 && selectedSubTopicIndex == 0)
	{
		document.escalation.TOPIC_ID.value = -1;
		document.escalation.TOPIC_TYPE.value = 0;
		document.escalation.STARTING_ID.value = 0;
		document.escalation.submit();
	}
	else if(selectedSubTopicIndex == 0 && selectedTopicIndex>0 )
	{
		var topics = topLevelTopics[selectedTopicIndex - 1];
		var topicVal = topics[0];
		document.escalation.TOPIC_ID.value = topicVal.id;
		document.escalation.TOPIC_TYPE.value = topicVal.type;
		document.escalation.STARTING_ID.value = topicVal.startingId;
		document.escalation.submit();

	}
	else if(selectedSubTopicIndex > 0)
	{
		var subTopics = topLevelTopics[selectedTopicIndex - 1];
		document.escalation.TOPIC_ID.value = subTopics[selectedSubTopicIndex].id;
		document.escalation.TOPIC_TYPE.value = subTopics[selectedSubTopicIndex].type;
		document.escalation.STARTING_ID.value = subTopics[selectedSubTopicIndex].startingId;
		document.escalation.submit();
	}
}

function submitFormUsingDropDown(cmdVal)
{
	
	var selectedTopicIndex = document.sidelinks.TOPIC.selectedIndex;
	var selectedSubTopicIndex = document.sidelinks.SUBTOPIC.selectedIndex;
	document.sidelinks.SIDE_LINK_TOPIC_INDEX.value = selectedTopicIndex;
	document.sidelinks.SIDE_LINK_SUB_TOPIC_INDEX.value = selectedSubTopicIndex;
	document.sidelinks.SIDE_LINK_TOPIC_ID.value = document.sidelinks.TOPIC.value;
	document.sidelinks.SIDE_LINK_SUB_TOPIC_ID.value = document.sidelinks.SUBTOPIC.value;	
	//alert("selected topic index-- > " + selectedTopicIndex);
	//alert("selected Sub Topic index-- > " + selectedSubTopicIndex);

	 
	document.sidelinks.CMD.value = cmdVal;
	if(cmdVal == "BROWSE_TOPIC")
		document.sidelinks.SOURCE_FORM.value = "SIDE_LINKS";

	//alert("document.sidelinks.CMD.value-- > " + document.sidelinks.CMD.value);
	if(selectedTopicIndex ==0 && selectedSubTopicIndex == 0)
	{
		document.sidelinks.TOPIC_ID.value = -1;
		document.sidelinks.TOPIC_TYPE.value = 0;
		document.sidelinks.STARTING_ID.value = 0;
		document.sidelinks.TOPIC_NAME.value = '';
		document.sidelinks.PARENT_TOPIC_ID.value = -1;
		document.sidelinks.PARENT_TOPIC_TYPE.value = 0;
		
		document.sidelinks.submit();
	}
	else if(selectedSubTopicIndex == 0 && selectedTopicIndex>0 )
	{
		var topics = topLevelTopics[selectedTopicIndex - 1];
		var topicVal = topics[0];
		//alert("topicVal.id->" + topicVal.id + "topicVal.tType->" + topicVal.tType);
		//alert("topicVal.startingId->" + topicVal.startingId + "topicVal.name-> " + topicVal.tType);
		document.sidelinks.TOPIC_ID.value = topicVal.id;
		document.sidelinks.TOPIC_TYPE.value = topicVal.tType;
		document.sidelinks.STARTING_ID.value = topicVal.startingId;
		document.sidelinks.TOPIC_NAME.value = topicVal.name;
		document.sidelinks.PARENT_TOPIC_ID.value = topicVal.parentId;
		document.sidelinks.PARENT_TOPIC_TYPE.value = topicVal.parentType;
		document.sidelinks.SUB_TOPIC_ID.value = -1;
		document.sidelinks.submit();

	}
	else if(selectedSubTopicIndex > 0 && cmdVal == "BROWSE_TOPIC")
	{
		var subTopics = topLevelTopics[selectedTopicIndex - 1];
		var topicVal = subTopics[0];
		document.sidelinks.TOPIC_ID.value = subTopics[selectedSubTopicIndex].id;
		document.sidelinks.TOPIC_TYPE.value = subTopics[selectedSubTopicIndex].tType;
		document.sidelinks.STARTING_ID.value = subTopics[selectedSubTopicIndex].startingId;
		document.sidelinks.TOPIC_NAME.value = subTopics[selectedSubTopicIndex].name;
		document.sidelinks.PARENT_TOPIC_NAME.value = topicVal.name;
		document.sidelinks.PARENT_TOPIC_ID.value = subTopics[selectedSubTopicIndex].parentId;
		document.sidelinks.PARENT_TOPIC_TYPE.value = subTopics[selectedSubTopicIndex].parentType;
		document.sidelinks.submit();
	}
	else if(selectedSubTopicIndex > 0 && cmdVal == "DFAQ")
	{
		var subTopics = topLevelTopics[selectedTopicIndex - 1];
		var topicVal = subTopics[0];
		document.sidelinks.TOPIC_ID.value = topicVal.id;
		document.sidelinks.TOPIC_TYPE.value = topicVal.tType;
		document.sidelinks.STARTING_ID.value = topicVal.startingId;
		document.sidelinks.TOPIC_NAME.value = topicVal.name;
		document.sidelinks.PARENT_TOPIC_ID.value = topicVal.parentId;
		document.sidelinks.PARENT_TOPIC_TYPE.value = topicVal.parentType;
//Setting Sub Topic Values
		document.sidelinks.SUB_TOPIC_ID.value = subTopics[selectedSubTopicIndex].id;
		document.sidelinks.SUB_TOPIC_TYPE.value = subTopics[selectedSubTopicIndex].tType;
		document.sidelinks.SUB_TOPIC_STARTING_ID.value = subTopics[selectedSubTopicIndex].startingId;
		document.sidelinks.submit();
	}

}




function submitFormUsingDropDown_morelink(cmdVal)
{
	
	

	//var selectedTopicIndex = document.sidelinks.TOPIC.selectedIndex;
	//var selectedSubTopicIndex = document.sidelinks.SUBTOPIC.selectedIndex;
	var selectedTopicIndex = 0;
	var selectedSubTopicIndex = 0;
	document.sidelinks.SIDE_LINK_TOPIC_INDEX.value = selectedTopicIndex;
	document.sidelinks.SIDE_LINK_SUB_TOPIC_INDEX.value = selectedSubTopicIndex;
	//document.sidelinks.SIDE_LINK_TOPIC_ID.value = document.sidelinks.TOPIC.value;
	//document.sidelinks.SIDE_LINK_SUB_TOPIC_ID.value = document.sidelinks.SUBTOPIC.value;	
	document.sidelinks.SIDE_LINK_TOPIC_ID.value = 0;
	document.sidelinks.SIDE_LINK_SUB_TOPIC_ID.value = 0;	
	//alert("selected topic index-- > " + selectedTopicIndex);
	//alert("selected Sub Topic index-- > " + selectedSubTopicIndex);

	 
	document.sidelinks.CMD.value = cmdVal;
	if(cmdVal == "BROWSE_TOPIC")
		document.sidelinks.SOURCE_FORM.value = "SIDE_LINKS";

	//alert("document.sidelinks.CMD.value-- > " + document.sidelinks.CMD.value);
	if(selectedTopicIndex ==0 && selectedSubTopicIndex == 0)
	{
		document.sidelinks.TOPIC_ID.value = -1;
		document.sidelinks.TOPIC_TYPE.value = 0;
		document.sidelinks.STARTING_ID.value = 0;
		document.sidelinks.TOPIC_NAME.value = '';
		document.sidelinks.PARENT_TOPIC_ID.value = -1;
		document.sidelinks.PARENT_TOPIC_TYPE.value = 0;
		
		document.sidelinks.submit();
	}
	else if(selectedSubTopicIndex == 0 && selectedTopicIndex>0 )
	{
		var topics = topLevelTopics[selectedTopicIndex - 1];
		var topicVal = topics[0];
		//alert("topicVal.id->" + topicVal.id + "topicVal.tType->" + topicVal.tType);
		//alert("topicVal.startingId->" + topicVal.startingId + "topicVal.name-> " + topicVal.tType);
		document.sidelinks.TOPIC_ID.value = topicVal.id;
		document.sidelinks.TOPIC_TYPE.value = topicVal.tType;
		document.sidelinks.STARTING_ID.value = topicVal.startingId;
		document.sidelinks.TOPIC_NAME.value = topicVal.name;
		document.sidelinks.PARENT_TOPIC_ID.value = topicVal.parentId;
		document.sidelinks.PARENT_TOPIC_TYPE.value = topicVal.parentType;
		document.sidelinks.SUB_TOPIC_ID.value = -1;
		document.sidelinks.submit();

	}
	else if(selectedSubTopicIndex > 0 && cmdVal == "BROWSE_TOPIC")
	{
		var subTopics = topLevelTopics[selectedTopicIndex - 1];
		var topicVal = subTopics[0];
		document.sidelinks.TOPIC_ID.value = subTopics[selectedSubTopicIndex].id;
		document.sidelinks.TOPIC_TYPE.value = subTopics[selectedSubTopicIndex].tType;
		document.sidelinks.STARTING_ID.value = subTopics[selectedSubTopicIndex].startingId;
		document.sidelinks.TOPIC_NAME.value = subTopics[selectedSubTopicIndex].name;
		document.sidelinks.PARENT_TOPIC_NAME.value = topicVal.name;
		document.sidelinks.PARENT_TOPIC_ID.value = subTopics[selectedSubTopicIndex].parentId;
		document.sidelinks.PARENT_TOPIC_TYPE.value = subTopics[selectedSubTopicIndex].parentType;
		document.sidelinks.submit();
	}
	else if(selectedSubTopicIndex > 0 && cmdVal == "DFAQ")
	{
		var subTopics = topLevelTopics[selectedTopicIndex - 1];
		var topicVal = subTopics[0];
		document.sidelinks.TOPIC_ID.value = topicVal.id;
		document.sidelinks.TOPIC_TYPE.value = topicVal.tType;
		document.sidelinks.STARTING_ID.value = topicVal.startingId;
		document.sidelinks.TOPIC_NAME.value = topicVal.name;
		document.sidelinks.PARENT_TOPIC_ID.value = topicVal.parentId;
		document.sidelinks.PARENT_TOPIC_TYPE.value = topicVal.parentType;
//Setting Sub Topic Values
		document.sidelinks.SUB_TOPIC_ID.value = subTopics[selectedSubTopicIndex].id;
		document.sidelinks.SUB_TOPIC_TYPE.value = subTopics[selectedSubTopicIndex].tType;
		document.sidelinks.SUB_TOPIC_STARTING_ID.value = subTopics[selectedSubTopicIndex].startingId;
		document.sidelinks.submit();
	}
	

}

function go()
{
	try{
	
	var selectedTopicIndex = document.searchForm.TOPIC.selectedIndex;
	var selectedSubTopicIndex = document.searchForm.SUBTOPIC.selectedIndex;
	document.searchForm.SIDE_LINK_TOPIC_ID.value = document.searchForm.TOPIC.options[selectedTopicIndex].value;
	document.searchForm.SIDE_LINK_SUB_TOPIC_ID.value = document.searchForm.SUBTOPIC.options[selectedSubTopicIndex].value;
	}
	catch(exception)
	{
		//Done to handle the case when the topic dropdown is disabled in the configuration
	}	
    var criterion = "";
    var userInputStr = "";
	var keywords = "";
    for(i=0;i<len;i++)
    {
        if(crList[i] != "deleted")
        {
			var attributeName = crList[i].attribute;
			if(attributeName.indexOf("all_attributes")>0)
			{
				var attr;
				var ops;
				// We start the loop from 1 for avoiding the all attributes object we have deliberately
				// put in the first place of attribs array
				for(k=1;k<attribList.length;k++)
				{
					var cr = '';
					var isValidDate = true;
					var isValidNum = true;

					attr = attribList[k];
					var opType = attr.operatorType;
					cr = cr + attr.objectName + "#" + attr.attribName + "#" + opType + "#";
					crList[i].val = replaceSpecialChars(crList[i].val);
					if(opType == "date" || opType == "int")
					{	
						ops = operatorMap[opType];
						cr = cr + ops[0];
						//validate the input search string for valid date format if opType is date.
						if(opType == "date")
						{
							isValidDate = dateValidate(crList[i].val, false);
						}
						if(opType == "int")
						{
							if(isNaN(crList[i].val))
								isValidNum = false;
						}
					}
					else
					{
						cr = cr + crList[i].operator;
					}
					cr = cr + "#" + crList[i].val + "#";
					if(k == (attribList.length-1))
						cr = cr + crList[i].andOr.toLowerCase() + "$";		
					else
						cr = cr + "or" + "$";

					if(isValidDate && isValidNum)
						criterion += cr;
				}
			}
			else
			{
				criterion = criterion + crList[i].attribute + "#" + 
					crList[i].operator + "#" + replaceSpecialChars(crList[i].val) + "#" + crList[i].andOr.toLowerCase() + "$";
			}
			keywords += replaceSpecialChars(crList[i].val) + "$";
        }
    
    	if(userInputList[i] != "deleted")
    	{
    		userInputStr = userInputStr + userInputList[i].selectedAttributeIndex + ':' +userInputList[i].selectedOperatorIndex 
    			+ ':' +userInputList[i].selectedAndOrIndex + ':' +replaceSpecialChars(userInputList[i].val) + '#';
    	}
    	
      }
    
	if(document.searchForm.TOPIC)
	{
		var selectedTopicIndex = document.searchForm.TOPIC.selectedIndex;
		var selectedSubTopicIndex = document.searchForm.SUBTOPIC.selectedIndex;
		if(selectedTopicIndex > 0)
		{
			var subTopics = topLevelTopics[selectedTopicIndex - 1];
			document.searchForm.subTopicType.value = subTopics[selectedSubTopicIndex].type;
			document.searchForm.TOPIC_NAME.value = subTopics[0].name;
			document.searchForm.SUBTOPIC_NAME.value = document.searchForm.SUBTOPIC.options[selectedSubTopicIndex].text;
		}
		else
			document.searchForm.subTopicType.value = 0;
    }
	document.searchForm.criterion.value = criterion;
	//alert(criterion);
	document.searchForm.KEYWORDS.value = keywords.replace(/ /g, "$");
	document.searchForm.userInput.value = userInputStr;
	return true;
}
function populateOperators(operator, externalizedOperator)
{
    operators[operators.length] = operator;
	externalizedOperators[operator] = externalizedOperator;
}
function populateOperatorMap(operatorType)
{
    operatorMap[operatorType] = operators;
    operators = new Array();
}
function populateAttribList(objectName, attribName, operatorType)
{
    attrib = new Object();
    attrib.objectName = objectName;
    attrib.attribName = attribName;
    attrib.operatorType = operatorType;
    attribList[attribList.length] = attrib;
}
function onAttributeChange()
{
    if(attribList.length > 0)
	{
		var selectedAttribIndex = document.searchForm.attributes.selectedIndex;
//		if(selectedAttribIndex > 0)
//			selectedAttribIndex--;
		var operatorType = attribList[selectedAttribIndex].operatorType;
	    var operators = operatorMap[operatorType];
		var len = operators.length;
	    document.searchForm.operators.options.length = len;
		for(i=0;i<len;i++)
	    {
		    document.searchForm.operators.options[i].value = operators[i];
		    document.searchForm.operators.options[i].text = externalizedOperators[operators[i]];
	    }
	}
}
function disableEdit(e)
{
	var code;
	if (!e) var e = window.event;
	//alert(eval(!e));
	if(e)
	{
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		if(code != 37 && code != 39)
		{
			event.returnValue=false;
		}
	}	
//	var character = String.fromCharCode(code);
//	alert('Character was ' + character);
}
var previousValue = '';
function NSonKeyDown(e)
{
	var ID = e.target.id;
	if(ID.indexOf("criteria#") > -1)
	{
		var code = e.which;
		if(code != 37 && code != 39)
		{
			document.addButton.handleEvent(e);
		}
	}
}
function storeValue(e)
{
	if(e.target)
	{
		previousValue = e.target.value;
	}
}
function reprint(e)
{
	if(e.target)
	{
		if(e.which != 37 && e.which != 39 && e.which != 9 )
			e.target.value = previousValue;
	}
}
//This function is for date validation
function dateValidate(str, toShowAlertsOrNot)
{
	if(str.length != 8 && str.length != 9 && str.length != 10)
	{
		if(toShowAlertsOrNot)
			alert(L10N_INVALID_DATE_FORMAT);
		return false;
	}
	
	if(str.length == 8)
	{	
		var delim1 = str.charAt(1);
		var delim2 = str.charAt(3);
		if(delim1 != delim2)
		{
			if(toShowAlertsOrNot)
				alert(L10N_INVALID_DATE_FORMAT);
			return false;
		}
		if(delim1 != '/' && delim1 != '-')
	{
		if(toShowAlertsOrNot)
			alert(L10N_INVALID_DATE_FORMAT);
		return false;
	}
		var monthStr = str.substring(0,1);
		var month = parseInt(monthStr, 10);
		var dayStr = str.substring(2,3);
		var day = parseInt(dayStr, 10);
		var yearStr = str.substring(4,8);
		var year = parseInt(yearStr, 10);
	}
	
	if(str.length == 9)
	{	
		var delim1 = str.charAt(1);
		var delim2 = str.charAt(2);
		var delim3 = str.charAt(4);
		if((delim1 != delim3) && (delim2 != delim3))
		{
			if(toShowAlertsOrNot)
				alert(L10N_INVALID_DATE_FORMAT);
			return false;
		}
		if(delim3 != '/' && delim3 != '-')
		{
			if(toShowAlertsOrNot)
				alert(L10N_INVALID_DATE_FORMAT);
			return false;
		}
		
		// Date format m-dd-yyyy or m/dd/yyyy
		if(delim1 == delim3)
		{		
			var monthStr = str.substring(0,1);
			var month = parseInt(monthStr, 10);
			var dayStr = str.substring(2,4);
			var day = parseInt(dayStr, 10);
			var yearStr = str.substring(5,9);
			var year = parseInt(yearStr, 10);
		}
		// Date format mm-d-yyyy or mm/d/yyyy
		if(delim2 == delim3)
		{	
			var monthStr = str.substring(0,2);
			var month = parseInt(monthStr, 10);
			var dayStr = str.substring(3,4);
			var day = parseInt(dayStr, 10);
			var yearStr = str.substring(5,9);
			var year = parseInt(yearStr, 10);
		}
		
	}
	
	
	if(str.length == 10)
	{
	var delim1 = str.charAt(2);
	var delim2 = str.charAt(5);
	if(delim1 != delim2)
	{
		if(toShowAlertsOrNot)
			alert(L10N_INVALID_DATE_FORMAT);
		return false;
	}
	else
	{
		if(delim1 != '/' && delim1 != '-')
		{
			if(toShowAlertsOrNot)
				alert(L10N_INVALID_DATE_FORMAT);
			return false;
		}
	}
	var monthStr = str.substring(0,2);
	var month = parseInt(monthStr, 10);
	var dayStr = str.substring(3,5);
	var day = parseInt(dayStr, 10);
	var yearStr = str.substring(6,10);
	var year = parseInt(yearStr, 10);
	}

	if(isNaN(month) || isNaN(day) || isNaN(year))
	{
		if(toShowAlertsOrNot)
				alert(L10N_INVALID_DATE_FORMAT);
		return false;
	}
	if(month > 12 || month < 1) 
	{
		if(toShowAlertsOrNot)
			alert(L10N_MONTH_VALUE_OUT_OF_RANGE);
		return false;
	}
	if(year < 1754 || year > 9998)
	{
		if(toShowAlertsOrNot)
			alert(L10N_YEAR_VALUE_OUT_OF_RANGE);
		return false;
	}
	if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
	{
		if(day < 1 || day > 31)
		{
			if(toShowAlertsOrNot)
				alert(L10N_DAY_VALUE_OUT_OF_RANGE);
			return false;
		}
	}
	else if(month == 4 || month == 6 || month == 9 || month == 11)
	{
		if(day < 1 || day > 30)
		{
			if(toShowAlertsOrNot)
				alert(L10N_DAY_VALUE_OUT_OF_RANGE_THIRTY);
			return false;
		}
	}
	else if(month == 2)
	{
		if((year % 4) != 0 && (day > 28 || day < 1))
		{
			if(toShowAlertsOrNot)
				alert(L10N_DAY_VALUE_OUT_OF_RANGE_NON_LEAP_YR);
			return false;
		}
		else if(day > 29 || day < 1)
		{
			if(toShowAlertsOrNot)
				alert(L10N_DAY_VALUE_OUT_OF_RANGE_LEAP_YR);
			return false;
		}
	}
	return true;
}
//The code for expanding and validating basic search input string starts here

// Stack of tokens as found in search input,in addition, AND oprator at required positions
var _tokens = new Array();

function validateExp(exp)
{
	try
	{
		var retValue = eval(exp);
		return true;
	}
	catch(exception )
	{
		return false;
	}

}
/**
 * Utility method to check if a give string is a boolean operator used in
 * search strings - and/or/not
 * @param a string
 * @return boolean
 */
function isOperator(str)
{
	if(str.toLowerCase()=='and' || str.toLowerCase()=='not' || str.toLowerCase()=='or')
		return true;
	else
		return false;
}

/**
 * Utility method to check if a give char is parenthesis
 * @param a string
 * @return boolean
 */
function isParenthesis(str)
{
	if(str=='(' || str==')')
		return true;
	else
		return false;
}

function isPhrase(str)
{
	if(str.charAt(0) == '"')
		return true;
	else
		return false;
}

/**
 * Inserts a AND operator in the token stack at the appropriate location. Called when an
 * expression i.e. non operator token is found in search input string
 */
function insertAnd()
{
	var keepPopping = true;
	var count = 0;
	var poppedTokens = new Array();
	var sz = _tokens.length;
	if(sz==0)
		return;

	while(keepPopping && count<sz)
	{
		poppedTokens[count]	= _tokens[sz-count-1];

		if(poppedTokens[count]!='(')
			keepPopping = false;
		count++;

	}
	if(!keepPopping)
	{
		//check last popped token
		var lastPoppedToken = poppedTokens[count-1]
		if(lastPoppedToken==')' || (lastPoppedToken.toLowerCase()!='and' && lastPoppedToken.toLowerCase()!='or' && lastPoppedToken.toLowerCase()!='not'))
		{
			_tokens[sz-count+1] = 'and';
			for(j=1;j<count;j++)
				_tokens[sz-count+1+j] = poppedTokens[count-j-1]
		}
	}
}

/**
 * Pushes a token into Token stack
 */
function pushToken(currentToken)
{
	_tokens[_tokens.length] = currentToken;
}

/**
 * Populates the stack of token by parsing input string. AND operator is pushed if operator
 * missing between to expressions
 */
function populateTokenStack(exp)
{
	_tokens = new Array();
	var size = exp.length;
	var retExp = '';
	var inQuotedToken = false;
	var tokenEnded = false;
	var wasQuotedToken = false;
	var currentToken = '';
	var lastToken = '';
	for(i=0;i<size;i++)
	{
		ch = exp.charAt(i);

		if(ch=='"')
		{
			tokenEnded = true;
			if(inQuotedToken)
			{
				inQuotedToken = false;
				wasQuotedToken =  true;
			}
			else
			{
				inQuotedToken = true;
			}
		}
		else if(isParenthesis(ch) || ch==' ')
		{
			if(inQuotedToken)
			{
				currentToken=currentToken+ch;
			}
			else
			{
				tokenEnded = true;
			}
		}
		else
		{
			currentToken=currentToken+ch;
		}

		if(tokenEnded)
		{
			if(currentToken.length>0 && currentToken!= ' ' )
			{
				if(!isOperator(currentToken))
				{
					populateKeywords(currentToken);
					insertAnd();
				}
				if(wasQuotedToken)
				{
					currentToken = '"' + currentToken + '"';
					wasQuotedToken = false;
				}
				pushToken(currentToken);
			}
			if(ch=='(' || ch==')')
				pushToken(ch);

			currentToken = '';
			tokenEnded=false;
		}

		if(ch!='"')
			retExp= retExp+ ch;
	}

	lastToken = currentToken;
	if(currentToken.length>0 && currentToken!= ' ')
	{
		if(!isOperator(currentToken))
		{
			populateKeywords(currentToken);
			insertAnd();
		}

		pushToken(currentToken);
	}
	currentToken = '';
	return _tokens;
}

function expandAndValidate(exp)
{
	var tokens = populateTokenStack(exp);
	var booleanStr ='';
	expandedStr = '';
	var sz = tokens.length;
	for(i=0;i<sz;i++)
	{
		var token = tokens[i];
		if(isOperator(token))
		{
			booleanStr = booleanStr + ' && ';
			if(token.toLowerCase() == 'not')
			{
				expandedStr = expandedStr + ' ' + 'and ' + token.toLowerCase() + ' ';
			}
			else
			{
				expandedStr = expandedStr  + ' '+ token.toLowerCase() + ' ';
			}
		}
		else if(isParenthesis(token))
		{
			booleanStr = booleanStr + token;
			expandedStr = expandedStr  + ' '+ token + ' ';
		}
		else
		{
			booleanStr = booleanStr + ' true ';
			if(isPhrase(token))
			{
				expandedStr = expandedStr  + ' &wtquote;' + token + '&wtquote; ';
				//expandedStr = expandedStr  + ' ' + token + ' ';
			}
			else
			{
				// Add an encoded styring as suffix and prefix to tokens
				// that are none phrases.
				// These suffix, prefix will be converted to double quote for
				// ntext column and removed for other column types
				expandedStr = expandedStr  + ' &wtquote;' + token + '&wtquote; ';
			}
		}
	}
	//alert(booleanStr);
	//alert(expandedStr);
	if(validateExp(booleanStr))
	{
		return true;
	}
	else
	{
		return false;
	}

}

function searchSpellCorrectStr(searchString,formAction)
{
	document.searchForm.searchString.value = searchString;
	basicSearchSubmit(formAction);
}

function populateKeywords(keyword)
{
	if(keywords.length == 0)
		keywords += keyword;
	else
		keywords += "$" + keyword;
}
