﻿function SearchReplace()
{
	var searchText = document.getElementById('txtSearchText');
	var replaceText = searchText.value;
	
	if(replaceText.indexOf('<') != -1)
	{
		replaceText = replaceText.replace(/</gi, "");
	}
	if(replaceText.indexOf('>') != -1)
	{
		replaceText = replaceText.replace(/>/gi, "");
	}
	searchText.value = replaceText;
	document.forms[0].btnSearch.click();     
}

function SearchInternalReplace()
{
	var searchText = document.getElementById('ctl00_txtSearchText');
	var replaceText = searchText.value;
	
	if(replaceText.indexOf('<') != -1)
	{
		replaceText = replaceText.replace(/</gi, "");
	}
	if(replaceText.indexOf('>') != -1)
	{
		replaceText = replaceText.replace(/>/gi, "");
	}
	searchText.value = replaceText;
	document.forms[0].ctl00_btnSearch.click();     
}

function SearchFacilityReplace() {
    var searchText = document.getElementById('txtZipCity');
    var replaceText = searchText.value;

    if (replaceText.indexOf('<') != -1) replaceText = replaceText.replace(/</gi, "");
    if (replaceText.indexOf('>') != -1) replaceText = replaceText.replace(/>/gi, "");

    if (replaceText.toLowerCase() == 'name, city or zip') {
        alert('Please enter a facility name, city or zip code');
        return false;
    }
    else {
        searchText.value = replaceText;
        document.forms[0].btnSearchFacility.click();
    }
    return true;
}

function ShowHide(title, id,expand)
{
	var element = document.getElementById(id);
	if(element.style.display == "none")
	{
		element.style.display = "block";
		title.className = "facility-type-title-open";
		document.getElementById(expand).innerHTML = "Minimize results"
	}
	else
	{
		element.style.display = "none";
		title.className = "facility-type-title-close";
		document.getElementById(expand).innerHTML = "Expand results"
	}
}

function ltrim(s) { return s.replace( /^\s*/, "" ); }
function rtrim(s) { return s.replace( /\s*$/, "" ); }
function trim(s){ return rtrim(ltrim(s)); }

function ValidateZipCounty()
{
	var zip = trim(document.getElementById('ctl00_ContentPlaceHolder2_FacilitySearch1_txtZip').value);
	var county = trim(document.getElementById('ctl00_ContentPlaceHolder2_FacilitySearch1_drpCounty').value);
	var facility = trim(document.getElementById('ctl00_ContentPlaceHolder2_FacilitySearch1_facilityName').value);
	var city = trim(document.getElementById('ctl00_ContentPlaceHolder2_FacilitySearch1_drpCity').value);
	var facilityType = trim(document.getElementById('ctl00_ContentPlaceHolder2_FacilitySearch1_lbxType').value);
	var service = trim(document.getElementById('ctl00_ContentPlaceHolder2_FacilitySearch1_lbxService').value);
	var languajes = trim(document.getElementById('ctl00_ContentPlaceHolder2_FacilitySearch1_lbxLanguages').value);
	var units = trim(document.getElementById('ctl00_ContentPlaceHolder2_FacilitySearch1_lbxUnits').value);
	var populations = trim(document.getElementById('ctl00_ContentPlaceHolder2_FacilitySearch1_lbxPopulations').value);	
	if (zip == "" && county == "" && facility == "" && city == "" && facilityType == "" && service == "" && languajes == "" && units == "" && populations == "") {
		alert('Please enter a zip code or facility name, or select at least one of the options listed in the other fields.');
		return false;
	}else {
	    if (county != "")
	        return ValidateCounty();
	}
	return true;
	
}
function ValidateCounty() {
    county = document.getElementById('ctl00_ContentPlaceHolder2_FacilitySearch1_drpCounty');
    count = 0;
    for (j = 0; j < county.length; j++) {
        if (county.options[j].selected)
            count++;
    }
    if (count > 4) {
        alert('You can select up to four counties');
        return false;
    }
    else {
        return ValidateCity();
    }
}
function ValidateCity() {
    city = document.getElementById('ctl00_ContentPlaceHolder2_FacilitySearch1_drpCity');
    count = 0;
    for (j = 0; j < city.length; j++) {
        if (city.options[j].selected)
            count++;
    }
    if (count > 4) {
        alert('You can select up to four cities');
        return false;
    }
    else
        return true;
}

function ShowHideSearchCriterias(title, id, expand) {
    var element = document.getElementById(id);
    if (element.style.display == "none") {
        element.style.display = "block";
        title.className = "search-criteria-title-open";
        document.getElementById(expand).innerHTML = "Minimize Search Criteria"
    }
    else {
        element.style.display = "none";
        title.className = "search-criteria-title-close";
        document.getElementById(expand).innerHTML = "Expand Search Criteria"
    }
}

$(document).ready(function() {
	
	var numH = $('input#hdnAdsH').val();
	var numV = $('input#hdnAdsV').val();
	
	var numChildH = $(".auto-Horizontal .jCarouselLite ul").children("li").length;
	var numChildV = $(".auto-Vertical .jCarouselLite ul").children("li").length;
	
	var autoScrollH = (numChildH > numH) ? 6500 : 0;
	var autoScrollV = (numChildV > numV) ? 6500 : 0;
	
	if(autoScrollH > 0)
	{
		$(".auto-Horizontal .jCarouselLite").jCarouselLite({
			visible: parseInt(numH, 10),
			auto: autoScrollH,
			speed: 1000
		});
	}
	
	if(autoScrollV > 0)
	{
		$(".auto-Vertical .jCarouselLite").jCarouselLite({
			visible: parseInt(numV, 10),
			vertical: true,
			auto: autoScrollV,
			speed: 1000
		});
	}
});
