<!--
//==============================================================================
// 15Mar08 
//==============================================================================


var isTest=0;	//   0= not testing,   1=test and submit query,  2= test only, no submit query	



//==============================================================================
function writethis(s) {
	window.status=s;
}
//==============================================================================



//==============================================================================
//     moveOver  -   adds the selected town/village name to the choicebox
//==============================================================================  
function moveOver() {

    var selectedItem  	= document.frmSearch.available.selectedIndex;
    var selectedValue 	= document.frmSearch.available.options[selectedItem].value;
    var selectedDisplayText =  document.frmSearch.available.options[selectedItem].text;  
    var selectedText  	=  trim(selectedDisplayText);  
    var maxTowns      	= 10;   
    var i;
    var isNewLoc = true;
    var newoption;
    var thisitem;
    var boxLength = document.frmSearch.choiceBox.length;

	// :::  If the only item in choicebox is the "(nothing selected)',
	// :::  (default) option, whose value is 0, then delete it.
	if (boxLength == 1 && 
		document.frmSearch.choiceBox.options[0].value == 0) {
			document.frmSearch.choiceBox.options[0] = null;
			boxLength = 0;
	}

	// If it's already in the "Selected" list, don't add it again
	if (boxLength != 0) {
	    for (i = 0; i < boxLength; i++) 
	    {
		thisitem = document.frmSearch.choiceBox.options[i].value;
		if (thisitem == selectedValue) {    		
			isNewLoc = false;
			break;
		}
	    }
	} // -- ends "if (boxLength..."

	if (isNewLoc && boxLength < maxTowns ) {
		newoption = new Option(selectedText, selectedValue, false, false);
		document.frmSearch.choiceBox.options[boxLength] = newoption;
	} else {
		if (isNewLoc) {
			mssg = "Sorry! Only " + maxTowns + " locations can be searched at once. ";
			mssg += " Before you can add this location, you must";
			mssg += " remove one of your Selected Locations";
			mssg += " from the list on the right. You can remove";
			mssg += " an item by clicking on it.";
			mssg += "\n\n Hint: ";
			mssg += " Try selecting towns instead of villages.";
			mssg += " When you select a town, all villages are ";
			mssg += " automatically included.";
			alert(mssg); 
		}
	}  // -- ends "if (isNewLoc && boxLength ..." 

}


// ================================================================================
//     removeMe(id)   -   removes the selected town/village name from the choicebox 
// =================================================================================  
function removeMe() {
	var boxLength = document.frmSearch.choiceBox.length;
	var arrSelected = new Array();
	var count = 0;
	var i, x;
	for (i = 0; i < boxLength; i++) {
		if (document.frmSearch.choiceBox.options[i].selected) {
			arrSelected[count] = document.frmSearch.choiceBox.options[i].value;
		}
		count++;
	}
	for (i = 0; i < boxLength; i++) {
		for (x = 0; x < arrSelected.length; x++) {
			if (document.frmSearch.choiceBox.options[i].value == arrSelected[x]) {
				document.frmSearch.choiceBox.options[i] = null;
			}
		}
		boxLength = document.frmSearch.choiceBox.length;
	}

}  // Ends the "removeMe" function


