/*

	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: 12-21-11

*/

//
// 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 solid #000000"></IMG>';
	} else {
		wcontent += '<IMG src="' + file + '" width=' + width + ' height=' + height + ' style="border: 1px solid #000000"></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:
// title    = Form title (optional)
// page     = Web page on which to display listings (do not include "http://")
// client   = Client number (0NNN)
// location = Street address to search for (with + for spaces)
//
// Return code:
//  0 = Success
// -1 = Invalid argument
//
function mlsform ( title, page, client, location ) {

	if ( ! title ) {
		// Use default title
		title = "Search MLS";
	} else if ( ! page || ! client || ! location ) {
		// Invalid argument
		return(-1);
	}

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

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

	//
	// Table styles
	//
	var tabstyle1 = "border-width: 1px; border-style: outset; border-color: #ffffff #000000 #000000 #ffffff; 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";

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

	// Title
	document.write('<P><B>' + title + '</B></P>');

	// Start form
	document.write('<FORM name="mlsform" action="http://' + page + '" method="get">');

	// Hidden fields
	document.write('<INPUT type="hidden" name="c" value="' + client + '" />');   // Client number
	document.write('<INPUT type="hidden" name="v" value="' + location + '" />'); // Value to search for
	document.write('<INPUT type="hidden" name="q" value="SA" />');               // Search request (above value is street address)
	document.write('<INPUT type="hidden" name="s" value="D" />');                // Sort listings by date

	//
	// Sale or rent?
	//
	// -Default is "sale"
	//
	document.write('<TABLE border=0 cellpadding=0 cellspacing=4 style="' + tabstyle2 + '"><TR>');
	document.write('<TD><INPUT type="radio" name="r" value="S" checked /> For Sale</TD>');
	document.write('<TD><INPUT type="radio" name="r" value="R" /> For Rent</TD>');
	document.write('</TR></TABLE>');

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

	//
	// Bedrooms
	//
	// -Default is "ANY"
	//
	document.write('<TR><TD>Bedrooms:</TD>');
	document.write('<TD><SELECT name="b" style="' + inpstyle1 + '">');

	document.write('<OPTION value="A">ANY</OPTION>');
	document.write('<OPTION value="0">STUDIO</OPTION>');
	document.write('<OPTION value="1">1</OPTION>');
	document.write('<OPTION value="2">2</OPTION>');
	document.write('<OPTION value="3">3+</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);

}
