// wehaveSearch script v1.0
// requires jQuery

// searchBox: name of input box to search from
function isNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

function wehaveSearch(_url, _script, _s, _r, _keyword)
{
	this.url = _url;
	this.script = _script;
	this.s = $(_s);
	this.r = $(_r);
	this.keyword = _keyword;
	this.lastRec = 0;

	this.setKeyup = function(parent) {
		parent.s.keyup(function(e) {
			var d = new Date();
	
			$.ajax({
				cache: false,
				ifModified: true,
				type: 'GET',
				url: 'ajax/' + parent.script,
				data: {
					s: parent.s.val(),
					t: d.valueOf()
				},
				dataType: 'json',
				success: function(data) {
					if( data && data.data ) {
						if( data.t >= parent.lastRec ) {
							parent.lastRec = data.t;
							str = '';
							type = 0;
							$.each(data.data, function(key, val) {
								switch( val.type ) {
									case 0:
										type = 0;
										str += '\
											<div class="s-result">\
												<div city="' + val.city + '" state="' + val.stateFull + '">\
													<div style="width:50%;"><div style="padding-left:3px;"><strong>' + val.city + ', ' + val.state + '</strong></div></div>\
													<div style="width:25%;"><div><strong>Zip:</strong> ' + val.zip + '</div></div>\
													<div style="width:25%;"><div style="text-align:right;">(' + val.count + (val.count==1 ? ' ' + parent.keyword : ' ' + parent.keyword + 's') + ')&nbsp;</div></div>\
												</div>\
											</div>';
										break;
										
									case 1:
										type = 0;
										str += '\
											<div class="s-result">\
												<div state="' + val.stateFull + '">\
													<div style="width:50%;"><div style="padding-left:3px;"><strong>' + val.stateFull + '</strong></div></div>\
													<div style="width:50%;"><div style="text-align:right;">(' + val.count + (val.count==1 ? ' ' + parent.keyword : ' ' + parent.keyword + 's') + ')&nbsp;</div></div>\
												</div>\
											</div>';
										break;
										
									case 2:
										type = 0;
										str += '\
											<div class="s-result">\
												<div zip="' + val.zip + '">\
													<div style="width:25%;"><div style="padding-left:3px;"><strong>' + val.zip + '</strong></div></div>\
													<div style="width:50%;"><div>' + val.city + ', ' + val.state + '</div></div>\
													<div style="width:25%;"><div style="text-align:right;">(' + val.count + (val.count==1 ? ' ' + parent.keyword : ' ' + parent.keyword + 's') + ')&nbsp;</div></div>\
												</div>\
											</div>';
										break;
										
									case 3:
										type = 0;
										str += '\
											<div class="innertube-5">\
												<strong>' + val.city + ', ' + val.stateFull + ' ' + val.zipcode + '</strong><br />\
												Sorry, no ' + parent.keyword + 's registered in this area.<br />Please try another search.</div>';
	
										break;
										
									case 4:
										type = 1;
										str += '\
											<div class="s-result">\
												<div id="' + val.ID +'" permalink="' + val.permalink + '" p="' + val.package + '">\
													<div style="width:50%;"><div style="padding-left:3px;"><strong>' + val.name + '</strong></div></div>\
													<div style="width:50%;"><div style="text-align:right;">' + val.city + ', '+ val.state + '&nbsp;</div></div>\
												</div>\
											</div>';
											
										break;
								} // end switch
							});
	
							parent.r.html(str);

							if( type == 0 ) {
								parent.r.find('.s-result > div').click(function() {
									var arr = new Array();
									
									if( $(this).attr('state') )
										arr.push($(this).attr('state'));
									
									if( $(this).attr('city') )
										arr.push($(this).attr('city'));
										
									if( $(this).attr('zip') )
										arr.push($(this).attr('zip'));

//									window.location = parent.url + "/" + String.toLowerCase(arr.join("/"));
//									val.city = val.city.replace(/ /g, '_').toLowerCase();
									window.location = parent.url + "/" + arr.join("/").replace(/ /g, '_').toLowerCase();
								});
							} else {
								parent.r.find('.s-result > div').click(function() {
									switch( parseInt($(this).attr('p')) ) {
										case 0:
											window.location = 'results.php?id=' + $(this).attr('id');
											break;
										
										case 1:
											window.location = 'results.php?id=' + $(this).attr('id');
											break;
										
										case 2:
											window.location = parent.url + '/' + $(this).attr('permalink');
											break;
									}
								});
							}
							
							parent.r.show();
						}
					} else {
						parent.r.hide();
					}
	
				} // success
			}); // ajax
		}); // keyup
	}
	
	// go ahead and bind keyup
	this.setKeyup(this);

	return true;
}
