var browser
if (navigator.appName.indexOf("Netscape") >= 0 && parseFloat(navigator.appVersion) >= 4) { browser = 'ns'; }
if (navigator.appName.indexOf("Microsoft") >= 0 && parseFloat(navigator.appVersion) >= 4) { browser = 'ie'; }

function getRapidSearchTickerLookup() {
	var frm = document.frmSearch
	
	if (frm.selType.selectedIndex != 1) {
		frm.selType.selectedIndex = 1	// set it to Ticker
		LoadDropDown (frm.selType);  }
	
	var strTicker = document.frmSearch.companySearchSymbol.value;

    window.open("/SCOnline/Secure/Control/ControlLoop.asp?type=doTickerLookup", "Lookup", "height=320,width=550,scrollbars=yes,screenY=200,screenX=200,left=200,top=200");
}


function getTickerLookup() {
	var strTicker = document.frmSearch.companySearchSymbol.value;
	var strCoName = document.frmSearch.companySearchName.value;
	
    window.open("/SCOnline/Secure/Control/ControlLoop.asp?type=doTickerLookup", "Lookup", "height=330,width=450,scrollbars=yes,screenY=200,screenX=200,left=200,top=200");
}

function submitSearch (strType) {
	var frm = document.frmSearch

    if(frm.fromMonth+'' != 'undefined') {
		frm.searchDate.value = frm.fromMonth.options[frm.fromMonth.selectedIndex].value + "/" + frm.fromDay.options[frm.fromDay.selectedIndex].value + "/" + frm.fromYear.options[frm.fromYear.selectedIndex].value
		if (frm.searchDate.value != "//")
			if (!isDate(frm.searchDate, false)) {
				frm.fromDay.focus()
				alert("Please select a valid from date.")
				return
			}

		frm.searchDate.value = frm.toMonth.options[frm.toMonth.selectedIndex].value + "/" + frm.toDay.options[frm.toDay.selectedIndex].value + "/" + frm.toYear.options[frm.toYear.selectedIndex].value
		if (frm.searchDate.value != "//")
			if (!isDate(frm.searchDate, false)) {
				frm.toDay.focus()
				alert("Please select a valid to date.")
				return
			}
	}

	if (strType=="CountOnly" || strType=="CountOnlyProfile")
		frm.advSearchQueryType.value = "countonly"

	var strCriteria = ""

    if(frm.contributors+'' != 'undefined') {
		if (frm.contributors.options[frm.contributors.selectedIndex].value != "")
			frm.searchByContributor.value = "1"
			strCriteria = "(" + frm.contributors.options[frm.contributors.selectedIndex].text + ")"
	}

	var strTemp = frm.companySearchSymbol.value

	if (strTemp.length >0) {
		if (strCriteria!='') {
			strCriteria += " and (" + strTemp + ") "
		}
		else {
			strCriteria += "(" + strTemp + ") "
		}
	}

    if(frm.categories+'' != 'undefined')
		strCriteria += selectedItem (frm.categories, ", ")

    if(frm.analysts+'' != 'undefined')
		for (var i=0; i < frm.analysts.length; i++) {
			strCriteria += selectedItems (frm.analysts[i], "; ") 
		}

    if(frm.industries+'' != 'undefined')
		for (var i=0; i < frm.industries.length; i++) {
			strCriteria += selectedItems (frm.industries[i], ", ") 
		}
	
	strCriteria += selectedItems (frm.regions, ", ")
	strCriteria += selectedItems (frm.countries, ", ")
	strCriteria += selectedItems (frm.currencies, ", ")

    if(frm.subjects+'' != 'undefined')
		for (var i=0; i < frm.subjects.length; i++) {
			strCriteria += selectedItems (frm.subjects[i], ", ") 
		}
	
	frm.searchCriteria.value = strCriteria
	frm.submit()
}


function selectAllItem (frm) {
    if(frm.industries+'' != 'undefined')
		for (var i=0; i < frm.industries.length; i++) {
			checkAllSelected (frm.industries[i], false);		
		}

    if(frm.subjects+'' != 'undefined')
		for (var i=0; i < frm.subjects.length; i++) {
			checkAllSelected (frm.subjects[i], false);
		}

}


function selectedItems (sel,delimiter) {
	var selectedList = ""
    if(sel+'' == 'undefined')
		return ""
	
	for (var i = 0; i < sel.length; i++) {
		if (sel.options[i].selected && sel.options[i].value != "")
			selectedList += delimiter + sel.options[i].text;
	}

	if (selectedList.length > 0)
		return " and (" + selectedList.substr(2) + ")"
	else
		return ""
}

