var errors, hddnBuildingId, selClientId, selAreaId, edtBuildingName, txtBuildingNeeds;

var hddnAddressId, hddnClientId, hddnAddressTypeId, hddnCountryId, hddnPostId, edtAddressStreet, edtAddressSuburb, edtAddressCity, edtAddressCounty, edtAddressPostalCode, edtAddressLng, edtAddressLat;

var module = baseRootURL + "building/";

function registerBuilding(action)
{
	errors = false;
	validate_hddnBuildingId();
	validate_selClientId();
	validate_selAreaId();
	validate_edtBuildingName();
	validate_txtBuildingNeeds();
	if (!errors)
	{
		var data = {};
		data.dummy = ""; //we seem to get errors from the first value, so always add a dummy value first.
		data.action = action;
		if (document.getElementById("hddnBuildingId") != null) data.hddnBuildingId = hddnBuildingId;
		data.selClientId = selClientId;
		data.selAreaId = selAreaId;
		data.edtBuildingName = edtBuildingName;
		data.txtBuildingNeeds = txtBuildingNeeds;
		callModule("building/building_val.php", data);
        
        registerAddress(0);
        
	}
}
function insertBuilding(){registerBuilding(0)}
function modifyBuilding(){registerBuilding(1)}

function validate_hddnBuildingId(){
	if (document.getElementById("hddnBuildingId") != null) hddnBuildingId=document.getElementById("hddnBuildingId").value;}
function validate_selClientId(){selClientId=validateRequiredSelection("selClientId", "Please make a valid selection.");}
function validate_selAreaId(){selAreaId=validateRequiredSelection("selAreaId", "Please make a valid selection.");}
function validate_edtBuildingName(){edtBuildingName=validateRequiredText("edtBuildingName", "This field is required.");}
function validate_txtBuildingNeeds(){txtBuildingNeeds=document.getElementById("txtBuildingNeeds").value;}

function viewBuilding(id)
{
	var data = {}
	data.dummy = "";
	data.hddnBuildingId = id;
	data.action = 2;
	callModule("building/building_val.php", data);
}

function viewClient(id)
{
	var data = {}
	data.dummy = "";
	data.hddnBuildingId = id;
	data.action = 5;
	callModule("building/building_val.php", data);
}

function editBuilding(id)
{
	var data = {}
	data.dummy = "";
	data.hddnBuildingId = id;
	data.action = 3;
	callModule("building/building_val.php", data);
}

function deleteBuilding(id, desc)
{
	if (confirm("Are you sure you want to delete "+desc+" and all associated records?"))
	{
		var data = {}
		data.dummy = "";
		data.hddnBuildingId = id;
		data.action = 4;
		callModule("building/building_val.php", data);
	}
}

function doClientRefinement(obj)
{
	var data = {}
	data.dummy = "";
	data.field_id = 'selClientId';
	data.input_id = obj.value;
	callModule("lib/getClientsByName.php", data);
}

function setClientName()
{
	"use strict"; 
	var sel = document.getElementById('selClientId');
	document.getElementById('edtClientName').value = sel.options[sel.selectedIndex].innerHTML;
}

function doAreaRefinement(obj)
{
	var data = {}
	data.dummy = "";
	data.field_id = 'selAreaId';
	data.input_id = obj.value;
	callModule("lib/getAreasByName.php", data);
}

function setAreaName()
{
	"use strict"; 
	var sel = document.getElementById('selAreaId');
	document.getElementById('edtAreaName').value = sel.options[sel.selectedIndex].innerHTML;
}

function registerAddress(action)
{
	errors = false;
	validate_hddnAddressId();
	validate_hddnClientId();
	validate_hddnAddressTypeId();
	validate_hddnCountryId();
	validate_hddnPostId();
	validate_edtAddressStreet();
	validate_edtAddressSuburb();
	validate_edtAddressCity();
	validate_edtAddressCounty();
	validate_edtAddressPostalCode();
	validate_edtAddressLng();
	validate_edtAddressLat();
	if (!errors)
	{
		var data = {};
		data.dummy = ""; //we seem to get errors from the first value, so always add a dummy value first.
		data.action = action;
		if (document.getElementById("hddnAddressId") != null) data.hddnAddressId = hddnAddressId;
		data.hddnClientId = hddnClientId;
		data.hddnAddressTypeId = hddnAddressTypeId;
		data.hddnCountryId = hddnCountryId;
		data.hddnPostId = hddnPostId;
		data.edtAddressStreet = edtAddressStreet;
		data.edtAddressSuburb = edtAddressSuburb;
		data.edtAddressCity = edtAddressCity;
		data.edtAddressCounty = edtAddressCounty;
		data.edtAddressPostalCode = edtAddressPostalCode;
		data.edtAddressLng = edtAddressLng;
		data.edtAddressLat = edtAddressLat;
		callModule("address/address_val.php", data);
	}
}

function validate_hddnAddressId(){
	if (document.getElementById("hddnAddressId") != null) hddnAddressId=document.getElementById("hddnAddressId").value;}
function validate_hddnClientId(){
	if (document.getElementById("hddnClientId") != null) hddnClientId=document.getElementById("hddnClientId").value;}
function validate_hddnAddressTypeId(){
	if (document.getElementById("hddnAddressTypeId") != null) hddnAddressTypeId=document.getElementById("hddnAddressTypeId").value;}
function validate_hddnCountryId(){
	if (document.getElementById("hddnCountryId") != null) hddnCountryId=document.getElementById("hddnCountryId").value;}
function validate_hddnPostId(){
	if (document.getElementById("hddnPostId") != null) hddnPostId=document.getElementById("hddnPostId").value;}
function validate_edtAddressStreet(){edtAddressStreet=validateRequiredText("edtAddressStreet", "Address Street is required.");}
function validate_edtAddressSuburb(){edtAddressSuburb=validateRequiredText("edtAddressSuburb", "Address Suburb is required.");}
function validate_edtAddressCity(){edtAddressCity=validateRequiredText("edtAddressCity", "Address City is required.");}
function validate_edtAddressCounty(){edtAddressCounty=validateRequiredText("edtAddressCounty", "Address County is required.");}
function validate_edtAddressPostalCode(){edtAddressPostalCode=validateRequiredText("edtAddressPostalCode", "Address Postal Code is required.");}
