// JavaScript Document
//RESTORE POINT notes_v2_3.js, EdNotes_v3_6.php, Notes_v7_6.php
//RESTORE POINT notes_v2_2.js, EdNotes_v3_3.php, Notes_v7_3.php
//RESTORE POINT notes_v2_1.js, EdNotes_v3_2.php, Notes_v7_2.php
//Integrating menu structure to page structure
//RESTORE POINT notes_v2.js EdNotes_v3.php Notes_v7.php
// RESTORE POINT notes_v1_3.js Ednotes_v2_7.php Notes_v6_5.php

/*********************************************************\

                       BROWSER VERSION

\*************************************************************/

var ua=navigator.userAgent;
var d=document;

function get_IE_version(){
	var offset=ua.indexOf("MSIE ");
	return parseFloat(ua.substring(offset+5, ua.indexOf(";", offset)));	
	}
	
var version=get_IE_version();

/*********************************************************\

                            ADMIN PANEL FNS

\*************************************************************/

function find_edit_fns(){
d.getElementById('admin-panel').style.left="175px"
d.getElementById('admin-panel').style.top=-(d.getElementById('rh-menu').offsetHeight+d.getElementById('admin-panel').offsetHeight)+"px"
}

function init_draggie(userid){
		new Draggable('admin-panel', {handle:'handle', scroll:window, onEnd:function(){
				//report_pos();
				new Ajax.Request("update_edit_pos.php", {  
            method: "post",  
            parameters: { left: document.getElementById('admin-panel').style.left, top:document.getElementById('admin-panel').style.top, uid:userid}  });  
		}
		});
}


/*********************************************************\

                        EXPAND ELEMENT

\*************************************************************/

var exWait=25 // delay between steps (ms)
var exInc=20 // height steps
var exHeight // height of element
var exClock // timer 
var expanded=new Array();
//el - element object

function expand_element(elid, sH, eH){
		if(typeof(expanded[elid])=="undefined") expanded[elid]=false
		el=document.getElementById(elid)
		var heightNum=parseInt(el.style.height)
		if(expanded[elid]){
				heightNum=heightNum-exInc
				if(heightNum<sH) {
					el.style.height=sH+'px'
					clearInterval(exClock)
					expanded[elid]=false
				}else{
					el.style.height=heightNum+'px'
				}
		}else{
				heightNum=heightNum+exInc
				if(heightNum>=eH) {
					el.style.height=eH+'px'
					clearInterval(exClock)
					expanded[elid]=true
				}else{
					el.style.height=heightNum+'px'
				}
		}
}

function start_expand(elid, minHeight, maxHeight){
		exClock=setInterval("expand_element('"+elid+"', "+minHeight+", "+maxHeight+")", exWait);
}

function show_save_page_panel(){
	start_expand('save_page_panel', 30, 420);
	}


/*********************************************************\

COLLECT PAGE SECTION IDS AND MAKE CSV STRING

\*************************************************************/
var sids
	
function collect_section_ids(){
	sids=new Array()
	var container=document.getElementById('content')
	var cls='section_id'
	tags=container.getElementsByTagName('div')
			for (var i=0; i<tags.length; i++) {		
					if (tags[i].className==cls) {
						sids.push(tags[i].innerHTML)
						//alert(tags[i].className)
					}
		}
	}
	
function make_section_ids_string(){
	var id_collector="";
	for(n=0; n<sids.length;n++){
		id_collector+=sids[n]
		id_collector+=(n==(sids.length-1))? '' : ", "
		}
		//alert(id_collector)
		return id_collector
	}
	
/*********************************************************\

       SUBMIT FORM AND FORM FUNCTIONS

\*************************************************************/

function update_docid_report(tdiv, selobjid){
	var d=document
	var selobj=d.getElementById(selobjid)
	d.getElementById('docid_container').style.display='block'
	d.getElementById('docid_report').value=d.getElementById('doc_select').options[selobj.selectedIndex].text
	d.getElementById('docid').value=d.getElementById('doc_select').value
	toggle_choose_doc_form(tdiv);
	}

var isvisible=new Array();

function switch_visibility_by_tag(tag, containerid){
	if(typeof(isvisible[tag])=='undefined') isvisible[tag]=true
		var container=(containerid)? document.getElementById(containerid) : document
		tags=container.getElementsByTagName(tag)
		var newvisibility=(isvisible[tag])? 'hidden' : 'visible'
		for(var i=0; i<tags.length; i++){
					tags[i].style.visibility=newvisibility;
			}			
			isvisible[tag]=(isvisible[tag])? false : true
	}


function hideall_by_class(cls, containerid) {
		var container=(containerid)? document.getElementById(containerid) : document
		tags=container.getElementsByTagName('*')
		for (var i=0; i<tags.length; i++) {		
					if (tags[i].className==cls) tags[i].style.display='none';
		}
} 