// ================================================================================
//  saveMe() -  handles submit button 
// ================================================================================
function saveMe() {
	var strValues="";
	var boxLength = document.frmSearch.choiceBox.length;
	var count = 0;
	var i;

	// First, if the only town option is the "(nothing selected)" default (0),
	// wipe it out, it doesn't count
	if (boxLength == 1 && document.frmSearch.choiceBox.options[0].value == 0) {
			document.frmSearch.choiceBox.options[0] = null;
			boxLength = 0;
	}

	// ... any options remaining at this point will be REAL selected Locs 
	if (boxLength != 0) {
	    // means at least one location was selected
	    for (i = 0; i < boxLength; i++) {
		if (count == 0) {
			strValues = document.frmSearch.choiceBox.options[i].value;
		} else {
			strValues = strValues + "," + document.frmSearch.choiceBox.options[i].value;
		}
		count++;
	    }
	}

	document.frmSearch.txttown.value = strValues;

	if (strValues != "") {
	 	// :::::::::::::::::::::  SEARCH BY LOCATION

		// ~~~ Make sure   min price not greater than max price   
		var txtminp = document.frmSearch.txtMinPrice.value;
		var txtmaxp = document.frmSearch.txtMaxPrice.value;
		if ( (isNum(txtminp)) && (isNum(txtmaxp)) && (parseInt(txtmaxp) < parseInt(txtminp)) )   {
			alert(" Min Price can not be greater than Max Price. \n Please select a different value.");
			return(false);
		} 

		// ~~~~~~ Are we searching "New Listings Only"
		if ( parseInt(document.frmSearch.when.value) == 0)  {
			 document.frmSearch.new_listing.value = false; // ?????????
		} else {
			 document.frmSearch.new_listing.value = true; // ?????????
		}
		

		// --- Seem to have valid search loc query. 

		// Clear Listing number rather than try to validate it.
		document.frmSearch.txtnumber.value = "";

		
		if (isTest == 0) {
			return(true);	// real query - not testing
		} else {
			// ~~~~~ just testing, generate test output -----
 			var Response=testAlert("Search by Location");	// Display test info alert
			//alert("Response was "+Response.toString() );
			return(Response);
		}	 

	}   else   {	

		//::::::::::: SEARCH BY LISTING NUM  ( presumably, since txttown.value is "")
   
		if (document.frmSearch.txtnumber.value.toString() == "") {  
			// No listing num EITHER! No location, no listing num, nada.

			mssg3="Please select at least one location to search";
			mssg3+="\n - OR - enter a valid Listing Number";
			alert(mssg3);
			return(false);
		} else  if(!isListingNumValid())   {
			alert("The listing number entered is not valid.");
			return(false);
		} else { 
				// OK - we're searching by Listing Number
			  document.frmSearch.new_listing.value = false; // must do this else it won't work 

			 // -------- Listing num entered is (potentially) valid
			if (isTest == 0) {
				return(true);	// real query - not testing, just return true
			} else {
				// ~~~~~ just testing, generate test output -----
 				var Response=testAlert("Search by Listing Num"); //  show test info
				//alert("Response was "+Response.toString() );
				return Response;
			}		
		 }

	} // ---- ends if(strValues != "")

}  // Ends the "saveMe" function

