//for resolving jquery conflict with other javascript libraries 
var $j = jQuery.noConflict();

$j(document).ready(function(){ 

	if($j('#thought_leadership')) {
		init();
		$j('#thought_leadership a#refineToggle').click(function(){
			if($j(this).hasClass('closed')) {
				$j(this).removeClass('closed');
				$j(this).addClass('opened');
				$j('#searchLink').removeClass('lineOff').addClass('lineOn');
				$j(this).parent().next().slideDown();
			}
			else {
				$j(this).removeClass('opened');
				$j(this).addClass('closed');
				$j('#searchLink').removeClass('lineOn').addClass('lineOff');
				$j(this).parent().next().slideUp();
			}
			window.onresize=null;
			return false;	
		});

	}
	
	$j('#thought_leadership .library_sections a[href*=".pdf"]').append('&nbsp;<span>PDF</span>');
	$j('#thought_leadership .library_sections a[href*=".xls"]').append('&nbsp;<span>XLS</span>');
	$j('#thought_leadership .library_sections a[href*=".doc"]').append('&nbsp;<span>DOC</span>');
	$j('#thought_leadership .library_sections a[href*="openVideoWindow"]').append('&nbsp;<img src=\"/images/iconVideoSmall.gif\"/>');
});

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->


function loadSelectedCriteria(){


	
	//check for investment catagory
	for(k=0;;k++){
		var value = getCookieValue("PublicationType"+k)
		if(value == null)break;
		
		if(value ==  "POVW")
    		document.librarySearchForm.PointofView.checked=true;
		if(value ==  "RESH")
			document.librarySearchForm.Research.checked=true;
		if(value ==  "MKCM")
			document.librarySearchForm.MarketCommentary.checked=true;
		
    		if(value ==  "EXCHNG")
			document.librarySearchForm.Exchange.checked=true;			
    		if(value ==  "TCKR")
			document.librarySearchForm.Ticker.checked=true;
   		
	}
	
	
	//check for Media Type
	for(k=0;;k++){
		var value = getCookieValue("MediaType"+k)
		if(value == null)break;
		if(value ==  "VIDEO")
			document.librarySearchForm.Video.checked=true;
		if(value ==  "PDF")
    		document.librarySearchForm.AdobeAcrobatPDFDocument.checked=true;
		if(value ==  "TXT")
			document.librarySearchForm.PlanTextFile.checked=true;
    }

	//check Start and End Date
 	//check Strat Month
	var sMonth = getCookieValue("DatePart0");
	var startMonthIndex = 0;
	for(i=0; i<12 ;i++){
		if(document.librarySearchForm.StartMonth.options[i].value==sMonth){
 			startMonthIndex=i;
			break;
		}
	}
	if(sMonth!='demo' && sMonth!=null){//fix
		document.librarySearchForm.StartMonth.options[startMonthIndex].selected = true;
	}
	//check Start Year
	var sYear = getCookieValue("DatePart1");
	var startYearIndex = 0; 
	for(i=0; i<document.librarySearchForm.StartYear.options.length ;i++){
		if(document.librarySearchForm.StartYear.options[i].value==sYear){
 			startYearIndex=i;
			break;
		}
	}
	if(sYear!='demo' && sYear!=null){//fix
		document.librarySearchForm.StartYear.options[startYearIndex].selected = true;
	}
	//check End Month
	var eMonth = getCookieValue("DatePart2");

	if(eMonth!=null ){
		var endMonthIndex = 0;
		for(i=0; i<12 ;i++){
	 		if(document.librarySearchForm.EndMonth.options[i].value==eMonth){
 				endMonthIndex=i;
				break;
			}
		}
		if(eMonth!='demo' && eMonth!=null){//fix
			document.librarySearchForm.EndMonth.options[endMonthIndex].selected = true;
		}
	}

	//check End Year
	var eYear = getCookieValue("DatePart3");

	if(eYear!=null ){
		var endYearIndex = 0; 
		for(i=0; i<document.librarySearchForm.EndYear.options.length ;i++){
	 		if(document.librarySearchForm.EndYear.options[i].value==eYear){
 				endYearIndex=i;
				break;
			}
		}
		if(eYear!='demo' && eYear!=null){//fix
			document.librarySearchForm.EndYear.options[endYearIndex].selected = true;
	  	}
	}
	

			
}

function getCookieValue(name){
	var allCookies = document.cookie;
 	var pos = allCookies.indexOf(name);	
  	if( pos!= -1 ){
  		var start = pos + name.length + 1 ;
		var end = allCookies.indexOf(";",start);
		if( end==-1 ) end = allCookies.length;
  		var value = allCookies.substring(start,end);
 		value = unescape(value);
  		//alert(name+"="+value);
  		return value;
  }
  else{
   return null;
  }
}