function go_selected(selectobj){ //v3.0
  //eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  location.href=selectobj.options[selectobj.selectedIndex].value
  //if (restore) selObj.selectedIndex=0;
}

function confirm_delete_image(){
	if(confirm("Are you sure you want to delete this image")){
			   document.getElementById('').value=null
			   document.getElementById('').innerHTML="The image will be deleted when you save the section"
			   document.getElementById('').style.color="red"
			   }
	}
	
function confirm_delete_document(docname){
	if(confirm("Are you sure you want to delete "+docname+"?")){
			   document.getElementById('docid').value=null
			   document.getElementById('doc_delete_msg').innerHTML="The document will be deleted from this section when you save the section; if you also want to delete it from the archive, go to documents and do it there."
			   document.getElementById('doc_delete_msg').style.color="red"
			   }
	}	
//imsize is passed from the class to js in the inhead include	
function change_image_size(imgidx){
				imsrc=document.getElementById('screen'+imgidx).src
				imfile=imsrc.substring(imsrc.lastIndexOf('/')+1)
				obsi=document.getElementById('small_image_check')
				obsi.checked=(obsi.checked)? false : true
				impath=(obsi.checked)? 'assets/image/articles/'+imsize[0]+'/' : 'assets/image/articles/'+imsize[1]+'/'
				document.getElementById('small_screen'+imgidx).style.width=(obsi.checked)? (imsize[0]+4)+'px' : (imsize[1]+4)+'px'
				document.getElementById('screen'+imgidx).src=impath+imfile
				//changing the source may well fail if more than one form with an image is open cos the img (id=screen) is not indexed
	}

function confirm_ajax_delete(vic, vicid){
	if(confirm("Are you sure you want to delete "+vic+"? This can't be undone!")){
			ajax_delete(vicid)   
		}
	}
	
function confirm_delete(vic, vicID, tgt){
	if(confirm("Are you sure you want to delete "+vic+"? This can't be undone!")){
			location.href=tgt+"?pass="+vicID
		}
	}

function remake_page(t){
document.getElementById('tacontent').style.width="222px";
document.getElementById('article_image_container').style.display="block";
document.getElementById('article_image').src=t.value;
document.getElementById('img').src=t.value;
}

//this should be written using doc.write and the two fns should be one
function toggle_image_form(panel){
	switch_visibility_by_tag('select', 'select_inputs');
	obj=document.getElementById(panel).style
	goforupload=(obj.display=="none")? true : false
	//document.getElementById('misc_inputs').style.visibility=	(goforupload)? 'hidden' : 'visible';	while I work out the IE z-index bug	
	document.getElementById('upload_image_flag').value=(goforupload)? 'upload' : ""
	//alert(document.getElementById('upload_image_flag').value)
	obj.display=(goforupload)? 'block' : 'none'
}

function toggle_document_form(panel){
	switch_visibility_by_tag('select', 'select_inputs');
	obj=document.getElementById(panel).style
	//document.getElementById('misc_inputs').style.visibility=	(obj.display=="block")? 'visible' : 'hidden';	// while I work out the IE z-index bug this can be put back when you remove the form indexing
	document.getElementById('upload_doc_flag').value=(obj.display=="block")? '' : 'upload'
	obj.display=(obj.display=="block")? 'none' : 'block';		
}

function toggle_choose_doc_form(panel){
	switch_visibility_by_tag('select', 'select_inputs');
	obj=document.getElementById(panel).style
	//document.getElementById('misc_inputs').style.visibility=	(obj.display=="block")? 'visible' : 'hidden';	// while I work out the IE z-index bug this can be put back when you remove the form indexing
	document.getElementById('choose_doc_flag').value=(obj.display=="block")? '' : 'choose'
	obj.display=(obj.display=="block")? 'none' : 'block';		
	
	}
	
/*function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
	
MM_preloadImages('');*/
	
function clear_field(v, defString){
	if(v.value==defString) v.value="";
	}
	
	
function paste_selected_item(fieldid, selected_value){
	document.getElementById(fieldid).value=selected_value
	}

function paste_selected(fieldid, selectobject){
	document.getElementById(fieldid).value=selectobject.options[selectobject.selectedIndex].value
	}

function handle_add_submit(){
	collect_section_ids()
	collector=make_section_ids_string()
	document.getElementById('section_ids').value=collector
	//alert(document.getElementById('section_ids').value)
	if(document.getElementById('page_label').value=='') document.getElementById('page_label').value='Unlabelled'
	if(document.getElementById('page_title').value=='') document.getElementById('page_title').value='Untitled'
	document.forms['notes_form'].submit();
	//document.forms['save_page_form'].submit();
	}
	
function handle_recycled_submit(){
	document.getElementById('recycled_submit_flag').value='true'
	handle_add_submit();
	}	

/**********************************************************\

			AJAX Functions

\*********************************************************/
/*********************************************************\

     		MAKE ADD FORM BLOCK

\*************************************************************/	

