﻿// requirements: mozxpath.js,ASXML.js

function updateCountryByMarketDD(ddlCountryID, ddlCountyID, ddlDistrictID, ddlMarketID, DefaultMsg, DefaultMsg_county, DefaultMsg_district) { //takes an input of Market Dropdown before calling updateCountry()
    var _objMarket = document.getElementById(ddlMarketID);

    //verify valid inputs
    if (_objMarket) {
        var iMarketID = _objMarket.options[_objMarket.selectedIndex].value;
        updateCountry(ddlCountryID, ddlCountyID, ddlDistrictID, iMarketID, 0, DefaultMsg, DefaultMsg_county, DefaultMsg_district);
    }
}

function updateCountry(ddlCountryID, ddlCountyID, ddlDistrictID, marketID, classID, DefaultMsg, DefaultMsg_county, DefaultMsg_district) { //updates Country dropdown based on input marketID/classID
                                                                                                                                         //also updates the County and District dds if specified
    var _objCountry = document.getElementById(ddlCountryID);
    var _objCounty = document.getElementById(ddlCountyID);

    //verify valid inputs (county and district is optional)
    if (_objCountry) {

        var remoteConnection = new ASXML();

        remoteConnection.setCallback(
		    function(loadOK) {
		        if (loadOK) {

		            var x = remoteConnection.xmlResponse();

		            //repopulate Country dropdown
		            remoteConnection.populateDDL(_objCountry, x.selectNodes("//root/country"), DefaultMsg, 0);

		            //populate County & District dropdowns (if they exist)
		            if (_objCounty) {
		                updateCounty(ddlCountryID, ddlCountyID, ddlDistrictID, marketID, classID, DefaultMsg_county, DefaultMsg_district);
		            }
		        }
		    }
	    );
        remoteConnection.getXML("/ajax/generalAJAX.aspx?t=country&mid=" + marketID + "&cid=" + classID);
    }
}

function updateCountyByMarketDD(ddlCountryID, ddlCountyID, ddlDistrictID, ddlMarketID, DefaultMsg, DefaultMsg_district) { //takes an input of Market Dropdown before calling updateCounty()
    var _objMarket = document.getElementById(ddlMarketID);
    
    //verify valid inputs
    if (_objMarket) {
        var iMarketID = _objMarket.options[_objMarket.selectedIndex].value;
        updateCounty(ddlCountryID, ddlCountyID, ddlDistrictID, iMarketID, 0, DefaultMsg, DefaultMsg_district);
    }
}

function updateCounty(ddlCountryID, ddlCountyID, ddlDistrictID, marketID, classID, DefaultMsg, DefaultMsg_district) { //updates County dropdown based on input Country/marketID/classID
                                                                                                                      //also updates the District dd if specified
    var _objCountry = document.getElementById(ddlCountryID);
    var _objCounty = document.getElementById(ddlCountyID);
    var _objDistrict = document.getElementById(ddlDistrictID);

    //verify valid inputs (district is optional)
    if (_objCountry && _objCounty) {

        var iCountryID = _objCountry.options[_objCountry.selectedIndex].value;

        var remoteConnection = new ASXML();

        remoteConnection.setCallback(
		    function(loadOK) {
		        if (loadOK) {

		            var x = remoteConnection.xmlResponse();

		            //repopulate County dropdown
		            remoteConnection.populateDDL(_objCounty, x.selectNodes("//root/county"), DefaultMsg, 0);

		            //populate District dropdown (if it exists)
		            if (_objDistrict) {
		                updateDistrict(ddlCountyID, ddlDistrictID, marketID, classID, DefaultMsg_district);
		            }
		        }
		    }
	    );
        remoteConnection.getXML("/ajax/generalAJAX.aspx?t=county&id=" + iCountryID + "&mid=" + marketID + "&cid=" + classID);
    }
}

function updateDistrictByMarketDD(ddlCountyID, ddlDistrictID, ddlMarketID, DefaultMsg) { //takes an input of Market Dropdown before calling updateDistrict()
    var _objMarket = document.getElementById(ddlMarketID);

    //verify valid inputs
    if (_objMarket) {
        var iMarketID = _objMarket.options[_objMarket.selectedIndex].value;
        updateDistrict(ddlCountyID, ddlDistrictID, iMarketID, 0, DefaultMsg);
    }
}

