﻿var curCategory;
var curState = 'DEPA';
var mrisAreaExist = true; // agent sites might not have MRIS areas

//since we are replacing onload/onunload events, save them in variables
var existingOnloadEvent = window.onload;
var existingOnunloadEvent = window.onunload;

window.onload = DoLoad;
window.onunload = DoUnload;

function ShowCategoryItems(bForceUpdate) {
    var frm = formObject();
    if (!frm) return;
    if (!frm.category) return;
    var categoryInd = frm.category.options[frm.category.selectedIndex].value;
	if (!bForceUpdate && curCategory == categoryInd) return;
	var useTextForValue = (curState == 'MD');
	UpdateList(eval('type_' + curState + '_' + categoryInd),frm.propType, useTextForValue);
	UpdateList(eval('style_' + curState + '_' + categoryInd),frm.propStyle, useTextForValue);
	curCategory = categoryInd;
	if (categoryInd == 2){
	    frm.newHomes.disabled = true;
	    frm.newHomes.checked = false;
	} else {
	    frm.newHomes.disabled = false;
	}
	if (categoryInd == 1){
	    frm.activeAdult.disabled = false;
	} else {
	    frm.activeAdult.disabled = true;
	    frm.activeAdult.checked = false;
	}
//	UpdateList (eval("schoolDist" + categoryInd),frm.schoolDistrict, true)
}

function UpdateList(optionsList, objSelect, useTextForValue) {
	ClearList(objSelect)
	//add new items
	var objOption = null;
	for (var i = 0; i < optionsList.length; i++) {
		//objOption = new Option(optionsList[i], (useTextForValue) ? i + 1 + "|" + optionsList[i] : i + 1, false, false);
		objOption = new Option(optionsList[i], (useTextForValue) ? optionsList[i] : i + 1, false, false);
		objSelect.options[objSelect.options.length] = objOption;
	}
	objSelect.selectedIndex = 0;
}

function UpdateList2(optionsList, objSelect) {
	ClearList(objSelect)
	var objOption = null;
	for (var i = 0; i < optionsList.length; i+=2) {
		objOption = new Option(optionsList[i+1], optionsList[i], false, false);
		objSelect.options[objSelect.options.length] = objOption;
	}
	objSelect.selectedIndex = 0;
}

function ClearList(objSelect){
	objSelect.options.length = 1;
	return;
	while (objSelect.options.length > 1){
		objSelect.options[1] = null;
	}
}

function ShowSubAreas(ind, stateID) {
    var frm = formObject();
    if (!frm) return;
	if (frm.subArea) {
		UpdateList2(eval('area_' + stateID + '_' + ind), frm.subArea);
	}
	if (curState != stateID){
	    curState = stateID;
	    ShowCategoryItems(true);
	}
	var oSchoolDistrict = frm.schoolDistrict;
	if (stateID == 'MD') {
	    frm.area.selectedIndex = -1;
	    //no School Districts in MD
	    if(oSchoolDistrict)
	        ClearList(oSchoolDistrict)
	} else {
	    if(mrisAreaExist){
	        frm.areaMRIS.selectedIndex = -1;
	    }
	    if (oSchoolDistrict) {
		    UpdateList(eval('schoolDist' + ind), oSchoolDistrict, true);
	    }
	}
}

function ValidateCustomSearch() {
    var frm = formObject();
    if (!frm) return;
    if (frm.area.selectedIndex == -1 && frm.ctl00$PageContent$community.value == "" && (mrisAreaExist && frm.areaMRIS.selectedIndex == -1) && frm.zipCode.value == "") {
		alert("All fields are optional except for area or comunnity or zip code.");
		return false;
	}
}

function DoLoad(){
    init();
	if(existingOnloadEvent){
        //call previously saved onload event
        existingOnloadEvent();
    }
}

function DoUnload(){
    SaveSelections();
	if(existingOnunloadEvent){
	    //call previously saved onunload event
	    existingOnunloadEvent();
	}
}

function init() {
    var frm = formObject();
    if (!frm) return;
	if (!frm.areaMRIS){
	    mrisAreaExist = false;
	}
	if (frm.area.selectedIndex != -1){
		ShowSubAreas(frm.area.selectedIndex, 'DEPA');
	}
	if (mrisAreaExist && frm.areaMRIS.selectedIndex != -1){
		ShowSubAreas(frm.areaMRIS.selectedIndex, 'MD');
	}
	ShowCategoryItems();
	var propTypeLastInd = GetCookie("propTypeLastInd");
	var propStyleLastInd = GetCookie("propStyleLastInd");
	var subAreaInd = GetCookie("subAreaInd");
	var schoolDistInd = GetCookie("schoolDistInd");
	if(propTypeLastInd && frm.propType){
		frm.propType.selectedIndex = propTypeLastInd;
	}
	if(propStyleLastInd && frm.propStyle){
		frm.propStyle.selectedIndex = propStyleLastInd;
	}
	if(frm.subArea && subAreaInd && frm.area.selectedIndex != -1){
		frm.subArea.selectedIndex = subAreaInd;
	}
	if(schoolDistInd){
		frm.schoolDistrict.selectedIndex = schoolDistInd;
	}

}

function SaveSelections() {
    var frm = formObject();
    if (!frm) return;
    SetCookie('propTypeLastInd', frm.propType.selectedIndex);
    SetCookie('propStyleLastInd', frm.propStyle.selectedIndex);
	if (frm.subArea)
	    SetCookie('subAreaInd', frm.subArea.selectedIndex);
	SetCookie('schoolDistInd', frm.schoolDistrict.selectedIndex);
}

function ResetFastForms(){
	for (i=0; i < document.forms.length - 1; i++){
		document.forms[i].reset();
	}
}

function ResetCustomForm() {
    var frm = formObject();
    if (!frm) return;
	frm.reset();
	SaveSelections()
	if (frm.subArea)
		ClearList(frm.subArea);
	init(true);
	// for Netscape
	//setTimeout("document.formCustom.propType.selectedIndex = 0; document.formCustom.propStyle.selectedIndex = 0; document.formCustom.schoolDistrict.selectedIndex = 0;", 1);
}

//function Register(){
//	if (confirm("This feature is available to registered users. Would you like to register for your free My Home File account now?"))
//		window.location = "/login.aspx"
//}

function DoQuickMap(coords) {
    var locValue;
    var locDrop;
    
    if (coords) {
        locValue = coords;
    } else {
        locDrop  = document.getElementById('dropLocations');
        locValue = locDrop.options[locDrop.selectedIndex].value;
    }
    if (locValue != "0,0,0") {
        locValue = locValue.split(",");
        qstr_loc = "goto=yes&gotoLoc=" + locValue[0] + "%2C" + locValue[1] + "%2C" + locValue[2] + "&";	
        if (window.location.hostname.indexOf("pattersonschwartz.com") == -1){
            qstr_loc += "agtid=" + window.location.hostname + "&";
        }
        window.open("http://patterson1.mapmuse.com/re1/map_patterson_re1.php?sp_theme=PATTERSON&" + qstr_loc + "noOfLayers=1&layer0=PATTERSON&searchMM01=" + document.getElementById('searchMM01').options[document.getElementById('searchMM01').selectedIndex].value  + "&searchMM101=" + document.getElementById('searchMM101').options[document.getElementById('searchMM101').selectedIndex].value);
    }
}

function formObject() {
    var frm = document.formCustom;
    if (!frm) {
        frm = document.aspnetForm;
    }
    return frm;
}
