


var totaltemp // number of tempoary controls created when hiding select boxes


 function fetchcodelist(domaincode, ctlid, listtype) {

 	// set the calling control to fill when item is selected from the list
 	fillctl= ctlid

 	// fetches code list from xml file
 	switch (listtype){
 		case 'multiselect':
 			multiselect = 'multiselect'
 			str_function = 'tick_ms_checkboxes("'+ctlid+'")'
  			popup_div('div_codelist',600,400,'',$F('ctl_app_path')+'fetchcodelist.asp?id='+domaincode+'&multiselect=true','',str_function)
 			break;

 		case 'single':
 			multiselect = 'single'
 			popup_div('div_codelist',600,400,'',$F('ctl_app_path')+'fetchcodelist.asp?id='+domaincode)
 			break;

 		case 'noarray':
 			multiselect = 'noarray'
 			popup_div('div_codelist',600,400,'',$F('ctl_app_path')+'fetchcodelist.asp?id='+domaincode+'&multiselect=true')
 			break;
 	}
 }



function updatehelp(ctlname){

//	if (Object.isElement($('div_help'))) {

//	var divtype = 'description';


//	var div_return = $('div_help')

//	AjaxRequest.get(
//			{
//			 'url': 'fetchhelp.asp?ctlname='+ctlname
//			,'onSuccess':function(req){ div_return.innerHTML = req.responseText;
										// if the new help doesn't have an example switch to the description
										//if (divtype == 'example' && document.getElementById('div_help_example').innerHTML=="") {
//												divtype = 'description';
    									//}
  //  									showhelp(divtype)
//										;
//									  }
//			,'onError':function(req){alert('fetchhelp.asp?ctlname='+ctlname)}
	//		}
//	);

//	}
}

function tick_ms_checkboxes(ctlid) {

	var lng_ctls = $F('arr_'+ctlid);
	for (var i=1;i<=lng_ctls;i++){
		str_val = $F('ari_'+ctlid+'_'+i);
		$('list_chk_'+str_val).checked = true;
	}
}

 function selectoption(val,textval){

 	if (val!='cancel_list'){
 		// fill ctl with list value, fillctl is set by the fetchcodelist function
 		$(fillctl).value = val;
 		$(fillctl +'_view').value = textval;
 	}
 	popup_close()

 }


function set_step_heights(){
	var winheight
	var availheight
	var stepheight

	if ($('div_popup_metadata_edit')) {
		availheight = $('div_popup_metadata_edit').style.height
	} else {
		winheight = winH();
		availheight = winheight -155
	}

	stepheight = parseInt(availheight)-52;

	for (var i=1;i<=8;i++){
		$("step"+i).style.height=  stepheight +"px"
		$("step"+i).style.overflow = 'auto';
	}
}

function show_browse(ctl_id){
	var ctl_rb = document.getElementById(ctl_id + '_file')
	var ctl_fb = document.getElementById(ctl_id + '_text')
	var but_fb = document.getElementById('fb_' + ctl_id)

	ctl_rb.className = ''
	ctl_fb.className = 'hidden'
	//ctl_fb.name='dum_'+ctl_id
	//ctl_rb.name='txt_'+ctl_id
	but_fb.className = 'hidden'

}

function showstep(stepNo){
	 var maxstep = 8;
	 for (i=1;i<= maxstep;i++){
	 	$('step'+i).style.display = "none"
	 	$('td_navbar_'+i).className="navbar_inactive"
	 }
	 $('step'+stepNo).style.display = ""
	 $('td_navbar_'+stepNo).className="navbar_active"
}


function showcontact(contactNo){

	 var maxstep = 4;
	 for (i=1;i<= maxstep;i++){
	 	$('Contact_row_'+i).style.display = "none"
	 	$('Contact_tab_'+i).className="navbar_inactive"
	 }
	 $('Contact_row_'+contactNo).style.display = ""
	 $('Contact_tab_'+contactNo).className="navbar_active"
}


function fetchcontactlist(prefix){
	popup_div('div_contact_list',600,400,'',$F('ctl_app_path')+'fetchcontactlist.asp?p='+prefix)
}

