var Data = new Query();
function RemoveAll(dropdown)
{
	dropdown.length = 0; //reset
	dropdown.length = 1; // add blank field
}
function Provinces(dropdown,selection, msg){ // Get List of Provinces and returns it
		
	if (dropdown == null) return;
	RemoveAll(dropdown);
	var query = new Array();	
	query['Type'] = 'Province';
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("province");
	
	msg = msg || "Select a Province";
	
	if(Id.length==0){	
			dropdown.options[0].value="";
			dropdown.options[0].text = "No Provinces Available";
			if(dropdown.onchange) dropdown.onchange();
		}else{		
			dropdown.options[0].value="";
			dropdown.options[0].text = msg;
	}
	
	var Selection = new Array();
	if(selection!=undefined){	
		Selection = selection.split(","); 
	}
	
	for (var i = 0 ; i < Id.length; i++ ){ 
		dropdown.length++;		
		dropdown.options[dropdown.length-1].value= Id[i].firstChild.nodeValue;
		dropdown.options[dropdown.length-1].text = Id[i].firstChild.nodeValue;
		
		if ( inArray(Selection,Id[i].firstChild.nodeValue) ){		
			dropdown.options[dropdown.length-1].selected = "true";		
		}
		
	}	
}
function TownsByProvince(dropdown,Province,selection, msg){ // Get List of Towns by Province and returns it
		
	if (dropdown == null) return;
	RemoveAll(dropdown);
	
	if(Province==""){
		dropdown.options[0].value="";
		dropdown.options[0].text = "Select a Province First";
		if(dropdown.onchange) dropdown.onchange();
		return;
	}
	
	var query = new Array();	
	if(Province!=undefined){	
		query['Province'] = Province;
	}
	query['Type'] = 'Town';	
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("town");
	
	msg = msg || "Select a Town";
	
	if(Id.length==0){	
			dropdown.options[0].value="";
			dropdown.options[0].text = "No Towns Available";
            if(dropdown.onchange) dropdown.onchange();
		}else{		
			dropdown.options[0].value="";
			dropdown.options[0].text = msg;
	}	
		
	
	var Selection = new Array();
	if(selection!=undefined){	
		Selection = selection.split(","); 
	}
	
	
	for (var i = 0 ; i < Id.length; i++ ){ 
		dropdown.length++;		
		dropdown.options[dropdown.length-1].value= Id[i].firstChild.nodeValue;
		dropdown.options[dropdown.length-1].text = Id[i].firstChild.nodeValue;
		
		if ( inArray(Selection,Id[i].firstChild.nodeValue) ){		
			dropdown.options[dropdown.length-1].selected = "true";		
		}
		
	}	
}
function ProvincesByAgency(dropdown,selection, msg){ // Get List of Provinces and returns it
		
	if (dropdown == null) return;
	RemoveAll(dropdown);
	var query = new Array();	
	query['Type'] = 'AProvince';
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("province");
	
	msg = msg || "Select a Province";
	
	if(Id.length==0){	
			dropdown.options[0].value="";
			dropdown.options[0].text = "No Provinces Available";
			if(dropdown.onchange) dropdown.onchange();
		}else{		
			dropdown.options[0].value="";
			dropdown.options[0].text = msg;
	}
	
	var Selection = new Array();
	if(selection!=undefined){	
		Selection = selection.split(","); 
	}
	
	for (var i = 0 ; i < Id.length; i++ ){ 
		dropdown.length++;		
		dropdown.options[dropdown.length-1].value= Id[i].firstChild.nodeValue;
		dropdown.options[dropdown.length-1].text = Id[i].firstChild.nodeValue;
		
		if ( inArray(Selection,Id[i].firstChild.nodeValue) ){		
			dropdown.options[dropdown.length-1].selected = "true";		
		}
		
	}	
}
function TownsByAgency(dropdown,Province,selection, msg){ // Get List of Towns by Province and returns it
		
	if (dropdown == null) return;
	RemoveAll(dropdown);
	
	if(Province==""){
		dropdown.options[0].value="";
		dropdown.options[0].text = "Select a Province First";
		if(dropdown.onchange) dropdown.onchange();
		return
	}
	
	var query = new Array();	
	if(Province!=undefined){	
		query['Province'] = Province;
	}
	query['Type'] = 'ATown';	
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("town");
	
	msg = msg || "Select a Town";
	
	if(Id.length==0){	
			dropdown.options[0].value="";
			dropdown.options[0].text = "No Towns Available";
            if(dropdown.onchange) dropdown.onchange();
		}else{		
			dropdown.options[0].value="";
			dropdown.options[0].text = msg;
	}	
		
	
	var Selection = new Array();
	if(selection!=undefined){	
		Selection = selection.split(","); 
	}
	
	
	for (var i = 0 ; i < Id.length; i++ ){ 
		dropdown.length++;		
		dropdown.options[dropdown.length-1].value= Id[i].firstChild.nodeValue;
		dropdown.options[dropdown.length-1].text = Id[i].firstChild.nodeValue;
		
		if ( inArray(Selection,Id[i].firstChild.nodeValue) ){		
			dropdown.options[dropdown.length-1].selected = "true";		
		}
		
	}	
}
function ProvincesByMember(dropdown,selection, msg){ // Get List of Provinces and returns it
		
	if (dropdown == null) return;
	RemoveAll(dropdown);
	var query = new Array();	
	query['Type'] = 'MProvince';
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("province");
	
	msg = msg || "Select a Province";
	
	if(Id.length==0){	
			dropdown.options[0].value="";
			dropdown.options[0].text = "No Provinces Available";
			dropdown.onchange();
		}else{		
			dropdown.options[0].value="";
			dropdown.options[0].text = msg;
	}
	
	var Selection = new Array();
	if(selection!=undefined){	
		Selection = selection.split(","); 
	}
	
	for (var i = 0 ; i < Id.length; i++ ){ 
		dropdown.length++;		
		dropdown.options[dropdown.length-1].value= Id[i].firstChild.nodeValue;
		dropdown.options[dropdown.length-1].text = Id[i].firstChild.nodeValue;
		
		if ( inArray(Selection,Id[i].firstChild.nodeValue) ){		
			dropdown.options[dropdown.length-1].selected = "true";		
		}
		
	}	
}
function TownsByMember(dropdown,Province,selection, msg){ // Get List of Towns by Province and returns it
		
	if (dropdown == null) return;
	RemoveAll(dropdown);
	
	if(Province==""){
		dropdown.options[0].value="";
		dropdown.options[0].text = "Select a Province First";
		if(dropdown.onchange) dropdown.onchange();
		return
	}
	
	var query = new Array();	
	if(Province!=undefined){	
		query['Province'] = Province;
	}
	query['Type'] = 'MTown';	
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("town");
	
	msg = msg || "Select a Town";
	
	if(Id.length==0){	
			dropdown.options[0].value="";
			dropdown.options[0].text = "No Towns Available";
            if(dropdown.onchange) dropdown.onchange();
		}else{		
			dropdown.options[0].value="";
			dropdown.options[0].text = msg;
	}	
		
	
	var Selection = new Array();
	if(selection!=undefined){	
		Selection = selection.split(","); 
	}
	
	
	for (var i = 0 ; i < Id.length; i++ ){ 
		dropdown.length++;		
		dropdown.options[dropdown.length-1].value= Id[i].firstChild.nodeValue;
		dropdown.options[dropdown.length-1].text = Id[i].firstChild.nodeValue;
		
		if ( inArray(Selection,Id[i].firstChild.nodeValue) ){		
			dropdown.options[dropdown.length-1].selected = "true";		
		}
		
	}	
}
function ProvincesByAtt(dropdown,selection, msg){ // Get List of Provinces and returns it
		
	if (dropdown == null) return;
	RemoveAll(dropdown);
	var query = new Array();	
	query['Type'] = 'AttProvince';
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("province");
	
	msg = msg || "Select a Province";
	
	if(Id.length==0){	
			dropdown.options[0].value="";
			dropdown.options[0].text = "No Provinces Available";
			dropdown.onchange();
		}else{		
			dropdown.options[0].value="";
			dropdown.options[0].text = msg;
	}
	
	var Selection = new Array();
	if(selection!=undefined){	
		Selection = selection.split(","); 
	}
	
	for (var i = 0 ; i < Id.length; i++ ){ 
		dropdown.length++;		
		dropdown.options[dropdown.length-1].value= Id[i].firstChild.nodeValue;
		dropdown.options[dropdown.length-1].text = Id[i].firstChild.nodeValue;
		
		if ( inArray(Selection,Id[i].firstChild.nodeValue) ){		
			dropdown.options[dropdown.length-1].selected = "true";		
		}
		
	}	
}
function TownsByAtt(dropdown,Province,selection, msg){ // Get List of Towns by Province and returns it
		
	if (dropdown == null) return;
	RemoveAll(dropdown);
	
	if(Province==""){
		dropdown.options[0].value="";
		dropdown.options[0].text = "Select a Province First";
		if(dropdown.onchange) dropdown.onchange();
		return
	}
	
	var query = new Array();	
	if(Province!=undefined){	
		query['Province'] = Province;
	}
	query['Type'] = 'AttTown';	
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("town");
	
	msg = msg || "Select a Town";
	
	if(Id.length==0){	
			dropdown.options[0].value="";
			dropdown.options[0].text = "No Towns Available";
            if(dropdown.onchange) dropdown.onchange();
		}else{		
			dropdown.options[0].value="";
			dropdown.options[0].text = msg;
	}	
		
	
	var Selection = new Array();
	if(selection!=undefined){	
		Selection = selection.split(","); 
	}
	
	
	for (var i = 0 ; i < Id.length; i++ ){ 
		dropdown.length++;		
		dropdown.options[dropdown.length-1].value= Id[i].firstChild.nodeValue;
		dropdown.options[dropdown.length-1].text = Id[i].firstChild.nodeValue;
		
		if ( inArray(Selection,Id[i].firstChild.nodeValue) ){		
			dropdown.options[dropdown.length-1].selected = "true";		
		}
		
	}	
}
function AgencyByTown(dropdown,Town,selection, msg){ // Get List of Agency by Town and returns it
		
	if (dropdown == null) return;
	RemoveAll(dropdown);
	
	if(Town==""){
		dropdown.options[0].value="";
		dropdown.options[0].text = "Select a Town First";
		if(dropdown.onchange) dropdown.onchange();
		return
	}
	
	var query = new Array();	
	if(Town!=undefined){	
		query['Town'] = Town;
	}
	query['Type'] = 'Agency';	
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("agency");
	msg = msg || "Select an Agency";
	
	if(Id.length==0){	
			dropdown.options[0].value="";
			dropdown.options[0].text = "No Agencies Available";

		}else{		
			dropdown.options[0].value="";
			dropdown.options[0].text = msg;
	}	
		
	
	var Selection = new Array();
	if(selection!=undefined){	
		Selection = selection.split(","); 
	}
	
	
	for (var i = 0 ; i < Id.length; i++ ){ 
		dropdown.length++;		
		dropdown.options[dropdown.length-1].value= Id[i].firstChild.nodeValue;
		dropdown.options[dropdown.length-1].text = Id[i].firstChild.nodeValue;
		
		if ( inArray(Selection,Id[i].firstChild.nodeValue) ){		
			dropdown.options[dropdown.length-1].selected = "true";		
		}
	}	
}	