function removePanel(){// redundant
	el=document.getElementById('save_page_panel')
	document.getElementById('new_block_wrapper_0').removeChild(el)
	}

function addDiv(){
	newdiv=document.createElement('div')
	newdiv.id='my_new_div'
	newdiv.innerHTML="<p>Hello World</p>"
	document.getElementById('content').appendChild(newdiv);
	
	}
	
var block_number=0
function ajax_new_block(){
	d=document;
	//if(document.getElementById('sectype_select').value=='funders panel'){ajax_save();return}
			showhide_by_class('edit_links', 'content', true)
	var choose=(d.getElementById('choose_flag'))? true : false;
	var type=(d.getElementById('sectype_select'))? d.getElementById('sectype_select').value : type_of_page;
	
			if(document.getElementById('add_new_block_button')) document.getElementById('add_new_block_button').value="loading..."

		  var xmlHttp=make_ajax_object();	
			
			//var bidx=(block_number==0)? '' : block_number
			if(type_of_page=='page block' && !choose){
				var pageidbit=(page_id!="undefined")? "&pageid="+page_id : "&pageid="+document.getElementById('page_id').value
			}else{
				var pageidbit=''
				}
			
			//page_id is set in the inhead include where get[pageid] or post[pageid] are set but held in a hidden form control on page_add
			/*page_id and page_self are set in the inhead.php include where GET pageid is set*/
			if(!choose){
			collect_section_ids()
			var page_order=make_section_ids_string()
			}else page_order=''
		
			//alert('block number='+block_number+'page order='+page_order)
			
			var url = "page_block_add_script.php";
			var params = "block_number="+block_number+"&from_ajax=true&caller="+page_self+pageidbit+"&section_ids="+page_order+"&TYPE="+document.getElementById('sectype_select').value;//was type_of_page
			
			//alert('params='+params+' and url='+url)
			xmlHttp.open("POST", url, true);
			
			//Send the proper header information along with the request
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			
			xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
				if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
					//if(type=='page block' || choose){ mmmm...
					if(	document.getElementById('save_page_panel')){
					var togo=document.getElementById('save_page_panel')
					var parent=togo.parentNode
						parent.removeChild(togo)
					}

					document.getElementById('content').innerHTML+=xmlHttp.responseText;
					if(!choose) block_number++
					sids=new Array()
					//document.getElementById('add_new_block_button').value="new section"
				}
			}
			xmlHttp.send(params);
	
  } // end ajax_new_block
  
  /*********************************************************\

               MAKE EDIT FORM BLOCK

\*************************************************************/

function ajax_make_edit(blockidx, sectype){
	
			showhide_by_class('edit_links', 'content') // this fn might allow you to force off

		  var xmlHttp=make_ajax_object();	

			var pageidbit=(typeof(page_id)!="undefined")? "&pageid="+page_id : ''
			//var self = location.href.substring(0,location.href.lastIndexOf('/') + 1)
			
			var script_url = "page_block_make_edit_script.php"
			
			/*page_self and type_of_page are set in the inhead.php include, page_id is set where POST or GET pageid is set*/
			
			var params = "pass="+blockidx+"&from_ajax=true"+"&caller="+page_self+pageidbit+"&type_of_page="+type_of_page+"&sectype="+sectype
/*			alert(params)*/
			xmlHttp.open("POST", script_url, true);
			
			//Send the proper header information along with the request
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			
			xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
				if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
					document.getElementById('page_section_'+blockidx).innerHTML=xmlHttp.responseText;
					if(document.getElementById('save_page_panel')){
											var togo=document.getElementById('save_page_panel')
											var parent=togo.parentNode
											parent.removeChild(togo)
						}//if save_page_panel
					//document.getElementById('save_note_button').value="save article"
				}
			}// readystatechange
			xmlHttp.send(params);
	
  } // end ajax_make_edit  form block

  