function checkcontactlist(prefix){

	fillctl = prefix
	var str_name = document.getElementById(prefix+'ConName').value
	var str_org = document.getElementById(prefix+'ConOrganisation').value

	if (str_name == '' || str_org == ''){
		alert ("Name and organisation must be entered\nbefore data can be saved to the contacts list")
	} else {
		if (document.all) {
			var xml_doc = new ActiveXObject("Microsoft.XMLDOM");
		} else {
			var xml_doc  = document.implementation.createDocument("", "", null);
		}

		var n_root =  xml_doc.createElement('ROOT')
		xml_doc.appendChild(n_root);

		var n_contact =  xml_doc.createElement('CONTACT')
		n_root.appendChild(n_contact);

		var attr = xml_doc.createAttribute('name');
		attr.value = str_name
		n_contact.setAttributeNode(attr);

		attr = xml_doc.createAttribute('org');
		attr.value = str_org
		n_contact.setAttributeNode(attr);

		attr = xml_doc.createAttribute('pos');
		attr.value = document.getElementById(prefix+'ConPos').value
		n_contact.setAttributeNode(attr);

		attr = xml_doc.createAttribute('del_point');
		attr.value = document.getElementById(prefix+'ConDelPoint').value
		n_contact.setAttributeNode(attr);

		attr = xml_doc.createAttribute('city');
		attr.value = document.getElementById(prefix+'ConCity').value
		n_contact.setAttributeNode(attr);

		attr = xml_doc.createAttribute('state');
		attr.value = document.getElementById(prefix+'ConState').value
		n_contact.setAttributeNode(attr);

		attr = xml_doc.createAttribute('postcode');
		attr.value = document.getElementById(prefix+'ConPostcode').value
		n_contact.setAttributeNode(attr);

		attr = xml_doc.createAttribute('country');
		attr.value = document.getElementById(prefix+'ConCountry').value
		n_contact.setAttributeNode(attr);

		attr = xml_doc.createAttribute('email');
		attr.value = document.getElementById(prefix+'ConEmail').value
		n_contact.setAttributeNode(attr);

		attr = xml_doc.createAttribute('telephone');
		attr.value = document.getElementById(prefix+'ConTelephone').value
		n_contact.setAttributeNode(attr);

		attr = xml_doc.createAttribute('fax');
		attr.value = document.getElementById(prefix+'ConFax').value
		n_contact.setAttributeNode(attr);

		if (document.all) {
			var str_xml = xml_doc.xml
		} else {
			var serializer = new XMLSerializer();
			var str_xml = serializer.serializeToString(xml_doc)
		}




		AjaxRequest.post(
				{
				 'url': $F('ctl_app_path')+'savecontact.asp',
				 'parameters': {'check':'TRUE', 'prefix': prefix, 'xml':str_xml}
				,'onSuccess':function(req){
											  if (req.responseText.substr(0,14) == "Update Contact") {
													if (confirm("This contact is already saved\nDo you want to update this contact")){
														AjaxRequest.post(
																		{
																		 'url': $F('ctl_app_path')+'savecontact.asp',
																		 'parameters': {'prefix': prefix, 'xml':str_xml}
																		,'onSuccess':function(req){
																									 document.getElementById(fillctl+'_Save_Link').innerHTML = req.responseText;
																								  }
																		 ,'onError':function(req){alert('savecontact.asp?prefix='+prefix+'&xml='+str_xml)}
																		 }
														);


													}
											  } else {
											  	 document.getElementById(fillctl+'_Save_Link').innerHTML = req.responseText;
											  }

										  }

				,'onError':function(req){alert('savecontact.asp?check=TRUE&prefix='+prefix+'&xml='+str_xml)}
				}
		);


	}
}

function deleteContactFromList(contact_id) {
	if (confirm("Are you sure you want to delete this contact?")){
		AjaxRequest.get(
					{
					 'url': $F('ctl_app_path')+'savecontact.asp?delid='+contact_id
					,'onSuccess':function(req){
												var contact_id = req.responseText;
												var tab_row = document.getElementById("contactlist_row_"+contact_id);
												tab_row.parentNode.removeChild(tab_row);
											  }

					,'onError':function(req){alert('savecontact.asp?&xml='+str_xml)}
					}
		);
	}
}

function fillcontact(fill_control,contact_id) {

	AjaxRequest.get(
		{
		 'url': $F('ctl_app_path')+'fetchcontactlist.asp?id='+contact_id+'&rt=xml'
		,'onSuccess':function(req){
									var xmlval = req.responseXML
									var objs = xmlval.getElementsByTagName("CONTACT")

									$(fill_control+'ConName').value = objs[0].getAttribute("name")
									$(fill_control+'ConOrganisation').value = objs[0].getAttribute("org")
									$(fill_control+'ConPos').value = objs[0].getAttribute("pos")
									$(fill_control+'ConDelPoint').value = objs[0].getAttribute("del_point")
									$(fill_control+'ConCity').value = objs[0].getAttribute("city")
									$(fill_control+'ConState').value = objs[0].getAttribute("state")
									$(fill_control+'ConPostcode').value = objs[0].getAttribute("postcode")
									$(fill_control+'ConCountry').value = objs[0].getAttribute("country")
									$(fill_control+'ConEmail').value = objs[0].getAttribute("email")
									$(fill_control+'ConTelephone').value = objs[0].getAttribute("telephone")
									$(fill_control+'ConFax').value = objs[0].getAttribute("fax")

									popup_close()
								  }
		,'onError':function(req){alert(page_name+params)}
		}
	);

}

