function cleardfocus(element,devault){
	if (element.value == devault){
		element.value = '';
	}
	element.setAttribute("onblur","if(this.value=='') this.value='"+devault+"';");
}

function openre(id,btn){
	if($(id).style.display == 'none'){
		btn.style.backgroundImage = 'url(images/btn_minus.gif)';
		$(id).style.display = 'block';
	} else {
		btn.style.backgroundImage = 'url(images/btn_plus.gif)';
		$(id).style.display = 'none';
	}
	btn.blur();
}


// homepage flyouts

var lastfly = null;

var mpgflyoutLoaded = false;
var distanceflyoutLoaded = false;
var styleflyoutLoaded = false;
var makeflyoutLoaded = false;

function flyout(tab){
	if (lastfly != null)
		lastfly.style.display = 'none';
	$('hm_'+tab).blur();
	$('spanbar').style.top = $('hm_'+tab).positionedOffset().top + 'px';
	$(tab+'flyout').style.display = 'block';
	$('spanbar').style.display = 'block';
	lastfly = $(tab+'flyout');
	if (tab == 'mpg' && ! mpgflyoutLoaded){
		mpgflyoutLoaded = true;
		new Ajax.Updater('mpgflyoutLoad','ajax/homefly.cfm', {
		  method: 'post',
		  evalScripts: true,
		  parameters:  { tab: tab }
		});
	} else if (tab == 'make' && ! makeflyoutLoaded){
		makeflyoutLoaded = true;
		new Ajax.Updater('makeflyoutLoad','ajax/homefly.cfm', {
		  method: 'post',
		  evalScripts: true,
		  parameters:  { tab: tab }
		});
	} else if (tab == 'style' && ! styleflyoutLoaded){
		styleflyoutLoaded = true;
		new Ajax.Updater('styleflyoutLoad','ajax/homefly.cfm', {
		  method: 'post',
		  evalScripts: true,
		  parameters:  { tab: tab }
		});
	} else if (tab == 'distance' && ! distanceflyoutLoaded){
		distanceflyoutLoaded = true;
		new Ajax.Updater('distanceflyoutLoad','ajax/homefly.cfm', {
		  method: 'post',
		  evalScripts: true,
		  parameters:  { tab: tab }
		});
	}
	
}
function closefly(){
	lastfly.style.display = 'none';
	$('spanbar').style.display='none';
	lsatfly = null;
}


// photo scrubins
function photoscrubit(d,holder){
	if (holder == undefined)
		holder = 'photoscrub';
	if ($(holder).movin != true && (curpage + d) >= 1 && (curpage + d) <= photopages){
		$(holder).movin = true;
		var newpos = d * -290;
		new Effect.Move(holder, { x:newpos , y: 0, mode: 'relative', afterFinish: function(){
			$(holder).movin = false;
			curpage = curpage + d;
		}});
	}
}

// detail tabs
function tabit(tab,tblock){
	$('tabbox').childElements().each(function(item){
		item.style.display = 'none';
	});
	$('tbtabs').childElements().each(function(item){
		item.removeClassName('tbtabbed');
	});
	tab.addClassName('tbtabbed');
	$(tblock).style.display = 'block';
}

// Showme
var stopflag = 0;
var oldmenu = '';

function showme(ID){
	stopnoshow('');
	if (oldmenu != ''){
		$(oldmenu).style.display='none';
		$(oldmenu+'b').removeClassName('out');
	}
	$(ID+'b').addClassName('out');
	$(ID).style.display='block';
	oldmenu = ID;
}

function startnoshow(ID){
	stopflag = 1;
	oldmenu = ID;
	setTimeout("noshow()",'500');
}

function noshow(){
	if (stopflag == 1){
		if (oldmenu != ''){
			$(oldmenu).style.display='none';
			$(oldmenu+'b').removeClassName('out');
		}
		oldmenu = '';
		stopflag = 0;
	}
}

function stopnoshow(){
	stopflag = 0;
}


function signpop(what,where){
	if (what == 'open'){
		Effect.BlindDown('logpop', {duration:0.5});
		new Ajax.Updater('updater','ajax/settarget.cfm', {
		  method: 'post',
		  parameters:  { target: where }
		});
	} else {
		Effect.BlindUp('logpop', {duration:0.5});
	}
}

// search selection

	function selectmake(make){
		new Ajax.Updater('updater','ajax/selectmodel.cfm', {
		  method: 'get',
		  evalScripts: true,
		  parameters:  { make: make }
		});
	}

	function selectmodel(model){
		new Ajax.Updater('updater','ajax/selectmodel.cfm', {
		  method: 'get',
		  evalScripts: true,
		  parameters:  { model: model }
		});
	}
	
// chrome selection

	function ss(tab,rp){
		var ajax = '../ajax/smap.cfm';
		if (rp == 'n'){
			ajax = 'ajax/smap.cfm';
		}
		if(parent.$('sstrim')) 
			parent.$('sstrim').options.length = 1;
		if(tab <= 2)
			parent.$('ssmodel').options.length = 1;
		if(tab <= 1)
			parent.$('ssmake').options.length = 1;
		
		
		new Ajax.Updater('updater',ajax, {
		  method: 'post',
		  evalScripts: true,
		  parameters: $('postcar').serialize()
		});
		
	}
	
	