/*********************************************************\

                   SAVE PAGE - ALSO UPDATE PAGE

\*************************************************************/
function ajax_save_page(type){
			var upd=(type=='edit')? true : false
			//document.getElementById('save_page_button').value="Saving..."
			var title=escape(document.getElementById('page_title').value, 1)
			var label=escape(document.getElementById('page_label').value, 1)
			if(upd) var page_id=document.getElementById('page_id').value
			var notes_edit=(upd)? 'update_page' : 'insert_page'
			collect_section_ids()
			var collector=make_section_ids_string()
			var level=document.getElementById('level').value
			var L1parent=escape(document.getElementById('l1parent').value, 1)
			var L2parent=escape(document.getElementById('l2parent').value, 1)
			var L3parent=escape(document.getElementById('l3parent').value, 1)
			var clearance=document.getElementById('clearance').value
			var page_url=escape(document.getElementById('page_url').value, 1)
			
		  var xmlHttp=make_ajax_object();	
		  
			var script_url = (upd)? "page_update_script.php" : "page_save_script.php";
			var params = "page_title="+title+"&LABEL="+label+"&notes_edit="+notes_edit+"&section_ids="+collector+"&from_ajax=true"+"&LEVEL="+level+"&L1PARENT="+L1parent+"&L2PARENT="+L2parent+"&L3PARENT="+L3parent+"&CLEARANCE="+clearance+"&URL="+page_url;
		//alert(params)
		if(upd) params+="&pageid="+page_id
		
			xmlHttp.open("POST", script_url, true);
			
			//Send the proper header information along with the request
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			
			xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
				if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
					//document.getElementById('save_page_panel').innerHTML+=xmlHttp.responseText;
					var new_el = document.createElement('div');
					new_el.id="page_save_report"
					document.getElementById('save_page_panel').appendChild(new_el)
					document.getElementById('page_save_report').innerHTML="<p>"+xmlHttp.responseText
					sids=new Array()// clear out the previous values
					document.getElementById('save_page_button').value="Edit page details"
				}
			}
			xmlHttp.send(params);
	}// ajax_save_page
	
/*********************************************************\

                 SAVE PAGE BLOCK

\*************************************************************/