function updatemultiselect(){

	switch (multiselect)
		{
		case 'multiselect':

			// clear the array controls
			var arrctl
			var lnbr
			var cnt = document.getElementById("arr_"+fillctl)
			for (var i=cnt.value;i>0;i--)
				{
				arrctl = document.getElementById('ari_'+fillctl+'_'+i)
				arrctl.parentNode.removeChild(arrctl);
				arrctl = document.getElementById('ari_'+fillctl+'_'+i+'_view')
				arrctl.parentNode.removeChild(arrctl);

				lnbr = document.getElementById('lnbr_'+fillctl+'_'+i)
				lnbr.parentNode.removeChild(lnbr);
				cnt.value--
				}

			var mslist = document.getElementById("multiselect_list")
			var ctls = mslist.getElementsByTagName("input")
			for (var i=0;i<ctls.length;i++)
				{
				if (ctls[i].checked == true)
					{
					cnt.value++
					var newvctl = document.createElement('input')
					newvctl.type = 'text'
					newvctl.size = cnt.size

					newvctl.id = 'ari_'+fillctl+'_'+cnt.value+'_view'
					newvctl.value = ctls[i].value
					//newvctl.attachEvent("onfocus",function () {updatehelp(fillctl)})

					var newctl = document.createElement('input')
					newctl.type = 'hidden'

					newctl.id = 'ari_'+fillctl+'_'+cnt.value
					newctl.name = 'ari_'+fillctl+'_'+cnt.value
					newctl.value = ctls[i].id.substring(9)

					var newlnbr =  document.createElement('br')
					newlnbr.id = 'lnbr_'+fillctl+'_'+cnt.value

					var newobjctl = document.getElementById('newobjects_'+fillctl)

					newobjctl.appendChild(newvctl);
					newobjctl.appendChild(newctl);
					newobjctl.appendChild(newlnbr);
					}
				}
			break;

		case 'noarray':

			var ctl = document.getElementById(fillctl)
			var ctl_view = document.getElementById(fillctl+"_view")

			ctl.value = '';
			ctl_view.value = '';

			var mslist = document.getElementById("multiselect_list")
			var ctls = mslist.getElementsByTagName("input")
			for (var i=0;i<ctls.length;i++)
				{
				if (ctls[i].checked == true)
					{
					if (ctl.value != '')
						{
						ctl.value += '\n\n'
						ctl_view.value += '\n\n'
						}
					ctl.value += ctls[i].value
					ctl_view.value += ctls[i].value
					}
				}
			break;

		}

	popup_close()


}

function deletearrayobject(ctlid, pos){
	var ctl = document.getElementById('ari_'+ctlid+'_'+pos)
	ctl.parentNode.removeChild(ctl);
	var lnk = document.getElementById('del_'+ctlid+'_'+pos)
	lnk.parentNode.removeChild(lnk);
	var lnbr = document.getElementById('lnbr_'+ctlid+'_'+pos)
	lnbr.parentNode.removeChild(lnbr);
}

function addarrayobject(ctlid){
	var cnt = document.getElementById('arr_'+ctlid)
	var newposid = parseInt(cnt.value)+1
	cnt.value++

	if (cnt.type=="textarea"){
		var newctl = document.createElement('textarea')
		newctl.rows = cnt.rows
		newctl.cols = cnt.cols
	} else {
		var newctl = document.createElement('input')
		newctl.type = 'text'
		newctl.size = cnt.size
	}

	newctl.id = 'ari_'+ctlid+'_'+newposid
	newctl.name = 'ari_'+ctlid+'_'+newposid
	if (document.all) {
		newctl.attachEvent("onfocus",function () {updatehelp(ctlid)})
	} else {
		newctl.addEventListener('focus',function () {updatehelp(ctlid)},false)
	}

	var newlnk =  document.createElement('a')
	newlnk.href = 'javascript:deletearrayobject("'+ctlid+'",'+newposid+')'
	newlnk.id = 'del_'+ctlid+'_'+newposid
	var newlnktext=document.createTextNode('Delete');
	newlnk.appendChild(newlnktext);

	var newlnbr =  document.createElement('br')
	newlnbr.id = 'lnbr_'+ctlid+'_'+newposid

	var newobjctl = document.getElementById('newobjects_'+ctlid)

	newobjctl.appendChild(newctl);
	newobjctl.appendChild(newlnk);
	newobjctl.appendChild(newlnbr);
}

