/* $Header: /u01/app/cvs/src_wds/projects/WDS/htdocs/iws/WebRoot/scripts/search/SearchHelper.js,v 1.46 2009/06/12 01:32:27 mlevin Exp $ */ function setGlobalFlag(dropDownObj) { document.searchForm.go.value = globalOnly; } function submitForm() { if (checkValues()) { document.searchForm.submit(); } return false; } function submitAdvForm() { if (advCheckValues()) { document.searchForm.submit(); } return false; } function toggleSection(sectionId) { var section = document.getElementById("a" + sectionId); var linkObj = document.getElementById("l" + sectionId); if (section.style.display == "none") { setExpanded(section, linkObj, sectionId, true); } else { setExpanded(section, linkObj, sectionId, false); } } function setExpanded(section, linkObj, sectionId, expanded) { linkObj.innerHTML = (expanded ? "Hide" : "Show") + " Results"; section.style.display = expanded ? "block" : "none"; setLinks(sectionId, expanded); } function setLinks(sectionId, expanded) { var navLinks = document.getElementsByTagName("a"); for (var i = 0; i < navLinks.length; i++) { var obj = navLinks[i]; if (obj.className == "navLink") { var regex = new RegExp("a=" + sectionId + "\\|[ce]"); var replacement = "a=" + sectionId + "|" + (expanded ? "e" : "c"); obj.href = obj.href.replace(regex, replacement); } } } function setGlobal(newVal) { var regex = new RegExp("g=" + !newVal); var replacement = "g=" + newVal; window.location.href = window.location.href.replace(regex, replacement); } function toggleExpand() { if (allExpanded) { collapseAll(); allExpanded = false; document.getElementById("toggleExpandLink").innerHTML = "Expand All Sections"; } else { expandAll(); allExpanded = true; document.getElementById("toggleExpandLink").innerHTML = "Collapse All Sections"; } setCookie("allExpanded", allExpanded ? 1 : 0); } function toggleDescriptions() { if (descriptionsShown) { descriptionsShown = false; document.getElementById("toggleDescriptionsLink").innerHTML = "Show Descriptions"; } else { descriptionsShown = true; document.getElementById("toggleDescriptionsLink").innerHTML = "Hide Descriptions"; } setCookie("descriptionsShown", descriptionsShown ? 1 : 0); var paragraphs = document.getElementsByTagName("p"); for (var i = 0; i < paragraphs.length; i++) { var obj = paragraphs[i]; if (obj.className == "short_desc") { obj.style.display = descriptionsShown ? "block" : "none"; } } } function getParameterValue(urlVarName) { //divide the URL in half at the '?' var urlHalves = String(document.location).split("?"); var urlVarValue = ""; if (urlHalves[1]) { //load all the name/value pairs into an array var urlVars = urlHalves[1].split("&"); //loop over the list, and find the specified url variable for (i = 0; i <= (urlVars.length); i++) { if (urlVars[i]) { //load the name/value pair into an array var urlVarPair = urlVars[i].split("="); if (urlVarPair[0] && urlVarPair[0] == urlVarName) { //I found a variable that matches, load it's value into the return variable urlVarValue = urlVarPair[1]; } } } } return urlVarValue; } function getParameterValues(urlVarName) { //divide the URL in half at the '?' var urlHalves = String(document.location).split("?"); var urlVarValue = ""; if (urlHalves[1]) { //load all the name/value pairs into an array var urlVars = urlHalves[1].split("&"); //loop over the list, and find the specified url variable for (i = 0; i <= (urlVars.length); i++) { if (urlVars[i]) { //load the name/value pair into an array var urlVarPair = urlVars[i].split("="); if (urlVarPair[0] && urlVarPair[0] == urlVarName) { //I found a variable that matches, load it's value into the return variable if (urlVarPair[1].trim() != "") { urlVarValue = urlVarPair[1].replace("\\|.*$", "") + "-" + urlVarValue; } } } } } return urlVarValue; } function toggleCountryRegion() { var regionDiv = document.getElementById("regionLayer"); var countryDiv = document.getElementById("countryLayer"); if (document.searchForm.rc[0].checked) { //region selected regionDiv.style.display = "block"; countryDiv.style.display = "none"; document.searchForm.c.selectedIndex = 0; } else { regionDiv.style.display = "none"; countryDiv.style.display = "block"; document.searchForm.r.selectedIndex = 0; } } function checkValues() { document.searchForm.g.value=true; var noRegion = document.searchForm.rc[0].checked && document.searchForm.r.selectedIndex == 0; var noCountry = document.searchForm.rc[1].checked && document.searchForm.c.selectedIndex == 0; var noKeywords = document.searchForm.k.value.trim() == "" || hasOnlyStopWords(document.searchForm.k.value); var noSchool = document.searchForm.s.selectedIndex == 0; var noActivity = document.searchForm.a.selectedIndex == 0; var coursesSelected = document.searchForm.a.selectedIndex == 1; var researchSelected = document.searchForm.a.selectedIndex == 9; var allDefaults = (noRegion || noCountry) && noKeywords && noSchool && noActivity; var tooMany = (noRegion || noCountry) && noKeywords && noSchool && (coursesSelected || researchSelected); var facPlusGlobal = document.searchForm.a.selectedIndex == 5 && document.searchForm.rc[0].checked && document.searchForm.r.options[9].selected; if(document.searchForm.rc[0].checked && document.searchForm.r.selectedIndex == 9){ document.searchForm.go.value=true; } if (document.searchForm.rc[0].checked && (document.searchForm.r.options[9].selected || document.searchForm.r.options[0].selected)) { document.searchForm.g.value=true; } else { document.searchForm.g.value=false; } if (document.searchForm.rc[1].checked && document.searchForm.c.selectedIndex != 0) { document.searchForm.g.value=false; } else if (document.searchForm.rc[1].checked && document.searchForm.c.selectedIndex == 0) { document.searchForm.g.value=true; } if (allDefaults || tooMany || facPlusGlobal) { alert("The search you have requested will return too many results. Please refine your search."); return false; } return true; } function advCheckValues() { var i, isActivityType; isActivityType = 0; var noRegion = document.searchForm.rc[0].checked && (document.searchForm.r.value.trim() == "" || document.searchForm.r.selectedIndex == -1); var noCountry = document.searchForm.rc[1].checked && (document.searchForm.c.value.trim() == "" || document.searchForm.c.selectedIndex == -1); var noKeywords = document.searchForm.k.value.trim() == "" || hasOnlyStopWords(document.searchForm.k.value); var noSchool = document.searchForm.s.selectedIndex == -1 || document.searchForm.s.value.trim() == ""; var facPlusGlobal = document.searchForm.a[5].checked && document.searchForm.rc[0].checked && document.searchForm.r.options[9].selected; var regionCounter = 0; var allRegions = false; var countryCounter = 0; var allCountries = false; var globalRegionSelected = false; for (var i = 1; i < document.searchForm.r.length; i++) { if (document.searchForm.r.options[i].selected) { regionCounter++; if(i == 9){ globalRegionSelected = true; } } } if ((regionCounter == document.searchForm.r.length - 1 || regionCounter == 0) && document.searchForm.rc[0].checked) { allRegions = true; } if (document.searchForm.rc[0].checked) { if (allRegions || globalRegionSelected) { document.searchForm.g.value = true; } else { document.searchForm.g.value = false; } if (regionCounter == 1 && globalRegionSelected) { document.searchForm.go.value = true; } else { document.searchForm.go.value = false; } } if (document.searchForm.rc[1].checked && document.searchForm.c.selectedIndex != 0) { document.searchForm.g.value = false; } else if (document.searchForm.rc[1].checked && document.searchForm.c.selectedIndex == 0) { document.searchForm.g.value=true; } for (var i = 1; i < document.searchForm.c.length; i++) { if (document.searchForm.c.options[i].selected) { countryCounter++; } } if (countryCounter == document.searchForm.c.length - 1) { allCountries = true; } for (i = 0; i < 11; ++i) { if (document.searchForm.a[i].checked) { isActivityType++; } } var facPlusGlobal = document.searchForm.a[4].checked && isActivityType == 1 && document.searchForm.rc[0].checked && document.searchForm.r.options[9].selected; var noActivity = isActivityType == 0; var coursesSelected = document.searchForm.a[0].checked == true; var researchSelected = document.searchForm.a[8].checked == true; var coursesOnly = coursesSelected && isActivityType == 1 && (document.searchForm.r.value.trim() == "" || document.searchForm.r.selectedIndex == -1) && (document.searchForm.c.value.trim() == "" || document.searchForm.c.selectedIndex == -1) && noKeywords && noSchool; var allDefaults = (noRegion || noCountry) && noKeywords && noSchool && (isActivityType > 10) && !globalRegionSelected; if (allDefaults) { alert("The search you have requested will return too many results. Please limit your search."); return false; } //var tooMany = (noRegion || noCountry || allRegions || allCountries) && noKeywords && noSchool && (coursesSelected || researchSelected); // now allow global course search var tooMany = (noRegion || noCountry || allRegions || allCountries) && noKeywords && noSchool && researchSelected && !globalRegionSelected; if (tooMany || facPlusGlobal || coursesOnly) { alert("The search you have requested will return too many results. Please refine your search."); return false; } if (!checkMultiList(document.searchForm.c)) { return false; } return true; } function hasOnlyStopWords(keywordString) { var keywords = keywordString.trim().toLowerCase().replace(/[^a-z ]/g, "").split(" "); keywords.removeItems(stopWords); return keywords.length == 0; } // http://www.somacon.com/p355.php String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, ""); }; // http://webdevel.blogspot.com/2003/07/javascript-remove-items-from-array.html Array.prototype.removeItems = function (itemsToRemove) { if (!/Array/.test(itemsToRemove.constructor)) { itemsToRemove = [itemsToRemove]; } var j; for (var i = 0; i < itemsToRemove.length; i++) { j = 0; while (j < this.length) { if (this[j] == itemsToRemove[i]) { this.splice(j, 1); } else { j++; } } } }; function prefillBasicSearch() { var region_country = getParameterValue("rc"); if (region_country.toLowerCase() == "region") { document.searchForm.rc[0].checked = true; toggleCountryRegion(); var region = getParameterValue("r"); for (var i = 0; i < document.searchForm.r.length; i++) { if (document.searchForm.r[i].value.toUpperCase() == region.toUpperCase()) { document.searchForm.r.selectedIndex = i; } } } else { if (region_country.toLowerCase() == "country") { document.searchForm.rc[1].checked = true; toggleCountryRegion(); var country = getParameterValue("c"); for (var i = 0; i < document.searchForm.c.length; i++) { if (document.searchForm.c[i].value.toUpperCase() == country.toUpperCase()) { document.searchForm.c.selectedIndex = i; } } } } var activity = getParameterValue("a"); if (activity != null) { for (var i = 0; i < document.searchForm.a.length; i++) { if (document.searchForm.a[i].value == activity) { document.searchForm.a.selectedIndex = i; } } } var school = getParameterValue("s"); if (school != null) { for (var i = 0; i < document.searchForm.s.length; i++) { if (document.searchForm.s[i].value.toUpperCase() == school.toUpperCase()) { document.searchForm.s.selectedIndex = i; } } } } if (!Array.prototype.contains) { Array.prototype.contains = function (obj) { var len = this.length; for (var i = 0; i < len; i++) { if (this[i] === obj) { return true; } } return false; }; } function prefillAdvanceSearch() { var regionArray = getParameterValues("r").split("-"); var countryArray = getParameterValues("c").split("-"); if(regionArray == "" && countryArray == "") { //initial value document.searchForm.r.options[0].selected = true; } if (regionArray != null && regionArray.length > 0 && regionArray != "") { document.searchForm.rc[0].checked = true; toggleCountryRegion(); if (regionArray == null || regionArray.length == 0 || regionArray == "") { document.searchForm.r.options[0].selected = true; } for (var i = 1; i < document.searchForm.r.length; i++) { if (i == 9) { if (getParameterValues("g") == "true" || getParameterValues("go") == "true") { document.searchForm.r.options[9].selected = true; } else { document.searchForm.r.options[9].selected = false; } } else { if (regionArray.contains(document.searchForm.r[i].value)) { document.searchForm.r.options[i].selected = true; } } } } else { if (countryArray != null && countryArray.length > 0 && countryArray != "") { var countryArray = getParameterValues("c").split("-"); document.searchForm.rc[1].checked = true; toggleCountryRegion(); if (countryArray == null || countryArray.length == 0 || countryArray == "") { document.searchForm.c.options[0].selected = true; } for (var i = 1; i < document.searchForm.c.length; i++) { if (countryArray.contains(document.searchForm.c[i].value)) { document.searchForm.c.options[i].selected = true; } } } } var activityArray = getParameterValues("a").split("-"); var parsedActivityArray = new Array(); var regex = new RegExp("\\|.*$"); for (var i = 0; i < activityArray.length; i++) { var activityType = activityArray[i]; if (activityType != null) { activityType = activityType.replace(regex, ""); parsedActivityArray.push(activityType); } } var isActivityArrayEmpty = false; if (parsedActivityArray == null || parsedActivityArray.length == 0 || parsedActivityArray == "") { isActivityArrayEmpty = true; } for (var i = 0; i < document.searchForm.a.length; i++) { if (parsedActivityArray.contains(document.searchForm.a[i].value) || isActivityArrayEmpty) { document.searchForm.a[i].checked = true; } } var schoolArray = getParameterValues("s").split("-"); if (schoolArray == null || schoolArray.length == 0 || schoolArray == "") { document.searchForm.s.options[0].selected = true; } for (var i = 1; i < document.searchForm.s.length; i++) { if (schoolArray.contains(document.searchForm.s[i].value)) { document.searchForm.s.options[i].selected = true; } } if (getParameterValues("g") == "true" || getParameterValues("go") == "true") { document.searchForm.r.options[9].selected = true; } else { document.searchForm.r.options[9].selected = false; } } function prefillAddUpdateSearch() { document.searchForm.r.options[0].selected = true; for (var i = 1; i < document.searchForm.a.length; i++) { document.searchForm.a[i].checked = true; } document.searchForm.a[0].checked = false; document.searchForm.a[4].checked = false; document.searchForm.a[0].disabled = true; document.searchForm.a[4].disabled = true; document.searchForm.s.options[0].selected = true; } // from http://techpatterns.com/downloads/javascript_cookies.php function setCookie(name, value, expires, path, domain, secure) { var today = new Date(); today.setTime(today.getTime()); if (expires) { expires = expires * 1000 * 60 * 60 * 24; } var expires_date = new Date(today.getTime() + (expires)); document.cookie = name + "=" + escape(value) + ((expires) ? ";expires=" + expires_date.toGMTString() : "") + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ((secure) ? ";secure" : ""); } // from http://techpatterns.com/downloads/javascript_cookies.php function getCookie(check_name) { var a_all_cookies = document.cookie.split(";"); var a_temp_cookie = ""; var cookie_name = ""; var cookie_value = ""; var b_cookie_found = false; for (i = 0; i < a_all_cookies.length; i++) { a_temp_cookie = a_all_cookies[i].split("="); cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, ""); if (cookie_name == check_name) { b_cookie_found = true; if (a_temp_cookie.length > 1) { cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, "")); } return cookie_value; break; } a_temp_cookie = null; cookie_name = ""; } if (!b_cookie_found) { return null; } } function checkMultiList(mList) { var counter = 0; for (i = 0; i < mList.length; i++) { if (mList[i].selected == true) { counter++; } } // If the counter is greater than 10, display an alert message. if (counter > 10) { if (mList.id == "c") { alert("Please do not select more than 10 countries."); } return false; } return true; } function chkSchoolList() { var counter = 0; for (var i = 1; i < document.searchForm.s.length; i++) { if (document.searchForm.s.options[i].selected) { counter++; } } if (counter == document.searchForm.s.length - 1) { document.searchForm.s.options[0].selected = true; } else if (counter == 0) { // leave as is } else { document.searchForm.s.options[0].selected = false; } }