function isNumber(textObj) {
	while (isNaN(textObj.value) == true) {
		textObj.value = (textObj.value).substr(0, textObj.value.length -1)
	}
}

function isSelected(ques, val) {
	if (getKey(ques) == val) {
		return " checked";
	}
}

function updateRedirect() {
	if (document.housingWizard.q2.value == "" || document.housingWizard.q3.value == "" || document.housingWizard.q4.value == "") {
		window.location = "housing.shtml?q2=" + document.housingWizard.q2.value + "|q3=" + document.housingWizard.q3.value + "|q4=" + document.housingWizard.q4.value;
		return false;
	} else {
		window.location = "housing.shtml?q2=" + document.housingWizard.q2.value + "|q3=" + document.housingWizard.q3.value + "|q4=" + document.housingWizard.q4.value;
		//document.housingWizard.redirect.value = "http://www.nyc.gov/html/housinginfo/html/apartments/housing.shtml?q2=" + document.housingWizard.q2.value + "|q3=" + document.housingWizard.q3.value + "|q4=" + document.housingWizard.q4.value;
		//document.housingWizard.submit();
		return false;
	}
}

function writeQues() {
	document.write('Through this site you will find City-sponsored apartments that are currently accepting applications ');
    document.write('for which you <em><strong>may</strong></em> qualify. Future tenants will be selected through ');
    document.write('a lottery among all of the applicants who meet the eligibility criteria ');
    document.write('for the specific development.<br><br>');
	document.write('<font color="#FF0000">*</font> Indicates required fields.<br><br>');
	//document.write('<form method="get">');

	document.write('<form name="housingWizard" action="housing.shtml" method="get" onSubmit="return updateRedirect();">');
	//document.write('<form name="housingWizard" action="/cgi-bin/misc/mailform.cgi" method="post" onSubmit="return updateRedirect();">');
	//document.write('<input type="hidden" value="Housing Info Lottery Wizard Statistics" name="subject">');
	//document.write('<input type="hidden" value="survey@doitt.nyc.gov" name="recipient">');
	//document.write('<input type="hidden" value="http://www.nyc.gov/html/housinginfo/html/apartments/housing.shtml" name="redirect">');
	//document.write('<input type="hidden" value="q2, q3, q4" name="required">');
	//document.write('<input type="hidden" value="REMOTE_HOST,HTTP_ADDR,HTTP_USER_AGENT" name="env_report">');


	/*var tmpSplit = housing_ques[0].split("|");
	if (searchStr!="" && getKey("q1") == "") {document.write('<font color="#FF0000">');}
	document.write('<font color="#FF0000">*</font>' + tmpSplit[0] + "<br>");
	if (searchStr!="" && getKey("q1") == "") {document.write('</font>');}
	document.write('<input type="radio" name="q1" value="Rent"' + isSelected("q1", "Rent") + '>Rent ');
	document.write('<input type="radio" name="q1" value="Buy"' + isSelected("q1", "Buy") + '>Buy ');
	document.write('<input type="radio" name="q1" value="Either"' + isSelected("q1", "Either") + '>Either <br><br>');*/

	tmpSplit = housing_ques[0].split("|");
	if (searchStr!="" && getKey("q2") == "") {document.write('<font color="#FF0000">');}
	document.write('<font color="#FF0000">*</font>' + tmpSplit[0] + "<br>");
	if (searchStr!="" && getKey("q2") == "") {document.write('</font>');}
	document.write('<input name="q2" type="text" size="1" maxlength="1" value="' + getKey("q2") + '" onKeyUp="isNumber(this);" onKeyDown="isNumber(this);">');
	document.write(' <small>(# of Bedrooms, enter 0 for Studio.)</small><br><br>');

	tmpSplit = housing_ques[1].split("|");
	if (searchStr!="" && getKey("q3") == "") {document.write('<font color="#FF0000">');}
	document.write('<font color="#FF0000">*</font>' + tmpSplit[0] + "<br>");
	if (searchStr!="" && getKey("q3") == "") {document.write('</font>');}
	document.write('<input name="q3" type="text" size="2" maxlength="2" value="' + getKey("q3") + '" onKeyUp="isNumber(this);" onKeyDown="isNumber(this);"><br><br>');

	tmpSplit = housing_ques[2].split("|");
	if (searchStr!="" && getKey("q4") == "") {document.write('<font color="#FF0000">');}
	document.write('<font color="#FF0000">*</font>' + tmpSplit[0] + "<br>");
	if (searchStr!="" && getKey("q4") == "") {document.write('</font>');}
	document.write('<input name="q4" type="text" size="6" maxlength="6" value="' + getKey("q4") + '" onKeyUp="isNumber(this);" onKeyDown="isNumber(this);"><br><br>');

	/*tmpSplit = housing_ques[3].split("|");
	document.write(tmpSplit[0] + "<br>");
	document.write('<input type="radio" name="q5" value="Yes"' + isSelected("q5", "Yes") + '>Yes ');
	document.write('<input type="radio" name="q5" value="No"' + isSelected("q5", "No") + '>No <br><br>');

	tmpSplit = housing_ques[4].split("|");
	document.write(tmpSplit[0] + "<br>");
	document.write('<input type="radio" name="q6" value="Yes"' + isSelected("q6", "Yes") + '>Yes ');
	document.write('<input type="radio" name="q6" value="No"' + isSelected("q6", "No") + '>No <br><br>');

	tmpSplit = housing_ques[5].split("|");
	document.write(tmpSplit[0] + "<br>");
	document.write('<input type="radio" name="q7" value="Yes"' + isSelected("q7", "Yes") + '>Yes ');
	document.write('<input type="radio" name="q7" value="No"' + isSelected("q7", "No") + '>No <br><br>');

	tmpSplit = housing_ques[6].split("|");
	document.write(tmpSplit[0] + "<br>");
	document.write('<input type="radio" name="q8" value="Yes"' + isSelected("q8", "Yes") + '>Yes ');
	document.write('<input type="radio" name="q8" value="No"' + isSelected("q8", "No") + '>No <br><br>');

	tmpSplit = housing_ques[7].split("|");
	document.write(tmpSplit[0] + "<br>");
	document.write('<input type="radio" name="q9" value="Yes"' + isSelected("q9", "Yes") + '>Yes ');
	document.write('<input type="radio" name="q9" value="No"' + isSelected("q9", "No") + '>No <br><br>');

	tmpSplit = housing_ques[8].split("|");
	document.write(tmpSplit[0] + "<br>");
	document.write('<input type="radio" name="q10" value="Yes"' + isSelected("q10", "Yes") + '>Yes ');
	document.write('<input type="radio" name="q10" value="No"' + isSelected("q10", "No") + '>No <br><br>');*/

	document.write('<input type="Submit" name="Submit" value="Submit"> ');
	document.write('<input type="reset" name="Reset" value="Reset">');
	document.write('</form>');
}

