function checkKeycode(e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	if (keycode == 13) { mlsSubmit('list'); }
}
function addThatThing(x) {
	var lastChar = x.length - 1;
	
	if (x.charAt(lastChar)!="&") {
		x += "&";
	}
	
	return x;
}
function Set_Cookie( name, value, expires, path, domain, secure ){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct expires time, the current script below will set 
	it for x number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
	
function Get_Cookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ){
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
	
function addCommas(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
	
// Change Price Ranges
function ChangeRange(){
	var rentrange = '500;750;1000;1250;1500;1750;2000;2500;3000;3500;4000;5000;6000;7000;8000;9000;10000;20000;30000;40000';
	var salerange = '50000;60000;70000;80000;90000;100000;110000;120000;130000;140000;150000;160000;170000;180000;190000;' + 
	'200000;210000;220000;230000;240000;250000;260000;270000;280000;290000;' + 
	'300000;310000;320000;330000;340000;350000;360000;370000;380000;390000;' + 
	'400000;420000;440000;460000;480000;500000;520000;540000;560000;580000;' + 
	'600000;620000;640000;660000;680000;700000;725000;750000;775000;' + 
	'800000;825000;850000;875000;900000;925000;950000;975000;' + 
	'1000000;1100000;1200000;1300000;1400000;1500000;1600000;1700000;1800000;1900000;' + 
	'2000000;2250000;2500000;2750000;3000000;3250000;3500000;3750000;4000000;4500000;' + 
	'5000000;5500000;6000000;6500000;7000000;8000000;9000000;10000000;20000000;30000000;40000000;50000000';
	var searchtype = document.getElementById('type').value;
	var icount=0;
	var myrange='';
	var objpmin = document.getElementById('lpmin');                                         
	var objpmax = document.getElementById('lpmax');
	var mymin = String(objpmin.value);
	var mymax = String(objpmax.value);                            
	
	//rental only
	if (searchtype==118){
		myrange = rentrange;
	}
	//non-rental
	else {
		myrange = salerange;
	}
	var pricerange = new Array();
	pricerange = myrange.split(';');
	
	objpmin.options.length = 0;
	var proption = new Option('No Min', '0');
	objpmin.options.add(proption, objpmin.options.length); 
	while (icount < pricerange.length){
		if (mymin == String(pricerange[icount])){
			var proption = new Option('$' + addCommas(pricerange[icount]), pricerange[icount], true, true);
		}
		else {
			var proption = new Option('$' + addCommas(pricerange[icount]), pricerange[icount]);
		}
		objpmin.options.add(proption, objpmin.options.length);
		icount +=1;
	}
	objpmax.options.length = 0; 
	var proption = new Option('No Max', '0');
	objpmax.options.add(proption, objpmax.options.length);
	icount = 0;
	while (icount < String(pricerange.length)){
		if (mymax == pricerange[icount]){
			var proption = new Option('$' + addCommas(pricerange[icount]), pricerange[icount], true, true);
		}
		else {
			var proption = new Option('$' + addCommas(pricerange[icount]), pricerange[icount]);
		}
		objpmax.options.add(proption, objpmax.options.length);
		icount +=1;
	}

}
// Custom Submit Function
function doSubmission () {
	// initialize work variables
	var theString='';
	var theReDirect = "search_listings.aspx?state=RI&";
	var y;
			
	// initialize all control variables
	var ctrls = new Array(3);
	ctrls[0] = document.getElementById("form1").lpmin;
	ctrls[1] = document.getElementById("form1").lpmax;
	ctrls[2] = document.getElementById("form1").type;
	ctrls[3] = document.getElementById("form1").town;
	
	if (((parseInt(ctrls[0].value)) > (parseInt(ctrls[1].value))) && ((parseInt(ctrls[1].value)) > 0)) {
		alert('The minimum price cannot be greater than the maximum price.\n\nPlease adjust your search');
		return;
	}
	
	
	
	// create query string & bypass any x=0 values
	for (y=0;y<4;y++) {
		if (ctrls[y].value!=0) {
			theString += ctrls[y].name;
			theString += "=";
			theString += ctrls[y].value;
			
			theString = addThatThing(theString);
		}
	}
	
	// save search
	Set_Cookie('HomeSearchFromPrice',ctrls[0].value,30,'','','');
	Set_Cookie('HomeSearchToPrice',ctrls[1].value,30,'','','');
	Set_Cookie('HomeSearchType',ctrls[2].value,30,'','','');
	
	// git 'r done!
	theReDirect += theString;
	window.location=theReDirect;
}
		
function StartupCookies(){
var cokfp = Get_Cookie('HomeSearchFromPrice');
var coktp = Get_Cookie('HomeSearchToPrice');
var coktype = Get_Cookie('HomeSearchType');
if (cokfp!=null) document.getElementById("lpmin").value = Get_Cookie('HomeSearchFromPrice');
if (coktp!=null) document.getElementById("lpmax").value = Get_Cookie('HomeSearchToPrice');
if (coktype!=null) document.getElementById("type").value = Get_Cookie('HomeSearchType');
}
navHover = function() {
	var lis = document.getElementById("navmenu").getElementsByTagName("LI");
	for (var i=0; i<lis.length; i++) {
		lis[i].onmouseover=function() {
			this.className+=" iehover";
		}
		lis[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", navHover);