 function addCookie(cookieName,newCookieValue){
      	var futdate = new Date()		
	var expdate = futdate.getTime()  
	expdate += 31536000*1000  //expires in 1 hour(milliseconds)
	futdate.setTime(expdate)
	var newCookie=""+cookieName+"="+newCookieValue+"; path=/;"   
	newCookie += " expires=" + futdate.toGMTString()
	window.document.cookie=newCookie
}
//Search history cookie
function searchHistoryCookie(searchString,sortType){
	var cookieSearchString=escape(searchString);
	var newCookieValue =cookieSearchString+"$"+sortType;
	var cookie=document.cookie
	var chkdCookie=delBlanks(cookie)
	var nvpair=chkdCookie.split(";")
	var cookieName="SEARCH_HISTORY";
	if(nameDefined(nvpair,cookieName))
	{
	var cookieValue = getCookieValue(nvpair,cookieName);
	var cookieArray = cookieValue.split("%%");
	var count=0;
	for(var i=0;i<cookieArray.length;i++)
	{
		var tempCookieValue ="";
		var cookieValues = cookieArray[i].split("$");
		if(cookieValues[0]!=cookieSearchString){
			tempCookieValue = tempCookieValue+cookieArray[i];
			count ++;
		}
		if(tempCookieValue !=""){
			newCookieValue =newCookieValue+"%%"+tempCookieValue;
		}
		if(count==3){
			break;
		}
	}
	}
	addCookie(cookieName,newCookieValue);
}
// Resource Library Recently Viewed cookie
function reslibCookie(prdUrl,awebcurl,prdTitle){
	var newCookieValue = prdUrl+"$"+awebcurl+"$"+escape(prdTitle);
	var cookie=document.cookie
	var chkdCookie=delBlanks(cookie)
	var nvpair=chkdCookie.split(";")
	var cname="RESOURCE_HISTORY";
	if(nameDefined(nvpair,cname))       
	{   
	    var cookieValue = getCookieValue(nvpair,cname);
	    var cookieArray = cookieValue.split("%%");
	    var count=0;
	    for(var i=0;i<cookieArray.length;i++){
		var cookieValues = cookieArray[i].split("$");
		var tempCookieValue ="";
		if(cookieValues[0] != prdUrl){
			tempCookieValue = cookieArray[i];
			count ++;
		}
		if(tempCookieValue !=""){
			newCookieValue =newCookieValue+"%%"+tempCookieValue;
		}
		if(count==2){
			break;
		}
	    }
	}
	
	addCookie(cname,newCookieValue);
}
// New Method added for New tab -- Resource Library
function reslibNewCookie(extUrl,awebcUrl,title){
	var tempUrl ="";
	if(extUrl==null||extUrl==""){
		tempUrl = "/"+awebcUrl;
		
	}else{
		tempUrl = extUrl;
	}
	reslibCookie(tempUrl,awebcUrl,title);
}
// Function used to read values from the stored cookie
function readCookie(cookieName){
      var cookie=document.cookie
      var chkdCookie=delBlanks(cookie)
      var nvpair=chkdCookie.split(";")
      if(nameDefined(nvpair,cookieName))       
      {   
	    var cookieValue = getCookieValue(nvpair,cookieName);
	    return cookieValue;
      }
      return null;
}
//function used to get the cookie value
function getCookieValue(ckie,nme)
{
	var splitValues
	var i
	for(i=0;i<ckie.length;++i) {
		splitValues=ckie[i].split("=")
		if(splitValues.length > 1){
			if(trim(splitValues[0])==nme){
			    if(nme=="RESOURCE_HISTORY"){
				return ckie[i].substring(18,ckie[i].length);
			     }
			return splitValues[1];
			}
		}
	}
	return "";
}

function nameDefined(ckie,nme)
{
   var splitValues
   var i
   for (i=0;i<ckie.length;++i)
   {
      splitValues=ckie[i].split("=")
      if (trim(splitValues[0])==nme) {
	return true
      }

   }
   return false
}

function delBlanks(strng)
{
   var result=""
   var i
   var chrn
   for (i=0;i<strng.length;++i) {
      chrn=strng.charAt(i)
      result += chrn
   }
   return result
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
//function used to store the no of records per page info in cookie
function addResultsPerPageCookie(resultsPerPage){
      var newCookieValue = resultsPerPage;
      var cookie=document.cookie
      var chkdCookie=delBlanks(cookie)
      var nvpair=chkdCookie.split(";")
      var cname="RESOURCE_LIBRARY_NO_REC";
      var futdate = new Date()		
	var expdate = futdate.getTime()  
	expdate += 31536000*1000  //expires in 1 hour(milliseconds)
	futdate.setTime(expdate)
	var newCookie="RESOURCE_LIBRARY_NO_REC="+newCookieValue+"; path=/;"   
	newCookie += " expires=" + futdate.toGMTString()
	window.document.cookie=newCookie
}

function clearCookie(name,divId){
	document.getElementById(divId).style.display="none";
	var newCookieValue = "";
	var cookie=document.cookie
	var futdate = new Date()
	futdate.setTime(futdate.getTime()-1); 
	var newCookie=""+name+"="+newCookieValue+"; path=/;"   
	newCookie += " expires=" + futdate
	window.document.cookie=newCookie
}