function getAgencyDetails(Field,Agency){ // Get Agency Details
	var Place = document.getElementById(Field);
	var query = new Array();	
	if(Agency!=undefined){	
		query['Agency'] = Agency;
	}
	if(Agency==""){
		Place.innerHTML = "<div align='center'>Select an agency first</div>";
		return;
	}
	query['Type'] = 'AgencyDetails';
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var agency = Data.field("agency");
	var address = Data.field("address");
	var tel = Data.field("tel");
	var email = Data.field("email");
	var website = Data.field("website");
	
	
	
     var  Details="<b>Agency:</b> "+agency[0].firstChild.nodeValue+"<BR>";
	  Details+="<b>Physical Address:</b> "+address[0].firstChild.nodeValue+"<BR>";
      Details+="<b>Office Telephone:</b> "+tel[0].firstChild.nodeValue+"<BR>";
      Details+="<b>Email:</b> <a href='mailto:"+email[0].firstChild.nodeValue+"'>"+email[0].firstChild.nodeValue+"</a><BR>";
      Details+="<b>Website:</b> <a href='"+website[0].firstChild.nodeValue+"' target='_blank'>"+website[0].firstChild.nodeValue+"</a>";

	  
	  Place.innerHTML = Details;    
}

function getMemberDetails(Field,Agency){ // Get Member details
	var Place = document.getElementById(Field);
	var query = new Array();	
	if(Agency!=undefined){	
		query['Agency'] = Agency;
	}
	if(Agency==""){
		Place.innerHTML = "<div align='center'>Select an member first</div>";
		return;
	}
	query['Type'] = 'MemberDetails';
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var agency = Data.field("agency");
	var address = Data.field("address");
	var tel = Data.field("tel");
	var email = Data.field("email");
	var website = Data.field("website");
	
	
	
     var Details="<b>Agency:</b> "+agency[0].firstChild.nodeValue+"<BR>";
	  Details+="<b>Physical Address:</b> "+address[0].firstChild.nodeValue+"<BR>";
      Details+="<b>Office Telephone:</b> "+tel[0].firstChild.nodeValue+"<BR>";
      Details+="<b>Email:</b> <a href='mailto:"+email[0].firstChild.nodeValue+"'>"+email[0].firstChild.nodeValue+"</a><BR>";
      Details+="<b>Website:</b> <a href='"+website[0].firstChild.nodeValue+"' target='_blank'>"+website[0].firstChild.nodeValue+"</a>";
      
	  
	  Place.innerHTML = Details;    
}

