var range;
var sortType = '1';
var sortOption = '1';
$(document).ready(function() {
	if (latitude != '' && longitude != '') getDealerList(sortOption,sortType);
	var addressValue = $('#address').val();
	addressValue = ($('#address').val() != searchAddressDefault) ? $('#address').val() : '';
	$("#iFooterSend2Friend").attr("href", s2fURL + encodeURIComponent(encodeURIComponent('&range=' + $('#range').val() + '&add=' + encodeURIComponent(addressValue) + '&lat=' + latitude + '&lon=' + longitude)));
})
function checkFrameset() {
	if (top.location.href == self.location.href) window.location.replace(frameRedirectURL);
}
function createResult(data) {
	if (data != '') {
		var i;
		var jsonResponse = eval('(' + data + ')');
		if (jsonResponse.address != null) {
			$('#addressmessage').html(multipleSelectionMessage);
			$('#addressmessage').show();
			$('#results').html('');
			var addressOptions;
			jsonResponse = jsonResponse.address;
			$.each(jsonResponse, function(i, val) {
				addressOptions= addressOptions + '<option value=\"' + val + '\">' + val + '<\/option>';
			});
			$('#address').replaceWith('<select class=\"text\" id=\"address\" name=\"address\">' + addressOptions + '<\/select>');
		}
		else {
			if (jsonResponse.dealer != null || jsonResponse.failover != null) {
				$('#addressmessage').hide();
				var description = jsonResponse.description;
				if (description != undefined) $('#address').replaceWith('<input class=\"text\" id=\"address\" maxlength=\"30\" name=\"address\" type=\"text" value=\"' + description + '\" \/>');
				var results = (jsonResponse.dealer != null) ? '' : jsonResponse.message;
				var poi = '';
				var contactTemplate = jsonResponse.template;
				var contactBox = '';
				var dealerCount = parseInt(jsonResponse.count);
				var distanceunit = jsonResponse.distanceunit;
				var faxlabel = jsonResponse.faxlabel;
				latitude = jsonResponse.latitude;
				longitude = jsonResponse.longitude;				
				var phonelabel = jsonResponse.phonelabel;
				var jsonResponse = (jsonResponse.dealer != null) ? jsonResponse.dealer : jsonResponse.failover;
				if (jsonResponse != null) {
					for(i = 0;i < jsonResponse.length; ++i) {
						contactBox = contactTemplate;
						contactBox = contactBox.replace(/##ADDRESS##/g,jsonResponse[i].address);
						contactBox = contactBox.replace(/##CITY##/g,jsonResponse[i].city);
						contactBox = contactBox.replace(/##COMMENT##/g,jsonResponse[i].comment);
						contactBox = contactBox.replace(/##COUNTRY##/g,jsonResponse[i].country);
						contactBox = contactBox.replace(/##DEALERTYPE##/g,jsonResponse[i].dealertype);
						contactBox = contactBox.replace(/##DIRECTION##/g,jsonResponse[i].direction);
						contactBox = contactBox.replace(/##DISTANCE##/g,jsonResponse[i].distance);
						contactBox = contactBox.replace(/##DISTANCEUNIT##/g,distanceunit);
						contactBox = contactBox.replace(/##EMAIL##/g,jsonResponse[i].email);
						if (jsonResponse[i].fax != '') {
							contactBox = contactBox.replace(/##FAXLABEL##/g,faxlabel);
						}
						else {
							contactBox = contactBox.replace(/##FAXLABEL##/g,'');
						};
						contactBox = contactBox.replace(/##FAX##/g,jsonResponse[i].fax);
						contactBox = contactBox.replace(/##HOMEPAGE##/g,jsonResponse[i].homepage);
						contactBox = contactBox.replace(/##IMAGE##/g,jsonResponse[i].image);
						contactBox = contactBox.replace(/##NAME1##/g,jsonResponse[i].name1);
						contactBox = contactBox.replace(/##NAME2##/g,jsonResponse[i].name2);
						contactBox = contactBox.replace(/##NAME3##/g,jsonResponse[i].name3);
						if (jsonResponse[i].phone != '') {
							contactBox = contactBox.replace(/##PHONELABEL##/g,phonelabel);
						}
						else {
							contactBox = contactBox.replace(/##PHONELABEL##/g,'');
						};
						contactBox = contactBox.replace(/##PHONE##/g,jsonResponse[i].phone);
						contactBox = contactBox.replace(/##PRODUCTGROUP##/g,jsonResponse[i].productgroup);
						contactBox = contactBox.replace(/##PRODUCTS##/g,jsonResponse[i].products);
						contactBox = contactBox.replace(/##POSTALCODE##/g,jsonResponse[i].postalcode);
						contactBox = contactBox.replace(/##REGION##/g,jsonResponse[i].region);
						results = results.replace(/##DISTANCEUNIT##/g,distanceunit);
						results = results.replace(/##RANGE##/g,range);
						results = results + contactBox;
						poi = (i > 0) ? poi + ';' : poi;
						poi = poi + jsonResponse[i].longitude + ',' + jsonResponse[i].latitude;
					}
				}
				$("#results").replaceWith('<div id=\"results\">' + results + '<\/div>');
				$('[id^=iSort]').css('font-weight','normal');	
				$('#iSort' + sortOption).css('font-weight','bold');
				$("#sort").show();
				$("#subtitle").show();
				if (sortType == '1' && jsonResponse.length < dealerCount) $('a#iFooterMore').show();
				$("#iFooterSend2Friend").attr("href", s2fURL + encodeURIComponent(encodeURIComponent('&range=' + range + '&add=' + encodeURIComponent($('#address').val()) + '&lat=' + latitude + '&lon=' + longitude)));
				latitude = '';
				longitude = '';
			}
		}
	}
	else {
		$('#results').replaceWith('<div id=\"results\">' + searchAddressErrorMessage + '<\/div>');
	}
}
function getDealerList(so,st) {
	var searchForm = document.forms[0];
	if ($('#address').val() != searchAddressDefault && ($('#address').val() != '') || (latitude != '' && longitude != '')) {
		$('#addressmessage').hide();
		$('#sort').hide();
		$('#subtitle').hide();
		$('a#iFooterMore').hide();
		$('#results').replaceWith('<div id=\"results\"><p style=\"margin-top:32px;text-align:center;\"><img src=\"progress.gif\" \/><\/p><\/div>');
		var address;
		var i;
		var rangeForm = document.forms[1];
		address = (searchForm.address.length > 0) ? $('#address option:selected').val() : $('#address').val();
		if (rangeForm.range.length > 0) range = $("#range option:selected").val();
		if (searchForm.product) {
			var product = searchForm.product.length;
			for(i = 1; i <= searchForm.product.length; ++i) {
				product = product + '|';
				if(searchForm.product[i - 1].checked) product = product + searchForm.product[i - 1].value;
			};
		}
		else {
			var product = "0|";
		};
		if (searchForm.property) {
			var properties = $("input[name='property']");
			var property = properties.length;
			for(i = 1; i <= properties.length; ++i) {
				property = property + '|';
				if(properties[i - 1].checked) property = property + properties[i - 1].value;
			};
		}
		else {
			var property = '0|';
		};
		var serviceURL = dbPath + '/getDealer?OpenAgent';
		var serviceParameter = '{"key":"' + formKey + '","address":"' +  escape(address) + '","range":"' + range + '","product":"' + product + '","property":"' + property + '","sort":"' + so + '","type":"' + st + '", "latitude":"' + latitude + '","longitude":"' + longitude + '"}';
		if (trackingPath != '') urchinTracker(trackingPath);
		$.get(serviceURL, serviceParameter, createResult)
	}
	else {
		$('#results').replaceWith('<div id=\"results\"><\/div>');
	}
	return false;
}
