

function nodeChangeName(node) {

	var changedNode=node.nodeName.toLowerCase();
	
	switch (changedNode) {
		case 'div':
			changedNode='p';
			break;
		case 'b':
			changedNode='strong';
			break;
		case 'i':
			changedNode='em';
			break;
		case 'u':
			changedNode='span';
			break;
		case 'strike':
			changedNode='span';
			break;
		case 'img':
			if (node.className=='anchor') changedNode='a';
			if (node.className=='baner') changedNode='script';
			break;
		default:
			break;
	}

	return changedNode;
}

function getDefaultAttr(node) {
	var attrs=node.attributes;
	var exattr=new String();
	
	for(var i=0;attrs && i<attrs.length;i++) {
		if (attrs[i].name.toLowerCase().indexOf('_moz_dirty')==-1 && attrs[i].value!='' && attrs[i].value!='null' && attrs[i].value!='0' &&
		attrs[i].name.toLowerCase().indexOf('hidefocus')==-1 && attrs[i].name.toLowerCase().indexOf('contenteditable')==-1 &&
		attrs[i].name.toLowerCase().indexOf('disabled')==-1 &&  attrs[i].name.toLowerCase().indexOf('shape')==-1) exattr+=' '+attrs[i].name.toLowerCase()+'="'+attrs[i].value+'"';
	}
	return exattr;
}

function getPAttr(node) {
	var attrs=node.attributes;
	var exattr=new String(), styleattr=new String();
	
	for (var i=0;attrs && i<attrs.length;i++) {
		switch (attrs[i].name.toLowerCase()) {
			case 'align':
				styleattr+='text-align:'+attrs[i].value.toLowerCase();
				break;
/*			default:
				if (attrs[i].name.toLowerCase().indexOf('_moz_dirty')==-1 || attrs[i].value!='' || attrs[i].value!='null' || attrs[i].value!='0') exattr+=' '+attrs[i].name.toLowerCase()+'="'+attrs[i].value+'"';
				break;*/
		}
	}
	
	if (styleattr!='') exattr=' style="'+styleattr+'"';
	return exattr;
}

function getAAttr(node) {
	var attrs=node.attributes;
	var exattr=new String();
	
	if (node.className=='anchor') {
		for(var i=0;i<attrs.length;i++) {
			if(attrs[i].name.toLowerCase()=='alt' || attrs[i].name.toLowerCase()=='title') 				
				break;
		}
		exattr=' name="'+attrs[i].value+'"';
	} else
		exattr=getDefaultAttr(node);
	return exattr;
}

function getScriptAttr(node) {
	var attrs=node.attributes;
	var exattr=new String();
	
	if (node.className=='baner') {
		for(var i=0;i<attrs.length;i++) {
			if(attrs[i].name.toLowerCase()=='alt' || attrs[i].name.toLowerCase()=='title') 				
				break;
		}
		exattr='showBaner('+attrs[i].value+');';
	}
	return exattr;
}

function getIMGAttr(node) {
	var attrs=node.attributes;
	var exattr=new String();
	var accattribs='src alt align hspace vspace border width height';

	for(var i=0;i<attrs.length;i++) {
		if (accattribs.indexOf(attrs[i].name.toLowerCase())!=-1) {
			exattr+=' '+attrs[i].name+'="'+attrs[i].value+'"';
		}
	}
	
	return exattr;
}


function getNodeAttrs(nodeName, node) {
	var txtAttribs=new String();
	
	switch (nodeName) {
	
		case 'p':
			txtAttribs=getPAttr(node);
			break;
			
		case 'a':
			txtAttribs=getAAttr(node);
			break;
			
		case 'span':
			var tmpnode=node.nodeName.toLowerCase();
			if (tmpnode=='u') txtAttribs=' style="text-decoration:underline;"';
			if (tmpnode=='strike') txtAttribs=' style="text-decoration:line-through;"';
			break;
			
		case 'strong':
		case 'em':
			break;
			
		case 'script':
			txtAttribs=getScriptAttr(node);
			break;
			
		case 'br':
			txtAttribs='';
			break;
			
		case 'img':
			txtAttribs=getIMGAttr(node);
			break;
	
		default:
			txtAttribs=getDefaultAttr(node);
	}

	return txtAttribs;
}


function exportloop (node) {

	var code=new String();

	while (node!=null) {
		var nName=nodeChangeName(node);
		var nAttribs=getNodeAttrs(nName,node);
		
		if (nName!='#text') {
			switch(nName) {
				case 'img':
				case 'hr':
				case 'br':
					code=code+'<'+nName+nAttribs+' />';
				break;
				
				case 'script':
					code=code+'<'+nName+' type="text/javascript">'+nAttribs+'</script>';
				break;
				
				default:
					code=code+'<'+nName+nAttribs+'>';
					if (node.hasChildNodes())
						code=code+exportloop(node.firstChild);
					code=code+'</'+nName+'>';
				break;
			}
		} else
			code=code+node.nodeValue;
		node=node.nextSibling;
	}
	return code;
}

function importloop(str) {

	str=str.replace(/<span style=\"text-decoration:line-through;\">(.*?)<\/span>/gi,'<strike>$1</strike>');
	str=str.replace(/<span style=\"text-decoration:underline;\">(.*?)<\/span>/gi,'<u>$1</u>');
	str=str.replace(/<em>(.*?)<\/em>/gi,'<i>$1</i>');
	str=str.replace(/<strong>(.*?)<\/strong>/gi,'<b>$1</b>');
	str=str.replace(/<a name=\"(.*?)\"><\/a>/gi,'<img src="/images/xedit/icon_anchor.gif" class="anchor" title="$1" alt="$1" />');
	str=str.replace(/<script type=\"text\/javascript\">showBaner\((.*?)\);<\/script>/gi,'<img src="/images/xedit/icon_baner.gif" class="baner" title="$1" alt="$1" />');
	str=str.replace(/<p style=\"text-align:\s*(\w*)\">/gi,'<p align="$1">');	//P align import
	return str;

}


