//<![CDATA[
var needValidate = false;
 

function checkSelectVal(elm) {
	
	if ((elm).val() != '') {
		$(elm).removeClass('empty');
	 }	else {
		$(elm).addClass('empty') 
	 }
}

function removeVal(elm, initVal) {
	if ($(elm).val() == initVal) {
		$(elm).removeClass('empty')
		$(elm).val('');
	}	 	
}
 
function restoreVal(elm, initVal) {
	if ($(elm).val() == '') {
		$(elm).addClass('empty')
		$(elm).val(initVal);
	}	 	
}

function cleanValue(numVal) {
	
	var emptyVal = '';
	var alphaNumExp = /^[0-9]+$/;
	
	var fieldToClean = numVal;
	
	if ((fieldToClean != "")) {
		var tempName = '';
		
		for(var i = 0; i < fieldToClean.length; i++) {
			var c = fieldToClean.charAt(i);
			if (c.match(alphaNumExp)) {
				tempName += c;
			}				
		}
		
		return tempName;
	}
	
	return emptyVal;
}

function updateQuickSearchCount(searchType, getCount) {
	var formData;
	var frmAction;
	var submitButton;
	var counter;
	var city = '';
	var subdivision = '';
	var zip = '';
	var lprice = '';
	var uprice = '';
	var lbeds = '';
	var lbaths = '';
	var lsqft = '';
	var lgaragecap = '';
	var daysonmarket = '';
	var mls = '';
	var streetname1 = '';
	var streetnumber1 = '';
	var streetname2 = '';
	var streetnumber2 = '';	
	var streetname3 = '';
	var streetnumber3 = '';
	var streetname4 = '';
	var streetnumber4 = '';
	var quicksearchid = '';
	var foreclosure = '';
	var shortsale = '';
	var waterfront = '';
	var beachfront = '';
	var newconstruction = '';
	
	var sep = '';	
	
	
	
	if(getCount === undefined) getCount = true;
	
	switch (searchType) {
		
		case '1': 
			var locationType = document.frmQSArea.qs_loc[document.frmQSArea.qs_loc.selectedIndex].parentNode.getAttribute('label');
			var location = document.frmQSArea.qs_loc[document.frmQSArea.qs_loc.selectedIndex].value;
			
			if(locationType === 'City') city = location;
			if(locationType === 'Subdivision') subdivision = location;
			if(locationType === 'Zip Code') zip = location;
			
			var propertyFeature = document.frmQSArea.qs_propfeatures[document.frmQSArea.qs_propfeatures.selectedIndex].value;
			
			if(propertyFeature === 'foreclosure') foreclosure = 'true';
			if(propertyFeature === 'shortsale') shortsale = 'true';
			if(propertyFeature === 'waterfront') waterfront = 'true';
			if(propertyFeature === 'beachfront') beachfront = 'true';
			if(propertyFeature === 'newconstruction') newconstruction = 'true';			
			
			lprice = document.frmQSArea.qs_minprice[document.frmQSArea.qs_minprice.selectedIndex].value;
			uprice = document.frmQSArea.qs_maxprice[document.frmQSArea.qs_maxprice.selectedIndex].value;
			lbeds = document.frmQSArea.qs_lbeds[document.frmQSArea.qs_lbeds.selectedIndex].value;
			lbaths = document.frmQSArea.qs_lbaths[document.frmQSArea.qs_lbaths.selectedIndex].value;
			lsqft = document.frmQSArea.qs_lsqfeet[document.frmQSArea.qs_lsqfeet.selectedIndex].value;
			lgaragecap = document.frmQSArea.qs_garagecap[document.frmQSArea.qs_garagecap.selectedIndex].value;
			daysonmarket = document.frmQSArea.qs_daysonmarket[document.frmQSArea.qs_daysonmarket.selectedIndex].value;
			
			submitButton = $('#area_search div.boxSubmit div.totallistings');
			
			break;
	    case '2':
			for (counter = 0; counter < document.frmQSMLS.qs_mls.length; counter++) {
				if ((document.frmQSMLS.qs_mls[counter].value != 'MLS #') && (document.frmQSMLS.qs_mls[counter].value != '')) {
					mls += sep + document.frmQSMLS.qs_mls[counter].value;
					sep = ',';
				}
			}
			sep = '';
			
			quicksearchid = document.frmQSMLS.quicksearchid.value;
			
			submitButton = $('#mls_search div.boxSubmit div.totallistings');
			
			if(document.frmQSMLS.mlsregion.value != document.frmQSSubmitter.mlsregion.value) { 
				document.frmQSMLS.mlsregion.value = document.frmQSSubmitter.mlsregion.value
			}
			
			break;	
		case '3':
			if (document.frmQSStreet.qs_streetname1.value != 'Enter Street Name') { streetname1 = document.frmQSStreet.qs_streetname1.value }
			if (document.frmQSStreet.qs_streetname2.value != 'Enter Street Name') { streetname2 = document.frmQSStreet.qs_streetname2.value }
			if (document.frmQSStreet.qs_streetname3.value != 'Enter Street Name') { streetname3 = document.frmQSStreet.qs_streetname3.value }
			if (document.frmQSStreet.qs_streetname4.value != 'Enter Street Name') { streetname4 = document.frmQSStreet.qs_streetname4.value }
			

			if (document.frmQSStreet.qs_streetnumber1.value != 'Enter Street #') { streetnumber1 = document.frmQSStreet.qs_streetnumber1.value }
			if (document.frmQSStreet.qs_streetnumber2.value != 'Enter Street #') { streetnumber2 = document.frmQSStreet.qs_streetnumber2.value }
			if (document.frmQSStreet.qs_streetnumber3.value != 'Enter Street #') { streetnumber3 = document.frmQSStreet.qs_streetnumber3.value }
			if (document.frmQSStreet.qs_streetnumber4.value != 'Enter Street #') { streetnumber4 = document.frmQSStreet.qs_streetnumber4.value }
			
			quicksearchid = document.frmQSStreet.quicksearchid.value;
			
			submitButton = $('#add_search div.boxSubmit div.totallistings');
			
			if(document.frmQSStreet.mlsregion.value != document.frmQSSubmitter.mlsregion.value) { 
				document.frmQSStreet.mlsregion.value = document.frmQSSubmitter.mlsregion.value
			}
			
			break;	
		default:
			var locationType = document.frmQSArea.qs_loc[document.frmQSArea.qs_loc.selectedIndex].parentNode.getAttribute('label');
			var location = document.frmQSArea.qs_loc[document.frmQSArea.qs_loc.selectedIndex].value;
			
			if(locationType === 'City') city = location;
			if(locationType === 'Subdivision') subdivision = location;
			if(locationType === 'Zip Code') zip = location;
			
			var propertyFeature = document.frmQSArea.qs_propfeatures[document.frmQSArea.qs_propfeatures.selectedIndex].value;
			
			if(propertyFeature === 'foreclosure') foreclosure = 'true';
			if(propertyFeature === 'shortsale') shortsale = 'true';
			if(propertyFeature === 'waterfront') waterfront = 'true';
			if(propertyFeature === 'beachfront') beachfront = 'true';
			if(propertyFeature === 'newconstruction') newconstruction = 'true';			
			
			lprice = document.frmQSArea.qs_minprice[document.frmQSArea.qs_minprice.selectedIndex].value;
			uprice = document.frmQSArea.qs_maxprice[document.frmQSArea.qs_maxprice.selectedIndex].value;
			lbeds = document.frmQSArea.qs_lbeds[document.frmQSArea.qs_lbeds.selectedIndex].value;
			lbaths = document.frmQSArea.qs_lbaths[document.frmQSArea.qs_lbaths.selectedIndex].value;
			lsqft = document.frmQSArea.qs_lsqfeet[document.frmQSArea.qs_lsqfeet.selectedIndex].value;
			lgaragecap = document.frmQSArea.qs_garagecap[document.frmQSArea.qs_garagecap.selectedIndex].value;
			daysonmarket = document.frmQSArea.qs_daysonmarket[document.frmQSArea.qs_daysonmarket.selectedIndex].value;
			
			submitButton = $('#area_search div.boxSubmit div.totallistings');
			
			break;
	}
	
	//alert('searchType4: ' + searchType);
	
	document.frmQSSubmitter.quicksearchid.value = quicksearchid;
	document.frmQSSubmitter.city.value = city;
	document.frmQSSubmitter.subdivision.value = subdivision;
	document.frmQSSubmitter.zip.value = zip;
	document.frmQSSubmitter.lprice.value = lprice;
	document.frmQSSubmitter.uprice.value = uprice;
	document.frmQSSubmitter.foreclosure.value = foreclosure;	
	document.frmQSSubmitter.shortsale.value = shortsale;
	document.frmQSSubmitter.waterfront.value = waterfront;
	document.frmQSSubmitter.beachfront.value = beachfront;
	document.frmQSSubmitter.newconstruction.value = newconstruction;
	document.frmQSSubmitter.lbeds.value = lbeds;
	document.frmQSSubmitter.lbaths.value = lbaths;	
	document.frmQSSubmitter.lsqft.value = lsqft;
	document.frmQSSubmitter.lgaragecap.value = lgaragecap;
	document.frmQSSubmitter.daysonmarket.value = daysonmarket;
	document.frmQSSubmitter.mls.value = mls;
	document.frmQSSubmitter.streetname1.value = streetname1;
	document.frmQSSubmitter.streetnumber1.value = streetnumber1;
	document.frmQSSubmitter.streetname2.value = streetname2;
	document.frmQSSubmitter.streetnumber2.value = streetnumber2;	
	document.frmQSSubmitter.streetname3.value = streetname3;
	document.frmQSSubmitter.streetnumber3.value = streetnumber3;
	document.frmQSSubmitter.streetname4.value = streetname4;
	document.frmQSSubmitter.streetnumber4.value = streetnumber4;
	
	if(getCount) {	
		var formData = $('#frmQSSubmitter').serialize();	
		var pointCount;
		
		$.ajax({
			type:'POST',
			data:formData,
			url:'/property-search/sist_ajax/mls_mapmarkerscount.asp',
			dataType:'xml',
			error:function(result,mes1,tperr){
			  alert('error: ' + result.responseText  + "\n" + mes1 + "\n" + tperr);
			},
			success:function(xml){
				
				$(xml).find('results').each(function(){
					pointCount = $(this).find('count').attr('value');
					
					//alert('pointCount: ' + pointCount);
					
					if ((pointCount + '') != '') {
						pointCount = parseFloat(pointCount)
					} else {
						pointCount = 0
					}
				});
				
				if (pointCount >= 1) { 
					var listings = pointCount + '';
					listings = listings.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
					submitButton.html( '<span>' + listings + '</span> Matching Listings');
				} else {
					submitButton.text('No Matching Listings');
				}
				
			}	
		});
	}
	
}

