function trimString(str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
var objhttp=new Array(6);
function GetXmlHttpObject()
{
	var xmlhttp;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		xmlhttp= new XMLHttpRequest();
		if (xmlhttp.overrideMimeType) 
    		xmlhttp.overrideMimeType('text/xml');
	} 
	else if (window.ActiveXObject) 
	{ // IE
		try{
    		xmlhttp= new ActiveXObject("Msxml2.XMLHTTP");
   	 		} 
		catch (e) 
		{
			try{
    			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
        		} 
			catch (e) {}
    	}
	}
	return xmlhttp;
}
objhttp['general']=GetXmlHttpObject();
objhttp['show_tag']=GetXmlHttpObject();
objhttp['emailFriend']=GetXmlHttpObject();
objhttp['tvMain']=GetXmlHttpObject();
objhttp['search_word']=GetXmlHttpObject();
objhttp['talkback']=GetXmlHttpObject();

var activeTab = 0;//the active tab
var QUAN_RESULTS_PER_TALKBACKS_PAGE = 10;
var COMPLETE_STATE = 4;
var UNINITIATED_STATE  = 0;
var STATUS_OK = 200;
var tabsType = new Array("יומי","שבועי","חודשי");
var rvwName="mostPopTop";//the rvw name for the top5 in the index page

function replaceCharsBack(str)
{
	while(str.indexOf("~") != -1)
		str =str.replace("~","\"");
	while(str.indexOf("#") != -1)
		str =str.replace("#",":");	
	return str;
}

function handleGetTopArticlesResults(mostType,xmlhttp)
{	
	try
	{
		if ((xmlhttp.readyState == COMPLETE_STATE) && (xmlhttp.status == STATUS_OK)){
			var respText=trimString(xmlhttp.responseText);
				var resultsDiv = document.getElementById("top_results_"+mostType);
				var resultsHtml='';
				var line=respText.split("@@@");
				var column;
				var headlineLen,i;
				var slug; var headline;var size;
				var tvArchive;
				size=line.length-1;
				for(i = 1;i <=size;i++)
				{
					line[i] = trimString(line[i]);
					column = line[i].split("@@");
					slug = replaceCharsBack(trimString(column[0]));
					headline= replaceCharsBack(trimString(column[1]));
					section=replaceCharsBack(trimString(column[2]));
					headlineLen=headline.length;
			
					resultsHtml+= "<div id=result_line>";
					resultsHtml+= "<div id=result_seq>"+(i)+"</div>";
					if(mostType=='tvMain')
					{
						if(slug.indexOf(".xml") != -1)
							resultsHtml+= "<div id=result_txt><a class=result_txt href='"+section+"/index.jhtml?ElementId="+slug+"'>"+headline+"</a></div>"
						else
							resultsHtml+= "<div id=result_txt><a class=result_txt href='"+section+"/tvArchive.jhtml?ElementId="+slug+"'>"+headline+"</a></div>"
					}
					else if(mostType=='general' || mostType=='emailFriend')
					{
						resultsHtml+= "<div id=result_txt><a class=result_txt href='"+section+"/tmc/article.jhtml?ElementId="+slug+"&layer=hp'>"+headline+"</a></div>"
					}
					else if(mostType=='talkback')
					{
						if(headlineLen > 28)
							headline=headline.substring(0,28).concat("...");
						resultsHtml+= "<div id=result_txt><a class=result_txt href='/mostpop/inner.jhtml?mostType=talkback&from=tkHeadline&activeTab="+activeTab+"'>"+headline+"</a></div>"
					}
					else if(mostType == 'show_tag')
					{
						if(headlineLen > 28)
							headline=headline.substring(0,28).concat("...");
						resultsHtml+="<div id=result_txt><a class=result_txt href='/web2/tags.jhtml?tag="+headline+"'>"+headline+"</a></div>"	
					}
					else
					{
						if(headlineLen > 28)
							headline=headline.substring(0,28).concat("...");
						resultsHtml+="<div id=result_txt><a class=result_txt href='/tmc/archive/arcArticleSearch.jhtml?searchText="+headline+"&val="+headline+"&namesymbol=article&from=aonline'>"+headline+"</a></div>"	
					}
					resultsHtml+= "<div class=clear></div>";	
					resultsHtml+= "</div>";
		
					if (resultsDiv){			
						resultsDiv.innerHTML = resultsHtml;
					}
				}
				// no results to show
				if(size==0)
				{
					var resultsDiv = document.getElementById("top_results_"+mostType);
					resultsDiv.innerHTML="<div class=noResults>.לא קיימים נתונים להצגה עבור לשונית זו</div>";
				}	
			}
		}
		catch(e)
		{
		}
}
function getTopArticles(pType,mostType)
{	
	if (objhttp[mostType].readyState == COMPLETE_STATE || objhttp[mostType].readyState == UNINITIATED_STATE) 
	{	
		try{
			if(mostType!='talkback')	
				objhttp[mostType].open ("GET", "/mostpop/getArticles.jhtml?type="+pType+"&rvwName="+rvwName+"&layer="+mostType, true);
			else
			objhttp[mostType].open ("GET", "/mostpop/getTalkbacks.jhtml?type="+pType, true);
			objhttp[mostType].onreadystatechange=function callGetResult(){handleGetTopArticlesResults(mostType,objhttp[mostType]);};		
			objhttp[mostType].setRequestHeader ("Content-Type", "application/x-www-form-urlencoded;");	
			objhttp[mostType].send(null);					
		}catch(e)
		{
		}	
	}
}
/*choose active tab according to periodType - day,week,month and mostType - article, talkback, tv .....*/
function chooseTab(mostType, periodType)
{
	changeTabStatus("right_tab", periodType, mostType);
	changeTabStatus("data_tab", periodType, mostType);
	changeTabStatus("left_tab", periodType, mostType); 
}
/*change status of tabName for type tab. type - day,week,month. tabName - right,middle,left.*/
function changeTabStatus(tabName, periodType, mostType)
{
	var aElm = document.getElementById(mostType+"Link");
	aElm.href="inner.jhtml?mostType="+mostType+"&activeTab="+periodType;
	activeTab=periodType;
	var elm = document.getElementsByName(tabName+"_"+mostType);	
	for(i=0;i<=2;i++)
	{
		if(i==periodType)
		{
			getTopArticles(i,mostType);
			if ((elm) && (elm[periodType]))
				eval("elm["+periodType+"].className='"+tabName+" "+tabName+"_on'");
		}	
		else
		{
			if ((elm) && (elm[i]))
				eval("elm["+i+"].className='"+tabName+" "+tabName+"_off'");
		}
	}	
}
/*	show period type tabs - daily, weekly,monthly.
	periodType is the type of the most popular data*/
function showTabs(mostType)
{
	for(var cnt = 0; cnt < tabsType.length; cnt++)
	{
		if (cnt != 0)//not first
		document.write("<div class=tabs_space></div>");
		document.write("<div id=right_tab_"+mostType+" name=right_tab_"+mostType+" class='right_tab right_tab_off'></div>");		
		document.write("<div id=data_tab_"+mostType+" name=data_tab_"+mostType+" class='data_tab data_tab_off'><div class='data_tab_inner handPoint'  onclick=chooseTab('" + mostType + "'," + cnt + ")>"+tabsType[cnt]+"</div></div>");
		document.write("<div id=left_tab_"+mostType+" name=left_tab_"+mostType+" class='left_tab left_tab_off'></div>");			
	}
}




	