function updateDistrict(ddlCountyID, ddlDistrictID, marketID, classID, DefaultMsg) { //updates district dropdown based on input county/marketID/classID

    var _objCounty = document.getElementById(ddlCountyID);
    var _objDistrict = document.getElementById(ddlDistrictID);

    //verify valid inputs
    if (_objCounty && _objDistrict) {

        var iCountyID = _objCounty.options[_objCounty.selectedIndex].value;

        var remoteConnection = new ASXML();

        remoteConnection.setCallback(
		    function(loadOK) {
		        if (loadOK) {

		            var x = remoteConnection.xmlResponse();

		            //repopulate District dropdown
		            remoteConnection.populateDDL(_objDistrict, x.selectNodes("//root/district"), DefaultMsg, 0);

		            MarkOption(_objDistrict, 'dg', 'dgOption');
		        }
		    }
	    );
        remoteConnection.getXML("/ajax/generalAJAX.aspx?t=district&id=" + iCountyID + "&mid=" + marketID + "&cid=" + classID);
    }
}

var MarkOption = function(ddl, valueStartWord, className) {
    for (var i = 0; i < ddl.childNodes.length; i++) {
        if (ddl.childNodes[i].value.indexOf(valueStartWord) == 0) { ddl.childNodes[i].setAttribute('class', className); }
    }
}

function updatePType(ddlMarketID, ddlPTypeID, ddlLTypeID, DefaultMsg, DefaultMsg_ltype) { //updates PropType dropdown based on input marketID
                                                                                          //also updates the LivingType dd if specified
    var _objMarket = document.getElementById(ddlMarketID);
    var _objPType = document.getElementById(ddlPTypeID);
    var _objLType = document.getElementById(ddlLTypeID);

    //verify valid inputs (livingtype is optional)
    if (_objMarket && _objPType) {

        var iMarketID = _objMarket.options[_objMarket.selectedIndex].value;

        var remoteConnection = new ASXML();

        remoteConnection.setCallback(
		    function(loadOK) {
		        if (loadOK) {

		            var x = remoteConnection.xmlResponse();

		            //repopulate PType dropdown
		            remoteConnection.populateDDL(_objPType, x.selectNodes("//root/type"), DefaultMsg, 0);

		            //populate LType dropdown (if it exists)
		            if (_objLType) {
		                updateLType(ddlPTypeID, ddlLTypeID, DefaultMsg_ltype);
		            }
		        }
		    }
	    );
        remoteConnection.getXML("/ajax/generalAJAX.aspx?t=proptype&mid=" + iMarketID);
    }
}

function updateLType(ddlPTypeID, ddlLTypeID, DefaultMsg) { //updates living type dropdown based on input property type

    var _objPType = document.getElementById(ddlPTypeID);
    var _objLType = document.getElementById(ddlLTypeID);

    //verify valid inputs
    if (_objPType && _objLType) {

        var iPTypeID = _objPType.options[_objPType.selectedIndex].value;

        var remoteConnection = new ASXML();

        remoteConnection.setCallback(
		    function(loadOK) {
		        if (loadOK) {

		            var x = remoteConnection.xmlResponse();

		            //repopulate living type dropdown
		            remoteConnection.populateDDL(_objLType, x.selectNodes("//root/type"), DefaultMsg, 0);
		        }
		    }
	    );
        remoteConnection.getXML("/ajax/generalAJAX.aspx?t=livingtype&id=" + iPTypeID);
    }
}

function updatePrice(type, ddlPriceID, ddlMarketID, DefaultMsg) { //updates Price dropdown based on input marketID

    var _objPrice = document.getElementById(ddlPriceID);
    var _objMarket = document.getElementById(ddlMarketID);

    //verify valid inputs
    if (_objPrice && _objMarket) {

        var iMarketID = _objMarket.options[_objMarket.selectedIndex].value;

        var remoteConnection = new ASXML();

        remoteConnection.setCallback(
		    function(loadOK) {
		        if (loadOK) {

		            var x = remoteConnection.xmlResponse();

		            //repopulate price dropdown
		            remoteConnection.populateDDL(_objPrice, x.selectNodes("//root/price"), DefaultMsg, 0);
		        }
		    }
	    );
        remoteConnection.getXML("/ajax/generalAJAX.aspx?t=pricebymarket&mid=" + iMarketID + "&pt=" + type);
    }
}

