var fillctl
var multiselect
var lng_popup_count = 0;

var prev_folders = new Array;

var uploadcheck


var totaltemp // number of tempoary controls created when hiding select boxes



function update_div (div_name, page_name, params, callfunction) {

	var div_return = $(div_name)
	if (params != undefined && params != '') {
		str_params = '&'+params
	} else {
		str_params = ''
	}

	AjaxRequest.get(
		{
		 'url': page_name+str_params
		,'onSuccess':function(req){ div_return.innerHTML = req.responseText;
									//alert(req.responseText);
									eval (callfunction);
								  }
		,'onError':function(req){alert(page_name+str_params)}
		}
	);

}




function ctl_exists(ctlid){
	var frm = document.getElementById('frm_Metadata');
	var ctls = frm.elements
	for(var i=0;i<ctls.length;i++){
		if (ctls[i].id == ctlid) {
			return true;
		}
	}

	return false;
}

function object_exists(objid,objtype){

	var objs = document.getElementsByTagName(objtype)
	for(var i=0;i<objs.length;i++){
		if (objs[i].id == objid){
			return true;
		}
	}

	return false;
}

function get_node_val (nodename, parent, datasettype){
	// function gets the text value from a node based on the relevant datasettype

	// get the relevant display properties for the datasettype
	var nl_node = parent.getElementsByTagName(nodename)
	if (nl_node.length > 1){
		// if there is more than one node pick the relvent one for the datasettype
		for (var j=0;j<nl_node.length;j++){
			if (nl_node[j].getAttribute("datasettype") == datasettype) {
				return nl_node[j].firstChild.nodeValue
			}
		}
	} else {
		// just use the node
		if (nl_node.length==1) {
			return nl_node[0].firstChild.nodeValue
		} else {
			return 'error no '+nodename+' property found'
		}
	}

}


function popup_div	(str_id, lng_width, lng_height, str_title, str_page, str_params, callfunction, bol_reuse) {
	
	bol_reuse = bol_reuse || false;
	
	if (lng_popup_count > 0 && bol_reuse) {
	
		popup_resize(lng_width, lng_height);
		
		
		$('div_popup_title_'+lng_popup_count).innerHTML = str_title;
		
		
		var arr_div = $$('div#div_popup_outer_'+lng_popup_count+' .popup_inner')
		
		arr_div[0].parentNode.removeChild(arr_div[0]);
	
		var div_inner = Builder.node('div',{id:str_id,
													className:'popup_inner',
													style:'height:'+(lng_height-40)+'px'
													})
		
		$('div_popup_outer_'+lng_popup_count).appendChild(div_inner)
		
		update_div(str_id,str_page,str_params, callfunction)

		
	} else {
		lng_popup_count++;
		
		
		disable_all_controls ()
		hide_select()
		
		var ctl_body = document.getElementsByTagName("body")[0]

		var lng_left = Math.floor( (winW() - lng_width) / 2);
		var lng_top = Math.floor( (winH() - lng_height) / 2);

		var div_outer = Builder.node('div', {id:'div_popup_outer_'+lng_popup_count,
											 className:'popup',
											 style:'height:'+lng_height+'px'+
												   ';width:'+lng_width+'px'+
												   ';left:'+lng_left+'px'+
												   ';top:'+lng_top+'px'+
												   ';display:none'+
												   ';z-index:'+lng_popup_count*100
											 })
		var div_header = Builder.node('div')
		div_outer.appendChild(div_header)

		if (str_title==undefined) {
			str_title=''
		}
		var div_title = Builder.node('div', {id:'div_popup_title_'+lng_popup_count}, str_title)
		div_outer.appendChild(div_title)


		var a_close = Builder.node('a', {href:'javascript:popup_close()', title:'close', className:'close_button'},[Builder.node('img', {src:'img/popup_close.gif', border:'0'})])
		div_header.appendChild(a_close)

		var div_inner = Builder.node('div',{id:str_id,
											className:'popup_inner',
											style:'height:'+(lng_height-40)+'px'
											})

		div_outer.appendChild(div_inner)






		ctl_body.appendChild(div_outer)
		
		if (str_page != undefined) {
			update_div(str_id,str_page,str_params, callfunction)
		}
		new Effect.Grow('div_popup_outer_'+lng_popup_count,{duration:0.5});
	}

}

function popup_resize(lng_width, lng_height){
	

	var lng_left = Math.floor( (winW() - lng_width) / 2);
	var lng_top = Math.floor( (winH() - lng_height) / 2);
	var ctl_div = $('div_popup_outer_'+lng_popup_count)

	ctl_div.style.top = lng_top+'px'
	ctl_div.style.left = lng_left+'px'
	ctl_div.style.width = lng_width+'px'
	ctl_div.style.height = lng_height+'px'
}


function popup_close(){
	
	
	var cur_popup_no = lng_popup_count
	new Effect.Shrink('div_popup_outer_'+cur_popup_no,{
						afterFinish:function(){Element.remove('div_popup_outer_'+cur_popup_no)},
						duration:0.5}
	);
	show_select()
	var div_overlay= $('overlay_'+cur_popup_no)
	div_overlay.parentNode.removeChild(div_overlay);
	lng_popup_count--;

}