function ajax_save(blockidx){
			document.getElementById('save_note_button').value="Saving..."
			var title=escape(document.getElementById('title').value, 1)
			var content=escape(document.getElementById('tacontent').value, 1)
			var type=document.getElementById('type').value // this goes through a form field, but I think it's just type_of_page (set in inhead) don't think it is any more since we went to indiv block types
			var page_block_insert='notes_form'
			var from_ajax='&from_ajax=true'
			var firstblock=document.getElementById('firstblock').value
			var top_chrome=(document.getElementById('top_chrome').checked)? '1' : '0'
			var bottom_chrome=(document.getElementById('bottom_chrome').checked)? '1' : '0'
			
			if(page_id=='undefined' && firstblock=='false'){
				page_id=document.getElementById('page_id').value
				section_ids=document.getElementById('section_ids').value
				} else {
					if(firstblock=='false'){
					section_ids=document.getElementById('section_ids').value
						}else section_ids='not set in ajax save'
				}//page_id undefined
			var page_title=escape(document.getElementById('page_title').value, 1)
			var page_label=escape(document.getElementById('page_label').value, 1)
			
		  var xmlHttp=make_ajax_object();			

// pageid is set in the calling doc head by js from php
			var script_url = "page_block_save_script.php";
			var params = "block_number="+blockidx+"&TITLE="+title+"&CONTENT="+content+"&TYPE="+type+"&page_block_insert="+page_block_insert+from_ajax+"&firstblock="+firstblock+"&pageid="+page_id+"&section_ids="+section_ids+"&page_title="+page_title+"&LABEL="+page_label+"&top_chrome="+top_chrome+"&bottom_chrome="+bottom_chrome
			
			if(type=='news' || type=='page block' || type=='funders panel' || type=='map panel' || type=='unordered list'){// used to be type of page
					var subtitle=escape(document.getElementById('subtitle').value, 1)		
					var content2=escape(document.getElementById('tacontent2').value, 1)
					
					params+="&SUBTITLE="+subtitle+"&CONTENT2="+content2
			}//type of page new pb
			
			if(type=='news' || type=='page block'){
				var content2type=document.getElementById('content2type').value
				params+="&CONTENT2TYPE="+content2type
			}
			
			if((type!='jargon') && (type!='map panel') && (type!='funders panel') && (type!='unordered list')){// WATCH OUT FOR THESE
					var url=escape(document.getElementById('form_url').value, 1)
					params+="&URL="+url
			}
			
			if(type_of_page=='news' || type_of_page=='links'){
						if(typeof(document.getElementById('subtype'))!='undefined'){
							var subtype=document.getElementById('subtype').value // eecda and hwn may not have a news subtype - set it anyway for future
						}else var subtype='none'
				
					params+="&SUBTYPE="+subtype
			}
/*			if(type_of_page=='news') {
					var killdate=document.getElementById('killdate').value
					var audience='selected audience' // wecdc doesn't filter it's news audience, set it anyway for future				
					params+="&KILLDATE="+killdate+"&AUDIENCE="+audience;
			}*/
		//alert(params)
			xmlHttp.open("POST", script_url, true);
			
			//Send the proper header information along with the request
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			
			xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
				if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
						if(firstblock=='true') {
								location.href=site+'_page.php?pageid='+xmlHttp.responseText;
								//document.getElementById('content').innerHTML+=xmlHttp.responseText;
						}else{
								var formdiv=document.getElementById('form_wrapper')
								document.getElementById('content').removeChild(formdiv)
								if(type_of_page=='links'){
									document.getElementById('content').innerHTML+=xmlHttp.responseText;
								}else if(firstblock=='true' && type_of_page=='page block') {
										document.getElementById('content').innerHTML=xmlHttp.responseText;
								}else{
										document.getElementById('content').innerHTML+=xmlHttp.responseText;
								}
						}// if firstblock else
				}// if xmlhttp readyState
				showhide_by_class('edit_links', 'content', true)
			}// xmlhttp statechange function
			xmlHttp.send(params);
	
  } // end ajax_save_block
 
 /*********************************************************\

                     SAVE EDITED BLOCK

\*************************************************************/
 function ajax_edit(blockidx){
			document.getElementById('save_note_button').value="Saving..."
			var title=escape(document.getElementById('title').value, 1)
			var content=escape(document.getElementById('tacontent').value, 1)
			var type=document.getElementById('type').value // type_of_page as in news, page block NOT Add, List set in inhead.php This might transfer as type_of_page now it's the type of block, which is not the type of page
			//alert(type);
			var update_note='update page block'
			var pass_id=document.getElementById('pass_id').value
			var top_chrome=(document.getElementById('top_chrome').checked)? '1' : '0'
			var bottom_chrome=(document.getElementById('bottom_chrome').checked)? '1' : '0'			
			 if((type!='jargon') && (type!='map panel') && (type!='funders panel') && (type!='unordered list')){
												var url=escape(document.getElementById('form_url').value, 1)
								}
			
		  var xmlHttp=make_ajax_object();

			var script_url = "pb_edit_save_script.php";
// page_id is set in inhead.php include
var params = "block_number="+blockidx+"&TITLE="+title+"&CONTENT="+content+"&TYPE="+type+"&update_note="+update_note+"&ID="+pass_id+"&page_action="+page_action+"&from_ajax=true"+"&pageid="+page_id+"&top_chrome="+top_chrome+"&bottom_chrome="+bottom_chrome;

			if(type=='news' || type=='page block' || type=='funders panel' || type=='map panel' || type=='unordered list'){//was type_of_page
					var subtitle=escape(document.getElementById('subtitle').value, 1)			
					var content2=escape(document.getElementById('tacontent2').value, 1)
					params+="&SUBTITLE="+subtitle+"&CONTENT2="+content2
			}
			
			if(type=='news' || type=='page block'){
					var content2type=document.getElementById('content2type').value
					var imgid=document.getElementById('imgid').value
					var docid=document.getElementById('docid').value
					var small_image=(document.getElementById('small_image_check').checked)? '1' : ''
					params+="&CONTENT2TYPE="+content2type+"&IMGID="+imgid+"&DOCID="+docid+"&small_image_check="+small_image					
			}
			
			if((type!='jargon') && (type!='map panel') && (type!='funders panel') && (type!='unordered list')){
						params+="&URL="+url
				}

			if(type=='news' || type=='links'){
						if(document.getElementById('subtype')){
							//alert(document.getElementById('subtype'))
							var subtype=document.getElementById('subtype').value // eecda and hwn may not have a news subtype - set it anyway for future
						}else var subtype='none'
					params+="&SUBTYPE="+subtype
			}
/*			if(type_of_page=='news') {
					var killdate=document.getElementById('killdate').value
					var audience='selected audience'				
					params+="&KILLDATE="+killdate+"&AUDIENCE="+audience;
			}	*/		
			
			//alert(params)
			xmlHttp.open("POST", script_url, true);
			
			//Send the proper header information along with the request
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			
			xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
				if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
					document.getElementById('page_section_'+blockidx).innerHTML=xmlHttp.responseText;
					init_sort()
					if(!document.getElementById('save_page_panel')){
								ajax_make_update_page_section(page_id)
						}//make save panel div
						
				}//readystate
				showhide_by_class('edit_links', 'content', true)				
			}//ready state change function
			xmlHttp.send(params);
	
  } // end ajax_edit_block (save edited block)
  
  function make_ajax_object(){
	  		  var xmlHttp;
		  try{
					// Firefox, Opera 8.0+, Safari
					xmlHttp=new XMLHttpRequest();
			}
		  catch (e){
			// Internet Explorer
			try{
					  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			  }
			catch (e){
			  try{
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			  catch (e){
						alert("Your browser does not support AJAX!");
						return false;
				}
			  }
			}// catches closing
			return xmlHttp;
	  }
  
  function ajax_make_update_page_section($pgid){
	  
			  var xmlHttp=make_ajax_object();
/*		  try{
					// Firefox, Opera 8.0+, Safari
					xmlHttp=new XMLHttpRequest();
			}
		  catch (e){
			// Internet Explorer
			try{
					  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			  }
			catch (e){
			  try{
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			  catch (e){
						alert("Your browser does not support AJAX!");
						return false;
				}
			  }
			}// catches closing*/
			var params="pageid="+page_id
			var script_url='make_update_page_section_script.php'
			
			xmlHttp.open("POST", script_url, true);
			
			//Send the proper header information along with the request
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			
			xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
				if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
					document.getElementById('content').innerHTML+=xmlHttp.responseText
				}//if ready
			}//onreadystatechange function
			xmlHttp.send(params)
	  }
  
  function ajax_save_recycled(){
	  			var d=document
				var firstblock=(d.getElementById('firstblock').value=='true')? true : false
				var update_page_recycled=(firstblock)? false : true
				var rec_page_label=d.getElementById('recycle_select').options[d.getElementById('recycle_select').selectedIndex].text
	  			document.getElementById('recycle_section_button').value='saving recycled...'
				document.getElementById('recycle_section_button').style.backgroundColor='green'
				d.getElementById('recycled_submit_flag').value=(firstblock)? 'true' : 'false'// not sure where this is used as update page recycled takes care of the flag here. maybe in post by submit used in insert_page()
				
				var recycle_id=document.getElementById('recycle_select').value
	  			var section_ids=document.getElementById('section_ids').value+", "+recycle_id				
				var page_id=document.getElementById('page_id').value
				
	  		  var xmlHttp;
		  try{
					// Firefox, Opera 8.0+, Safari
					xmlHttp=new XMLHttpRequest();
			}
		  catch (e){
			// Internet Explorer
			try{
					  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			  }
			catch (e){
			  try{
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			  catch (e){
						alert("Your browser does not support AJAX!");
						return false;
				}
			  }
			}// catches closing
			//there is much redundancy here
			var params="recycle_id="+recycle_id+"&pageid="+page_id+"&section_ids="+section_ids+"&update_page_recycled="+update_page_recycled+"&recycled_submit_flag="+d.getElementById('recycled_submit_flag').value+"&firstblock="+d.getElementById('firstblock').value+"&LABEL="+rec_page_label+"&page_title=undefined"+"&recycle_select="+recycle_id+"&from_ajax=true"


			var script_url = (firstblock)? 'page_add_from_recycled_script.php' : "page_update_recycled_script.php";
						
			xmlHttp.open("POST", script_url, true);
			
			//Send the proper header information along with the request
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			
			xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
				if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
							//if(type_of_page=='page block'){ again trepidacious about what this is for
							
							if(firstblock) {
								location.href='eecda_page.php?pageid='+xmlHttp.responseText;
							}else{
									if(document.getElementById('save_page_panel')){
											var togo=document.getElementById('save_page_panel')
											var parent=togo.parentNode
												parent.removeChild(togo)
									}// type of page : id d.savepage panel
									if(document.getElementById('form_wrapper')){
											var togo=document.getElementById('form_wrapper')
											var parent=togo.parentNode
												parent.removeChild(togo)
									}// type of page : id d.savepage panel
									document.getElementById('content').innerHTML+=xmlHttp.responseText;
							}// if firstblock
				}
			}		
			xmlHttp.send(params);
	  }

