//<script>
/***************************************************************
 *	Current Version: 4
 *  Authors: Pete Freitag
 *  		 Matt Finn
 *	File: user.js
 *	Description: Activedit API (AEAPI) User Controlled Functions
 *
****************************************************************/

/*
function aeapi_init(aeObject) {
	//Run at component initialization
	//This may be before the control is completely loaded
	//...
}
*/

function aeapi_onLoad(aeObject) {
	//Run once activedit is loaded

	//ShowBorders: control default: false; activedit default: true
	aeObject.ShowBorders = true;	//Show a 1 pixel border on tables and images with border=0
	
	//ShowDetails: control default; false; activedit default: false
	//Uncomment to enable "Show hidden tags as glyphs" on startup
	//aeObject.ShowDetails = true;
}

function aeapi_onBeforeSave(aeObject) {
	//run just before calling the save / submit function
	//overrides native function, an aeapi_local_onBeforeSave() will override this function
	
	//so the pages look ok in netscape, use attributes instead of style="width:x;etc"
	//for images
	var images=aeObject.contentDocument.images;
	for (var i=0; i < images.length;i++) {
		hs=images[i].getAttribute("STYLE",0).height;
		ws=images[i].getAttribute("STYLE",0).width;
		
		if(hs.length) {
			images[i].removeAttribute("HEIGHT", 0); 			
			images[i].setAttribute("HEIGHT", replaceString("px", "", hs),0); 
			images[i].getAttribute("STYLE",0).removeAttribute("HEIGHT",0);
		}
		if(ws.length) {
			images[i].removeAttribute("WIDTH", 0);	
			images[i].setAttribute("WIDTH", replaceString("px", "", ws),0); 
			images[i].getAttribute("STYLE",0).removeAttribute("WIDTH",0);
		}
	}

	//for tables
	var tables=aeObject.contentDocument.getElementsByTagName("TABLE");
	for (var k=0;k<tables.length;k++) {
		hs=tables[k].getAttribute("STYLE",0).height;
		ws=tables[k].getAttribute("STYLE",0).width;
		
		if(hs.length) {
			tables[k].removeAttribute("HEIGHT", 0); 			
			tables[k].setAttribute("HEIGHT", replaceString("px", "", hs),0); 
			tables[k].getAttribute("STYLE",0).removeAttribute("HEIGHT",0);
		}
		if(ws.length) {
			tables[k].removeAttribute("WIDTH", 0);	
			tables[k].setAttribute("WIDTH", replaceString("px", "", ws),0); 
			tables[k].getAttribute("STYLE",0).removeAttribute("WIDTH",0);
		}
	}
	
	//use the DOM to filter out just what's inside the body
	var content = aeObject.contentDocument.body.innerHTML;
	if(content.length && aeObject.FilterSourceCode) {
		content = aeObject.FilterSourceCode(content);
	}
	//get rid of "the strange character" 
	//note: replaceString() is an activedit specific function
	replaceString("&#65279;", " ", content); 
	//convert to xhtml
	content = (ae_xhtml[1]) ? formatHTML(content) : content;
	//convert style attributes to lower case
	content = content.replace(/style="[^"]+"/gi, function(w) { return w.toLowerCase() })
	return content;
}


/*
function aeapi_onAfterSave(aeObject) {
	//runs after sucessful completion of the save functionality
	//this function will not be called if type=form
	
	//This function will override the CF_Activedit onsave="x" attribute
	//so keep it commented out, unless you intend to replace the current onsave implementation
	
	//...
}
*/

//</script>
