// file:	/javascript/saff/links.js
//
// needs	/javascript/openFeedback.js
//			/javascript/panel_id.js
//			/javascript/epss_open.js
//			/javascript/gotoMainPage.js

//
// Local variables and setters/getters
//
var	saffLangDir = "en";		// global language directory

function setLangDir(lang) { saffLangDir = lang; }


//
// Debug tools - remove before production!
//
function DEBUG_ALERT(msg) {
//	alert("DEBUG: " + msg);
}


//
// Get GeoId from parameter or page if possible
//
function saffGetGeoId(geoId) {
	if (geoId == null || geoId == "") {
		if (document.hidden_navigation_form) {
			geoId = document.hidden_navigation_form.geo_id.value;
		}
		else {
			DEBUG_ALERT("/javascript/saff/links.js: Error - calling page missing hidden_navigation_form!");
		}
	}
	return geoId;
}


//
// Open pop-up window
//
function openPopUp(winName, url) {
	var winParams = "menubar=yes,scrollbars=yes,toolbar=yes,resizable=yes,top=20,left=20,width=550,height=320";
	if (winName == "feedbackwin") {
		winParams = "menubar=no,resizable=yes,scrollbars=yes,top=10,left=10,width=620,height=560";
	}
	else if (winName == "helpwin") {
		winParams = "menubar=yes,scrollbars=yes,toolbar=yes,resizable=yes,top=20,left=20,width=550,height=320";
	}
	else if (winName == "identify") {
		winParams = "menubar=yes,scrollbars=yes,resizable=yes,top=10,left=10,width=680,height=500";
	}
	else if (winName == "explainwin") {
		winParams = "menubar=yes,scrollbars=yes,toolbar=yes,resizable=yes,top=20,left=20,width=600,height=400"; 
	}
	else if (winName == "epsswin") {
		winParams = "menubar=yes,scrollbars=yes,toolbar=yes,resizable=yes,top=20,left=20,width=600,height=380";
	}
	var newWin = window.open(url, winName, winParams);
	if (window.focus) {
		newWin.focus();
	}
}

function saffShowPage(url) {
	location.href = url;
}

// Global Topic options
var	GT_MAINPAGE		= 0x000001
var	GT_SEARCH		= 0x000002
var	GT_FEEDBACK		= 0x000003
var	GT_FAQS			= 0x000004
var	GT_GLOSSARY		= 0x000005
var	GT_SITEMAP		= 0x000006
var	GT_HELP			= 0x000007
var GT_MASK			= 0x00000F;

// Global Naviation options
var GN_FACTSHEET	= 0x000010;	// these must match IGlobalNavigation
var GN_PEOPLE		= 0x000020;
var GN_HOUSING		= 0x000030;
var GN_BUSINESS		= 0x000040;
var GN_DATASETSINTRO= 0x000050;
var GN_DATASETS		= 0x000060;
var	GN_MAPS			= 0x000070;
var	GN_REFSHELF		= 0x000080;
var	GN_TOOLS		= 0x000090;
var GN_POPULATION	= 0x0000A0;
var GN_MASK			= 0x0000F0;
var GN_SHIFT		= 0x000010;

// Other options
var GN_IS_TOP_LEVEL	= 0x000100;	// enable GN link (pass gnId)