/*********************************************************\

      DELETE BLOCK OR DELETE FROM PAGE

\*************************************************************/

 function ajax_delete(blockidx){
			
// Full list added in here 080806
			if(page_action=='Page'){ //No idea what this condition was for - it's to delete from page only
					var victim=document.getElementById('page_section_'+blockidx)
						document.getElementById('content').removeChild(victim)
						ajax_save_page('edit')	
						show_save_page_panel();
				}else{	
				
		  var xmlHttp;
		  try{
					// Firefox, Opera 8.0+, Safari
					xmlHttp=new XMLHttpRequest();
			}
		  catch (e){
			// Internet Explorer
			try{
					  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			  }
			catch (e){
			  try{
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			  catch (e){
						alert("Your browser does not support AJAX!");
						return false;
				}
			  }
			}// catches closing				
			var script_url = "page_block_delete_script.php?pass="+blockidx+"&from_ajax=true";

			xmlHttp.open("GET", script_url, true);
			
			xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
				if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
					document.getElementById('page_section_'+blockidx).innerHTML=xmlHttp.responseText;
/*					sobj=document.getElementById('save_page_button')
					sobj.style.backgroundColor="red"
					sobj.style.color="white"
					sobj.style.fontWeight="bold"*/
					if(page_action=='Page' || page_action=='List') ajax_save_page('edit')
					//document.getElementById('save_note_button').value="save article"
				}
			}
			xmlHttp.send(null);
		} // delete from page only condition
  } // end ajax_delete


/*********************************************************\

          SHOW HIDDEN ELEMENT FUNCTIONS

\*************************************************************/
//var top_chrome=true
var bottom_chrome=true
function toggle_top_chrome(){
	document.getElementById('notes_form').style.backgroundImage=(document.getElementById('notes_form').style.backgroundImage=='none')? ' url(assets/image/layout/widgets/comma-open.gif)' : 'none';
	//top_chrome=(top_chrome)? false : true	
	}
	
