/*

        Filename:          search_mls.js
        Type:              Javascript
        Description:       Functions to search MLS listings
        Site:              LoopNorth.com
        Author:            Steven Dahlman, DCM Software
        Start date:        12-04-09
        Last modification: 07-07-10

*/

//
// Function:    showlistimg
//
// Description: Display listing image in new window
//
// Input:
//
// title   = Window title
// baseurl = Base URL
// file    = Image path/filename
// width   = Image width (pixels)
// height  = Image height (pixels)
//
// actual  = 0 to use specified width and height (default)
//         = 1 to display image at actual size
//
function showlistimg (title, baseurl, file, width, height, actual) {

	var wwidth = width + 40;                 // Window width (pixels)
	var wheight = height + 80;               // Window height (pixels)

	var maxwidth = screen.availWidth - 10;   // Maximum width (pixels)
	var maxheight = screen.availHeight - 10; // Maximum height (pixels)

	// Shrink to fit available space
	if ( wwidth > maxwidth )   { wwidth = maxwidth; }
	if ( wheight > maxheight ) { wheight = maxheight; }

	// Center window on screen
	var wleft = (screen.availWidth - wwidth) / 2;
	var wtop = (screen.availHeight - wheight) / 2;

	// Window features
	var wfeatures = 'width=' + wwidth + ',height=' + wheight + ',left=' + wleft + ',top=' + wtop;
	wfeatures += ',directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no';

	// Open window
	var imgWin = window.open('','',wfeatures);

	//
	// Display image
	//
	var wcontent = '<HTML>';

	wcontent += '<HEAD>';
	wcontent += '<TITLE>' + title + '</TITLE>';
	wcontent += '<BASE href="' + baseurl + '"></BASE>';
	wcontent += '</HEAD>';

	wcontent += '<BODY leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>';

	wcontent += '<TABLE width="100%" height="100%" border=0 cellpadding=0 cellspacing=0><TR><TD align="center">';

	if ( actual == 1 ) {
		wcontent += '<IMG src="' + file + '" style="border: 1px #000000 solid"></IMG>';
	} else {
		wcontent += '<IMG src="' + file + '" width=' + width + ' height=' + height + ' style="border: 1px #000000 solid"></IMG>';
	}

	var bstyle = "font-family: Helvetica, Arial; font-size: 11pt; font-weight: normal; color: #191970; background-color: #ffffff; border: 1px outset #f0f0f0 #000000 #000000 #f0f0f0;";

	wcontent += '<P><TABLE border=0 cellpadding=0 cellspacing=4><TR>';
	wcontent += '<TD><A href="" onclick="window.close();"><INPUT type="button" value="Close" style="' + bstyle + '" /></A></TD>';
	wcontent += '<TD><A href="" onclick="window.print();"><INPUT type="button" value="Print" style="' + bstyle + '" /></A></TD>';
	wcontent += '</TR></TABLE></P>';

	wcontent += '</TD></TR></TABLE></BODY></HTML>';

	imgWin.document.write(wcontent);
	imgWin.document.close();

	// Return
	return(0);

}