function showSAFFInfo(geoId, content, title, options) {
	var gnId = 0;
	var gtId = 0;
	var watermark = "";
	
	geoId = saffGetGeoId(geoId);
	if (!title || title == null) {
		title = "";
	}
	
	if (options) {
		var gtId = (options & GT_MASK);
		if (gtId) {
			if (gtId == GT_SITEMAP) {
				watermark = "sitemap_watermark.gif";
			}
		}
		var gnBits = (options & GN_MASK);
		var gnId = (gnBits) / GN_SHIFT;
		if (gnId) {
			if (gnBits == GN_POPULATION) {
				watermark = "population_watermark.gif";
			}
			else if (gnBits == GN_FACTSHEET) {
				watermark = "factsheet_watermark.gif";
			}
			else if (gnBits == GN_PEOPLE) {
				watermark = "people_watermark.gif";
			}
			else if (gnBits == GN_HOUSING) {
				watermark = "housing_watermark.gif";
			}
			else if (gnBits == GN_BUSINESS) {
				watermark = "business_watermark.gif";
			}
			else if (gnBits == GN_DATASETSINTRO) {
				watermark = "datasets_watermark.gif";
			}
			else if (gnBits == GN_DATASETS) {
				watermark = "datasets_watermark.gif";
			}
			else if (gnBits == GN_MAPS) {
				watermark = "maps_watermark.gif";
			}
			else if (gnBits == GN_REFSHELF) {
				watermark = "refshelf_watermark.gif";
			}
			else if (gnBits == GN_TOOLS) {
				watermark = "tools_watermark.gif";
			}
		}
	}
	if (gnId > 0 && !(options & GN_IS_TOP_LEVEL)) {
		gnId = 0; // don't show link selected
	}

	if (document.hidden_static_page_form) {
		document.hidden_static_page_form.action = "/jsp/saff/SAFFInfo.jsp";
		document.hidden_static_page_form.geo_id.value = geoId;
		document.hidden_static_page_form._content.value = content;
		document.hidden_static_page_form._gnId.value = gnId;
		document.hidden_static_page_form._gtId.value = gtId;
		document.hidden_static_page_form._watermark.value = watermark;
		document.hidden_static_page_form._title.value = title;
		document.hidden_static_page_form.submit();
	}
	else {
		var url= "/jsp/saff/SAFFInfo.jsp";
		url = url + "?geo_id=" + geoId;
		url = url + "&_content=" + content;
		url = url + "&_gnId=" + gnId;
		url = url + "&_gtId=" + gtId;
		url = url + "&_watermark=" + watermark;
		url = url +  "&_title=" + title;
		DEBUG_ALERT("debug: using direct method instead of embeded form");
		saffShowPage(url);
	}
}

function showServletPage(geoId, servlet) {
	
	geoId = saffGetGeoId(geoId);
	
	if (document.hidden_navigation_form) {
		document.hidden_navigation_form.action = "/servlet/" + servlet;
		document.hidden_navigation_form.geo_id.value = geoId;
		document.hidden_navigation_form.submit();
	}
	else {
		DEBUG_ALERT("debug: using direct method instead of embeded form");
		saffShowPage("/servlet/" + servlet + "?geo_id=" + geoId + "&_lang=" + saffLangDir + "&_sse=on");
	}
}


//
// Global Tools -- public APIs
//
// gtShowMainPage() should call gotoMainPage() after integration into AFF R9.2.
// function gtShowMainPage()	{ gotoMainPage(saffLangDir, ''); }
function gtShowMainPage()	{ saffShowPage("/home/saff/main.html?_lang=" + saffLangDir); }
function gtShowSearch()		{ saffShowPage("/servlet/AdvSearchByKeywordServlet?_lang=" + saffLangDir + "&_sse=on"); }
function gtOpenFeedback()	{ openFeedback(saffLangDir + "/"); }
function gtOpenFAQ()		{ openEPSS(saffLangDir, "faq"); }
function gtOpenGlossary()	{ openEPSS(saffLangDir, "glossary_a"); }
function gtShowSiteMap()	{ showSAFFInfo(null, "gt1_site_map.html", "Site Map", GT_SITEMAP); }
function gtOpenHelp()		{ openEPSS(saffLangDir); }


//
// Global Navigation -- public APIs
//
function gnShowFactSheet(geoId)	{ showServletPage(geoId, "SAFFFacts"); }
function gnShowPeople(geoId)	{ showServletPage(geoId, "SAFFPeople"); }
function gnShowHousing(geoId)	{ showServletPage(geoId, "SAFFHousing"); }
function gnShowBusiness(geoId)	{ showServletPage(geoId, "SAFFBusiness"); }

function gnShowDataSetsIntro(geoId) {
	showSAFFInfo(geoId, "gn6_datasets_intro.html", "Census Overview", GN_DATASETSINTRO + GN_IS_TOP_LEVEL);
}

function gnShowDataSets(geoId) {
	suShowProgramDataSets("DEC");
}

function gnShowMaps(geoId) {
	showSAFFInfo(geoId, "gn7_maps.html", "Maps and Geography", GN_MAPS + GN_IS_TOP_LEVEL);
}

function gnShowReferenceShelf(geoId) {
	showSAFFInfo(geoId, "gn8_refshelf.html", "Reference Shelf", GN_REFSHELF + GN_IS_TOP_LEVEL);
}

function gnShowTools(geoId) {
	showSAFFInfo(geoId, "gn9_tools.html", "Tools", GN_TOOLS + GN_IS_TOP_LEVEL);
}