// this needs more work - getComputedStyle is probably involved
function toggle_bottom_chrome(){
	var sobj=document.getElementById('content-text-lower-form').style
	sobj.backgroundImage=(sobj.backgroundImage=='none')? 'url(assets/image/layout/widgets/comma-close.gif)' : 'none';
	//alert('bBottom='+sobj.borderBottom+'bColor='+sobj.borderColor+'bWidth='+sobj.borderWidth)
	
	sobj.borderBottom=(sobj.borderBottom=='solid')? 'none' : 'solid';
	sobj.borderColor=(sobj.borderColor=='#e7d3e2')? 'none' : '#e7d3e2';
	sobj.borderWidth=(sobj.borderWidth=='none')? '2px' : 'none;';
	//bottom_chrome=(bottom_chrome)? false : true	
	}
	
function show_inline_div_by_select_trigger(id, selected, trigger){
			document.getElementById(id).style.display=(selected==trigger)? 'inline' : 'none'
	}
	
var div_showing=new Array();

function show_hidden_div(div_num){
		if(typeof(div_showing[div_num])=="undefined") div_showing[div_num]=false
		document.getElementById('hidden_'+div_num).style.display=(div_showing[div_num])? "none" : "block"
		if(type_of_page!='links' && type_of_page!='jargon') document.getElementById('continue_'+div_num).innerHTML=(div_showing[div_num])? "More about this >>" : "Put this away"
		div_showing[div_num]=!div_showing[div_num]
}

var edit_on=false
function showhide_by_class(cls, containerid, switch_edit_on) {
	if(typeof(switch_edit_on)!='undefined') edit_on=switch_edit_on
		var container=(containerid)? document.getElementById(containerid) : document
		tags=container.getElementsByTagName('*')
		attr=(edit_on)? 'none' : 'inline'
		for (var i=0; i<tags.length; i++) {		
					if (tags[i].className==cls) tags[i].style.display=attr;
		}
		edit_on=!edit_on
} 

function replace_edit_fns(y){
	//topOffset isthe height of the menu+header and should be calculated - the panel posn is relative to accomodate the drag function
	var topOffset=825
var panelTopHome=(typeof(y)=="undefined")? "-30px" : (y-topOffset)+"px"
document.getElementById('edit_fns').style.left='0px'
document.getElementById('edit_fns').style.top=panelTopHome
}
	
/**********************************************************\

	OTHER FUNCTIONS

\*********************************************************/

function cat_fns(v){
		if(v=="new category"){
			document.getElementById('cat').style.display="inline";
		}
		if(v=="edit categories"){
			location.href="category.php";
		}
}

function do_search(fm_id){
	document.getElementById(fm_id).submit();	
	}
	


/*************************************************\

		FADE FUNCTION
		Together with the divs and maps below 
		this makes a neat little fader upper
		cut down from wecdc_fade.js

\*************************************************/

var div_opacity=0;
var div_timer=0;
var prog_timer=0
var ov_id='' // the overlay panel id
version=get_IE_version();

/**********************************************************\
	Some of these variables are just in to allow options for less dependency
	pnl :: the large panel to be faded up // hardwired as big_screen in Notes
	endOp :: final opacity
	imgId :: id of the big image in the big screen which are created in Notes - pnl+_image ie big_screen_image
	ov_id :: id of the overlay panel which is created in Notes - pnl+_overlay ie big_screen_overlay
	anchorObject :: big_screen moves to 100px up from the offsetTop of this object. The left side and lines up with it's offsetParent.offsetLeft to cover it usually being right floated. It's hardwired to small_screen which is created in Notes
	
	This is very cut down from wecdc_fade/slideshow as it doen't need a progress bar because the big image loads with the page, but let's just keep an eye on that - it's going to have to come back for news listings if we want thumbs to fade up to big - better to use a different function
	

\*********************************************************/

function fadeStart(pnl, endOp, anchorObjectID, imgId, imgSrc){
	if(typeof(anchorObjectID)!='undefined') anchorObject=document.getElementById(anchorObjectID)
	var args=fadeStart.arguments
	var ov_id=pnl+"_overlay" //put overlay back?
			if(typeof(imgSrc)!='undefined'){
					document.getElementById(args[3]).src=args[4];
			}

		document.getElementById(pnl).style.opacity=0
		document.getElementById(pnl).style.display="block";

		if(version>=5.5){
			make_overlay(pnl, "Loading...");
				if(typeof(anchorObject)!='undefined'){
							document.getElementById(pnl).style.top=anchorObject.offsetTop+20+"px";
							document.getElementById(pnl).style.left=anchorObject.offsetParent.offsetLeft+"px";
							document.getElementById(ov_id).style.top=anchorObject.offsetTop-100+"px";
							}
				endOp=endOp*100
				document.getElementById(pnl).style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+0+")"
				//document.getElementById(pnl+"_image").onload=function(){
																					document.getElementById(ov_id).style.display="none"		
																					div_timer=setInterval("fade_up_ie('"+pnl+"',"+endOp+")", 30);
																					//clearInterval(prog_timer)
																			//}
		}else{
						make_overlay(pnl, "Loading...");
						if(typeof(anchorObject)!='undefined'){
							document.getElementById(pnl).style.top=anchorObject.offsetTop-11+"px";
							document.getElementById(pnl).style.left=anchorObject.offsetParent.offsetLeft-100+"px";
							document.getElementById(ov_id).style.top=anchorObject.offsetTop+40+"px";
						}
					//document.getElementById(pnl+"_image").onload=function(){ NOTE : this appears to be unnecessary as the image loads with the page but I'm keeping it in in case we start to get errors from incomplete img loads
							document.getElementById(ov_id).style.display="none"
							div_timer=setInterval("fade_up_ff('"+pnl+"',"+endOp+")", 30);
							//clearInterval(prog_timer)
					//}
		}
}