function selectedItem (sel) {
	if (sel.options.length==0) return ""
	
	if (sel.options[sel.selectedIndex].value != "")
		return " and (" + sel.options[sel.selectedIndex].text + ")"
	else
		return ""
}

function checkAllSelected (sel, blnSelect) {
    if(sel+'' == 'undefined')
		return

	if (sel.type=="select-one")
		return

	if (blnSelect) { 		// first item in the list [All] is selected, then select all items in the list by default
		if (sel.selectedIndex==0)
			for (var i = 0; i < sel.length; i++) {
				sel.options[i].selected = true
			}
	}
	else	{ 		// if all the items in the list are selected, then de-select all and select only the first item [All] by default
		var selCount = 0
		for (var i = 1; i < sel.length; i++) {
			if (sel.options[i].selected)
				 selCount += 1
		}
		
		if (sel.options.length-1 == selCount) {		//first item contains [All] which will not be checked
			sel.options[0].selected = true	// select [All]
			
			for (var i = 1; i < sel.length; i++) {
				sel.options[i].selected = false
			}
		}
	}
		
}

function doAddToTickers (strTickers, frm) {

	if (frm == "")
		frm = "frmSearch";
		
	var txtSymbol = eval('document.'+frm).companySearchSymbolVisible
	if(txtSymbol == null)
		txtSymbol = eval('document.'+frm).companySearchSymbol
	
	if (txtSymbol.value == "")
		txtSymbol.value=strTickers
	else
		txtSymbol.value = txtSymbol.value+","+strTickers
}


function LoadDropDown( selType )
{
	var strVal = selType.options[selType.selectedIndex].value
	var strText = selType.options[selType.selectedIndex].text

	var sel = document.frmSearch.selContent

	for(i = sel.length-1; i >= 0; i--)
		sel.options[i]=null;

	if (strVal.length==0 || strVal=="ticker" || strVal=="company" || strVal=="csfbOnly") {
		return;	}

	document.frmSearch.companySearchSymbol.value = ""
	document.frmSearch.companySearchSymbolVisible.value = ""
	
	var arrayName = ""
	
	switch (strText) {
		case "Analyst":
			arrayName = "scAnalysts"
			sel.options[sel.options.length]= new Option("[Select an Analyst]", "")	
			break;
		case "Credit Suisse Analyst":
			arrayName = "csfbAnalysts"
			sel.options[sel.options.length]= new Option("[Select an Analyst]", "")	
			break;
		case "Equity Sector":
			arrayName = "EQSectors"
			sel.options[sel.options.length]= new Option("[All Equity Sectors]", "")	
			break;
		case "Fixed Income Sector":
			arrayName = "FISectors"
			sel.options[sel.options.length]= new Option("[All Fixed Income Sectors]", "")	
			break;
		case "Equity Publication":
			arrayName = "EQPublications"
			sel.options[sel.options.length]= new Option("[All Equity Publications]", "")	
			break;
		case "Economics Publication":
			arrayName = "ECPublications"
			sel.options[sel.options.length]= new Option("[All Economics Publications]", "")	
			break;
		case "Fixed Income Publication":
			arrayName = "FIPublications"
			sel.options[sel.options.length]= new Option("[All Fixed Income Publications]", "")	
			break;
		case "Foreign Exchange Publication":
			arrayName = "FXPublications"
			sel.options[sel.options.length]= new Option("[All Foreign Exchange Publications]", "")	
			break;
		case "Derivatives Publication":
			arrayName = "DVPublications"
			sel.options[sel.options.length]= new Option("[All Derivatives Publications]", "")	
			break;
	}

	var array = eval( arrayName );
	var oOption
	
	if ( array == null )
		return

	for (var i = 0; i < array.length; i=i+2) {
		if (!(array[i].length==0 && array[i + 1].length==0))
			sel.options[sel.options.length]= new Option(array[i + 1], array[i])	
	}

	if (i > 0)
		sel.selectedIndex=0
}


function getElt () 
{ 
    var currentLayer = document.layers[getElt.arguments[0]];
    for (var i=1; i<getElt.arguments.length && currentLayer; i++)
    {   currentLayer = currentLayer.document.layers[getElt.arguments[i]];
    }
    return currentLayer;
}


