function WaterMarkBusiness(txt, evt) {
	var defaultText = "Business Name, Type, URL, Phone";
	if (txt.value.length == 0 && evt.type == "blur") {
		txt.style.color = "gray";
		txt.value = defaultText;
	}

	if (txt.value == defaultText && evt.type == "focus") {
		txt.style.color = "black";
		txt.value = "";
	}
}

function WaterMarkPostal(txt, evt) {
	var defaultText = "City, State or Postal Code";
	if (txt.value.length == 0 && evt.type == "blur") {
		txt.style.color = "gray";
		txt.value = defaultText;
	}

	if (txt.value == defaultText && evt.type == "focus") {
		txt.style.color = "black";
		txt.value = "";
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function getUrlEncode(location) {
	location = trim(location);
	location = location.replace("/", " ");
	location = location.replace("\\", " ");
	location = encodeURI(location);
	location = location.replace(/%20/gi, "+");
	return location;
}

function searchRedirect() {

	if (document.getElementById("sloc") && document.getElementById("sloc").checked && baseURL != 'nashville.app.bbb.org') {
		searchRedirect3();
		return;
	}

	var searchTob = '';
	var stype = ( document.getElementById("stype") ? document.getElementById("stype").value : false );
	if( document.getElementById('tob') ) searchTob = document.getElementById("tob").value;
	var loc = trim($('txtSearchlocation').get('value'));

	if ( typeof tlist != 'undefined' && tlist==null ) stype = 'name';
	if ( typeof tlist != 'undefined' && !stype ) stype = 'type';
	
	if ( (typeof tlist != 'undefined' && tlist!=null) || ( typeof tlist == 'undefined' && 'name' != stype ) ) {
		if (loc == '') {
			showPopUp('dialog');
			$('dialog').setStyle('margin-top', (200));
			return;
		}else {
			searchTob = tlist;
		}
		if (!stype)	stype = 'type';
	}

	var searchParams;
	var searchIndex = document.getElementById("txtSearchbox").value.substr(0,13);
	if (searchIndex == "Business Name")
	searchIndex = "";
	var searchLocation = document.getElementById("txtSearchlocation").value.substr(0,5);
	if (searchLocation == "City,") searchLocation = "";
	else searchLocation = document.getElementById("txtSearchlocation").value;

	var isAccreditedSearch = document.getElementById("chbAccredited").checked;
	if (document.getElementById("fin")!=undefined && document.getElementById('fsrc').value === '1') {
		isAccreditedSearch = document.getElementById("fin").checked;
	}
	if (searchIndex == "")
	return;
	searchIndex = document.getElementById("txtSearchbox").value;
	searchIndex = trim(searchIndex);

	if (stype == 'name' && searchTob !='') searchIndex = trim(searchTob);

	if (stype != 'tob.location') {
		if (stype != 'type') {
			searchIndex = searchIndex.replace(/,/gi, "");
		}
		searchIndex = searchIndex.replace(/%/gi, "+");
		searchIndex = searchIndex.replace(/_/gi, "+");
		searchIndex = searchIndex.replace(/&/gi, "-and-");
		searchIndex = searchIndex.replace(/http:\/\//gi, "");
		searchIndex = searchIndex.replace(/https:\/\//gi, "");
	}

	if (stype == 'name' && searchTob !='') searchTob = searchIndex;

	var url_match = /^([a-zA-Z0-9\-]+\.)+(com|org|net|mil|edu|biz|COM|ORG|NET|MIL|EDU|BIZ)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;
	var phone_match = /^(\([\d+]{3,3}\)|[\d+]{3,3})\s*[\d+]{3,3}[\s\-]*[\d+]{4,4}$/;
	// check if numeric too
	var ph = searchIndex.replace(/\D/gi, "");

	ext = 'name';
	var local3value = ( document.getElementById("demo-local3") ? document.getElementById("demo-local3").value : false );
	//if (typeof tlist != 'undefined')
	if(local3value || (typeof tlist != 'undefined' && tlist) || (stype == 'tob.location') || (stype == 'type')){
		searchLocation = document.getElementById("txtSearchlocation").value;
		if (tlist) searchLocation = loc;
		ext = 'type';
		searchIndex = searchIndex.replace(/ /gi, "+");
	}else if(url_match.test(searchIndex)){
		ext = 'url';
		searchIndex = searchIndex.replace(/\./gi, "_");
		searchIndex = searchIndex.replace(/\//gi, "__");
	}else if(phone_match.test(searchIndex) || (ph && !isNaN(ph))){
		ext = 'phone';
		searchIndex = searchIndex.replace(/\(/gi, "");
		searchIndex = searchIndex.replace(/\)/gi, "");
		searchIndex = searchIndex.replace(/-/gi, "");
		searchIndex = searchIndex.replace(/ /gi, "");
	}else {
		searchIndex = searchIndex.replace(/ /gi, "+");
	}

	if (searchLocation) {
		var searchLocationArray = searchLocation.split(",");
		var city = searchLocationArray[0];
		city = city.replace(/ /gi, "+");
		searchLocation = city;
		if (searchLocationArray[1]) {
			var state = searchLocationArray[1];
			state = state.replace(/ /gi, "");
			searchLocation = searchLocation + '-' + state;
		}
	}

	//edit url to equal the current BBB's search page
	//if( !document.getElementById('local-url') ) {
	//var url = "/find-business-reviews/" + ext; // production national
	//} else {
	var url = baseURL + "/find-business-reviews/" + ext; // production local, edit domain to local domain
	//}

	//url = url.replace( '.bbb.org', '.app.bbb.org' );
	//url = url.replace( '.bbb.org', '.dev.app.hurdman.org' ); // redirect to dev server for now

	var searchParams = "";
	if ( ext != 'type' || (stype == 'tob.location') || (stype == 'type') ) {
		searchIndex = encodeURI(searchIndex);
		searchParams = "/" + searchIndex;
	}

	if (searchTob != "" && searchParams == '' && stype == '')
	searchParams += "/" + getUrlEncode(searchTob);

	if (searchLocation != "")
	searchParams += "/" + getUrlEncode(searchLocation);

	if (isAccreditedSearch)
	searchParams += "/bbb-accredited";

	if (document.getElementById("nloc")!=undefined) {
		var nlocal = document.getElementById("nloc").checked;
		if (nlocal==true) {
			searchParams += "/national";
			if ( ext == 'type' && stype != "") {
				if (stype == "name") {
					searchParams = "/" + searchTob + searchParams;
				}
			}
		}else {
			searchParams += "/local";
			if (stype == "name") {
				searchParams = "/" + searchTob + searchParams;
			}
		}
	}else {
		searchParams += "/local";
	}

	if (searchParams != "")
		url += searchParams + "/";
	
	if( url.search(/http:\/\//i) == -1 )  {
		url = url.replace(/\/\//gi, "/");
		url = "http://" + url;
	}
	//alert(url); return;
	window.location.assign(url);
}

function searchRedirect3() {
	if (tlist) {
		var searchIndex = trim(tlist);
	}else {
		var searchIndex = trim(document.getElementById("txtSearchbox").value);
	}
	var isAccreditedSearch = document.getElementById("chbAccredited").checked;
	var location = document.getElementById("txtSearchlocation").value;

	searchIndex = searchIndex.toLowerCase();
	searchIndex = searchIndex.replace(/ & /gi, "-and-");
	searchIndex = searchIndex.replace(/ - /gi, "-");
	searchIndex = searchIndex.replace(/ /gi, "-");
	searchIndex = searchIndex.replace(/,/gi, "");

	//edit url to equal the current BBB's search page
	//if( !document.getElementById('local-url') ) {
	//var url = "/directory2/" + searchIndex; // production national
	//} else {
	if( baseURL.search(/greater-san-francisco/i) != -1 )  {
		var url = baseURL + "/accredited-business-directory/" + searchIndex;
	}else {
		var url = baseURL + "/abpages2/" + searchIndex; // production local, edit domain to local domain
	}
	//}

	//url = url.replace( '.bbb.org', '.app.bbb.org' );
	//url = url.replace( '.bbb.org', '.dev.app.hurdman.org' ); // redirect to dev server for now

	if (location != '') {
		location = location.toLowerCase();
		location = location.replace(/,\s*/gi, "-");
		location = location.replace(/ /gi, "-");
		url += '/' + location;
	}

	if( url.search(/http:\/\//i) == -1 )  {
		url = "http://" + url;
	}
	//alert(url); return;
	window.location.assign(url);
	return;
}

function inArray(needle, haystack) {
	var length = haystack.length;
	for(var i = 0; i < length; i++) {
		if(haystack[i].toLowerCase() == needle.toLowerCase()) return true;
	}
	return false;
}