function disable_all_controls () {

		
		var ctl_body = document.getElementsByTagName("body")[0]
		var lng_zindex = lng_popup_count*100-1;
	

		var div_overlay = Builder.node('div', {id:'overlay_'+lng_popup_count,className:'overlay',
											  style:'height:'+winH()+'px'+
												    ';width:'+winW()+'px'+
												    ';left:0'+
												    ';top:0'+
												    ';z-index:'+lng_zindex
											})
											
		ctl_body.appendChild(div_overlay)

		new Effect.Opacity('overlay_'+lng_popup_count,
		    { duration: 0.5,
		      transition: Effect.Transitions.linear,
      		  from: 0.0, to: 0.4 });

}

function winH() {
	return document.documentElement.clientHeight

}

function winW() {
	return document.documentElement.clientWidth
}



function hide_select() {
	// function hides all select boxes to enable a div to be shown on top
	// select boxes are hidden and a new temporary input box is put in it's place
	// to minimise the change to the screen appearence.

	ctls = document.getElementsByTagName("select")
	for (i=0;i<ctls.length;i++){
		// only hide if not already hidden (user already clicked to show list and not selected or cancelled)
		if (ctls[i].style.display!='none'){
			ctls[i].style.display='none'
			var newctl = document.createElement('input')
			newctl.type = 'text'
			newctl.className = ctls[i].className;
			newctl.value = ctls[i].options[ctls[i].selectedIndex].text
			newctl.id = 'tempctl'+i
			totaltemp = i
			ctls[i].parentNode.insertBefore(newctl,ctls[i].nextSibling)
		}
	}
}

function show_select() {

	var ctl
	// redisplay all the hidden select boxes
	var ctls = document.getElementsByTagName("select")
	for (var i=0;i<ctls.length;i++){
		ctls[i].style.display='inline'
	}
	// remove tempory ctls

	for (var i=0;i<=totaltemp;i++){

		ctl = document.getElementById('tempctl'+i)
		if (ctl){
			ctl.parentNode.removeChild(ctl);
		}
	}

}

function list_update(str_list_name, str_ctl_id, lng_parent_id) {

	var str_url = 'list_update.asp?list_name='+str_list_name+'&ctl_id='+str_ctl_id
	if (lng_parent_id != undefined) {
		str_url += '&parent_id='+lng_parent_id
	}
	AjaxRequest.get(
		{

		 'url': str_url
		,'onSuccess':function(req){
									var xml_doc = req.responseXML
									// create a node list of all options
									var nl_option = xml_doc.getElementsByTagName("option")
									// clear the current options
									$(str_ctl_id).options.length = 0;
									// re-populate the options
									for (var i=0; i<nl_option.length; i++){
										$(str_ctl_id).options[i] = new Option(nl_option[i].childNodes[0].nodeValue,nl_option[i].getAttribute("value"));
									}
								   }
		,'onError':function(req){alert('error retriving list update ' + str_list_name)}
		}
	);

}

function insertAtCursor(myField, myValue) {

	if (myField) {
		//IE support
		if (document.selection) {
			myField.focus();
			var sel = document.selection.createRange();
			sel.text = myValue;
		}
		//MOZILLA/NETSCAPE support
		else if (myField.selectionStart || myField.selectionStart == '0') {
				var startPos = myField.selectionStart;
				var endPos = myField.selectionEnd;
				myField.value = myField.value.substring(0, startPos)
				+ myValue
				+ myField.value.substring(endPos, myField.value.length);
			} else {
				myField.value += myValue;
		}
	}
}



function popup_wait	(lng_width, lng_height, str_title, str_message) {

		lng_popup_count++;
		disable_all_controls ()


		hide_select()
	

		var ctl_body = document.getElementsByTagName("body")[0]
		var lng_left = Math.floor( (winW() - lng_width) / 2);
		var lng_top = Math.floor( (winH() - lng_height) / 2);
		var div_outer = Builder.node('div', {id:'div_popup_outer_'+lng_popup_count,
											 className:'popup',
											 style:'height:'+lng_height+'px'+
												   ';width:'+lng_width+'px'+
												   ';left:'+lng_left+'px'+
												   ';top:'+lng_top+'px'+
												   ';display:none'+
												   ';z-index:'+lng_popup_count*100
											 })
		var div_header = Builder.node('div')
		div_outer.appendChild(div_header)


		if (str_title==undefined) {
			str_title=''
		}
		var div_title = Builder.node('div', {id:'div_popup_title'+lng_popup_count}, str_title)
		div_outer.appendChild(div_title)


	
		var div_inner = Builder.node('div',{id:'div_popup_wait_inner',
											className:'popup_wait_inner',
											style:'height:'+(lng_height-40)+'px'
											},str_message)

		div_outer.appendChild(div_inner)




		ctl_body.appendChild(div_outer)
	
		new Effect.Grow('div_popup_outer_'+lng_popup_count,{duration:0.0});
	

}