// ================================================================================
//   testAlert() -  displays test output 
// ================================================================================
function testAlert(sTxt)  	{
		 	
	var selTowns="",selTypes="",selPrice="",selRooms="",hardCode="";
	var selNewOnly="",uMLSNum="",strListNum="",townList="";
	var srchBy="",mssg1="",mssg2="";

	if (sTxt.length > 0) srchBy = sTxt.toString(); 

	// :::::::::::::::  For testing only - verify form values  
	// :::::::::::::::  Create strings to display in alert window

	// -------------- Hard-coded form values info
	hardCode="Hard-coded form values:";
//	hardCode+="\n AgentID=" +document.frmSearch.AgentID.value;
//	hardCode+="\n txtStyle=" +document.frmSearch.txtStyle.value;
	hardCode+="\n strstate=" +document.frmSearch.strstate.value;

	// ------------- "Search by Location" info
	// ....  if(document.frmSearch.choiceBox.length > 0)  {

	if(document.frmSearch.txttown.value.toString != "")  {
		townList = document.frmSearch.txttown.value.replace(/,/g, "\n");
	} else {
		townList = "(none selected)";
	}
	selTowns = "Selected Towns: \n" + townList;

	selTypes = "-- Checked Types: ";
	if (document.frmSearch.SF.checked == true) {selTypes+="SF "};
	if (document.frmSearch.LD.checked == true) {selTypes+="LD "};
	if (document.frmSearch.MF.checked == true) {selTypes+="MF "};
	if (document.frmSearch.CC.checked == true) {selTypes+="CC "};
	if (document.frmSearch.HM.checked == true) {selTypes+="HM "};
	if (document.frmSearch.CI.checked == true) {selTypes+="CI "};
	if (document.frmSearch.RN.checked == true) {selTypes+="RN "};
	if (document.frmSearch.MM.checked == true) {selTypes+="MM "};
	if (document.frmSearch.WF.checked == true) {selTypes+="WF "};
	if (document.frmSearch.BU.checked == true) {selTypes+="BU "};
  	if (selTypes == "-- Checked Types: ") { selTypes += "(none checked)"; }

	selPrice= "-- txtMinPrice: [" +document.frmSearch.txtMinPrice.value+"],  txtMaxPrice: ["+document.frmSearch.txtMaxPrice.value+"]";
	selRooms= "-- txtMinBeds: " +document.frmSearch.txtMinBeds.value+ ", txtMinBaths: " +document.frmSearch.txtMinBaths.value;
	
	// ------------- "New Listings Only"  info --------------------
	selNewOnly = "-- New listings only?: ";
	selNewOnly +="\n   document.frmSearch.new_listing.value = " + document.frmSearch.new_listing.value.toString();
	selNewOnly +="\n   document.frmSearch.when.value = " + document.frmSearch.when.value.toString();

	// ------------- MLS number  info  --------------------------
	strListNum = document.frmSearch.txtnumber.value.toString();
	var sLen=strListNum.length;
	if ( isListingNumValid() )  { 	
		uMLSNum = "MSL number \n-- txtnumber = " + strListNum; 
	} else {
		uMLSNum = "MSL number \n-- txtnumber: (empty or not valid)"
	}

	// -------------   display the alert
	mssg1=hardCode+"\n\n" + selTowns ;
	mssg1+= "\n\n **** " + srchBy + " **** ";
	mssg1+= "\n\n" + uMLSNum;
	mssg1+= "\n\n Optional Fields:"
	mssg1+= "\n" + selTypes   + "\n" + selPrice + "\n" + selRooms;
	mssg1+= "\n" + selNewOnly;
	mssg1+= "\n\nNOTE: To stop testing, change isTest to 0.";	
	mssg2 = mssg1+"\n\n Click OK to submit search, else click Cancel.";
	return (confirm(mssg2));     //alert(mssg1); 
	

}  // Ends the "testAlert" function 
// ================================================================================
//  trim
// ================================================================================
function trim(inputstring) {
	// Removes leading and trailing spaces from the passed string. Also removes
	// consecutive spaces and replaces it with one space. If something besides
	// a string is passed in (null, custom object, etc.) then return the input.

	if (typeof inputstring != "string") { return inputstring; }
	var retValue = inputstring;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { // Check for spaces at the beginning of the string
	retValue = retValue.substring(1, retValue.length);
	ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { // Check for spaces at the end of the string
	retValue = retValue.substring(0, retValue.length-1);
	ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) {  // Note that there are two spaces in the string 
						// - look for multiple spaces within the string
	retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 			// Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

// ================================================================================
//   clearChoicebox
// ================================================================================
function clearChoicebox() {
	for (var q=document.frmSearch.choiceBox.options.length;q>=0;q--)  {
		document.frmSearch.choiceBox.options[q]=null;
	}
}

// ================================================================================
//  isAlphaNum(s) Does string s contains all alphanumeric chars  
// ================================================================================
function isAlphaNum(s) {

    if(s.length==0) {
	 return false;
    } else {
	//  		48-57  digits 0-9;  
	//  		65-90  alpha A-Z
	//  		97-122 alpha a-z;  
	//  		44     comma
	var c;
        var i;
	for (i=0; i < s.length; i++) { 
	    c = s.charCodeAt(i);
	    if (!(( c >= 48  &&  c <= 57 ) || 
		  ( c >= 65  &&  c <= 90 ) ||  
		  ( c >= 97  &&  c <= 122)) ) {
		return false;
	    }
	}
	return true;
    }
}

// ================================================================================
//  isListingNumValid() Is txtnumber.value a valid listing number  
// ================================================================================
function isListingNumValid()   {
	var strListNum = document.frmSearch.txtnumber.value.toString();
	var invalidChar_RegExp = /[^a-zA-Z0-9\x2C]/; 	// Note: "\x2C" is comma.

	// Allow up to 5 listing nums, comma-sep, 8 chars each = 44 max
	if ( (strListNum.length == 0) || (strListNum.length > 44) ) {
		return(false);
	} 

	if (invalidChar_RegExp.test(strListNum) )  {
		return(false);
	} else {
		return(true);
	}
}
// ================================================================================
//  isNum(s)  tests whether string s contains all numeric chars
// ================================================================================
function isNum(s) {

    if(s.length==0) {
	 return false;
    } else {
	var i, c, isanum=true;
	for (i=0; i < s.length && isanum; i++) { 
		c = s.charCodeAt(i);
		if (c  < 48  ||  c > 57 ) isanum=false;
	}
	return isanum;
    }
}

// ================================================================================
//  toggleNewOnly(s) handles check/unchecking of cbxNewListings checkbox
// ================================================================================
function toggleNewOnly() {

    if(document.frmSearch.cbxNewListings.checked == true) {
	document.frmSearch.when.disabled = false;
    } else {
	document.frmSearch.when.disabled = true;
	document.frmSearch.when.selectedIndex = 0;
    }
}
// ================================================================================
//  resetNew() - reset "New Listings Only" controls
// ================================================================================
function resetNew() {
	document.frmSearch.cbxNewListings.checked= false;
	document.frmSearch.when.disabled = true;
	document.frmSearch.when.selectedIndex = 0;
}

// -->