function fadeEnd(d){
		var args=fadeEnd.arguments
		
		//branch hide map masks
				if(version>=5.5){
						document.getElementById(d).style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+0+")"
		}else{
				if(version>=0){
						//ie lt 5.5 not supported yet
				}else{
						document.getElementById(d).style.opacity=0;
				}
		}
		//reset mid fade masks
		div_opacity=0;
		if(div_timer) clearInterval(div_timer)
		if(args.length>1 && args[1]==0) document.getElementById(d).style.display="none";
}

function fade_up_ie(d, endOp){
		if(!div_opacity) div_opacity=0;
		div_opacity+=10;
document.getElementById(d).style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+div_opacity+")"

		if(div_opacity>=endOp){
			clearInterval(div_timer)
			document.getElementById(ov_id).style.display='none'
			div_opacity=0;
		}
}

function fade_up_ff(d, endOp){
	//alert("fade_up_ff called")
		if(!div_opacity) div_opacity=0;
		div_opacity+=0.1;
		document.getElementById(d).style.opacity=div_opacity;
		if(div_opacity>=endOp){
			clearInterval(div_timer)
			div_opacity=0;
		}
}

function make_overlay(pnl_id, msg){
			ov_id=pnl_id+"_overlay"
			document.getElementById(pnl_id+"_image").onload=function(){
				clearInterval(prog_timer)
				document.getElementById(ov_id).style.display="none"
				}
			obj=document.getElementById(ov_id)
			obj.innerHTML="<p id=\"overlay_text\" style=\"margin:0px;\">"+msg+"</p>";
			obj.style.display="block";
			//prog_timer=setInterval("progress_bar('overlay_text')", 500)
	}

function progress_bar(ov){
	document.getElementById(ov).innerHTML+='.'
	}
/**********************************************************\

	This replaces fadestart so that the big_screen div can go outside the positioning context of the rest of the page and step outside of it's z-index stacking order, specifically to deal with f***ing moneysoft	

\*********************************************************/
function fadeStart_solo(pnl, endOp, anchorObjectID, imgId, imgSrc){
	if(typeof(anchorObjectID)!='undefined') anchorObject=document.getElementById(anchorObjectID)
	var args=fadeStart_solo.arguments
	var ov_id=pnl+"_overlay" //put overlay back?
			if(typeof(imgSrc)!='undefined'){
					document.getElementById(args[3]).src=args[4];
			}

		document.getElementById(pnl).style.opacity=0
		document.getElementById(pnl).style.display="block";

		if(version>=5.5){
			make_overlay(pnl, "Loading...");
				if(typeof(anchorObject)!='undefined'){
							document.getElementById(pnl).style.top=anchorObject.offsetTop+"px";
							document.getElementById(pnl).style.left=anchorObject.offsetParent.offsetLeft+150+"px";
							document.getElementById(ov_id).style.top=anchorObject.offsetTop+"px";
							}
				endOp=endOp*100
				document.getElementById(pnl).style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+0+")"
				//document.getElementById(pnl+"_image").onload=function(){
																					document.getElementById(ov_id).style.display="none"		
																					div_timer=setInterval("fade_up_ie('"+pnl+"',"+endOp+")", 30);
																					//clearInterval(prog_timer)
																			//}
		}else{
						make_overlay(pnl, "Loading...");
						if(typeof(anchorObject)!='undefined'){
							document.getElementById(pnl).style.top=anchorObject.offsetTop+108+"px";
							document.getElementById(pnl).style.left=anchorObject.offsetParent.offsetLeft+160+"px";
							document.getElementById(ov_id).style.top=anchorObject.offsetTop+40+"px";
						}
					//document.getElementById(pnl+"_image").onload=function(){ NOTE : this appears to be unnecessary as the image loads with the page but I'm keeping it in in case we start to get errors from incomplete img loads
							document.getElementById(ov_id).style.display="none"
							div_timer=setInterval("fade_up_ff('"+pnl+"',"+endOp+")", 30);
							//clearInterval(prog_timer)
					//}
		}
}