//
// Function:    mlsform
//
// Description: Display "Search MLS" form
//
// Input:
// sitecode  = Site code (XX) to pass to listing.pl
// orient    = orientation (V=Vertical H=Horizontal)
// clocation = Current location code (XX, optional)
// czip      = Current zip code (5-digit, optional)
// csalerent = Current listing type ("1" or "S" = For sale, "2" or "R" = For rent)
// cnumbed   = Current number of beds (N, optional)
// cnumbath  = Current number of bathrooms (N, optional)
// cprice    = Current price range (thousands NNN, optional)
//
// Return code:
//  0 = Success
// -1 = Invalid argument
//
function mlsform ( sitecode, orient, clocation, czip, csalerent, cnumbed, cnumbath, cprice ) {

	var cgipath = "http://www.loopnorth.com/cgi-bin/"; // Path to listing.pl
	var check = "";                                    // "checked" or null
	var twidth = 0;                                    // Table width (pixels)

	//
	// Table styles
	//
	var tabstyle1 = "border-style: outset; border-color: #ffffff #000000 #000000 #ffffff; border-width: 1px; font-family: Helvetica, Arial; font-size: 10pt; font-weight: normal; color: #ffffff;";

	var tabstyle2 = "font-family: Helvetica, Arial; font-size: 10pt; font-weight: normal; color: #ffffff;";

	// Input text style
	var inpstyle1 = "font-family: Helvetica, Arial; font-size: 8pt; font-weight: normal; color: #0000ff;";

	// Button style
	var butstyle1 = "border-style: outset; border-color: #f0f0f0 #000000 #000000 #f0f0f0; border-width: 1px; font-family: Helvetica, Arial; font-size: 8pt; font-weight: bold; color: #0000ff; background-color: #ffffff;";

	if ( orient == 'V' ) {
		// Vertical form width
		twidth = 200;
	} else if ( orient == 'H' ) {
		// Horizontal form width
		twidth = 400;
	} else {
		// Invalid argument
		return(-1);
	}

	if ( csalerent == 'S' ) {
		// Search listings for sale
		csalerent = "1";
	} else if ( csalerent == 'R' ) {
		// Search listings for rent
		csalerent = "2";
	}

	//
	// Start outer table
	//
	// -Blue background with white border
	//
	document.write('<TABLE width=' + twidth + ' border=0 cellpadding=4 cellspacing=0 bgcolor="#0000ff" style="' + tabstyle1 + '"><TR><TD align="center">');

	// Title
	document.write('<P><B>Search MLS listings</B></P>');

	//
	// Start form
	//
	// -Call listing.pl ("post" method)
	//
	document.write('<FORM name="mlsform" action="' + cgipath + 'listing.pl" method="post">');

	// Hidden fields
	document.write('<INPUT type="hidden" name="sitecode" value="' + sitecode + '" />'); // Site code
	document.write('<INPUT type="hidden" name="option" value="S" />');                  // Display listings for above site code only (default)
	document.write('<INPUT type="hidden" name="page" value="1" />');                    // Page = 1
	document.write('<INPUT type="hidden" name="display" value="1" />');                 // Display all matching listings

	//
	// Location code
	//
	// "LN" = Loop & River North (default)
	// "HC" = Hancock Center
	// "TH" = The Heritage
	// "JT" = Joffrey Tower
	// "60" = The Legacy
	// "MC" = Marina City
	// "65" = Park Monroe
	// "ZC" = See Zip Code
	//
	// -Pre-select current selection
	// -First option checked by default
	//
	document.write('<TABLE border=0 cellpadding=4 cellspacing=0 style="' + tabstyle2 + '"><TR><TD align="left">');

	if ( clocation == 'LN' || clocation == 'ANY' || ! clocation ) { check = "checked"; } else { check = ""; }

	document.write('<INPUT type="radio" name="location" value="LN" ' + check + ' /> Loop &amp; River North<BR />');

	if ( clocation == 'HC' ) { check = "checked"; } else { check = ""; }

	document.write('<INPUT type="radio" name="location" value="HC" ' + check + ' /> Hancock Center<BR />');

	if ( clocation == 'TH' ) { check = "checked"; } else { check = ""; }

	document.write('<INPUT type="radio" name="location" value="TH" ' + check + ' /> The Heritage<BR />');

	if ( clocation == 'JT' ) { check = "checked"; } else { check = ""; }

	document.write('<INPUT type="radio" name="location" value="JT" ' + check + ' /> Joffrey Tower<BR />');

	if ( clocation == '60' ) { check = "checked"; } else { check = ""; }

	document.write('<INPUT type="radio" name="location" value="60" ' + check + ' /> The Legacy<BR />');

	if ( clocation == 'MC' ) { check = "checked"; } else { check = ""; }

	document.write('<INPUT type="radio" name="location" value="MC" ' + check + ' /> Marina City<BR />');

	if ( clocation == '65' ) { check = "checked"; } else { check = ""; }

	document.write('<INPUT type="radio" name="location" value="65" ' + check + ' /> Park Monroe<BR />');

	if ( clocation == 'ZC' && czip.length == 5 ) {
		check = "checked";
	} else {
		check = ""; czip = "";
	}

	// zip = Zip Code (5-digit, optional)
	document.write('<INPUT type="radio" name="location" value="ZC" ' + check + ' onclick="document.mlsform.zip.focus();" /> Zip Code: <INPUT type="text" name="zip" value="' + czip +  '" size=5 maxlength=5 style="' + inpstyle1 + '" />');

	document.write('</TD></TR></TABLE>');

	// Hidden "address" field
	document.write('<INPUT type="hidden" name="address" value="ANY" />');

	if ( orient == 'H' ) {
		// Start new column
		document.write('</TD><TD align="center">');
	}

	//
	// Sale or rent?
	//
	// -Pre-select current selection
	// -Default is "sale"
	//
	document.write('<TABLE border=0 cellpadding=0 cellspacing=4 style="' + tabstyle2 + '"><TR>');

	if ( csalerent == '1' || csalerent == 'ANY' || ! csalerent ) { check = "checked"; } else { check = ""; }

	document.write('<TD><INPUT type="radio" name="salerent" value="S" ' + check + ' onclick="document.getElementById(\'pricerange\').style.visibility = \'visible\';" /> For Sale</TD>');

	if ( csalerent == '2' ) { check = "checked"; } else { check = ""; }

	document.write('<TD><INPUT type="radio" name="salerent" value="R" ' + check + ' onclick="document.getElementById(\'pricerange\').style.visibility = \'hidden\';" /> For Rent</TD>');

	document.write('</TR></TABLE>');

	// Start inner table
	document.write('<TABLE border=0 cellpadding=0 cellspacing=4 style="' + tabstyle2 + '">');

	//
	// Bedrooms
	//
	// numbed = Number of bedrooms (ANY, S, 1-4 and 5P; optional; default is "ANY")
	//
	// -Current selection appears first
	//
	document.write('<TR><TD>Bedrooms:</TD>');
	document.write('<TD><SELECT name="bedrooms" style="' + inpstyle1 + '">');

	if ( cnumbed == 'ANY' ) { document.write('<OPTION value="ANY">ANY</OPTION>'); }
	if ( cnumbed == 'S' )   { document.write('<OPTION value="S">STUDIO</OPTION>'); }
	if ( cnumbed == '1' )   { document.write('<OPTION value="1">1</OPTION>'); }
	if ( cnumbed == '2' )   { document.write('<OPTION value="2">2</OPTION>'); }
	if ( cnumbed == '3' )   { document.write('<OPTION value="3">3</OPTION>'); }
	if ( cnumbed == '4' )   { document.write('<OPTION value="4">4</OPTION>'); }
	if ( cnumbed == '5P' )  { document.write('<OPTION value="5P">5+</OPTION>'); }

	if ( cnumbed != 'ANY' ) { document.write('<OPTION value="ANY">ANY</OPTION>'); }
	if ( cnumbed != 'S' )   { document.write('<OPTION value="S">STUDIO</OPTION>'); }
	if ( cnumbed != '1' )   { document.write('<OPTION value="1">1</OPTION>'); }
	if ( cnumbed != '2' )   { document.write('<OPTION value="2">2</OPTION>'); }
	if ( cnumbed != '3' )   { document.write('<OPTION value="3">3</OPTION>'); }
	if ( cnumbed != '4' )   { document.write('<OPTION value="4">4</OPTION>'); }
	if ( cnumbed != '5P' )  { document.write('<OPTION value="5P">5+</OPTION>'); }

	document.write('</SELECT></TD></TR>');

	//
	// Bathrooms
	//
	// numbath = Number of bathrooms (ANY, 1-4 and 5P; optional; default is "ANY")
	//
	// -Current selection appears first
	//
	document.write('<TR><TD>Bathrooms:</TD>');
	document.write('<TD><SELECT name="bathrooms" style="' + inpstyle1 + '">');

	if ( cnumbath == 'ANY' ) { document.write('<OPTION value="ANY">ANY</OPTION>'); }
	if ( cnumbath == '1' )   { document.write('<OPTION value="1">1</OPTION>'); }
	if ( cnumbath == '2' )   { document.write('<OPTION value="2">2</OPTION>'); }
	if ( cnumbath == '3' )   { document.write('<OPTION value="3">3</OPTION>'); }
	if ( cnumbath == '4' )   { document.write('<OPTION value="4">4</OPTION>'); }
	if ( cnumbath == '5P' )  { document.write('<OPTION value="5P">5+</OPTION>'); }

	if ( cnumbath != 'ANY' ) { document.write('<OPTION value="ANY">ANY</OPTION>'); }
	if ( cnumbath != '1' )   { document.write('<OPTION value="1">1</OPTION>'); }
	if ( cnumbath != '2' )   { document.write('<OPTION value="2">2</OPTION>'); }
	if ( cnumbath != '3' )   { document.write('<OPTION value="3">3</OPTION>'); }
	if ( cnumbath != '4' )   { document.write('<OPTION value="4">4</OPTION>'); }
	if ( cnumbath != '5P' )  { document.write('<OPTION value="5P">5+</OPTION>'); }

	document.write('</SELECT></TD></TR>');

	//
	// Price range
	//
	// price = Price range (ANY, 100-900 and 1000P; optional; default is "ANY"; example: "300" = $300,000-$400,000)
	//
	// -Current selection appears first
	//
	document.write('<TR id="pricerange"><TD>Price:</TD>');
	document.write('<TD><SELECT name="price" style="' + inpstyle1 + '">');

	if ( cprice == 'ANY' )   { document.write('<OPTION value="ANY">ANY</OPTION>'); }
	if ( cprice == '100' )   { document.write('<OPTION value="100">$100,000-$200,000</OPTION>'); }
	if ( cprice == '200' )   { document.write('<OPTION value="200">$200,000-$300,000</OPTION>'); }
	if ( cprice == '300' )   { document.write('<OPTION value="300">$300,000-$400,000</OPTION>'); }
	if ( cprice == '400' )   { document.write('<OPTION value="400">$400,000-$500,000</OPTION>'); }
	if ( cprice == '500' )   { document.write('<OPTION value="500">$500,000-$600,000</OPTION>'); }
	if ( cprice == '600' )   { document.write('<OPTION value="600">$600,000-$700,000</OPTION>'); }
	if ( cprice == '700' )   { document.write('<OPTION value="700">$700,000-$800,000</OPTION>'); }
	if ( cprice == '800' )   { document.write('<OPTION value="800">$800,000-$900,000</OPTION>'); }
	if ( cprice == '900' )   { document.write('<OPTION value="900">$900,000-$1,000,000</OPTION>'); }
	if ( cprice == '1000P' ) { document.write('<OPTION value="1000P">$1,000,000+</OPTION>'); }

	if ( cprice != 'ANY' )   { document.write('<OPTION value="ANY">ANY</OPTION>'); }
	if ( cprice != '100' )   { document.write('<OPTION value="100">$100,000-$200,000</OPTION>'); }
	if ( cprice != '200' )   { document.write('<OPTION value="200">$200,000-$300,000</OPTION>'); }
	if ( cprice != '300' )   { document.write('<OPTION value="300">$300,000-$400,000</OPTION>'); }
	if ( cprice != '400' )   { document.write('<OPTION value="400">$400,000-$500,000</OPTION>'); }
	if ( cprice != '500' )   { document.write('<OPTION value="500">$500,000-$600,000</OPTION>'); }
	if ( cprice != '600' )   { document.write('<OPTION value="600">$600,000-$700,000</OPTION>'); }
	if ( cprice != '700' )   { document.write('<OPTION value="700">$700,000-$800,000</OPTION>'); }
	if ( cprice != '800' )   { document.write('<OPTION value="800">$800,000-$900,000</OPTION>'); }
	if ( cprice != '900' )   { document.write('<OPTION value="900">$900,000-$1,000,000</OPTION>'); }
	if ( cprice != '1000P' ) { document.write('<OPTION value="1000P">$1,000,000+</OPTION>'); }

	document.write('</SELECT></TD></TR>');

	// End inner table
	document.write('</TABLE>');

	// SEARCH button
	document.write('<P><INPUT type="button" value="SEARCH" style="' + butstyle1 + '" onclick="document.mlsform.submit();" /></P>');

	// End form
	document.write('</FORM>');

	// End outer table
	document.write('</TD></TR></TABLE>');

	// Return
	return(0);

}