﻿
/* =========================================================================== */
/* QuickSearch3.js - supports multiple quicksearch forms on the same page      */
/* =========================================================================== */


/* =========== configuration section ========================================== */

var searchResale = 1 ;
var searchRental = 2 ;
var searchRentalLongTerm = 3 ;
var searchRentalShortTerm = 4 ;

var qsForms = new Array(searchRentalShortTerm) ;

qsForms[searchResale] = "qsFormResale" ;
qsForms[searchRental] = "qsFormRental" ;
qsForms[searchRentalLongTerm] = "qsFormRentalLong" ;
qsForms[searchRentalShortTerm] = "qsFormRentalShort" ;

// fill in the name(s) of the search results pages for the different types of quick searches
var resultsPage = new Array(searchRentalShortTerm) ;

resultsPage[searchResale] = "suninvestresults.php" ;
resultsPage[searchRental] = "" ;
resultsPage[searchRentalLongTerm] = "" ;
resultsPage[searchRentalShortTerm] = "" ;

/* =========== end of configuration section =================================== */


function f_submitSearch(searchForm)
{
	var qsType, PriceMin, PriceMax, RentalType ;
	
	for ( qsType=searchResale; qsType<=searchRentalShortTerm; qsType++ )
	{
		if ( searchForm == qsForms[qsType] ) break ;
	}

	eval("PriceMin = document."+searchForm+".PriceMin.value") ;
	eval("PriceMax = document."+searchForm+".PriceMax.value") ;
	eval("RentalType = document."+searchForm+".RentalType.value") ;

	if ( PriceMin == '' )
	{
		PriceMin = 0 ;
		eval("document."+searchForm+".PriceMin.value = 0") ;
	}

	if ( PriceMax == '' )
	{
		PriceMax = 20000000 ;
		if ( RentalType == 'S' ) PriceMax = 2000 ;
		if ( RentalType == 'L' ) PriceMax = 20000 ;
		eval("document."+searchForm+".PriceMax.value = "+PriceMax) ;
	}
	
	var x = parseFloat(PriceMin);
	var y = parseFloat(PriceMax);
	var e = true ;

	if ( isNaN(x) )
	{
		alert("Minimum Price is Invalid ");
		e = false ;
	}
	else if ( isNaN(y) )
	{
		alert("Maximum Price is Invalid ");
		e = false ;
	}
	else if ( x > y )
	{
		alert("Maximum Price is less than Minimum Price ");
		e = false ;
	}
	
	if ( e )
	{
		document.getElementById(searchForm).action = resultsPage[qsType] ;
		document.getElementById(searchForm).submit() ;
	}
}

function f_checkNumber( objname, decplaces, reqdfield )
{
	var num  = objname;
	var dp   = decplaces;
	var reqd = reqdfield;
	var x    = num.value;

	if ( x == "" )
	{
		if ( reqd )
		{
			alert( "Please enter a numeric value in this field " );
			num.select();
			num.focus();
			return false;
		}
		else
		{
			num.value = 0;
			return true;
		}
	}

	var dpFound  = false;
	var dpCount  = 0;
	var allValid = true;
	var checkOK ;

	if ( dp > 0 )
		checkOK = ".0123456789"
	else
		checkOK = "0123456789";
  
	for ( i = 0;  i < x.length;  i++ )
	{
		ch = x.charAt(i);
	
		for ( j = 0;  j < checkOK.length;  j++ )

			if ( ch == checkOK.charAt(j) )
			{
				if ( ch == "." )
				{
					if ( dpFound )  // more than one decimal point found
					{
						allValid = false;
						break;
					}
					else
					{
						dpFound = true;
					}
				}
				else
				{
					if ( dpFound ) dpCount++ ;
				}
		
				break;
			}
	  
		if ( j == checkOK.length )
		{
			allValid = false;
			break;
		}
	}
  
	if ( !allValid )
	{
		alert( "Please enter only numeric values in this field " );
		num.select();
		num.focus();
		return false;
	}

	if ( dpCount > dp )
	{
		alert( "Please enter only "+dp+" decimal places in this field " );
		num.select();
		num.focus();
		return false;
	}

	return true;
}