// save search

function savesearch(search,count,email){
		new Ajax.Updater('updater','ajax/savesearch.cfm', {
		  method: 'post',
		  evalScripts: true,
		  parameters:  { search: search, count: count, email:email }
		});
}


// save car
function savecar(aid){
		new Ajax.Updater('updater','ajax/savecar.cfm', {
		  method: 'post',
		  evalScripts: true,
		  parameters:  { aid: aid}
		});
}

// Search check box

function checkers(page){
	var vals = $('rtsearch').serialize();
	$('ajaxer').src = 'ajax/'+page+'.cfm?'+vals;
}

function clearchex(what,val){
	$$('input.chk').each(function(item){
		if(item.name==what && item.value!=val)
			item.checked = false;
		else if(item.name==what && item.value==val)
			item.checked = true;
	});
}



// pop up

function showpop(){
	var arrayPageScroll = document.viewport.getScrollOffsets();
	var arrayPageSize = document.viewport.getDimensions();
	var lightboxTop = arrayPageSize.height/2 - 100 + arrayPageScroll[1];
	var lightboxLeft = arrayPageSize.width/2 - 200;
	$('popp').setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();
}



// post to clad

function postClad(aid){
	new Ajax.Updater('poppbod','ajax/post-cl-form.cfm', {
	  method: 'post',
	  evalScripts: true,
	  parameters: { aid: aid}
	});	
	showpop();
}
function refreshClad(leform){
	var formValues = leform.serialize();
	new Ajax.Updater('poppbod','ajax/post-cl-form.cfm', {
	  method: 'post',
	  evalScripts: true,
	  parameters: formValues
	});	
}




// pop up

function flagcar(aid){
	new Ajax.Updater('updater','ajax/flagcar_confirm.cfm', {
	  method: 'post',
	  evalScripts: true,
	  parameters: { aid: aid}
	});
}

function validateEmail(email) {
	AtPos = email.indexOf("@")
	StopPos = email.lastIndexOf(".")
	if (AtPos == -1 || StopPos == -1) {
		return false;
	} else if (StopPos < AtPos) {
		return false;
	} else if (StopPos - AtPos == 1) {
		return false;
	} else if (AtPos == 0) {
		return false;
	}  else if (StopPos == (email.length -1)) {
		return false;
	} else {
		return true; // good to go!
	}
}

// contact seller

function contactseller(leform){
	// validate form
	if (leform.name.value == ''){
		alert('Please enter your name.');
		return false;
	} else if (!validateEmail(leform.email.value)){
		alert('Please enter a valid email.');
		return false;
	} else {
		var formvals = leform.serialize(true);
		new Ajax.Updater('updater','ajax/contactseller.cfm', {
		  method: 'post',
		  evalScripts: true,
		  parameters: formvals
		});
		pageTracker._trackEvent('Lead', 'Contact Form', leform.et.value);
	}
	
	// message sent
	leform.hide();
	// confirmation
	var confirmdiv = leform.next('.message_sent');
	confirmdiv.update('&nbsp;<img src="images/ajax-loader.gif" width="35" height="35" alt="Sending message...">&nbsp;');
	confirmdiv.show();
	Element.update.delay(2,confirmdiv,'Your message has been sent.');

}


// contact seller

function tellafriend(leform){
	// validate form
	if (leform.name.value == ''){
		alert('Please enter your name.');
		return false;
	} else if (!validateEmail(leform.email.value)){
		alert('Please enter a valid "from" email.');
		return false;
	} else if (!validateEmail(leform.to.value)){
		alert('Please enter a valid "to" email.');
		return false;
	} else {
		var formvals = leform.serialize(true);
		new Ajax.Updater('updater','ajax/tellafriend.cfm', {
		  method: 'post',
		  evalScripts: true,
		  parameters: formvals
		});
		pageTracker._trackEvent('View', 'Tell a Friend', leform.et.value);
	}
	
	// message sent
	leform.hide();
	// confirmation
	var confirmdiv = leform.next('.message_sent');
	confirmdiv.setStyle({
		'float': 'none',
		height: '200px'
	});
	confirmdiv.update('&nbsp;<img src="images/ajax-loader.gif" width="35" height="35" alt="Sending message...">&nbsp;');
	confirmdiv.show();
	Element.update.delay(2,confirmdiv,'Your message has been sent.');

}



// login popup submit on enter

function entsub(loginform) {
  if (window.event && window.event.keyCode == 13)
    loginform.submit();
  else
    return true;}
	
// google maps

// GOOGLE MAPS...

    var map = null;
    var geocoder = null;

    function loadgmap(mapID,address) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(mapID));
		map.addControl(new GSmallMapControl());
     //   map.addControl(new GMapTypeControl());
        geocoder = new GClientGeocoder();
		geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
          	  map.addOverlay(marker);
            }
          }
        );
      }
    }
	
// JOIN news 
function joinnewsend(emailval){
	if (validateEmail(emailval)){
		$('joinnews').submit();
		$('joinup').style.display='none';
		$('joinupthx').style.display='block';
	} else {
		alert('Please enter a valid email');
	}
}