function homelinks(newid) {
	tabid = document.getElementById(newid+'_');
	boxid = document.getElementById(newid);
	var ids = new Array('hl','hb','hd','tb','sb');
	var top = ids.length-1;
	for(i=0;i<=top;i++) {
		var tid = document.getElementById(ids[i]+'_');
		tid.className = 'inactive';
		var bid = document.getElementById(ids[i]);
		bid.style.display = 'none';
	}
	tabid.blur();
	tabid.className = 'active';
	boxid.style.display = 'block';
}
function homelinks2(newid) {
	$('.linking div').hide() ;
	$('.linking div#'+newid).show() ;
	$('.homelinks > a').removeClass('active') ;
	$('#'+newid+'_').addClass('active') ;
}

function show_phone(type,phonetype,m,adid){
	$.get('/ajax.php?action=phone&type='+type+'&phonetype='+phonetype+'&m='+m+'&adid='+adid , function(number){
		$('#phone'+phonetype).html(number) ;
	}) ;
}
function update_ad_preview(){
	var fields = [
		['headline',25],
		['description1',35],
		['description2',35],
		['url_display',35]
		] ;
	for( var i=0,l=fields.length ; i<l ; i++ ){
		var field = fields[i][0] ;
		var length = fields[i][1] ;
		var text = document.getElementsByName(field)[0].value.substring(0,length) ;
		//if( text ){
			document.getElementById('ad_'+field).innerHTML = text ;
		//}
	}
}
function url(u){
	window.location = u;
	return;
}
// Open new pop-up window with url, width and height
function openWindow(url,width,height) {
	var left = (window.screen.width - width) / 2;
	var top = (window.screen.height - height) / 2;
	var parameters = 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + ',toolbar=0,menubar=0,location=0,status=0,scrollbars=1,titlebar=0,resizable=0';
	window.open(url,'popup',parameters);
}

function check_trafficad_avail(){
	var bt_o = document.getElementsByName('business_type')[0] ;
	var bs_o = document.getElementsByName('business_state')[0] ;
	var bt   = bt_o[bt_o.selectedIndex].value ;
	var bs   = bs_o[bs_o.selectedIndex].value ;
	var id = document.getElementsByName('id')[0].value ;
	var url  = '/ajax.php?action=trafficad_avail&bt='+bt+'&bs='+bs+'&id='+id ;
	if( bt && bs ){
		$.get( url , function(data){
			var results   = data.split('|') ;
			var available = results[0] ;
			var message   = results[1] ;
			if( 1==available ){
				$('#main_form').show() ;
				$('#result_message').html(message).show() ;
				$('#trafficad_preview').show() ;
			}else{
				$('#main_form').hide() ;
				$('#result_message').html(message).show() ;
				$('#trafficad_preview').hide() ;
			}
		}) ;
	}
}
function tab_over(id){
	$('#tab_'+id).addClass('hover') ;
}
function tab_out(id){
	$('#tab_'+id).removeClass('hover') ;
}
function switch_panel(id){
	$('#tab_bar .tab').removeClass('selected') ;
	$('#tab_'+id).addClass('selected') ;
	$('.panel').hide() ;
	$('#panel_'+id).show() ;
}
function loadscreen(){
	document.body.innerHTML = '' ;
	document.body.style.background = "url(/images/soleil.gif) no-repeat center center" ;
}
function input_out(el,s){
	if( '' == el.value ){
		el.value = s ;
	}
}