function MembersByTown(dropdown,Town,selection, msg){ // Get List of Members by Town and returns it
		
	if (dropdown == null) return;
	RemoveAll(dropdown);
	
	if(Town==""){
		dropdown.options[0].value="";
		dropdown.options[0].text = "Select a Town First";
		if(dropdown.onchange) dropdown.onchange();
		return
	}
	
	var query = new Array();	
	if(Town!=undefined){	
		query['Town'] = Town;
	}
	query['Type'] = 'Member';	
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("agency");
	msg = msg || "Select an Member";
	
	if(Id.length==0){	
			dropdown.options[0].value="";
			dropdown.options[0].text = "No Members Available";
			if(dropdown.onchange) dropdown.onchange();

		}else{		
			dropdown.options[0].value="";
			dropdown.options[0].text = msg;
	}	
		
	
	var Selection = new Array();
	if(selection!=undefined){	
		Selection = selection.split(","); 
	}
	
	
	for (var i = 0 ; i < Id.length; i++ ){ 
		dropdown.length++;		
		dropdown.options[dropdown.length-1].value= Id[i].firstChild.nodeValue;
		dropdown.options[dropdown.length-1].text = Id[i].firstChild.nodeValue;
		
		if ( inArray(Selection,Id[i].firstChild.nodeValue) ){		
			dropdown.options[dropdown.length-1].selected = "true";		
		}
	}	
}

