<!--
window.addEvent('domready', function() {
	if ($('contactDealerForm')) {
		$('bike_model').getParent().adopt( new Element('div', {'id':'er-bike_model', 'class':'error-message'}) );
		$('groupset').getParent().adopt( new Element('div', {'id':'er-groupset', 'class':'error-message'}) );
		$('crankset').getParent().adopt( new Element('div', {'id':'er-crankset', 'class':'error-message'}) );
		$('wheelset').getParent().adopt( new Element('div', {'id':'er-wheelset', 'class':'error-message'}) );
		$('colour').getParent().adopt( new Element('div', {'id':'er-colour', 'class':'error-message'}) );
		$('size').getParent().adopt( new Element('div', {'id':'er-size', 'class':'error-message'}) );
		$('qty').getParent().adopt( new Element('div', {'id':'er-qty', 'class':'error-message'}) );
		$('fullname').getParent().adopt( new Element('div', {'id':'er-fullname', 'class':'error-message'}) );
		$('address').getParent().adopt( new Element('div', {'id':'er-address', 'class':'error-message'}) );
		$('city').getParent().adopt( new Element('div', {'id':'er-city', 'class':'error-message'}) );
		$('country').getParent().adopt( new Element('div', {'id':'er-country', 'class':'error-message'}) );
		$('tel').getParent().adopt( new Element('div', {'id':'er-tel', 'class':'error-message'}) );
		$('email').getParent().adopt( new Element('div', {'id':'er-email', 'class':'error-message'}) );
		$('search-result-container').getParent().adopt( new Element('div', {'id':'er-dealer-info', 'class':'error-message'}) );
		
		$('contactDealerForm').addEvent('submit', function() {
			var isSubmit = true;
			$('er-bike_model').innerHTML = "";
			$('er-groupset').innerHTML = "";
			$('er-crankset').innerHTML = "";
			$('er-wheelset').innerHTML = "";
			$('er-colour').innerHTML = "";
			$('er-size').innerHTML = "";
			$('er-qty').innerHTML = "";
			$('er-fullname').innerHTML = "";
			$('er-address').innerHTML = "";
			$('er-city').innerHTML = "";
			$('er-country').innerHTML = "";
			$('er-tel').innerHTML = "";
			$('er-email').innerHTML = "";
			$('er-dealer-info').innerHTML = "";
				
			if ( $('bike_model').value == "" ) {
				$('er-bike_model').innerHTML = "Please select the model";
				isSubmit = false;
			}
			if ( $('groupset').value == "" ) {
				$('er-groupset').innerHTML = "Please select the group set";
				isSubmit = false;
			}
			if ( $('crankset').value == "" ) {
				$('er-crankset').innerHTML = "Please select the crank set";
				isSubmit = false;
			}
			if ( $('wheelset').value == "" ) {
				$('er-wheelset').innerHTML = "Please select the wheel set";
				isSubmit = false;
			}
			if ( $('colour').value == "" ) {
				$('er-colour').innerHTML = "Please select the colour";
				isSubmit = false;
			}
			if ( $('size').value == "" ) {
				$('er-size').innerHTML = "Please select the size";
				isSubmit = false;
			}
			if ( $('qty').value == "" ) {
				$('er-qty').innerHTML = "Please select the quantity";
				isSubmit = false;
			}
			if ( $('fullname').value == "" ) {
				$('er-fullname').innerHTML = "Please enter the full name";
				isSubmit = false;
			}
			if ( $('address').value == "" ) {
				$('er-address').innerHTML = "Please enter the street address";
				isSubmit = false;
			}
			if ( $('city').value == "" ) {
				$('er-city').innerHTML = "Please enter the city";
				isSubmit = false;
			}
			if ( $('country').value == "" ) {
				$('er-country').innerHTML = "Please enter the country";
				isSubmit = false;
			}
			if ( $('tel').value == "" ) {
				$('er-tel').innerHTML = "Please enter the telephone";
				isSubmit = false;
			}
			if ( $('email').value == "" ) {
				$('er-email').innerHTML = "Please enter the email address";
				isSubmit = false;
			}
			if ( $('email').value != "" ) {
				if (!isValidEmailFormat( $('email').value )) {
					$('er-email').innerHTML = "Please enter a valid email address";
					isSubmit = false;
				}
			}
			if ( $$('input[name=dealer-info]:checked') == "" ) {
				$('er-dealer-info').innerHTML = "Please select dealer";
				isSubmit = false;
			}
			return isSubmit;
		});
		$('dealer-search').addEvent('click', function(event) {
			event.stop();
			var searchList = new Array();
			var req = new Request({
				method: 'get',
				url: "http://www.neilprydebikes.com/index.php?option=com_npbikes&view=all&layout=xml&is_xml=1",
				onRequest: function() {  },
				onSuccess: function(html) {
					var obj = unserialize(html);
					for (var i in obj) {
						var dealer = obj[i];
						if ( dealer["address"].toLowerCase().indexOf($('search-txt').value.toLowerCase()) != -1 ) {
							searchList.push(dealer);
						}
					}
					genSearchResultList(searchList);
				}
			}).send();
		});
	}
});

function genSearchResultList(list) {
	var str = '';
	var remarkText = 'If the listed dealers are too far from your location please contact us directly: ';
	$('search-result-container').innerHTML = str;
	str += '<div class="searchList">';
	for (var i=0; i<list.length; i++) {
		str += '		<input class="input-radio dealer-email" name="dealer-info" type="radio" value="' + list[i]["email"] + '">';
		str += '		<div class="dealer-info">';
		str += '			<div class="companyname">';
		str += '				' + list[i]["name"];
		str += '			</div>'
		str += '			<div class="companydetail">';
		str += '			' + list[i]["address"] + "<br>" + list[i]["phone"] + "<br>" + list[i]["email"];
		str += '			</div>'
		str += '			<div class="clearfix"> </div>';
		str += '		</div>'
		str += '	<div class="clearfix"> </div>';
	}
	str += '	<div class="remark">' + remarkText  + '</div>';
	str += '	<input class="input-radio dealer-email validate-one-required" name="dealer-info" type="radio" value="info@neilprydebikes.com">';
	str += '	<div class="dealer-info">';
	str += '		<div class="companyname">NeilPryde Bikes</div>'
	str += '	</div>';
	str += '	<div class="clearfix"> </div>';
	str += '</div>';

	$('search-result-container').innerHTML = str;
	
}	

function isValidEmailFormat(str) {
	var re = /^\S+\@\S+\.[a-zA-Z0-9.-\\]+$/;	
	return re.test(str);
}
//-->