function gnShowSearchByTerm(geoId) {
	var searchterms = document.adv_search_form.searchterms.value;
	if (searchterms == "") {
		alert("Enter a search term. Click Go.");
		document.adv_search_form.searchterms.focus();
	}
	else if (searchterms == "*") {
		alert("Enter at least a word or a partial word. Click Go.");
		document.adv_search_form.searchterms.focus();
	}
	else if (document.adv_search_form.searchby[0].checked) {
		saffShowPage("/servlet/AdvSearchByKeywordServlet?_command=getResults&_keyword="
					+ escape(searchterms)
					+ "&_method=set&_programYear=&_synonyms=no&_lang=" + saffLangDir + "&_sse=on");
	}
	else {
		saffShowPage("/servlet/AdvSearchByPlacenameServlet?_command=getPlacenames&_keyword="
					+ escape(searchterms)
					+ "&_method=set&_programYear=&_placename=&_geoBucketId=&_treeId=&_state=&_lang=" + saffLangDir + "&_sse=on");
	}
}

function gnShowSearchByAddress(geoId) {
	saffShowPage("/servlet/AGSGeoAddressServlet?_programYear=50&_treeId=420&_lang=" + saffLangDir + "&_sse=on");
}

//
// Topic Pages -- public APIs
//
function tpShowBasicCounts(geoId)		{ showSAFFInfo(geoId, "tp1_basic_counts.html",      "Basic Counts", GN_PEOPLE); }
function tpShowAging(geoId)				{ showSAFFInfo(geoId, "tp2_aging.html",             "Aging", GN_PEOPLE); }
function tpShowGender(geoId)			{ showSAFFInfo(geoId, "tp3_gender.html",            "Gender", GN_PEOPLE); }
function tpShowDisability(geoId)		{ showSAFFInfo(geoId, "tp4_disability.html",        "Disability", GN_PEOPLE); }
function tpShowEducation(geoId)			{ showSAFFInfo(geoId, "tp5_education.html",         "Education", GN_PEOPLE); }
function tpShowIncomeEmployment(geoId)	{ showSAFFInfo(geoId, "tp6_income_employment.html", "Income and Employment", GN_PEOPLE); }
function tpShowOriginsLanguage(geoId)	{ showSAFFInfo(geoId, "tp7_origins_language.html",  "Origin and Language", GN_PEOPLE); }
function tpShowPoverty(geoId)			{ showSAFFInfo(geoId, "tp8_poverty.html",           "Poverty", GN_PEOPLE); }
function tpShowRaceEthnicity(geoId)		{ showSAFFInfo(geoId, "tp9_race_ethnicity.html",    "Race and Ethnicity", GN_PEOPLE); }
function tpShowRelationships(geoId)		{ showSAFFInfo(geoId, "tp10_relationships.html",    "Relationships", GN_PEOPLE); }
function tpShowTransportation(geoId)	{ showSAFFInfo(geoId, "tp11_transportation.html",   "Transportation", GN_PEOPLE); }
function tpShowVeterans(geoId)			{ showSAFFInfo(geoId, "tp12_veterans.html",         "Veterans", GN_PEOPLE); }
function tpShowHousingPhysical(geoId)	{ showSAFFInfo(geoId, "tp13_housing_physical.html", "Physical Characteristics", GN_HOUSING); }
function tpShowHousingFinancial(geoId)	{ showSAFFInfo(geoId, "tp14_housing_financial.html","Financial Characteristics", GN_HOUSING); }
function tpShowForeignTrade(geoId)		{ showSAFFInfo(geoId, "tp15_foreign_trade.html",    "Foreign Trade", GN_BUSINESS); }
function tpShowGovernment(geoId)		{ showSAFFInfo(geoId, "tp16_government.html",       "Government", GN_BUSINESS); }
function tpShowBusinessIndustry(geoId)	{ showSAFFInfo(geoId, "tp17_business_industry.html","Business And Industry", GN_BUSINESS); }