function getSearch (Action) {
	var frm = document.frmSearch
	var strTemp = frm.companySearchSymbolVisible.value
	
	if (strTemp != "" && frm.selType.value == "company" ) {
	  frm.companySearchSymbol.value = "";
	  frm.companySearchName.value = strTemp;
	}
	else
	{
		frm.companySearchSymbol.value = strTemp;
	}
	
	var err = false;
	
	if ((strTemp.indexOf(",") > 0) && (frm.selType.value == "ticker")) {
		alert("Multiple tickers are not allowed in the ticker search.");
		frm.companySearchSymbolVisible.focus();
		err = true
	}

	if(frm.selType.options[frm.selType.selectedIndex].value == ""){
		alert('Please select a search criteria.')
		frm.selType.focus();
		err = true
	}

	if(frm.selType.options[frm.selType.selectedIndex].value == "ticker" || frm.selType.options[frm.selType.selectedIndex].value == "csfbOnly"){
		if(strTemp.length < 1){
			alert('Please enter a Ticker in the Ticker/Company box.')
			frm.companySearchSymbolVisible.focus();
			err = true
		}
	}

	if((frm.selType.value == "company")&&(strTemp.length < 1)){
			alert('Please enter a Company in the Ticker/Company box.')
			frm.companySearchSymbolVisible.focus();
			err = true
	}
	
	if (frm.selType.selectedIndex > 2 && frm.selType.selectedIndex < 5 && frm.selContent.selectedIndex == 0) {
		alert('Please select a search criteria.')
		frm.selContent.focus();
		err = true
	}

	if (err) {	
		if (Action=="submit")
			return false
		else
			return; 
	}

	var strCriteria = ""

	if (frm.selType.options[frm.selType.selectedIndex].value == "csfbOnly")
		strCriteria = "(Credit Suisse) and "

	if (frm.selType.selectedIndex > 4 && frm.selContent.selectedIndex == 0) {
		//	All sectors or All publications
		var strList = ""
		for (var i = 1; i < frm.selContent.length; i++) {
			strList = strList + frm.selContent.options[i].value + ","
		}
		
		frm.selContent.options[0].value = strList
	}
	
	if (strTemp.length >0)
		strCriteria += " (" + strTemp + ")"

	strCriteria += selectedItem (frm.selContent)

	frm.searchCriteria.value = strCriteria
	frm.submit()
}


function getResults (strSubject, strDescr) {
	document.frmGetResult.subjects.value=strSubject
	document.frmGetResult.searchCriteria.value = strDescr
	document.frmGetResult.type.value="getCannedSearchResults"
	
	if (document.frmGetResult.analysts+'' != 'undefined') {
		if (strSubject == "ESL01" || strSubject == "ESL02")
			document.frmGetResult.analysts.value=""
	}
	
	document.frmGetResult.submit()
}

function getDocResults (strCode,type,strDescr,analystCode) {
	switch (type) {
		case 'Company':
			document.frmGetResult.companySearchSymbol.value=strCode
			break;
		case 'Industry':
			document.frmGetResult.industries.value=strCode
			break;
		case 'Subject':
			document.frmGetResult.subjects.value=strCode
			break;
	}

	document.frmGetResult.searchCriteria.value = strDescr
	document.frmGetResult.type.value="getCannedSearchResults"
	if(analystCode+'' != 'undefined')
		document.frmGetResult.analysts.value=analystCode
	document.frmGetResult.submit()
}


function isTickerEntryAllowed() {
	var frm = document.frmSearch
	
	if (frm.selType.options[frm.selType.selectedIndex].value=="")
		frm.selType.selectedIndex = 1

	if (!(frm.selType.options[frm.selType.selectedIndex].value=="ticker" || frm.selType.options[frm.selType.selectedIndex].value=="company" || frm.selType.options[frm.selType.selectedIndex].value=="csfbOnly")) {
		frm.selContent.focus();
		return; }

}

function doProfileEdit (strAction) {
	if (strAction=="getProfileCreatePage")
		document.upForm.alertProfileID.selectedIndex=-1; 
	else
		if (strAction=="getProfileEditPage" || strAction=="getProfileDeletePage") {
			if (document.upForm.alertProfileID.selectedIndex==-1 || document.upForm.alertProfileID.options[document.upForm.alertProfileID.selectedIndex].value=="") {
				alert("Please select a profile.")
				return }
		} 

	if (strAction=="getProfileDeletePage")
		if (!confirm("Are you sure to delete this Profile?"))
			return;

	document.upForm.type.value = strAction
	document.upForm.submit()
}

function ProceedToPage (strType, frm) {
	frm.type.value = strType;
	frm.submit();
}

