var isInternetExplorer = (navigator.appName.indexOf("Microsoft") != -1);

/*
future references:
var keycode = isInternetExplorer ? event.keyCode : event.which;
*/

function countSpaces(obj){
	var iLength = obj.value.length;
	var strSpaces = obj.value.match(new RegExp("( )", "g"));
	var countSpaces = strSpaces ? strSpaces.length : 0;
	return countSpaces;
}

function countLineBreaks(obj){
	var iLength = obj.value.length;
	var strLineBreaks = obj.value.match(new RegExp("(\\n)", "g"));
	var countLineBreaks = strLineBreaks ? strLineBreaks.length : 0;
	return countLineBreaks;
}

function textCounter(field, counter_field, maxlimit) {
	var lineBreaks = countLineBreaks(field);
	var adjust = isInternetExplorer ? 1 : 0;
	if (field.value.length - lineBreaks * adjust > maxlimit){
		field.value = field.value.substring(0, maxlimit + lineBreaks * adjust);
		field.focus();
	} else {
		counter_field.value = maxlimit - field.value.length + lineBreaks * adjust;
	}
}

function backToSection(backToURL, forceBackToURL){
	if(forceBackToURL == null) forceBackToURL = false;
	if(history.length > 1 && !forceBackToURL) history.back(); else window.location.href = backToURL;
}

function hideStatus() {
	window.defaultStatus='';
	window.status='';
	return true;
}

function searchReset() {
	tot = document.search_form.elements.length;
	for(i=0;i<tot;i++) {
		if (document.search_form.elements[i].type == 'text') {
			document.search_form.elements[i].value = "";
		} else if (document.search_form.elements[i].type == 'checkbox' || document.search_form.elements[i].type == 'radio') {
			document.search_form.elements[i].checked = false;
		} else if (document.search_form.elements[i].type == 'select-one') {
			document.search_form.elements[i].selectedIndex = 0;
		}
	}
	if ((document.search_form.country_id) || (document.search_form.state_id) || (document.search_form.region_id) || (document.search_form.city_id) || (document.search_form.area_id)) {
		searchLocationReset();
	}
}

function easyFriendlyUrl(name2friendlyurl, target, validchars, separator) {
	var str = "";
	var i;
	var exp_reg = new RegExp("[" + validchars + separator + "]");
	var exp_reg_space = new RegExp("[ ]");
	name2friendlyurl.toString();
	for (i=0 ; i<name2friendlyurl.length; i++) {
		if (exp_reg.test(name2friendlyurl.charAt(i))) {
			str = str+name2friendlyurl.charAt(i);
		} else {
			if (exp_reg_space.test(name2friendlyurl.charAt(i))) {
				if (str.charAt(str.length-1) != separator) {
					str = str + separator;
				}
			}
		}
	}
	if (str.charAt(str.length-1) == separator) str = str.substr(0, str.length-1);
	document.getElementById(target).value = str.toLowerCase();
}

function searchLocationReset() {
	if (document.search_form.country_id) {
		if (document.search_form.state_id) {
			while (document.search_form.state_id.options.length>1) {
				deleteIndex=document.search_form.state_id.options.length-1;
				document.search_form.state_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.region_id) {
			while (document.search_form.region_id.options.length>1) {
				deleteIndex=document.search_form.region_id.options.length-1;
				document.search_form.region_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.state_id) {
		if (document.search_form.region_id) {
			while (document.search_form.region_id.options.length>1) {
				deleteIndex=document.search_form.region_id.options.length-1;
				document.search_form.region_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.region_id) {
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.city_id) {
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	}
}

function $(id) {
	return document.getElementById(id);
}

function showText(text) {
	return unescape(text);
}



// Bubbles Javascript
// copyright 23rd February 2006, by Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration
// you can change the number of bubbles if you like

var num_bubbles = 15;
var bubbles = ["http:\/\/dev.handylocals.com.au\/uploads\/beating-heart.gif"];


// window size tests
function findLivePageWidth() {
		return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:700;
	}
	function findLivePageHeight() {
		return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:500;
		}
		
function posX() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;}
	
function posY() {
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;
	}
	
	

// make bubbles
var speed = 50;
var movw = new Array();
var move = new Array();
var stepw = new Array();
var posw = new Array();
var posh = new Array();
var dir = new Array();
var winWidth;
var winHeight;

function setSize() {
	winWidth = findLivePageWidth()-50;
	winHeight = findLivePageHeight()-50;}

function startBubbles() {
	setSize(); 
var content = ''; 
for (var i = 0; i < num_bubbles; i++){
	move[i] = 0;
	movw[i] = 11+ Math.random()*4;
	posw[i] = Math.random()*(winWidth-35)+12;
	posh[i] = Math.random()*winHeight;
	stepw[i] = 0.02 + Math.random()/10;dir[i] = (Math.random()>0.5)?1:-1;
	content += '<div id="bub'+ i +'" style="position: absolute; z-index: '+ i +'; visibility:hidden;" onclick="posh['+i+']=-50;"><img src="'+bubbles[Math.floor(Math.random()*bubbles.length)]+ '" border="0"></div>';
	} 
	
	document.getElementById('bubble').innerHTML = content;
	setTimeout("moreBubbles()", speed);
	}

function moreBubbles() {
	for (var i = 0; i < num_bubbles; i++) {
		if (posh[i] < 0) {
			posw[i] = 10+ Math.random()*(winWidth-movw[i]-30);
			posh[i] = winHeight;dir[i]=(Math.random()<0.5)?1:-1;
			stepw[i] = 0.02 + Math.random()/9;
			} 
			move[i] += stepw[i] *dir[i];  
			posh[i]-=1; 
			if (Math.abs(move[i]) > 3) {
				dir[i]=-dir[i];
				posw[i]+=movw[i]*move[i]; 
				move[i]=0;
				} 
				objstyle = document.getElementById('bub'+i).style; 
				objstyle.left = posX()+posw[i] + movw[i]*move[i]+"px"; 
				objstyle.top = posY()+posh[i] - 1+"px"; 
				objstyle.visibility = 'visible';
				} 
				setTimeout("moreBubbles()", speed);
				}
				