//
// Splash Pages -- public APIs
//
function spShowACS(geoId)				{ showSAFFInfo(geoId, "sp1_acs.html",          "American Community Survey", 0); }
function spShowEconomic(geoId)			{ showSAFFInfo(geoId, "sp2_economic.html",     "Economic Census", 0); }
function spShowPopEst(geoId)			{ showSAFFInfo(geoId, "sp3_pop_est.html",      "Population Estimates Program", 0); }
function spShowDecennial(geoId)			{ showSAFFInfo(geoId, "sp4_decennial.html",    "Decennial Census", 0); }
function spShowDecennialSF1(geoId)		{ showSAFFInfo(geoId, "sp4_decennial_sf1.html","Summary File 1 (SF 1)", 0); }
function spShowDecennialSF2(geoId)		{ showSAFFInfo(geoId, "sp4_decennial_sf2.html","Summary File 2 (SF 2)", 0); }
function spShowDecennialSF3(geoId)		{ showSAFFInfo(geoId, "sp4_decennial_sf3.html","Summary File 3 (SF 3)", 0); }
function spShowDecennialSF4(geoId)		{ showSAFFInfo(geoId, "sp4_decennial_sf4.html","Summary File 4 (SF 4)", 0); }
function spShowDecennialCD108(geoId)	{ showSAFFInfo(geoId, "sp4_decennial_cd108.html","108th Congressional District Summary Files", 0); }
function spShowDecennialPL(geoId)		{ showSAFFInfo(geoId, "sp4_decennial_pl.html", "Public Law 94-171", 0); }
function spShowDecennialAIAN(geoId)		{ showSAFFInfo(geoId, "sp4_decennial_aian.html","American Indian and Alaska Native Summary File", 0); }
function spShowIslandAreas(geoId)		{ showSAFFInfo(geoId, "sp5_island_areas.html", "Island Areas", 0); }
function spShowGov(geoId)				{ showSAFFInfo(geoId, "sp6_gov.html",          "Census of Governments", 0); }
function spShowPopProj(geoId)			{ showSAFFInfo(geoId, "sp7_pop_proj.html",     "Population Projections", 0); }
function spShowTableMapFormats(geoId)	{ showSAFFInfo(geoId, "sp8_table_map_formats.html", "Table and Map Formats", 0); }

//
// Supporting Pages -- public APIs
//
function suShowWhatsNew()				{ showSAFFInfo(null, "su1_whats_new.html", "What's New", 0); }
function suShowNewFeatures()			{ showSAFFInfo(null, "su2_new_features.html", "New Features and a New Look", 0); }
function suShowInTheSpotlight()			{ showSAFFInfo(null, "su3_spotlight.html", "In the Spotlight", 0); }
function suShowReleaseSchedule()		{ saffShowPage("http://www.census.gov/population/www/censusdata/c2kproducts.html"); }
function suShowPuertoRico()				{ gotoMainPage("es", ''); }
function suShowKidsCorner()				{ saffShowPage("/home/en/kids/kids.html"); }
function suShowAIAN()					{ saffShowPage("/home/aian/index.html"); }
function suShowConfidentiality()		{ showSAFFInfo(null, "su5_confidentiality.html", "Confidentiality", 0); }
function suShowCompatibility()			{ openEPSS(saffLangDir, "browser_note"); }
function suShowBasicFacts()				{ saffShowPage("/servlet/BasicFactsServlet?_lang=en&_classic="); } // old main page
function suShowDataSet(dsName) { 
	if (document.breadcrumbDataSetsHref) {
		document.breadcrumbDataSetsHref.action = "/servlet/DatasetMainPageServlet";
		document.breadcrumbDataSetsHref._ds_name.value = dsName;
		document.breadcrumbDataSetsHref.submit();
	}
	else {
		DEBUG_ALERT("suShowDataSets using direct method instead of embeded form");
		saffShowPage("/servlet/DatasetMainPageServlet?_ds_name=" + dsName + "&_lang=en");
	}
}
function suShowProgramDataSets(pgm) {
	if (document.breadcrumbProgramHref) {
		document.breadcrumbProgramHref.action = "/servlet/DatasetMainPageServlet";
		document.breadcrumbProgramHref._program.value = pgm;
		document.breadcrumbProgramHref.submit();
	}
	else {
		DEBUG_ALERT("suShowProgrmaDataSets using direct method instead of embeded form");
		saffShowPage("/servlet/DatasetMainPageServlet?_program=" + pgm + "&_lang=en");
	}
}


//
// Functions stored in metadata or maps to show in pop-up windows
//

//function openGlossary(lang, term) { <-- as spec'ed, but currently metadata is not returning language
function openGlossary(term) {
	if (!term || term == "") {
		term = "glossary_a.html"
	}
	openPopUp("epsswin", "/home/" + saffLangDir + "/epss/" + term);
}

