var _cboRegion;
var _cboDistric;
var _cboCommunity;
var _cboSubCommunity;
var _cboOwnerRefNumber;
var _lblOwnerRefNo;
var _txtOwnerName;
var _txtOwnerCompany;
var _txtOwnerCompany;
var _txtOwnerPhone;
var _txtOwnerMobile;
var _txtOwnerNotes;
var _txtOwnerAddress;

function initializeForm()
{
    _cboRegion = $get("ctl00_ContentPlaceHolder1_drpRegion");
    //_cboDistric = $get("ctl00_ContentPlaceHolder1_drpDistricts");
    _cboCommunity = $get("ctl00_ContentPlaceHolder1_drpCommunity");
    _cboSubCommunity = $get("ctl00_ContentPlaceHolder1_drpSubCommunity");
    _cboOwnerRefNumber = $get("ctl00_ContentPlaceHolder1_drpOwnerRefNumber");
    _lblOwnerRefNo = $get("ctl00_ContentPlaceHolder1_lblOwnerRefNo");
    _txtOwnerName = $get("ctl00_ContentPlaceHolder1_txtOwnerName");    
    _txtOwnerCompany = $get("ctl00_ContentPlaceHolder1_txtOwnerCompany");
    _txtOwnerPhone = $get("ctl00_ContentPlaceHolder1_txtOwnerPhone");
    _txtOwnerMobile = $get("ctl00_ContentPlaceHolder1_txtOwnerMobile");
    _txtOwnerNotes = $get("ctl00_ContentPlaceHolder1_txtOwnerNotes");
    _txtOwnerAddress = $get("ctl00_ContentPlaceHolder1_txtOwnerAddress");
    if(_cboOwnerRefNumber != null)
    {
        _cboOwnerRefNumber.onchange = function(){getOwnerDetails(this);};
    }
    if(_cboRegion != null)
    {
        //_cboRegion.onchange = function(){getDistricts(this);};
        _cboRegion.onchange = function(){getCommunities(this);};
        //_cboDistric.onchange = function(){getCommunities(this);};
        _cboCommunity.onchange = function(){getSubCommunities(this);};
    }
    
}   

function getDistricts(object)
{
    makeDistrictEmpty();
    makeCommunityEmpty();
    makeSubCommunityEmpty();
    var regionId = object.options[object.selectedIndex].value;
    var curTime = new Date();
    if(regionId != -1)
    {       
        PageMethods.getDistrictByRegionId(regionId, curTime.toString(), onSuccessDistrictInfo, onFailureDistrictInfo, curTime.toString())
    }    
} 

function onSuccessDistrictInfo(result, context, method)
{
    var DistrictInfo = result;
    var DistrictIndex = 0;   
    var objSlcDistrict = _cboDistric; 
    if(DistrictInfo != null)
    {
        if(DistrictInfo.length > 0)
        {
             var optnDistrict;                
            for(DistrictIndex = 0; DistrictIndex < DistrictInfo.length; DistrictIndex++)
            {
                optnDistrict = document.createElement("OPTION");                
                optnDistrict.text = DistrictInfo[DistrictIndex].DistrictName;
                optnDistrict.value = DistrictInfo[DistrictIndex].DistrictId ;                
                objSlcDistrict.options.add(optnDistrict);
            }                      
            objSlcDistrict.selectedIndex = 0;
        }
    }    
}
function onFailureDistrictInfo(result, context, method)
{
   makeDistrictEmpty();
}

function makeDistrictEmpty()
{
   var objSlcDistrict = _cboDistric;
    
    var i;
    for (i = objSlcDistrict.length - 1; i>=0; i--)
    {
       objSlcDistrict.remove(i);
    }
    for (i = objSlcDistrict.length - 1; i>=0; i--)
    {
       objSlcDistrict.remove(i);
    }
    
    var optnDistrict = document.createElement("OPTION");
    optnDistrict.text = "Please Select";
    optnDistrict.value = "-1";                
    objSlcDistrict.options.add(optnDistrict);  
}
//*****************************************************************************************************
function getCommunities(object)
{
    document.getElementById("imgLoadingCommunity").style.display = "";
    makeCommunityEmpty();
    makeSubCommunityEmpty();
    var regionId = object.options[object.selectedIndex].value;
    var curTime = new Date();
    
     if(regionId != -1)
    {
       
        PageMethods.getDistrictAndCommunityByRegionId(regionId, curTime.toString(), onSuccessCommunityInfo, onFailureCommunityInfo, curTime.toString())
        
    }
    else
    {
        document.getElementById("imgLoadingCommunity").style.display = "none";
    }
    
}