function AttorneyByTown(dropdown,Town,selection, msg){ // Get List of Attorney by Town and returns it
		
	if (dropdown == null) return;
	RemoveAll(dropdown);
	
	if(Town==""){
		dropdown.options[0].value="";
		dropdown.options[0].text = "Select a Town First";
		if(dropdown.onchange) dropdown.onchange();
		return
	}
	
	var query = new Array();	
	if(Town!=undefined){	
		query['Town'] = Town;
	}
	query['Type'] = 'Attorney';	
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Attorney = Data.field("attorney");
	var Id = Data.field("id");
	msg = msg || "Select an Attorney";
	
	if(Id.length==0){	
			dropdown.options[0].value="";
			dropdown.options[0].text = "No Attorney Available";
			if(dropdown.onchange) dropdown.onchange();

		}else{		
			dropdown.options[0].value="";
			dropdown.options[0].text = msg;
	}	
		
	
	var Selection = new Array();
	if(selection!=undefined){	
		Selection = selection.split(","); 
	}
	
	
	for (var i = 0 ; i < Id.length; i++ ){ 
		dropdown.length++;		
		dropdown.options[dropdown.length-1].value= Id[i].firstChild.nodeValue;
		dropdown.options[dropdown.length-1].text = Attorney[i].firstChild.nodeValue;
		
		if ( inArray(Selection,Id[i].firstChild.nodeValue) ){		
			dropdown.options[dropdown.length-1].selected = "true";		
		}
	}	
}

function getAttorneyDetails(Field,Attorney){ // Get Attorney Details
	var Place = document.getElementById(Field);
	var query = new Array();	
	if(Attorney!=undefined){	
		query['Attorney'] = Attorney;
	}
	if(Attorney=="")
	{
		Place.innerHTML = "<div align='center'>Select an attorney first</div>";
		return;
	}
	query['Type'] = 'AttorneyDetails';
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var attorney = Data.field("attorney");
	var address = Data.field("address");
	var tel = Data.field("tel");
	var email = Data.field("email");
	var website = Data.field("website");
	
	
     var Details ="<b>Firm:</b> "+attorney[0].firstChild.nodeValue+"<BR>";
	  Details+="<b>Physical Address:</b> "+address[0].firstChild.nodeValue+"<BR>";
      Details+="<b>Office Telephone:</b> "+tel[0].firstChild.nodeValue+"<BR>";
      Details+="<b>Email:</b> <a href='mailto:"+email[0].firstChild.nodeValue+"'>"+email[0].firstChild.nodeValue+"</a><BR>";
      Details+="<b>Website:</b> <a href='"+website[0].firstChild.nodeValue+"' target='_blank'>"+website[0].firstChild.nodeValue+"</a>";
     
	  Place.innerHTML = Details;    
}

function GetMaxSubmittionForm(Town){ // Get max submittion forms
	if(Town=="") return;
	
	var query = new Array();	
	query['Town'] = Town;
	query['Type'] = 'MaxSubmission';	

	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("max");
	var Num = Data.field("num");
	if(	Id.length >0 && Num[0].firstChild.nodeValue >= Id[0].firstChild.nodeValue ){
		alert("Unfortunately this city is fully booked.\n Please try again at a later stage");
	 	return false;
	}	
}

function GetMaxSubmittionForm2(Town){ // Get max submittion forms
	if(Town=="") return;
	
	var query = new Array();	
	query['Town'] = Town;
	query['Type'] = 'MaxSubmission2';
	
	Data.fetch("AgencySearch/Functions/xml.asp",query);
	var Id = Data.field("max");
	var Num = Data.field("num");
	return true;
}