function getKey(key) {
	var searchSplit = searchStr.split("|");
	var tmpSplit;

	for (x=0; x<searchSplit.length; x++) {
		tmpSplit = searchSplit[x].split("=");
		if (tmpSplit[0] == key) {
			return tmpSplit[1];
		}
	}

	return "";
}

function findQ1(answer) {
	var tmpReturn = "";
	var tmpSplit;

	for (x=0; x<housing.length; x++) {
		if (answer == "Either") {
			tmpReturn = tmpReturn + x + ",";
		} else {
			tmpSplit = housing[x].split("|");
			if (tmpSplit[3] == answer) {
				tmpReturn = tmpReturn + x + ",";
			}
		}
	}
	return "," + tmpReturn;
}

function findQ2(answer) {
	var tmpReturn = "";
	var tmpSplit;
	var tmpAptNum;

	for (x=0; x<housing.length; x++) {
		tmpSplit = housing[x].split("|");
		if (tmpSplit[4] == answer) {
			tmpReturn = tmpReturn + x + ",";
		}
	}

	return "," + tmpReturn;
}

function findQ3(answer) {
	var tmpReturn = "";
	var tmpSplit;
	var tmpFamilyNum;

	for (x=0; x<housing.length; x++) {
		tmpSplit = housing[x].split("|");
		if (tmpSplit[5] == tmpSplit[6]) {
			if (tmpSplit[5] == answer) {
				tmpReturn = tmpReturn + x + ",";
			}
		} else {
			if (answer >= tmpSplit[5] && answer <= tmpSplit[6]) {
				tmpReturn = tmpReturn + x + ",";
			}
		}
	}
	
	return "," + tmpReturn;
}

function findQ4(answer) {
	var tmpReturn = "";
	var tmpSplit;
	var tmpSplit2;
	var tmpFamilyNum;

	for (x=0; x<housing.length; x++) {
		tmpSplit = housing[x].split("|");
		if (tmpSplit[8] == tmpSplit[9]) {
			if (tmpSplit[8] == answer) {
				tmpReturn = tmpReturn + x + ",";
			}
		} else {
			if (parseInt(answer) >= parseInt(tmpSplit[8]) && parseInt(answer) <= parseInt(tmpSplit[9])) {
				tmpReturn = tmpReturn + x + ",";
			}
		}
	}
	
	return "," + tmpReturn;
}