function addscale() {
	var num = 0;
	var ctls = document.getElementsByTagName("input")
	for (i=0;i<ctls.length;i++){
		if (ctls[i].name=="med_scale"){
			if (ctls[i].value.substring(0,5) == 'scale'){
				ctlnum = parseInt(ctls[i].value.substring(5,ctls[i].value.length))
				if (ctlnum > num) {
					num=ctlnum;
				}
			}
		}
	}

	AjaxRequest.get(
			{
			 'url': $F('ctl_app_path')+'new_scale.asp?pf=scale'+(num+1)
			,'onSuccess':function(req){$("new_scale").innerHTML += req.responseText;

									  }
			,'onError':function(req){alert('new_scale.asp?pf=scale'+(num+1))}
			}
	);

}


function change_cov_date_type(doc_name,cov_date_type) {

	update_div('div_cov_date',$F('ctl_app_path')+'cov_date_type.asp?'+doc_name+'&inst='+cov_date_type,'','')
}



function select_list_item(list_item){
	// get the selected list
	var sel_list = document.getElementById("list_selected")
	//build an array of all the items in the list
	//and clear the selected list
	var ctls = sel_list.getElementsByTagName("li")
	var sel_array = new Array;
	var j=0
	var new_item = true

	for (var i=ctls.length-1;i>=0;i--){
		if (ctls[i].id == list_item) {
			new_item = false;
		}
		sel_array[j] = ctls[i].id
		j++
		ctls[i].parentNode.removeChild(ctls[i])
	}
	// add new item to the end of the array
	// change the class of the item in the select list
	if (new_item){
		sel_array.push(list_item)
		$(list_item+'_href').className="selected";
	}
	// sort the array
	sel_array.sort()
	// repopulate the selected list for the array
	var sel_name
	for (var i=0;i<sel_array.length;i++){
		sel_name = sel_array[i].replace("&apos;","'")
		sel_list.innerHTML += '<li id=\''+sel_array[i]+'\'><a href=\'javascript:deselect_list_item("'+sel_array[i]+'")\'>'+sel_name+'</a></li>'
	}

}

function deselect_list_item(list_item) {
	// remove the selected class from the item in the select list
	if (object_exists(list_item+'_href','a')){
		document.getElementById(list_item+'_href').className=""
	}
	// remove the item from the selected list
	ctl = document.getElementById(list_item)
	ctl.parentNode.removeChild(ctl)
}

function show_place_list(){
	popup_div('div_place_list',690,470,'',$F('ctl_app_path')+'district_ward.asp?','','fill_current_selected()')
}

function fill_current_selected(){
	// get the number of items in the keyword002 (place) array
	var arr_count = document.getElementById("arr_keyword002").value
	var arr_item
	var arr_item_id
	var sel_list = document.getElementById("list_selected")

	for (var i=1;i<=arr_count;i++){
		if (document.getElementById("ari_keyword002_"+i)){
			arr_item = document.getElementById("ari_keyword002_"+i).value
			arr_item_id = arr_item.replace("'","&apos;")
			sel_list.innerHTML += '<li id=\''+arr_item_id+'\'><a href=\'javascript:deselect_list_item("'+arr_item_id+'")\'>'+arr_item+'</a></li>'

			if (object_exists(arr_item_id+'_href','a')){
				document.getElementById(arr_item_id+'_href').className="selected";

			}
		}
	}

}

function update_palces_array(){

	var arr_counter = $("arr_keyword002")
	for (var i=1;i<=arr_counter.value;i++){
		if ($("ari_keyword002_"+i)){
			deletearrayobject('keyword002',i)
		}
	}

	var sel_list = document.getElementById("list_selected")
	var ctls = sel_list.getElementsByTagName("li")
	var newctl
	for (var i=0;i<ctls.length;i++){
		addarrayobject('keyword002')
		newctl = $("ari_keyword002_"+arr_counter.value)
		newctl.value = ctls[i].id
	}
	popup_close()
}

function removesec(sec_id){
	var sec_div = document.getElementById(sec_id)
	sec_div.parentNode.removeChild(sec_div);
}