function input_in(el,s){
	if( s == el.value ){
		el.value = '' ;
	}
}
function update_name_display(){
	var fn = document.getElementsByName('firstname')[0].value ;
	var ln = document.getElementsByName('lastname')[0].value ;
	var fn_new = (fn.length>0) ? fn : 'Firstname' ;
	var ln_new = (ln.length>0) ? ln : 'Lastname' ;
	document.getElementById('container_display1').innerHTML = fn_new+' '+ln_new.charAt(0).toUpperCase()+'.' ;
	document.getElementById('container_display2').innerHTML = fn_new+' '+ln_new ;
	var f = document.getElementsBytagname('form');
	alert(f[0]);
}
function update_highlights_counter(){
	var max = 64 ;
	var chars = document.getElementsByName('highlights')[0].value.length ;
	var left = max - chars ;
	if( left < 0 ){
		var message = '<span style="color:#F87311;">'+(-1*left) + ' too many characters</span>' ;
	}
	else{
		var message = left + ' characters left' ;
	}
	document.getElementById('highlights_counter').innerHTML = message ;
}
function update_counter(field,id,max){
	var chars = field.value.length ;
	var left = max - chars ;
	if( left < 0 ){
		var message = '<span style="color:#F87311;">'+(-1*left) + ' too many characters</span>' ;
	}
	else{
		var message = left + ' characters left' ;
	}
	document.getElementById('counter_'+id).innerHTML = message ;
}
function update_residence_options(){
	var residence = document.getElementById('checkbox_residence').checked ;
	var display = residence ? '' : 'none' ;
	var cr = getElementsByClass('container_residence') ;
	for( i=0,l=cr.length ; i<l ; i++ ){
		cr[i].style.display = display ;
	}
}
function update_trailer_options(){
	var tid_sel = document.getElementById('tid_selector') ;
	var size_r = document.getElementById('size_required') ;
	var size_sel = document.getElementById('size_selector') ;
	if( 1==tid_sel[tid_sel.selectedIndex].value ){
		size_sel.disabled = false ;
		size_r.innerHTML = '*' ;
	} else {
		size_sel.selectedIndex = 0 ;
		size_sel.disabled = true ;
		size_r.innerHTML = '(optional)' ;
	}
}
function update_property_options(){
	var pid_sel = document.getElementById('pid_selector') ;
	var pid = pid_sel[pid_sel.selectedIndex].value ;
	var clt = document.getElementById('container_landtype') ;
	var cft = document.getElementById('container_farmtype') ;
	var cht = document.getElementById('container_hometype') ;
	var crt = document.getElementById('container_ranchtype') ;
	var ccb = document.getElementById('container_category_blank') ;
	var la  = document.getElementById('label_area') ;
	clt.style.display = 'none' ;
	cft.style.display = 'none' ;
	cht.style.display = 'none' ;
	crt.style.display = 'none' ;
	ccb.style.display = 'none' ;
	var cr = document.getElementById('category_required') ;
	if( 1 == pid ){
		clt.style.display = '' ;
		//la.innerHTML = 'Area *' ;
		cr.innerHTML = '*' ;
	}
	else if( 2 == pid ){
		cft.style.display = '' ;
		//la.innerHTML = 'Area *' ;
		cr.innerHTML = '*' ;
	}
	else if( 3 == pid ){
		cht.style.display = '' ;
		//la.innerHTML = 'Lot Size *' ;
		cr.innerHTML = '(optional)' ;
	}
	else if( 4 == pid ){
		crt.style.display = '' ;
		//la.innerHTML = 'Area *' ;
		cr.innerHTML = '*' ;
	}
	else {
		document.getElementById('container_category_blank').style.display = '' ;
	}
	//var rooms_disabled = 3 == pid ? false : true ;
	//document.getElementById('bedrooms').disabled = rooms_disabled ;
	//document.getElementById('bathrooms').disabled = rooms_disabled ;
}
function update_county(current){
	var ss = document.getElementById('state_selector') ;
	var state = ss[ss.selectedIndex].value ;
	var url = 'http://www.horseclicks.com/ajax.php?action=counties&state=' + state + (current?('&current='+escape(current)):'') ;
	$('#county_container').load(url) ;
}
function update_state(){
	var state_required = document.getElementById('state_required') ;
	var state_selector = document.getElementsByName('state')[0] ;
	var country_selector = document.getElementsByName('country')[0] ;
	var current_country = country_selector[country_selector.selectedIndex].value ;
	if( 'US'==current_country || 'CA'==current_country ){
		state_required.innerHTML = '*' ;
		state_selector.disabled = false ;
	}
	else{
		state_selector.selectedIndex = 0 ;
		state_required.innerHTML = '(optional)' ;
		state_selector.disabled = true ;
	}
}
function update_county_disable(){
	var county_required = document.getElementById('county_required') ;
	var county_selector = document.getElementsByName('county')[0] ;
	var country_selector = document.getElementsByName('country')[0] ;
	var current_country = country_selector[country_selector.selectedIndex].value ;
	if( 'US'==current_country ){
		county_required.innerHTML = '*' ;
		if( county_selector ){
			county_selector.disabled = false ;
		}
	}
	else{
		county_selector.selectedIndex = 0 ;
		county_required.innerHTML = '(optional)' ;
		county_selector.disabled = true ;
	}
}
function update_state_disable(){
	var state_selector = document.getElementsByName('state')[0] ;
	var country_selector = document.getElementsByName('country')[0] ;
	var current_country = country_selector[country_selector.selectedIndex].value ;
	if( 'US'==current_country || 'CA'==current_country ){
		state_selector.disabled = false ;
	}
	else{
		state_selector.selectedIndex = 0 ;
		state_selector.disabled = true ;
	}
}
function update_price_label(){
	var price_labels = getElementsByClass('price_label') ;
	var type_selector = document.getElementsByName('tid')[0] ;
	var current_type = type_selector.selectedIndex ;
	if( 2==current_type ){
		var newlabel = 'Stud Fee' ;
	}
	else{
		var newlabel = 'Price' ;
	}
	for( var i=0,l=price_labels.length ; i<l ; i++ ){
		price_labels[i].innerHTML = newlabel ;
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function confirm_url(url,message){
	if ( window.confirm(message) ){
		window.location = url;
 	}
}

function switch_mainpic(path,xpath){
	var gmap = document.getElementById('gmap_container') ;
	if(gmap){
		gmap.style.display = 'none' ;
	}
	document.getElementById('mainpic_container').style.display = '' ;
	//if( xpath && path ){
	//	var html = '<a href="javascript:xpic(\''+xpath+'\');"><img id="mainpic" src="'+path+'" /></a>' ;
	//}else if( path ){
		var html = '<img id="mainpic" src="'+path+'" />' ;
	//}
	//if( html ){
		document.getElementById('mainpic_container').innerHTML = html ;
	//}
}
function xpic(path){
	var width = 497 ;
	var height = 500 ;
	var left = (window.screen.width - width) / 2 ;
	var top = (window.screen.height - height) / 2 ;
	var parameters = 'left='+left+',top='+top+',width='+width+',height='+height+',toolbar=0,menubar=0,location=0,status=0,titlebar=0,resizable=0' ;
	window.open('/m_bigpics.php?path='+path,'',parameters) ;
}


// optimize adsense by swapping units (bottom is more clickable but top is better payable)
function ga_optimize() {
	var ga_top = document.getElementById('ga_top');
	var ga_bot = document.getElementById('ga_bot');
	if( ga_top!=null && ga_bot!=null ) {
		var top = ga_top.innerHTML ;
		var bot = ga_bot.innerHTML ;
		ga_top.innerHTML = bot ;
		ga_bot.innerHTML = top ;
	}
}

window.onload = function() { setTimeout('ga_optimize()',1000); }



// google maps
var map = null;
var geocoder = null;
var gmap_marker = null;

function gmap_display(){
	document.getElementById('mainpic_container').style.display = 'none' ;
	document.getElementById('gmap_container').style.display = '' ;
	if( ! map ){
		gmap_initialize();
	}
	gmap_showAddress(document.getElementById('gmap_location').value) ;
}
function gmap_domap(){
}
function gmap_initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("gmap_container"));
		geocoder = new GClientGeocoder();
	}
}
function gmap_showAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " not found");
				} else {
					map.setCenter(point,8);
					if( ! gmap_marker ){
						gmap_marker = new GMarker(point);
						map.addOverlay(gmap_marker);
						map.addControl(new GSmallMapControl());
						map.addControl(new GMapTypeControl());
					}
				}
			}
		);
	}
}
