// JavaScript Document
function isValidEmail(str) { return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);}
function openChild(file,win) {
	var width = 920;
	var height = 625;
	var left = parseInt((screen.availWidth/2) - (width/2));
	var top = parseInt((screen.availHeight/2) - (height/2));
	var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable=no,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
	childWindow=open(file,win,windowFeatures);
	if (childWindow.opener == null) childWindow.opener = self;
}
function showTab(tab, highlight){
	highlight.blur()
	$$(".tab").each(function(i){
		i.hide()
	});
	$$(".tabs a").each(function(i){
		i.removeClassName('highlight')
	});
	$(tab).show()
	highlight.addClassName('highlight')
}
Element.observe(window, 'load', function(){
	$$(".thumbs div img").each(function(i){
		i.observe('click', function(){
			$('large_img').src=i.src.replace(/_t/,"_600")
		});
	});


	if ($('nav_used_make')){
		setInterval ( "$('specials').toggleClassName('latest_specials_flash')", 800 );

		$('nav_used_make').observe('change', function(){
			if ($('nav_used_make').value=="--- Select Make ---"){
				$('nav_used_model_container').innerHTML = ""
			}else{
				$('nav_used_model_container').innerHTML = '<select id="nav_used_model"><option>Please wait...</option></select>';
				new Ajax.Request('/ajax.php?action=nav_used_models&make='+$('nav_used_make').value,
				{
					method:'get',
					onSuccess: function(transport){
						$('nav_used_model_container').innerHTML = transport.responseText || "no response text";
						$('nav_used_model').observe('change', function(){
							if ($('nav_used_make').value!="--- Select Make ---"){
								window.location='/used-cars/'+$('nav_used_make').value+'/'+$('nav_used_model').value+'/';
							}
						});
					},
					onFailure: function(){  }
				});
			}
		});

		$$("a").each(function(el){
			if (el.href=="javascript:void(0);"){
				Element.observe(el, 'click', function(){
					$(el).blur();Effect.toggle("sub_"+el.id, 'blind', { duration: 0.4 });
				});
			}
		});

		$('mailing_list_email').observe('focus', function(){
			if (!$('mailing_list_email').hasClassName('focus')){
				$('mailing_list_email').value="";
				$('mailing_list_email').addClassName('focus');
			}
		});
		$('mailing_list_email').observe('blur', function(){
			if ($('mailing_list_email').value==""){
				$('mailing_list_email').removeClassName('focus');
				$('mailing_list_email').value="Join Our Mailing List";
			}
		});
		$('mailing_list_join').observe('click', function(){
			if (!$('mailing_list_email').hasClassName('focus') || !isValidEmail($('mailing_list_email').value)){
				alert('Please enter a valid email address')
				$('mailing_list_email').focus()
				return false;
			}
			email = $('mailing_list_email').value
			$('join_mailing').innerHTML = "Please wait...";

			new Ajax.Request('/ajax.php?action=mailing_list&email='+ email,
			{
				method:'get',
				onSuccess: function(transport){
					$('join_mailing').innerHTML = "Thank you for joining.";
				},
				onFailure: function(){  }
			});


		});

		$('send').observe('click', function(){
			var x = 0;
			if ($('department').value=="-- Select A Department --"){
				x++
				alert('Please select a department.')
				$('department').focus();
			}
			if (x==0){
				$$("#enquiry_form input, #enquiry_form textarea").each(function(i){
					if (!i.hasClassName('focus') && (x==0)){
						x++
						alert('Please complete the enquiry form.')
						i.focus()
					}
				});
			}

			if (x!=0){
				return false;
			}
			$('send_container').innerHTML = "Sending Enquiry...";

			send_enquiry()
		});

	}

	if ($('enq_enquire')){
		$('enq_enquire').observe('click', function(){
			if (!$('enq_name').value){
				alert('Please enter your name')
				$('enq_name').focus()
				return false;
			}
			if (!$('enq_phone').value){
				alert('Please enter your phone number')
				$('enq_phone').focus()
				return false;
			}
			if (!isValidEmail($('enq_email').value)){
				alert('Please correct your email address')
				$('enq_email').focus()
				return false;
			}
			if (!$('enq_car').value){
				alert('Please enter the car details')
				$('enq_car').focus()
				return false;
			}

			new Ajax.Request('/ajax.php?action='+$('a').value,
			{
				method:'get',
				parameters: $('enq').serialize(),
				onSuccess: function(transport){
					//$('book_a_service').innerHTML = transport.responseText;
					$('enq').innerHTML="<h2>Thank You</h2><p>We will be in contact shortly.</p>"
				},
				onFailure: function(){  }
			});
		});
	}

	if ($('send_booking_request')){
		$('send_booking_request').observe('click', function(){
			if (!$('bas_name').value){
				alert('Please enter your name')
				$('bas_name').focus()
				return false;
			}
			if (!$('bas_registration_number').value){
				alert('Please enter your cars registration number')
				$('bas_registration_number').focus()
				return false;
			}
			if (!$('bas_phone').value){
				alert('Please enter your phone phone')
				$('bas_phone').focus()
				return false;
			}
			if (!isValidEmail($('bas_email').value)){
				alert('Please correct your email address')
				$('bas_email').focus()
				return false;
			}
			if (!$('bas_car_make').value){
				alert('Please enter your cars make')
				$('bas_car_make').focus()
				return false;
			}
			if (!$('bas_car_model').value){
				alert('Please enter your cars model')
				$('bas_car_model').focus()
				return false;
			}
			if (!$('bas_year').value){
				alert('Please enter your cars year')
				$('bas_year').focus()
				return false;
			}
			if (!$('bas_kms_travelled').value){
				alert('Please enter KMs travelled')
				$('bas_kms_travelled').focus()
				return false;
			}

			new Ajax.Request('/ajax.php?action=book_service',
			{
				method:'get',
				parameters: $('book_a_service').serialize(true),
				onSuccess: function(transport){
					//$('book_a_service').innerHTML = transport.responseText;
					$('book_a_service').innerHTML="<h2>Thank You</h2><p>We will contact you shortly to confirm your booking.</p>"
				},
				onFailure: function(){  }
			});
		});
	}

	if ($('new_make')){
		//Front Page

		$('used_search_btn').observe('click', function(){
			if ($('used_make').value!="--- Select Make ---"){
				window.location='/used-cars/'+$('used_make').value+'/'+$('used_model').value+'/';
			}
		});
		$('new_search_btn').observe('click', function(){
			if ($('new_make').value!="--- Select Make ---"){
				window.location='/'+$('new_make').value+'/'+$('new_model').value+'/';
			}
		});
		
		

		$('new_make').observe('change', function(){
			$('new_model_container').innerHTML = '<select id="new_model"><option>Please wait...</option></select>';
			new Ajax.Request('/ajax.php?action=new_models&make='+$('new_make').value,
			{
				method:'get',
				onSuccess: function(transport){
					$('new_model_container').innerHTML = transport.responseText;
				},
				onFailure: function(){  }
			});
		});
		$('used_make').observe('change', function(){
			if ($('used_make').value=="--- Select Make ---"){
				$('used_model_container').innerHTML = ""
			}else{
				$('used_model_container').innerHTML = '<select id="used_model"><option>Please wait...</option></select>';
				new Ajax.Request('/ajax.php?action=used_models&make='+$('used_make').value,
				{
					method:'get',
					onSuccess: function(transport){
						$('used_model_container').innerHTML = transport.responseText;
					},
					onFailure: function(){  }
				});
			}
		});
	}

	function send_enquiry(){
		new Ajax.Request('/ajax.php?action=footer_enquiry',
		{
			method:'get',
			parameters: $('enquiry_form').serialize(true),
			onSuccess: function(transport){
				$('enquiry_form').hide();
				$('enquiry_form').innerHTML = "Thank you for your enquiry. We will be in touch shortly.";
				$('enquiry_form').appear({duration:0.5});
			},
			onFailure: function(){ alert('Please try again') }
		});


	}

	$$(".footer .middle form input, .footer .middle form textarea").each(function(el){
		window['value_' + el.id] = el.value;
		Element.observe(el, 'focus', function(){
			if (!el.hasClassName('focus')){
				el.addClassName('focus');
				el.value=""
			}
		});
		Element.observe(el, 'blur', function(){
			if (el.value==""){
				el.removeClassName('focus');
				el.value=window['value_' + el.id];
			}
		});
	});

});