function setCurrentDate(){

	var date = new Date();
	var mon = date.getMonth();
	var year = date.getFullYear();
		
	var j =0;

	for(i=0; i<document.librarySearchForm.EndYear.options.length ;i++){
	 if(document.librarySearchForm.EndYear.options[i].value==year){
 		j=i;
	 }
	}
	
	var k = 0;
	
	for(i = 0; i < document.librarySearchForm.StartYear.options.length; i++) {
	  if(document.librarySearchForm.StartYear.options[i].value==(year-1)) {
	  	k = i;
	  }
	}
	
	// Start at current date minus two years.
	document.librarySearchForm.StartMonth.options[mon].selected = true;
	document.librarySearchForm.StartYear.options[k].selected = true;
	
	// End at current month and year.
	document.librarySearchForm.EndMonth.options[mon].selected = true;
	document.librarySearchForm.EndYear.options[j].selected = true;
}


function goToResearch(){

	var date = new Date();
	var mon = date.getMonth() + 1;  
	var year = date.getFullYear();
	
	var mon2 = 0;
	var year2 = year;
	var urlString = "";

	if ( mon == 1){

		mon2 = 12;
		year2 = year1 - 1;

	}
	else{

		mon2 = mon - 1;

	}

	urlString = "/weblogic/librarySearchServlet?StartMonth=" + mon2 + "&StartYear=" + year2 + "&EndMonth=" + mon + "&EndYear=" + year + "&Geography=0&Author=0";

	window.open(urlString, '_self');

}








function SubmitForm(){
	var selectedSMonthIndex = document.librarySearchForm.StartMonth.selectedIndex;
	var sMonth = document.librarySearchForm.StartMonth.options[selectedSMonthIndex].value;

	var selectedSYearIndex = document.librarySearchForm.StartYear.selectedIndex;
	var sYear = document.librarySearchForm.StartYear.options[selectedSYearIndex].value;

	var selectedEMonthIndex = document.librarySearchForm.EndMonth.selectedIndex;
	var eMonth = document.librarySearchForm.EndMonth.options[selectedEMonthIndex].value;

	var selectedEYearIndex = document.librarySearchForm.EndYear.selectedIndex;
	var eYear = document.librarySearchForm.EndYear.options[selectedEYearIndex].value;

	var StartDate = new Date(sMonth+"/1/"+sYear);
	var EndDate = new Date(eMonth+"/1/"+eYear);

	var StartTime = Date.parse(StartDate);
	var EndTime = Date.parse(EndDate);
	if(EndTime >= StartTime){

		var cook = document.cookie;	
		var attrValue = getAttributes();
		var url = "/weblogic/librarySearchServlet?"+attrValue;
		if(cook!=""){
			resetCookies(document.librarySearchForm.Site.value);
		}
		window.parent.location = url;
		
	}else{
		alert("Please enter a start date that is on or before the end date.");
	}

}


function init(){
	setCurrentDate();//set the end date as current date.
	loadSelectedCriteria(); // by looking at the cookies marked the items as checked.
}

function resetCookies(site){

	if(site=="PUBL"){
		refresh("/");
	}else if(site=="CONS"){
		refresh("/consultant/library");
	}else if(site=="CLNT"){
		refresh("/client/library");
	}

}

function refresh(cookiePath){


	refreshCookies("PublicationType", cookiePath);

	refreshCookies("MediaType", cookiePath);
	refreshCookies("DatePart", cookiePath);


}

function refreshCookies(item, path){
	var i=0;
	var element = item+i;
	var value = getCookieValue(element);
	do{		
		if(value!=null){
			removeCookie(element, path);
			i++;
			element = item+i;
			value = getCookieValue(element);
		}
		else{
			break;
		}
		
	}while(value!=null);
}


function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

function removeCookie (name,path,domain,secure) {
  
  	var expdate = new Date ();
    FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
    expdate.setTime (expdate.getTime()+ (24 * 60 * 60 * 1000)); // 24 hrs from now 
	SetCookie (name, "demo", expdate, path);

}