function runSidebarQuickSearch(searchType, searchAction) {
	var frmAction;
	var submitButton;
	
	if (searchAction == 'map') {
		frmAction = '/property-search/results/?searchtype=3'
	}	else {
		frmAction = '/property-search/results/?searchtype=2/'
	}
	
	switch (searchType){
		case '1':
			submitButton = $('#area_search div.boxSubmit div.totallistings');
			break;
	    case '2':
			submitButton = $('#mls_search div.boxSubmit div.totallistings');
			break;
		case '3':
			submitButton = $('#add_search div.boxSubmit div.totallistings');
			break;
		default:
			submitButton = $('#area_search div.boxSubmit div.totallistings');
			break;
	}
	
	if (submitButton.text() != 'No Matching Listings' && submitButton.text() != 'Find Matching Listings') { 
		document.frmQSSubmitter.action = frmAction;
		document.frmQSSubmitter.target = '_parent';	
		document.frmQSSubmitter.submit();
	}
	return false
}

function qsvalidateLPrice()
{
		
	var lPrice = document.getElementById('qs_minprice');
	var uPrice = document.getElementById('qs_maxprice');
	
	var minPrice = parseInt(lPrice.options[lPrice.selectedIndex].value);
	var maxPrice = parseInt(uPrice.options[uPrice.selectedIndex].value);
	
	if(isNaN(maxPrice))
	{
		maxPrice = 0;
	}
	var newSelected = false;

	var newMax = lPrice.options[lPrice.selectedIndex + 1].value;
	if(maxPrice > newMax)
	{
		newMax = maxPrice;
	}
	
	for (var i=1;i<lPrice.options.length; i++)
	{
	
		if(parseInt(lPrice.options[i].value) < minPrice)
		{
			//alert("Set it grey");			
			//lPrice.options[i].style.color='#999999';
		}
		else if(parseInt(lPrice.options[i].value) <= newMax)
		{
			lPrice.options[i].style.color='#000000';
		}
		else
		{
			break;
		}
	}

	for (var i=uPrice.options.length - 1;i>=0; i--) {
		if(parseInt(uPrice.options[i].value) < minPrice)
		{
			//uPrice.options[i].disabled = true;					
			//alert(i);
			//alert(uPrice.options[i].style.color);
			uPrice.options[i].style.color = '#CCCCCC';
			//alert(uPrice.options[i].style.color);
			if(!newSelected && maxPrice < minPrice)
			{
				if( (i + 2) < uPrice.options.length)
				{					
					if(uPrice.selectedIndex != 0)
					{
						uPrice.options[i + 2].selected = true;
					}
					newSelected = true;
				}
			}
			//uPrice.remove(i);
		}
		else
		{
			uPrice.options[i].style.color='#000000';
			//uPrice.options[i].disabled = false;
		}
	}

}

