var objhttp;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	objhttp = new XMLHttpRequest();
    if (objhttp.overrideMimeType) 
    	objhttp.overrideMimeType('text/xml');
} 
else if (window.ActiveXObject) 
{ // IE
	try {
    	objhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
	catch (e) 
	{
	    try {
    	    objhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } 
		catch (e) {}
    }
 }	

var quanResults = 9;//quantity of suggestions to bring
var tagSug = "";//tag suggestions
var divPlace = -1;//place in suggestion div
var quanSug = 0;//quantity of suggestions
var searchTagLen = 0;//length of the search tag
var searchSecondaryTagLen = 0;//length of the secondary search tag
var gotSecCode = false;
var gotSecondarySecCode=false;
var error = false;
var secondaryError=false;
var errorArray = new Array();
var byUser1=false;
var byUser2=false;
errorArray[0] = "נייר הערך שהוקלד אינו קיים";
errorArray[1] = "נייר הערך שהוקלד להשוואה אינו קיים";

function submitCompareGraphForm()
{
	if(error)
		resetErrors();
	if(mainCodesToTypesList.size()==0)
	{
		mainCodesToTypesList.add(tmpMainCodesToTypesList);
	}
	else
	{
		if(!gotSecCode && !gotSecondarySecCode)
		{
			if(byUser1 && byUser2)
			{
			}
			else if(!byUser1 && !byUser2)
			{
			}
			else if (!byUser1 && byUser2)
			{
				if(tmpMainCodesToTypesList.size() == 1)
				{
					if(tmpMainCodesToTypesList.keys[0] != mainCodesToTypesList.keys[0])
					{
						tmpMainCodesToTypesList.add(mainCodesToTypesList);
						mainCodesToTypesList=tmpMainCodesToTypesList;
					}
				}
				else if(tmpMainCodesToTypesList.size() == 2)
				{
					if(tmpMainCodesToTypesList.keys[0] != mainCodesToTypesList.keys[0])
					{
						tmpMainCodesToTypesList.remove(tmpMainCodesToTypesList.keys[1]);
						tmpMainCodesToTypesList.add(mainCodesToTypesList);
						mainCodesToTypesList=tmpMainCodesToTypesList;
					}
				}
			}
			else
			{
			}
		}
		else if(gotSecCode && gotSecondarySecCode)
		{
			var indexKey=mainCodesToTypesList.getKeyOfValue("indexdaily");
			var indexKey2=mainCodesToTypesList.getKeyOfValue("indexdaily2");
			if(indexKey != null)
				mainCodesToTypesList.remove(indexKey);
			if (indexKey2 != null)
				mainCodesToTypesList.remove(indexKey2);
		}
		else if(!gotSecCode && gotSecondarySecCode)
		{
			if(byUser1)
			{
				var indexKey2=mainCodesToTypesList.getKeyOfValue("indexdaily2");
				if (indexKey2 != null)
					mainCodesToTypesList.remove(indexKey2);
			}
			if(!byUser1)
			{
				var indexKey2=mainCodesToTypesList.getKeyOfValue("indexdaily2");
				if (indexKey2 != null)
					mainCodesToTypesList.remove(indexKey2);
				if(tmpMainCodesToTypesList.size() == 1)
				{
					if(tmpMainCodesToTypesList.keys[0] != mainCodesToTypesList.keys[0])
					{
						tmpMainCodesToTypesList.add(mainCodesToTypesList);
						mainCodesToTypesList=tmpMainCodesToTypesList;
					}
				}
				else if(tmpMainCodesToTypesList.size() == 2)
				{
					if(tmpMainCodesToTypesList.keys[0] != mainCodesToTypesList.keys[0])
					{
						tmpMainCodesToTypesList.remove(tmpMainCodesToTypesList.keys[1]);
						tmpMainCodesToTypesList.add(mainCodesToTypesList);
						mainCodesToTypesList=tmpMainCodesToTypesList;
					}
				}
			}
		}
		else
		{
			var dailyKey2=mainCodesToTypesList.getKeyOfValue("daily2");
			var indexKey=mainCodesToTypesList.getKeyOfValue("indexdaily");
			if(dailyKey2 != null)
				mainCodesToTypesList.remove(dailyKey2);
			if (indexKey != null)
				mainCodesToTypesList.remove(indexKey);
		}
	}
	var curSecCodes="";
	var curGraphTypes="";
	
	if(document.byNameForm != undefined)
	{
		for(var i=0;i < mainCodesToTypesList.size();i++)
		{
			curSecCodes = curSecCodes + "," + mainCodesToTypesList.keys[i];
			curGraphTypes = curGraphTypes + "," + mainCodesToTypesList.hash[mainCodesToTypesList.keys[i]];
		}
		document.byNameForm.secCode.value=curSecCodes.substring(1);
		document.byNameForm.graphTypes.value=curGraphTypes.substring(1);
	}
	byNameForm.submit();
}		
function submitGraphForm()
{
	if(error)
		resetErrors();
	
	if(mainCodesToTypesList.size()==0)
	{
		mainCodesToTypesList.add(tmpMainCodesToTypesList);
	}
	else
	{
		if(gotSecCode)
		{
			var indexKey=mainCodesToTypesList.getKeyOfValue("indexdaily");
			if(indexKey != null)
				mainCodesToTypesList.remove(indexKey);
		}
		else
		{
			var dailyKey=mainCodesToTypesList.getKeyOfValue("daily");
			if(dailyKey != null)
				mainCodesToTypesList.remove(dailyKey);
		}
		var indexKey2=mainCodesToTypesList.getKeyOfValue("indexdaily2");
		var dailyKey2=mainCodesToTypesList.getKeyOfValue("daily2");
		if(indexKey2 != null)
		mainCodesToTypesList.remove(indexKey2);
		if (dailyKey2 != null)
		mainCodesToTypesList.remove(dailyKey2);
	}

	var curSecCodes="";
	var curGraphTypes="";
	
	if(document.byNameForm != undefined)
	{
		for(var i=0;i < mainCodesToTypesList.size();i++)
		{
			curSecCodes = curSecCodes + "," + mainCodesToTypesList.keys[i];
			curGraphTypes = curGraphTypes + "," + mainCodesToTypesList.hash[mainCodesToTypesList.keys[i]];
		}
		document.byNameForm.secCode.value=curSecCodes.substring(1);
		document.byNameForm.graphTypes.value=curGraphTypes.substring(1);
	}
	byNameForm.submit();
}
//***************index functions
function loadIndexSubTypes(fieldId,indexTypeSelected,byUser)
{
	if(indexSecType != null || indexSecType != -1)
	{
		var subTypesSelectObj = document.getElementById(fieldId);
		subTypesSelectObj.options.length=0;
		subTypesSelectObj.options[0] = new Option("-- בחר מדד --",-1);
		if(indexTypeSelected != -1)
		{
			var subTypesPerType = indexTypeToSecCodes.get(indexTypeSelected);
			var subTypeName;
			for(var i=0;i<subTypesPerType.length;i++)
			{
				subTypeName = replaceAllChars(indexCodeToName.get(subTypesPerType[i]));
				subTypesSelectObj.options[i+1] = new Option(subTypeName,subTypesPerType[i]);
			}
		}
		if(byUser)
		{
			//checking if there is already index in the hash
			if(mainCodesToTypesList.keys.length != 0)
				{
					if(fieldId=="subIndexTypesSelect")
					{
						var indexExist = false;
						var indexKeys = mainCodesToTypesList.getKeyOfValue("indexdaily");
						if(indexKeys != null)
						{
							indexExist = true;
							mainCodesToTypesList.remove(indexKeys);
						}
					}
					else
					{
						byUser2=byUser;
						var indexExist2 = false;
						var indexKeys2 = mainCodesToTypesList.getKeyOfValue("indexdaily2");
					
						if(indexKeys2 != null)
						{
							indexExist2 = true;
							mainCodesToTypesList.remove(indexKeys2);
						}
					}
				}
			// end adding index
			if(fieldId=="subIndexTypesSelect")
				byUser1=byUser;
			else
				byUser2=byUser;
			indexSecType = -1;
		}
	}
}
function addIndex(indexSelected)
{
	indexSecType = indexSelected;
	//checking if there is already index in the hash
	var indexExist = false;
	if(mainCodesToTypesList.keys.length != 0)
	{
		var indexKeys = mainCodesToTypesList.getKeyOfValue("indexdaily");
		if(indexKeys != null)
		{
			indexExist = true;
			if(indexKeys != indexSecType)
			{
				mainCodesToTypesList.remove(indexKeys);
				if(indexSecType != -1)
					mainCodesToTypesList.put(indexSecType,"indexdaily");
			}
		}
	}
	//end checking
	if(!indexExist && indexSecType != -1)
		mainCodesToTypesList.put(indexSecType,"indexdaily");
	// end adding index
}
function addSecondaryIndex(indexSelected)
{
	indexSecType = indexSelected;
	//checking if there is already index in the hash
	var indexExist2 = false;
	if(mainCodesToTypesList.keys.length != 0)
	{
		var indexKeys2 = mainCodesToTypesList.getKeyOfValue("indexdaily2");
		if(indexKeys2 != null)
		{
			indexExist2 = true;
			if(indexKeys2 != indexSecType)
			{
				mainCodesToTypesList.remove(indexKeys2);
				if(indexSecType != -1)
					mainCodesToTypesList.put(indexSecType,"indexdaily2");
			}
		}
	}
	//end checking
	if(!indexExist2 && indexSecType != -1)
		mainCodesToTypesList.put(indexSecType,"indexdaily2");
	// end adding index
}
/*
checks in the main hash if there's an index and if so shows the index in the index select
*/	
function initIndexFromHash()
{
	if(mainCodesToTypesList != undefined)
	{
		var indexKey = mainCodesToTypesList.getKeyOfValue("indexdaily");
		if(indexKey != null)
		{
			var indexType = indexCodeToType.get(indexKey);
			indexSecType = indexKey;
			doComboSelected("indexTypesSelect",indexType);
			if(indexType != undefined)
				loadIndexSubTypes("subIndexTypesSelect",indexType,false);
			doComboSelected("subIndexTypesSelect",indexKey);
		}
		else
		{
			doComboSelected("indexTypesSelect",-1);
			var subTypesSelectObj = document.getElementById("subIndexTypesSelect");
			subTypesSelectObj.options.length=0;
			subTypesSelectObj.options[0] = new Option("-- בחר מדד --",-1);
			doComboSelected("subIndexTypesSelect",-1);
			indexSecType = -1;
		}
	}
}
function initSecondaryIndexFromHash()
{
	if(mainCodesToTypesList != undefined)
	{
		var indexKey2 = mainCodesToTypesList.getKeyOfValue("indexdaily2");
		if(indexKey2 != null)
		{
			var indexType = indexCodeToType.get(indexKey2);
			indexSecType = indexKey2;
			doComboSelected("indexTypesSelect2",indexType);
			if(indexType != undefined)
				loadIndexSubTypes("subIndexTypesSelect2",indexType,false);
			doComboSelected("subIndexTypesSelect2",indexKey2);
		}
		else
		{
			doComboSelected("indexTypesSelect2",-1);
			var subTypesSelectObj = document.getElementById("subIndexTypesSelect2");
			subTypesSelectObj.options.length=0;
			subTypesSelectObj.options[0] = new Option("-- בחר מדד --",-1);
			doComboSelected("subIndexTypesSelect2",-1);
			indexSecType = -1;
		}
	}
}
//***************** end index functions
function doComboSelected(fieldId,val)
{	
	var combo = document.getElementById(fieldId);
	var cLen=combo.length;
	//alert('doComboSelected cLen= '+cLen+'selected value= '+combo[combo.selectedIndex].value);
	
	for (var i=0 ; i < cLen; i++){
		if (combo.options[i].value == val){
			combo.options[i].selected = true;
			//alert('combo.selectedIndex = '+combo.selectedIndex);
			return ;
		}
	}
}
function replaceCharsBack(str)
{	
	while(str.indexOf("~")!=-1)
		str =str.replace("~","\"");
	while(str.indexOf("*")!=-1) 
		str =str.replace("*","\'");	
	while(str.indexOf("^")!=-1) 
		str =str.replace("^","&");
	return str;
}
function replaceSpace(str)
{
	while(str.indexOf(" ")!=-1)
		str =str.replace(" ","&nbsp;");
	return str;
}
function replaceAndChar(str)
{
	while(str.indexOf("&amp;")!=-1)
		str =str.replace("&amp;","&");
	return str;
}
/*treat key event for input tag field*/
function treatKey(evt, isFeature)
{	
	if (evt)//if there is event
	{			
		if(evt.keyCode != 13)
			getSuggestions();
		var sugDiv = document.getElementById("tagSuggestions");//tag suggestion div	
		//suggestion div not displayed or enter pressed while in the search tag field
		if (sugDiv != undefined && ((sugDiv.style.display == 'none') || ((divPlace == -1) && (evt.keyCode == 13))))
			checkSubmitSearch(evt.keyCode,true);			
		else//suggestion div is displayed
			moveFocusInSuggestionDiv(evt.keyCode);					
	}
}
/*treat key event for secondary input tag field*/
function treatSecondaryKey(evt, isFeature)
{	
	if (evt)//if there is event
	{			
		if(evt.keyCode != 13)
			getSecondarySuggestions();
		var sugDiv = document.getElementById("secondaryTagSuggestions");//tag suggestion div	
		//suggestion div not displayed or enter pressed while in the search tag field
		if (sugDiv != undefined && ((sugDiv.style.display == 'none') || ((divPlace == -1) && (evt.keyCode == 13))))
			checkSubmitSearch(evt.keyCode,true);			
		else//suggestion div is displayed
			moveFocusInSecondarySuggestionDiv(evt.keyCode);					
	}
}
/*submit the tag search*/
function checkSubmitSearch(keyCode, isFeature)
{
	if (keyCode == 13 && isFeature)//enter pressed				
		submitCompareGraphForm();
}

/*move the focus in the suggestions div*/
function moveFocusInSuggestionDiv(keyCode)
{
	var lastLine = divPlace;//keep the last line
	if ((keyCode == 38) && (divPlace > 0))//up
		divPlace = divPlace - 1;
	else if ((keyCode == 40) && ((divPlace+1) < quanSug))//down
		divPlace = divPlace + 1;
	else if ((keyCode == 13) && (divPlace > -1) && (divPlace < quanSug)){//enter
		var elmTagName = document.getElementById("sugName"+divPlace);				
		var elmTagCode = document.getElementById("sugCode"+divPlace);				
		if (elmTagName != undefined && elmTagCode != undefined)
			chooseTag(escape(replaceAndChar(elmTagName.innerHTML)),elmTagCode.innerHTML);			
		return;
	}
	else
		return;
	//if up or down
	var elm = document.getElementById("sugLine"+divPlace);//paint the new line
	if (elm)
		elm.style.backgroundColor='b7caeb';									
	
	var elmLast = document.getElementById("sugLine"+lastLine);//unpaint the last line				
	if (elmLast)
		elmLast.style.backgroundColor='white';						
}
/*move the focus in the secondary suggestions div*/
function moveFocusInSecondarySuggestionDiv(keyCode)
{
	var lastLine = divPlace;//keep the last line
	if ((keyCode == 38) && (divPlace > 0))//up
		divPlace = divPlace - 1;
	else if ((keyCode == 40) && ((divPlace+1) < quanSug))//down
		divPlace = divPlace + 1;
	else if ((keyCode == 13) && (divPlace > -1) && (divPlace < quanSug)){//enter
		var elmTagName = document.getElementById("secondarySugName"+divPlace);				
		var elmTagCode = document.getElementById("secondarySugCode"+divPlace);				
		if (elmTagName != undefined && elmTagCode != undefined)
			chooseSecondaryTag(escape(replaceAndChar(elmTagName.innerHTML)),elmTagCode.innerHTML);			
		return;
	}
	else
		return;
	//if up or down
	var elm = document.getElementById("secondarySugLine"+divPlace);//paint the new line
	if (elm)
		elm.style.backgroundColor='b7caeb';									
	
	var elmLast = document.getElementById("secondarySugLine"+lastLine);//unpaint the last line				
	if (elmLast)
		elmLast.style.backgroundColor='white';						
}
/*display and place tag suggestion div*/
function displayTagSuggestionsDiv()
{	
	var pL = 0;
	var pT = 0;	
	var obj = document.getElementById("secNameInput");
	var sugDiv = document.getElementById("tagSuggestions");		
	while(obj.tagName != "BODY"){
		pL+=obj.offsetLeft;
		pT+=obj.offsetTop;
		obj=obj.offsetParent;			
	}		
	sugDiv.style.top = pT+20; 				
	if(navigator.appName != "Microsoft Internet Explorer")
		sugDiv.style.left = pL-436;		
	else	
		sugDiv.style.left = pL;		
	sugDiv.innerHTML = tagSug;
	sugDiv.style.display = "block";				
}
/*display and place secondary tag suggestion div*/
function displaySecondaryTagSuggestionsDiv()
{	
	var pL = 0;
	var pT = 0;	
	var obj = document.getElementById("secondarySecNameInput");
	var sugDiv = document.getElementById("secondaryTagSuggestions");		
	while(obj.tagName != "BODY"){
		pL+=obj.offsetLeft;
		pT+=obj.offsetTop;
		obj=obj.offsetParent;			
	}		
	sugDiv.style.top = pT+20; 				
	if(navigator.appName != "Microsoft Internet Explorer")
		sugDiv.style.left = pL-436;		
	else	
		sugDiv.style.left = pL;		
	sugDiv.innerHTML = tagSug;
	sugDiv.style.display = "block";				
}
/*choose tag from the suggestion div, the tag is encoded*/
function chooseTag(tag,code)
{	
	var curSecCodes="";
	var curGraphTypes="";
	gotSecCode = true;
	document.getElementById("secNameInput").value = unescape(tag);//decode the tag
	var dailyKey = mainCodesToTypesList.getKeyOfValue("daily");
	if(dailyKey != null)
		mainCodesToTypesList.remove(dailyKey);
	else
		mainCodesToTypesList.remove("-1");
	mainCodesToTypesList.put(code,"daily");
	notDisplaySuggestionDiv("tagSuggestions");
	searchTagLen = document.getElementById("secNameInput").value.length;
}
/*choose tag from the suggestion div, the tag is encoded*/
function chooseSecondaryTag(tag,code)
{	
	var curSecCodes="";
	var curGraphTypes="";
	gotSecondarySecCode = true;
	document.getElementById("secondarySecNameInput").value = unescape(tag);//decode the tag
	var dailyKey = mainCodesToTypesList.getKeyOfValue("daily2");
	if(dailyKey != null)
		mainCodesToTypesList.remove(dailyKey);
	else
		mainCodesToTypesList.remove("-2");
	mainCodesToTypesList.put(code,"daily2");
	notDisplaySuggestionDiv("secondaryTagSuggestions");
	searchTagLen = document.getElementById("secondarySecNameInput").value.length;
}
/*mouse over tag suggestion line*/
function mouseOverTagSugLine(lineId,place)
{
	var elmLast = document.getElementById(lineId+place+divPlace);//paint the last line in white				
	if (elmLast)
		elmLast.style.backgroundColor='white';			
				
	var elm = document.getElementById(lineId+place);//paint the current line
	if (elm)
		elm.style.backgroundColor='b7caeb';	
	
	divPlace = place;	
}

/*mouse out tag suggestion line*/
function mouseOutTagSugLine(lineId,place)
{
	var elm = document.getElementById(lineId+place);
	if ((elm) && (divPlace == place))
	{	
		elm.style.backgroundColor = 'white'
		divPlace = place;		
	}
}

/*limit the tag search field if there is a tag with no suggestions*/
function limitTagName(isLimit,inputId)
{
	var subStr =document.getElementById(inputId);
	if (subStr){
		if (isLimit == true)
			subStr.maxChars = document.getElementById(inputId).value.length;
		else
			subStr.maxChars = 30;
	}	
}

/*check whether the tag is over the limit*/
function limitText(evt,field) {
	if (evt.which != undefined && evt.which == 8)
		return true; // allow Netscape backspace
	else	
	{	
		if(field.maxChars == undefined)
			field.maxChars=30;
		return (field.value.length < field.maxChars);	
	}
}

/*not display the suggestion div*/
function notDisplaySuggestionDiv(fieldId)
{
	var sugDiv = document.getElementById(fieldId);
	if (sugDiv)					
		sugDiv.style.display = 'none';	
}

/*get suggestion tags*/
function getTags()
{		
	var secName="";
	var secCode="";
	if ((objhttp.readyState==4) && (objhttp.status == 200))
	{			
		var responseText = objhttp.responseText;
     	if(responseText != "")
		{
			try{
				var response = eval('(' + responseText + ')');                        
		     	tagSug = '<table cellpadding="0" cellspacing="0"  align="center" width="100%" bgcolor="White">';
				len = response.securities.length;
				quanSug = len;
				divPlace = -1;
				if (len > 0){//there are suggestions							
					limitTagName(false,"secNameInput");							
					for (var i = 0; i < len; i++)
					{								
						secName=replaceCharsBack(response.securities[i].name);
						secCode=response.securities[i].code;
						tagSug += '<tr id=sugLine'+(i)+' onclick="chooseTag(\''+escape(secName)+'\',\''+secCode+'\');" onmouseover="mouseOverTagSugLine(\'sugLine\','+(i)+');" onmouseout="mouseOutTagSugLine(\'sugLine\','+(i)+');"><td id=sugName'+(i)+' style="cursor:pointer; font-family: Arial; font-size: 12px;" dir="rtl">'+secName+'</td><td id=sugCode'+(i)+' style="display:none;">'+secCode+'</td></tr>';       
					}	
					tagSug += '</table>'; 	
					displayTagSuggestionsDiv(tagSug);
				}		
				else{//no suggestions
					limitTagName(true,"secNameInput");
					notDisplaySuggestionDiv("tagSuggestions");							
				}
			}catch(e){
				notDisplaySuggestionDiv("tagSuggestions");		
			}					
		}					
	}
}
/*get secondary suggestion tags*/
function getSecondaryTags()
{		
	var secName="";
	var secCode="";
	if ((objhttp.readyState==4) && (objhttp.status == 200))
	{			
		var responseText = objhttp.responseText;
     	if(responseText != "")
		{
			try{
				var response = eval('(' + responseText + ')');                        
		     	tagSug = '<table cellpadding="0" cellspacing="0"  align="center" width="100%" bgcolor="White">';
				len = response.securities.length;
				quanSug = len;
				divPlace = -1;
				if (len > 0){//there are suggestions							
					limitTagName(false,"secondarySecNameInput");							
					for (var i = 0; i < len; i++)
					{								
						secName=replaceCharsBack(response.securities[i].name);
						secCode=response.securities[i].code;
						tagSug += '<tr id=secondarySugLine'+(i)+' onclick="chooseSecondaryTag(\''+escape(secName)+'\',\''+secCode+'\');" onmouseover="mouseOverTagSugLine(\'secondarySugLine\','+(i)+');" onmouseout="mouseOutTagSugLine(\'secondarySugLine\','+(i)+');"><td id=secondarySugName'+(i)+' style="cursor:pointer; font-family: Arial; font-size: 12px;" dir="rtl">'+secName+'</td><td id=secondaryugCode'+(i)+' style="display:none;">'+secCode+'</td></tr>';       
					}	
					tagSug += '</table>'; 	
					displaySecondaryTagSuggestionsDiv(tagSug);
				}		
				else{//no suggestions
					limitTagName(true,"secondarySecNameInput");
					notDisplaySuggestionDiv("secondaryTagSuggestions");							
				}
			}catch(e){
				notDisplaySuggestionDiv("secondaryTagSuggestions");		
			}					
		}					
	}
}
//get suggestion tags - ajax call*/
function getSuggestionsFromUrl()
{
	try{					
		var parameters = "subStr="+buildURL(document.getElementById("secNameInput").value)+"&quanResults="+quanResults;
		objhttp.open ("POST", "/f/compareGraph/getAllStocks.jhtml", true);
		objhttp.onreadystatechange=getTags;				
		objhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded;");				
		objhttp.send(parameters);			
	}catch(e){}
}
//get secondary suggestion tags - ajax call*/
function getSecondarySuggestionsFromUrl()
{
	try{					
		var parameters = "subStr="+buildURL(document.getElementById("secondarySecNameInput").value)+"&quanResults="+quanResults;
		objhttp.open ("POST", "/f/compareGraph/getAllStocks.jhtml", true);
		objhttp.onreadystatechange=getSecondaryTags;				
		objhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded;");				
		objhttp.send(parameters);			
	}catch(e){}
}
/*get the automatic complete for the tags search*/
function getSuggestions()
{	
	gotSecCode=true;
	var subStr =document.getElementById("secNameInput").value;
	// adding/removing secCode 
	if(mainCodesToTypesList != undefined)
	{
		var dailyKey = mainCodesToTypesList.getKeyOfValue("daily");
		if(dailyKey != null)
			mainCodesToTypesList.remove(dailyKey);
		if(subStr.length != 0)
			mainCodesToTypesList.put("-1","daily");
		else
			mainCodesToTypesList.remove("-1");
	}
	//end adding/removing secCode
	if (subStr.length == 0){//no tag for search
		limitTagName(false,"secNameInput");
		notDisplaySuggestionDiv("tagSuggestions");		
		searchTagLen = 0;
		gotSecCode=false;
	}
	else if ((subStr.length == 1) && (subStr == " ")){//space in the search		
		document.getElementById("secNameInput").value = "";
		searchTagLen = 0;
	}	
	else if ((subStr.length == (searchTagLen + 1)) || (subStr.length == (searchTagLen - 1)))//one char difference from the last ajax update
	{		
		limitTagName(true,"secNameInput");
		searchTagLen = subStr.length;
		getSuggestionsFromUrl();
	}	
	else if (subStr.length != searchTagLen){		
		searchTagLen = subStr.length;
		getSuggestionsFromUrl();
	}
}
/*get the automatic complete for the SECONDARY tags search*/
function getSecondarySuggestions()
{	
	gotSecondarySecCode=true;		
	var subStr =document.getElementById("secondarySecNameInput").value;
	// adding/removing secCode for max secCodes computing
	if(mainCodesToTypesList != undefined)
	{
		var dailyKey = mainCodesToTypesList.getKeyOfValue("daily2");
		if(dailyKey != null)
			mainCodesToTypesList.remove(dailyKey);	
		if(subStr.length != 0)
			mainCodesToTypesList.put("-2","daily2");
		else
			mainCodesToTypesList.remove("-2");
	}
	//end adding/removing temp secCode
	if (subStr.length == 0){//no tag for search
		limitTagName(false,"secondarySecNameInput");
		notDisplaySuggestionDiv("secondaryTagSuggestions");		
		searchSecondaryTagLen = 0;
		gotSecondarySecCode=false;
	}
	else if ((subStr.length == 1) && (subStr == " ")){//space in the search		
		document.getElementById("secondarySecNameInput").value = "";
		searchSecondaryTagLen = 0;
	}	
	else if ((subStr.length == (searchSecondaryTagLen + 1)) || (subStr.length == (searchSecondaryTagLen - 1)))//one char difference from the last ajax update
	{		
		limitTagName(true,"secondarySecNameInput");
		searchSecondaryTagLen = subStr.length;
		getSecondarySuggestionsFromUrl();
	}	
	else if (subStr.length != searchSecondaryTagLen){		
		searchSecondaryTagLen = subStr.length;
		getSecondarySuggestionsFromUrl();
	}
}
/*trim string function to delete spaces at a begginnig and the end of the string*/
function trimString(str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
/**************************************/
/*script for encoding string to utf-8*/
function utf8(wide) {
  var c, s;
  var enc = "";
  var i = 0;
  while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  }
  return enc;
}
var hexchars = "0123456789ABCDEF";
function toHex(n) {
  return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}
var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
function encodeURIComponentNew(s) {
  var s = utf8(s);
  var c;
  var enc = "";
  for (var i= 0; i<s.length; i++) {
    if (okURIchars.indexOf(s.charAt(i))==-1)
      enc += "%"+toHex(s.charCodeAt(i));
    else
      enc += s.charAt(i);
  }
  return enc;
}
function buildURL(fld)
{
	if (fld == "") return false;
	var encodedField = "";
	var s = fld;
	if (typeof encodeURIComponent == "function")
	{
		// Use JavaScript built-in function
		// IE 5.5+ and Netscape 6+ and Mozilla
		encodedField = encodeURIComponent(s);
	}
	else 
	{
		// Need to mimic the JavaScript version
		// Netscape 4 and IE 4 and IE 5.0
		encodedField = encodeURIComponentNew(s);
	}
	return encodeURIComponentNew(fld);
}

function displayError(errorId,errorMessage)
	{
		var errorIdMessage = document.getElementById(errorId);
		if(errorIdMessage != undefined)
		{
			if(errorIdMessage.style.visibility == "hidden")
			{
				errorIdMessage.innerHTML = errorMessage;
				errorIdMessage.style.visibility="visible";
			}
		}
	}
	function resetErrors()
	{
		if(document.getElementById("secNameError") != undefined && document.getElementById("secNameError").style.visibility == "visible")
		{
			error = false;
			document.getElementById("secNameError").style.visibility = "hidden";
		}
		if(document.getElementById("secondarySecNameError") != undefined && document.getElementById("secondarySecNameError").style.visibility == "visible")
		{
			secondaryError = false;
			document.getElementById("secondarySecNameError").style.visibility = "hidden";
		}
	}