var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-3383698-19']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

$(function(){
		$('#contact_form').submit(function(){
			$('.not_null').removeClass('error');
			$('.not_null_select').removeClass('error');
			var valid = true;
			var notNulls = $('.not_null');
			$.each(notNulls, function(){
				if($(this).val() == ''){
					$(this).addClass('error');
					if(valid == true){
						$(this).focus();
					} 
					valid = false;
				}
			});
			
			var notNulls = $('.not_null_select');
			$.each(notNulls, function(){
				if($('option:selected',this).val() == ''){
					$(this).addClass('error');
					if(valid == true){
						$(this).focus();
					} 
					valid = false;
				}
			});
			if(!echeck($('#email').val()) && valid != false){
				$('#email').addClass('error');
				$('#lead_error').hide();
				$('#email_box').slideDown();
				return false;
			} else if(valid == false){
				$('#lead_error').slideDown();
				$('#email_box').hide();
				return false;
			}
		});
		$('#location').change(function(){
			if($(this).val() == 'Other'){
				$('#location_error').slideDown();
			} else {
				$('#location_error').slideUp();
			}
		});
		
		if($('#map').length > 0){
    	var geocoder = new google.maps.Geocoder();

			
			function codeAddress(address) {
				geocoder.geocode( { 'address': address}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						map.setCenter(results[0].geometry.location);
						var marker = new google.maps.Marker({
								map: map,
								position: results[0].geometry.location
						});
					}
				});
			}

			var latlng = new google.maps.LatLng(51.6887336672411, 0.0775909423828125);
			var myOptions = {
				zoom: 9,
				center: latlng,      
				disableDefaultUI: true,      
				zoomControl: true,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			var map = new google.maps.Map(document.getElementById("map"), myOptions);
			$.each($markers, function(index, value){
				codeAddress(value);
			});
			
			
		}

	});
	
	function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
			return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			return false
		}
		if (str.indexOf(at,(lat+1))!=-1){
			return false
		}
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			return false
		}
		if (str.indexOf(dot,(lat+2))==-1){
			return false
		}		
		if (str.indexOf(" ")!=-1){
			return false
		}
		return true					
	}