function clickCheck(chkBoxName){

	//if(chkBoxName!='AdvancedResearch')
	//	document.librarySearchForm.AdvancedResearch.checked=false;
	//if(chkBoxName!='IndexEquity')
	//	document.librarySearchForm.IndexEquity.checked=false;
	//if(chkBoxName!='ActiveEquity')
	//	document.librarySearchForm.ActiveEquity.checked=false;
	//if(chkBoxName!='FixedIncome')
	//	document.librarySearchForm.FixedIncome.checked=false;
	//if(chkBoxName!='Cash')
	//	document.librarySearchForm.Cash.checked=false;
	//if(chkBoxName!='RealEstate')
	//	document.librarySearchForm.RealEstate.checked=false;
	//if(chkBoxName!='Currency')
	//	document.librarySearchForm.Currency.checked=false;
	//if(chkBoxName!='AbsoluteReturn')
	//	document.librarySearchForm.AbsoluteReturn.checked=false;
	//if(chkBoxName!='PrivateEquity')
	//	document.librarySearchForm.PrivateEquity.checked=false;
	//if(chkBoxName!='AssetAllocation')
	//	document.librarySearchForm.AssetAllocation.checked=false;
	//if(chkBoxName!='GeneralInvesting')
	//	document.librarySearchForm.GeneralInvesting.checked=false;

}

function Sort(givenUrl){
		
			var url = window.parent.location + "";
			var indexOfSort = url.indexOf("Sort=");
			if(indexOfSort > 0){
				var str1 = url.substring(0, indexOfSort);
				var i = url.indexOf("&", indexOfSort)
				var str2 = "";
				if(i > 0 ){
					 str2 = url.substring(i+1);
				}
				url = str1 + str2;
				var urlLen = url.length;
				var subStr = url.substring(urlLen-1);
				if(subStr == "&"){
					window.parent.location =  url + givenUrl;				
				}else{
					window.parent.location =  url + "&" +givenUrl
				}
			}else{
				
				window.parent.location =  url + "&" +givenUrl+"&Cookie=false";
			}
				
}

function getAttributes(){
	
	var attrValue="";
	//check investment categories
	

	if(document.librarySearchForm.PointofView.checked==true){
		attrValue= attrValue+"&PointofView="+document.librarySearchForm.PointofView.value;
	}
	if(document.librarySearchForm.Research.checked==true){
		attrValue= attrValue+"&Research="+document.librarySearchForm.Research.value;
	}
	if(document.librarySearchForm.MarketCommentary.checked==true){
		attrValue= attrValue+"&MarketCommentary="+document.librarySearchForm.MarketCommentary.value;
	}
	
	if(document.librarySearchForm.Ticker.checked==true){
		attrValue= attrValue+"&Ticker="+document.librarySearchForm.Ticker.value;
	}
	if(document.librarySearchForm.Exchange.checked==true){
		attrValue= attrValue+"&Exchange="+document.librarySearchForm.Exchange.value;
	}
  

	//check media type
	if(document.librarySearchForm.Video.checked==true){
		attrValue= attrValue+"&Video="+document.librarySearchForm.Video.value;
	}
	if(document.librarySearchForm.AdobeAcrobatPDFDocument.checked==true){
		attrValue= attrValue+"&AdobeAcrobatPDFDocument="+document.librarySearchForm.AdobeAcrobatPDFDocument.value;
	}
	if(document.librarySearchForm.PlanTextFile.checked==true){
		attrValue= attrValue+"&PlanTextFile="+document.librarySearchForm.PlanTextFile.value;
	}
	 
	
	//incorporate dates 
	var selectedSMonthIndex = document.librarySearchForm.StartMonth.selectedIndex;
	attrValue= attrValue+"&StartMonth="+document.librarySearchForm.StartMonth.options[selectedSMonthIndex].value;
	
	var selectedSYearIndex = document.librarySearchForm.StartYear.selectedIndex;
    attrValue= attrValue+"&StartYear="+document.librarySearchForm.StartYear.options[selectedSYearIndex].value;
	
	var selectedEMonthIndex = document.librarySearchForm.EndMonth.selectedIndex;
	attrValue= attrValue+"&EndMonth="+document.librarySearchForm.EndMonth.options[selectedEMonthIndex].value;
	
	var selectedEYearIndex = document.librarySearchForm.EndYear.selectedIndex;
	attrValue= attrValue+"&EndYear="+document.librarySearchForm.EndYear.options[selectedEYearIndex].value;    
	
	
	 
	 if(attrValue.indexOf("&")==0){
	 	 attrValue = attrValue.substring(1);	 	
	 }
	 
	 if ( (attrValue.indexOf("PointofView") == -1 )&& (attrValue.indexOf("Research") == -1 ) &&
	 (attrValue.indexOf("MarketCommentary") == -1 ) && (attrValue.indexOf("Ticker") == -1 ) && 
   (attrValue.indexOf("Exchange") == -1 )){
      attrValue = "PointofView=POVW&Research=RESH&MarketCommentary=MKCM&Ticker=TCKR&Exchange=EXCHNG&" + attrValue;
   }
   
	return attrValue;
}