function addcontact(prefix, labelprefix, fixedrole) {

	var prefix_len = prefix.length;
	var num = 0;
	var pf = prefix;
	var new_div

	var ctls = document.getElementsByTagName("input")
	for (i=0;i<ctls.length;i++){
		if (ctls[i].name=="med_contact"){
			if (ctls[i].value.substring(0,prefix_len) == prefix){
				ctlnum = parseInt(ctls[i].value.substring(prefix_len,ctls[i].value.length))
				if (ctlnum > num) {
					num=ctlnum;
				}
			}
		}
	}



	var new_div = Builder.node('div', {id:'new_contact_'+pf+'_'+(num+1) })
	$('new_contact_'+pf).appendChild(new_div)


	if (fixedrole == undefined) {
		update_div('new_contact_'+pf+'_'+(num+1),$F('ctl_app_path')+'new_contact.asp?pf='+prefix+(num+1)+'&lpf='+labelprefix,'','')
	} else {
		update_div('new_contact_'+pf+'_'+(num+1),$F('ctl_app_path')+'new_contact.asp?pf='+prefix+(num+1)+'&lpf='+labelprefix+'&fr='+fixedrole,'','')
	}

}





function updatedate(ctl){

	var day = $(ctl.id+'_day').value
	var month = $(ctl.id+'_month').value
	var year = $(ctl.id+'_year').value

	// if the month is set to none but the day is still set then put mm in the month
	// this will cause the validation to fail
	if (month=='' && day!=''){
		month = 'mm'
	}
	if (month!='' | day!=''){
		// if year isn't 4 digits long add y's to the end
		for (var i=(4-year.length);i>0;i--) {
			year+='y'
		}
	}

	ctl.value = year+month+day;
}


function metadata_save(str_type, location, bol_popup){

	clear_val_errors()



	var bol_validate = validate();
	//bol_validate = true;

	if (bol_validate) {

		if (str_type == 'save') {
		   if (($('sp_file_name') && $('sp_file_name').innerHTML == 'Not saved') || $F('ctl_user_sal') == 'false') {
		   		str_type = 'saveas'
		   }

		}

		if (str_type == 'saveas' && location == undefined) {
			filewindow('saveas');
		} else {

			popup_wait(300,150,'Please wait','Saving Metadata');


			var frm = $("frm_Metadata")

			if (location != undefined) {
				frm.action = location
			}

			if ($("linkage_text").className=="hidden") {
				$("linkage_text").value = $F("linkage_file")
			}

			var ctl_attach = $("ctl_attachment")
			if (ctl_attach) {
				//copy the email fields to the main page ready for submit;

				var ctl = $("ctl_gis_email")
				ctl.value = $('chk_email').checked

				ctl = $("ctl_gis_email_comments")
				ctl.value = $F('ctl_email_comment')

				ctl = $("ctl_gis_email_attachment")


				var str_att = $F("ctl_attachment")
				var lng_s_pos = str_att.lastIndexOf("\\") // escaped single slash
				ctl.value = str_att.substr(lng_s_pos + 1)
			}

			if (bol_popup) {
				var div_return = $('div_popup_metadata_edit')
			} else {
				var div_return = $('div_main_content')
			}



			AjaxRequest.submit(
				frm,{
					 'parameters':{ 'action':'Save'}
					,'onSuccess':function(req){
						 div_return.innerHTML = req.responseText;
						 file_on_load()
						 popup_close();//wait message
						 if (bol_popup) {
						 	// accessed from the resource manager
							select_tab()
							popup_close(); // metadata editor popup
						 } else {
							 if (str_type == 'saveas') {

								popup_close();
							 }
					 	 }
						 // ** DV added the following line 04.08.2010 **			 
						 $('sp_upload_doc').innerHTML = '<a title="Upload document" href="http://geoserver.nottinghamcity.gov.uk/insight/admin/documentupload2.aspx?filename='+$F('ctl_doc_dbn')+'&amp;username='+$F('ctl_user_username')+'" target="_blank">Upload document</a>';
					 }
					,'onError':function(req){alert(req.responseText)}
				 }
	  		);
			// ** DV added the following line 25.02.2010 **
		 	//$('sp_upload_doc').innerHTML = '<a title="Upload document" href="http://geoserver.nottinghamcity.gov.uk/insight/admin/documentupload2.aspx?filename='+$F('ctl_doc_dbn')+'&amp;username='+$F('ctl_user_username')+'" target="_blank">Upload document</a>';
		}



	} else {

		alert('Validation failed, Please correct highlighted fields')
	}

}
