var form;
var vars;
var iM=42;

function sClick(){
	vars=new Array();
	if(!checkPush('mls')){
		checkPush('clas');
		checkPush('addr');
		checkPush('type');
		checkPush('city');
		if(form['city'].selectedIndex==[iM]){
			checkPush('area');
		}else if(area){
			vars.push('area='+area);
		}
		checkPush('pricA');
		checkPush('pricB');
		switch(form['clas'].selectedIndex){
			case 0:
				checkPush('bdrmA');
				checkPush('bdrmB');
				checkPush('garaA');
				checkPush('garaB');
				checkPush('sqftA');
				checkPush('sqftB');
				break;
			case 1:
				checkPush('terr');
				checkPush('acreA');
				checkPush('acreB');
				break;
			case 2:
				checkPush('prus','C');
				break;
			case 3:
				checkPush('styl');
				checkPush('unitA');
				checkPush('unitB');
				break;
			case 4:
				checkPush('prus','L');
				break;
		}
	}
	checkPush('disp');
	window.location='../search/?'+vars.join('&');
}
function checkPush(n,m){
	var v=form[n+(m?m:'')].value;
	if(v!=null&&v!=''){
		vars.push(n+'='+v);
		return true;
	}else{
		return false;
	}
}

function selected(n,v){
	v=v.toLowerCase();
	for(var i=0;i<form[n].options.length;i++){
		if(form[n].options[i].value.toLowerCase()==v){
			form[n].selectedIndex=i;
			return true;
		}
	}
	return false;	
}

function restrictNum(e){
	return (('0123456789').indexOf(String.fromCharCode(e.which))>-1);
}

function sClickE(){
	if(checkEmpty('name')&&checkEmail('emai')&&checkEmpty('mess')){
		jQuery.post('../php/function/emailD.php',{name:value('name'),emai:value('emai'),phon:value('phon0','phon1','phon2'),mess:value('mess'),mls:mls},
		function(data){
			if(data==1){
				alert('listing details and message successfully sent\n\ntalk to you soon, '+value('name').split(' ')[0]+'!');
				clear('name','emai','phon0','phon1','phon2','mess');
			}else{
				alert('message sending failed');
			}
		});
	}
}
function value(){
	var r=new Array();
	for (var i in arguments){
		r.push(document.getElementById(arguments[i]).value);
	}
	return r.join(' ');
}
function clear(){
	for (var i in arguments){
		document.getElementById(arguments[i]).value='';
	}
}
function checkEmpty(fID){
	var f=document.getElementById(fID);
	if(f.value.match(/\S/)){
		return true;
	}else{
		switch(fID){
			case 'name':
				alert('name required');
				break;
			case 'mess':
				alert('message required');
				break;
		}
		f.focus();
		return false;
	}
}
function checkEmail(f){
	f=document.getElementById(f);
	if(f.value.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/)){
		return true;
	}else{
		alert('email address invalid');
		f.focus();
		return false;
	}
}
function autoTab(f,n){
  if(f.value.length>=f.maxLength){
    document.getElementById(n).focus();
  }
}

var types=new Array(
	new Array('condo','manufactured/model with acreage','residential/acreage','single family','townhouse'),
	new Array('commercial','multi-family','ranch','recreational','residential acreage','residential lot'),
	new Array('business opportunity','commercial','industrial','recreational'),
	new Array('apartment building','duplex','more than 4 units','other','quadplex','residential with rental','triplex'),
	new Array('commercial space for lease')
);	
function changeClas(){
	var ind=form['clas'].selectedIndex;
	var t=document.getElementById('type');
	t.options.length=0;
	t.options.add(new Option('--ANY--',''));
	for(var i=0;i<types[ind].length;i++){
		t.options.add(new Option(types[ind][i],i+1));
	}
	var c=new Array('res','lan','com','mul','lea');
	for(var i=0;i<c.length;i++){
		document.getElementById('t_'+c[i]).style.display=(ind==i?'block':'none');
	}
}
var area;
function changeCity(){
	document.getElementById('tr_area').style.display=(form['city'].selectedIndex==iM?'table-row':'none');
}

function optionsCity(){
	var cities;
	if(area==95){
		cities=('Big Arm|Bigfork|Columbia Falls|Dayton|Kalispell|Kila|Lakeside|Marion|Polson|Rollins|Somers|Whitefish').split('|');
	}else{
		cities=('Alberton|Anaconda|Arlee|Big Arm|Bigfork|Bonner|Charlo|Clinton|Columbia Falls|Condon|Coram|Corvallis|Darby|Dayton|De Borgia|Deborgia|Deer Lodge|Dillon|Dixon|Drummond|East Missoula|Elmo|Florence|Frenchtown|Garrison|Georgetown Lake|Greenough|Hall|Hamilton|Haugan|Helena|Helmville|Hot Springs|Huson|Kalispell|Lakeside|Libby|Lincoln|Lolo|Marion|Maxville|Missoula|Moiese|Noxon|Ovando|Pablo|Paradise|Philipsburg|Plains|Polson|Potomac|Rollins|Ronan|Saint Ignatius|Seeley Lake|Somers|St. Ignatius|St. Regis|Stevensville|Sula|Superior|Swan Valley|Thompson Falls|Trout Creek|Troy|Turah|Victor|Whitefish').split('|');
	}
	var t=document.getElementById('city');
	t.options.length=0;
	t.options.add(new Option('--ANY--',''));
	for(var i=0;i<cities.length;i++){
		t.options.add(new Option(cities[i],cities[i]));
	}
	document.getElementById('a_flathead').style.display=(area==95?'table-row':'none');
}

function removeFlathead(){
	area=null;
	optionsCity();
}
