 // For changeActiveMediumSearchTab
var medNewsFormName = "";     	
var initFlag = 0;

var cookieName = locale + "MedCookieKeyword";
var keywordCookie = findSearchKeywordCookie(cookieName);
var pathName = location.protocol + "//" + location.host +'/info/web/search/';									
var htmlLocation = "mednews_searchresult.htm?bookmark=true";

var nMore = 1;
var nCookieCnt = 0;
var keywordCookie;

if (keywordCookie==null)
	keywordCookie="";
if (keywordCookie != "") {
	var keywordList = keywordCookie.split(',');
	var keywordListCnt = keywordList.length;
	if (keywordListCnt > 0) {
		nMore = keywordListCnt + 2;
		nCookieCnt = keywordListCnt;
	}
}			
		
// For Switching between within this session and search all
function changeActiveMediumSearchTab (activeObj) {
	if (activeObj!='null')
	{				
		 var lastActiveObjId = document.getElementById(medNewsFormName+"mediumSearchTabHidden").value;
		 var lastActiveObj = document.getElementById(lastActiveObjId);  
		 var lastActiveObjLine = document.getElementById(lastActiveObjId+"Line");  		         
		 
		 if (initFlag == 0)
		 {
			lastActiveObj = document.getElementById("tagWithinThisSection");  
			lastActiveObjLine = document.getElementById("tagWithinThisSectionLine");  		         	
			initFlag = 1;
		 }
		 if (lastActiveObj) {	          
				lastActiveObj.className = "tabSourcingHome_up";
				lastActiveObjLine.className = "background_white";		             	
		 }
		 
		 activeObj.className = "tabSourcingHome_down";     
		 var activeObjLine = document.getElementById(activeObj.id+"Line");
		 activeObjLine.className ="sourcingHome_search_orange_whiteLine_leftWhiteLine"  ;  
		 document.getElementById(medNewsFormName+"mediumSearchTabHidden").value = activeObj.id;
		 lastActiveObj = activeObj;
	 }
}  

function gotoMedNewsSearchPage() {
	var kword = document.getElementById("searchBox").value;
	
	if (kword && kword.length > 0) {
	
		var regex=/[\s&\(\)\/'\-%~!@#$^*_+=|{}\[\]\":\;`<>,.?\\]/gi;
		var regex2=/[<>]/gi;
		
		var str = kword.replace(regex, '');

		if (( str.length == 0 ) || ((locale == 'en') && (kword.length < 2))){
			// keyword is invalid 
			alert('Please enter a valid searching criteria!');
		}
		else {
			kword = kword.replace(regex2,'');
			
			if (keywordList==null)
				keywordList="";
			var updatedKeywordList = checkDup(keywordCookie, kword);
			setCookie(cookieName, updatedKeywordList , new Date("January 1, 2046"));
			
			return gotoSearchPageWithKeyWord2(kword);
		}
	}
	else {
		// keyword is empty
		alert('Please enter a keyword!');
	}
 }
 
function gotoSearchPageWithKeyWord2(keyword) 
{
	var searchPageLink="";
	var ActiveTabId = document.getElementById(medNewsFormName+"mediumSearchTabHidden").value;
	//var ActiveTabId = document.getElementById(medNewsFormName+":mediumSearchTabHidden").value;
	var overrideSearchView = 0; //override default value by getSearchParam()
	var overrideLanguage = 0; //override default value by getSearchParam()			
	
	if ((initFlag == 0) || (ActiveTabId == "tagWithinThisSection"))
	{
		//search within site
		var queryString = "&drawer="+ dataSrc + "&vp_name=&vp=&search_view=mednews&LANGUAGE=" + language;
		searchPageLink = pathName + htmlLocation + queryString + "&query=" + encodeURIComponent(keyword);					
	}
	else
	{
		//search in hktdc
		searchPageLink+="/info/sourcing/searchresult_all.htm?bookmark=true";
		// EMP has no /info/, replace it 
		searchPageLink=searchPageLink.replace("/info/","/");
		if (searchPageLink && searchPageLink.length > 0) 
		{		    						
			searchPageLink+="&locale=" + locale +"&search_view=all"+"&query="+encodeURIComponent(keyword);
		}				
	}			
	window.location = searchPageLink;		    
	return false;				
}

function findSearchKeywordCookie(cookie){
	var keywordCookie = "";
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(cookie+"=");
		if (c_start!=-1) {
			c_start=c_start + cookie.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			keywordCookie = unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return keywordCookie;
}
function setCookie(c_name,value,expiredays){
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+expiredays.toGMTString())+"; path=" +"/";
}
function checkDup(keywordCookie, kword) {
	var keywordList = new Array ();
	if (keywordCookie != ''){
		keywordList = keywordCookie.split(',');
	}
	var index = -1;
	//Deduplicate
	for (var i = 0; i < keywordList.length; i++) {
		var keyword = keywordList[i];
		if (keyword == kword) {
			index = i;
			break;
		}
	}
	if (i >= keywordList.length) {
		keywordList[keywordList.length] = kword;
	}
	
	//Only store 3 keywords in the cookie
	if (keywordList.length > 3){
		keywordList.splice(0, 1);
	}
	return keywordList.join(',');
}

function onSearchValueBoxEnter( evt ) {
	var keyCode = null;
	if( evt.which ) {
		keyCode = evt.which;
	} else if( evt.keyCode ) {
		keyCode = evt.keyCode;
	}
	
	if( 13 == keyCode ) {
		gotoMedNewsSearchPage();
		return false;
	}
	return true;
}