function onSuccessCommunityInfo(result, context, method)
{//debugger;
    var CommunityInfo = result;
    var objCommunity= _cboCommunity;
    var CommunityIndex = 0;      
    if(CommunityInfo != null)
    {
        if(CommunityInfo.length > 0)
        { 
             var optnCommunity;// = document.createElement("OPTION");
//             optnCommunity.text = "Please Select";
//             optnCommunity.value = "-1";
//             objCommunity.options.add(optnCommunity);
                     
            for(CommunityIndex = 0; CommunityIndex < CommunityInfo.length; CommunityIndex++)
            {                     
                optnCommunity = document.createElement("OPTION");
                optnCommunity.text = CommunityInfo[CommunityIndex].CommunityName;
                optnCommunity.value =  CommunityInfo[CommunityIndex].CommunityId;
                objCommunity.options.add(optnCommunity);
            }
        }
    }
    document.getElementById("imgLoadingCommunity").style.display = "none";
}

function onFailureCommunityInfo(result, context, method)
{
   makeCommunityEmpty();
   document.getElementById("imgLoadingCommunity").style.display = "none";
}
function makeCommunityEmpty()
{
   var objSlcCommunity = _cboCommunity;
    
    var i;
    for (i = objSlcCommunity.length - 1; i>0; i--)
    {
       objSlcCommunity.remove(i);
    }
    for (i = objSlcCommunity.length - 1; i>0; i--)
    {
       objSlcCommunity.remove(i);
    }
}
//*****************************************************
function getSubCommunities(object)
{    document.getElementById("imgLoadingSubCommunity").style.display = "";
    makeSubCommunityEmpty();
    var CommunityId = object.options[object.selectedIndex].value;
    var curTime = new Date();    
     if(CommunityId != -1)
    {       
        PageMethods.getSubCommunityByCommunityId(CommunityId, curTime.toString(), onSuccessSubCommunityInfo, onFailureSubCommunityInfo, curTime.toString())        
    }    
}

function onSuccessSubCommunityInfo(result, context, method)
{
    var SubCommunityInfo = result;
    var objSubCommunity= _cboSubCommunity;
    var SubCommunityIndex = 0;    
    if(SubCommunityInfo != null)
    {
        if(SubCommunityInfo.length > 0)
        { 
             var optnSubCommunity;// = document.createElement("OPTION");
//             optnSubCommunity.text = "Please Select";
//             optnSubCommunity.value = "-1";
//             objSubCommunity.options.add(optnSubCommunity);
                     
            for(SubCommunityIndex = 0; SubCommunityIndex < SubCommunityInfo.length; SubCommunityIndex++)
            {                     
                optnSubCommunity = document.createElement("OPTION");
                optnSubCommunity.text = SubCommunityInfo[SubCommunityIndex].SubCommunityName;
                optnSubCommunity.value =  SubCommunityInfo[SubCommunityIndex].SubCommunityId;
                objSubCommunity.options.add(optnSubCommunity);
            }
        }
    }   
    document.getElementById("imgLoadingSubCommunity").style.display = "none";
}

function onFailureSubCommunityInfo(result, context, method)
{
   makeSubCommunityEmpty();
   document.getElementById("imgLoadingSubCommunity").style.display = "none";
}
function makeSubCommunityEmpty()
{
   var objSlcSubCommunity = _cboSubCommunity;
    
    var i;
    for (i = objSlcSubCommunity.length - 1; i>0; i--)
    {
       objSlcSubCommunity.remove(i);
    }
    for (i = objSlcSubCommunity.length - 1; i>0; i--)
    {
       objSlcSubCommunity.remove(i);
    }
}
//***************************************************************************************

function getOwnerDetails(object)
{//debugger;
    //makeDistrictEmpty();
    
    var OwnerId = object.options[object.selectedIndex].value;
    var curTime = new Date();
    if(OwnerId != -1)
    {       
        PageMethods.getOwnerDetailsByOwnerId(OwnerId, curTime.toString(), onSuccessOwnerInfo, onFailureOwnerInfo, curTime.toString())
    }
    else
    {
        makeOwnerDetailsEmpty();
    }   
} 

function onSuccessOwnerInfo(result, context, method)
{
    var OwnerDetailsInfo = result;
    var OwnerDetailsIndex = 0;
    
    if(OwnerDetailsInfo != null)
    {
        if(OwnerDetailsInfo.length > 0)
        {
            _lblOwnerRefNo.innerHTML = OwnerDetailsInfo[0].OwnerRefNumber;
            _txtOwnerCompany.value = OwnerDetailsInfo[0].OwnerCompany;
            _txtOwnerName.value= OwnerDetailsInfo[0].OwnerName;            
            _txtOwnerPhone.value = OwnerDetailsInfo[0].OwnerPhone;
            _txtOwnerMobile.value = OwnerDetailsInfo[0].OwnerMobile;
            _txtOwnerNotes.value = OwnerDetailsInfo[0].OwnerNotes;
            _txtOwnerAddress.value = OwnerDetailsInfo[0].OwnerAddress;
        }
    }    
}
function onFailureOwnerInfo(result, context, method)
{
   makeOwnerDetailsEmpty();
}

function makeOwnerDetailsEmpty()
{
    _lblOwnerRefNo.innerHTML = "";
    _txtOwnerName.value= "";   
    _txtOwnerCompany.value = "";   
    _txtOwnerPhone.value = "";   
    _txtOwnerMobile.value = "";   
    _txtOwnerNotes.value = "";   
    _txtOwnerAddress.value = "";
}