function LoadProfile() {
	var frm =document.frmSearch
	
	if (frm.alertProfileID.options[frm.alertProfileID.selectedIndex].value=="") {
		if (!confirm("Do you want to clear all the current search criteria?")) {
			frm.alertProfileID.selectedIndex=frm.hidProfileIndex.value
			return; }

		else	{
			setValue ("SecurityDomicile", "")
			setValue ("dateRange", "")
			setValue ("fromDay", "")			
			setValue ("fromMonth", "")			
			setValue ("fromYear", "")
			setValue ("toDay", "")
			setValue ("toMonth", "")
			setValue ("toYear", "")

			if(frm.analysts[0].options+'' == 'undefined')
				setValue ("analysts", "")
			else
				if(frm.analysts+'' != 'undefined')
					for (var i=0; i < frm.analysts.length; i++) {
						setValue ('analysts['+i+']', "") }

			setValue ("dateRange", "")
			setValue ("companySearchSymbol", "")
			setValue ("categories", "")
			setValue ("regions", "")
			setValue ("countries", "")
			setValue ("currencies", "")

			if(frm.industries+'' != 'undefined')
				for (var i=0; i < frm.industries.length; i++) {
					setValue ('industries['+i+']', "") }

			if(frm.subjects+'' != 'undefined')
				for (var i=0; i < frm.subjects.length; i++) {
					setValue ('subjects['+i+']', "") }

			return }
	}
	else
		if (!confirm("Do you want to switch to the selected profile, losing all current search criteria?")) {
			frm.alertProfileID.selectedIndex=frm.hidProfileIndex.value			
			return; }

	mxPostEvent('getProfileData','frmSearch')
}


function setValue(nm, val)
{
  var oldvalue = "xyzzy";
  var frm = document.frmSearch
  
  if(frm+'' != 'undefined') {
    var tkr = eval('frm.' + nm);
    if(tkr+'' != 'undefined') {
      var typ = tkr.type.toLowerCase();
      if(typ.indexOf("select") >= 0) {
        for(i = 0; i < tkr.length; i++)
            tkr.options[i].selected =false;

        for(i = 0; i < tkr.length; i++) {
          if(tkr.options[i].value == val) {
            tkr.options[i].selected = true; 
            break; }
        }
      } else if(typ.indexOf("checkbox") >= 0) {
        tkr.checked = tkr.value == val;
      } else {
        oldvalue = tkr.value;
        tkr.value = val;
      }
      if(val != oldvalue && (nm == "profiletype" || nm == "secdomicile")) {
        needsupdate = true;
      }
    }
  }
  return '';
}



function mailTo(headLine, url){

  var subject = "Research Documents";

  if(navigator.appVersion.indexOf("5.01") > -1 && browser=="ie") {
	var body = headLine.replace(/&/g,'%26') + ':  ' + url.replace(/&/g,'%26');
	}
  else {
	var body = headLine + ': ' + url;
	}
	
  self.location="mailto:?subject="+escape(subject)+"&body="+escape(body)
}


function validateProfilesPage() {
	var frm = document.frmSearch

	if (frm.alertProfileName.value.length==0) {
		alert("Please enter the profile name.")
		return }

	if (parseInt(frm.EmailAlertCount.value)>=4 && frm.alertProfileDestination.checked) {
		alert("You cannot exceed the limit of 4 email alerts.")
		frm.alertProfileDestination.focus()
		return }

	frm.submit();

}

function isAllowed(txt) {	
	if (document.upForm.First.options[document.upForm.First.selectedIndex].value!="getCompanyPageWithTicker")
		document.upForm.First.focus();
}

function AllowTickerEntry() {
	if (document.upForm.First.options[document.upForm.First.selectedIndex].value=="getCompanyPageWithTicker") {
		document.upForm.companyPageSymbol.focus(); }
	else
		document.upForm.companyPageSymbol.value=""
	
}

function resetRapidSearch ()  {
	document.frmSearch.selType.selectedIndex = 0;
	LoadDropDown (document.frmSearch.selType);

	if (browser=='ns') {
		document.captureEvents(Event.KEYDOWN|Event.KEYUP);
		
//		document.onkeydown = keydown;
		document.frmSearch.companySearchSymbolVisible.onkeydown = keydown;
		document.frmSearch.companySearchSymbol.onkeydown = keydown;
	}

}

function loadNecessaryStylesheet(cssPath, sVers)
{
	var str;
	str = "<link rel='stylesheet' href='";
	if (navigator.userAgent.indexOf("Win") != -1) {
		if (navigator.appName == "Netscape") {
			str +=  cssPath + "/ns.css?" + sVers + "'/>";
		} else {
			str += cssPath + "/stylesheet.css?" + sVers + "'/>";
		}
	} else {
		str += cssPath + "/stylesheet.css?" + sVers + "'/>";
	}
	document.write(str);
}