function qsvalidateUPrice()
{	

	var lPrice = document.getElementById('qs_minprice');
	var uPrice = document.getElementById('qs_maxprice');
	
	var minPrice = parseInt(lPrice.options[lPrice.selectedIndex].value);
	var maxPrice = parseInt(uPrice.options[uPrice.selectedIndex].value);
	
	if(isNaN(minPrice))
	{
		minPrice = 0;
	}
	uPrice.options[uPrice.selectedIndex].style.color = '#000000';
	//var newSelected = false;
	for (var i=lPrice.options.length - 1;i>=0; i--) {
		if(parseInt(lPrice.options[i].value) >= maxPrice)
		{
			lPrice.options[i].style.color='#CCCCCC';
			//lPrice.options[i].disabled = true;
			//lPrice.options[i].style.display = "none"
			//lPrice.remove(i);
			if( minPrice > maxPrice)
			{
				lPrice.options[i - 1].selected = true;	
				lPrice.options[i - 1].style.color = '#000000';
			}
		}	
		else if(parseInt(lPrice.options[i].value) >= minPrice)
		{
			lPrice.options[i].style.color='#000000';
			//lPrice.options[i].disabled = false;
		}
	}
}

function popOHWin (theURL) { 
  var newWin = window.open(theURL, 'OHWin', 'location=1,status=1,scrollbars=1,resizable=1,toolbar=1,width=770,height=550');
}
		   
function popWin (theURL) { //v2.0
  var newWin = window.open(theURL, 'realtyInfo', 'location=1,status=1,scrollbars=1,resizable=1,toolbar=1,width=770,height=540');
  newWin.moveTo(10,10); 
}

function vidWin (theURL,theName,theFeatures) {
  var newVid = window.open(theURL, theName, theFeatures);
}
 
function noSpam(user,domain) {
locationstring = "mailto:" + user + "@" + domain;
window.location = locationstring;
}
 
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//]]>