function openHelp(lang, term) {
	if (!term || term == "") {
		term = "help.html"
	}
	openPopUp("helpwin", "/home/" + saffLangDir + "/epss/" + term);
}

// Build Reference Map URL
function getRMURL(geoId, bucketId, treeId) {
	if (!treeId) {
		treeId = '420';
	}
	if (!bucketId) {
		bucketId = '50';
	}
	return "/servlet/MapItDrawServlet?geo_id=" + geoId + "&_bucket_id=" + bucketId + "&tree_id=" + treeId + "&context=saff&_lang=en&_sse=on";
}

function openRM(geoId, bucketId, treeId) 	{ openPopUp("identify", getRMURL(geoId, bucketId, treeId)); }


//
// Functions to invoke AFF result pages via deep-linking.
//

function showDT(ds, geoId, table) {
	geoId = saffGetGeoId(geoId);
	if (document.hidden_dt_form) {
		document.hidden_dt_form.action = "/servlet/DTTable";
		document.hidden_dt_form.ds_name.value = ds;
		document.hidden_dt_form.geo_id.value = geoId;
		document.hidden_dt_form.mt_name.value = table;
		document.hidden_dt_form.submit();
	}
	else {
		DEBUG_ALERT("debug: using direct method instead of embeded form");
		saffShowPage("/servlet/DTTable?ds_name=" + ds + "&geo_id=" + geoId + "&mt_name=" + table + "&_sse=on&_lang=en");
	}
}

function showGCT(ds, geoId,boxhead, format) {
	geoId = saffGetGeoId(geoId);
	if (document.hidden_gct_form) {
		document.hidden_gct_form.action = "/servlet/GCTTable";
		document.hidden_gct_form.ds_name.value = ds;
		document.hidden_gct_form.geo_id.value = geoId;
		document.hidden_gct_form._box_head_nbr.value = boxhead;
		document.hidden_gct_form.format.value = format;
		document.hidden_gct_form.submit();
	}
	else {
		DEBUG_ALERT("debug: using direct method instead of embeded form");
		saffShowPage("/servlet/GCTTable?ds_name=" + ds + "&geo_id=" + geoId + "&_box_head_nbr=" + boxhead + "&format=" + format + "&_sse=on");
	}
}

function showQT(ds, geoId, qrName) {
	geoId = saffGetGeoId(geoId);
	// if puerto rico, and if dp2, change to dp2pr.
	if (geoId.indexOf("US72") >= 0 && qrName.indexOf("_DP2") >= 0) {
		qrName = qrName + "PR";
	}
	if (document.hidden_qt_form) {
		document.hidden_qt_form.action = "/servlet/QTTable";
		document.hidden_qt_form.ds_name.value = ds;
		document.hidden_qt_form.geo_id.value = geoId;
		document.hidden_qt_form.qr_name.value = qrName;
		document.hidden_qt_form.submit();
	}
	else {
		DEBUG_ALERT("debug: using direct method instead of embeded form");
		saffShowPage("/servlet/QTTable?ds_name=" + ds + "&geo_id=" + geoId + "&qr_name=" + qrName + "&_sse=on&_lang=en");
	}
}

function showTM(ds, geoId, byGsl, mapId) {
	geoId = saffGetGeoId(geoId);
	if (document.hidden_tm_form) {
		document.hidden_tm_form.action = "/servlet/ThematicMapFramesetServlet";
		document.hidden_tm_form.ds_name.value = ds;
		document.hidden_tm_form.geo_id.value = geoId;
		document.hidden_tm_form._dBy.value = byGsl;
		document.hidden_tm_form.tm_name.value = mapId;
		document.hidden_tm_form.submit();
	}
	else {
		DEBUG_ALERT("debug: using direct method instead of embeded form");
		saffShowPage("/servlet/ThematicMapFramesetServlet?ds_name=" + ds + "&geo_id=" + geoId + "&_dBy=" + byGsl + "&tm_name=" + mapId + "&_MapEvent=displayBy&_sse=on");
	}
}

function ShowTM(ds, geoId, byGsl, mapId) { // Fact Sheet Interface
	showTM(ds, geoId, byGsl, mapId);
}

function showRM(geoId, bucketId, treeId) {
	saffShowPage(getRMURL(geoId, bucketId, treeId));
}

// utility functions
function isEnterKey(event) {
	return ns4 ? (event.which == 13) : (event.keyCode == 13);
}