function getAddInfo(ques) {
	var tmpStr = "";
	if (getKey("q" + ques) == "Yes") {
		tmpSplit = housing_ques[ques-1].split("|");
		tmpStr = tmpStr + tmpSplit[1];
		if (tmpSplit[2] != "") {
			tmpStr = tmpStr + "<br><a href='" + tmpSplit[2] + "'>Learn more</a>";
		}
		return tmpStr + "<br><br>";
	} else {
		return "";
	}
}

var searchStr = (location.search).substr(1,location.search.length);

if (getKey("q2") == "" || getKey("q3") == "" || getKey("q4") == "") {
	writeQues();
} else {
	//var Q1 = findQ1(getKey("q1"));
	var Q2 = findQ2(getKey("q2"));
	var Q3 = findQ3(getKey("q3"));
	var Q4 = findQ4(getKey("q4"));

	var tmpQ1Split = Q2.split(",");
	var tmpQ1Split2;
	var searchFinal = "";
	var searchedText = "";
	var tmpCurDate = new Date();
	var curDate = new Date((tmpCurDate.getMonth() + 1) + "/" + tmpCurDate.getDate() + "/" + tmpCurDate.getFullYear());

	
	for (var y=1; y<tmpQ1Split.length-1; y++) {
		if (Q2.indexOf("," + tmpQ1Split[y] + ",") != -1 && Q3.indexOf("," + tmpQ1Split[y] + ",") != -1  && Q4.indexOf("," + tmpQ1Split[y] + ",") != -1) {
			//document.write(housing[tmpQ1Split[y]] + "<br>");
			
			tmpQ1Split2 = housing[tmpQ1Split[y]].split("|");
			var tmpDate = new Date(tmpQ1Split2[10]);

			if ((tmpDate.valueOf() >= curDate.valueOf()) || tmpDate == "NaN" || tmpDate == "Invalid Date") {
				if (tmpQ1Split2[0] != "") {
					searchFinal = searchFinal + "Development Name: <b>" + tmpQ1Split2[0] + "</b><br>";
				}
				searchFinal = searchFinal + "Location: <b>" + tmpQ1Split2[1] + "</b><br>";
				searchFinal = searchFinal + "Apt Available: <b>" + tmpQ1Split2[3] + "</b><br>";
				searchFinal = searchFinal + "Rent: <b>$" + tmpQ1Split2[7] + "</b><br>";
				if (tmpQ1Split2[11] != "") {
					searchFinal = searchFinal + "Deadline: <a href='" +   tmpQ1Split2[11] + "'><b>" + tmpQ1Split2[10] + "</b></a><br><br>";
				} else {
					searchFinal = searchFinal + "Deadline: <b>" + tmpQ1Split2[10] + "</b><br><br>";
				}
			}
			
		}
	}

	searchedText = searchedText +"Your search for a ";
	if (getKey("q2") == 0) {
		searchedText = searchedText + "<b>Studio</b> ";
	} else {
		searchedText = searchedText + "<b>" + getKey("q2") + " Bedroom</b> ";
	}
	searchedText = searchedText + "apartment for <b>" + getKey("q3") + "</b> ";
	if (getKey("q3") == 1) {
		searchedText = searchedText + "person ";
	} else {
		searchedText = searchedText + "people ";
	}
	searchedText = searchedText + "with a total household income of <b>$" + getKey("q4") + "</b> generated ";

	if (searchFinal == "") {
		searchedText = searchedText + "no results.  Please <a href='javascript:history.back();'>try again</a>.";
		searchedText = searchedText + "<br><br><a href='../../html/apartments/apt_rental_opportunities.shtml '>View all current rental housing lottery opportunities.</a>";
	} else {
		searchedText = searchedText + "the following results for which you may be eligible:";
	}

	document.write(searchedText + "<br><br>");
	document.write(searchFinal);

	var addInfoStr = "";
	addInfoStr = addInfoStr + getAddInfo(5);
	addInfoStr = addInfoStr + getAddInfo(6);
	addInfoStr = addInfoStr + getAddInfo(7);
	addInfoStr = addInfoStr + getAddInfo(8);
	addInfoStr = addInfoStr + getAddInfo(9);
	addInfoStr = addInfoStr + getAddInfo(10);
	if (addInfoStr != "") {
		addInfoStr = "<b>Additional Information:</b><br><br>" + addInfoStr;
	}

	document.write(addInfoStr);

}