//
//==========================================================
// This file contains utility functions to 
// 1. xmlEncode - to prepare data before sending to server
// 2. textDisplay - scrub text for display in a text element
// 3. inputDisplay - scrub text for display in an input element
//===========================================================



//
// call it to display read-only text in HTML
// It decodes all characters decoded by inputDisplay 
// and encodes \n as <BR/>, < as &lt;, > as &gt;
//
function textDisplay(text) {
	text = inputDisplay(text);
	
	// make sure to substitute '\n' by '<BR />' only after
	// substitutions for '<' and '>' have occurred.
	text = text.replace(/\n/ig,  '<BR />');
	return text;
};

//
// call it to display text in a HTML input element
// It decodes &amp;, &lt;, &gt;, &quot;, &apos; character encodings
//
function inputDisplay(text) {
	if (text == undefined || text == null || text == '') {
		return text;
	}
	text = text.replace(/\&amp\;/ig,  '&').replace(/\&lt\;/ig,  '<').replace(/\&gt\;/ig,  '>');
	text = text.replace(/\&quot\;/ig, '"').replace(/\&apos\;/ig, "'");
	text = text.replace(/\&lt\;BR\/\&gt\;/ig,  '\n').replace(/<BR\/>/ig, '\n');
	return text;
};

//
// call before sending XML data to server.
// This method encodes &, <, >, ', ", “, ”, ‘, ’ characters.
// There is no corresponding xmlDecode because 
// textDisplay() and inputDisplay() handle decoding.
// 
function xmlEncode(text) {
	if (text == null || text == '' || !isNaN(text)) {
		return text;
	}
	text = text.replace(/\&/ig,  '&amp;').replace(/\</ig,  '&lt;').replace(/\>/ig,  '&gt;');
	// handle double quotes
	text = text.replace(/\"/ig,  '&quot;').replace(/\“/ig,  '&quot;').replace(/\”/ig,  '&quot;');
	// handle single quotes
	text = text.replace(/\'/ig,  "'").replace(/\‘/ig,  "'").replace(/\’/ig,  "'");
	// handle newline characters
	return text;
};

// Prepare data for text display
// Process values for the specified keys in keyList
// KeyList is a comma seperated string of key names
//
function textDisplayData(data, keyList) {
	if (data == null || data.length == 0 || keyList == null) {
		return data;
	}
	var keys = keyList.split(',');
	for (var i=0; i<keys.length; i++) {
		keys[keys[i]] = true;
	}
	
	for (var i in data) {
		if (i == '__parent' || i == 'prototype' 
			|| i == 'extend' || i == 'bind' || i == 'bindAsEventListener' 
			|| data[i] == null) {
			continue;
		}
		if (Object.prototype.isPrototypeOf(data[i])) {
			data[i] = textDisplayData(data[i], keyList);
		} else {
			if (keyList != null && keys[i]!=null) {
				data[i] = textDisplay(data[i]);
			}
		}
	}
	return data;
};

//============================================================
// DEPRICATED FUNCTIONS BELOW
// Use xmlEncode to encode data before sending to server
// Use inputDisplay to scrub data before displaying in input field
// Use textDisplay to scrub data before displaying as a text
//============================================================

function htmlEncode(text) {
	if (text == null || text == '' || !isNaN(text)) {
		return text;
	}
	text = text.replace(/\&/ig,  '&amp;').replace(/\</ig,  '&lt;').replace(/\>/ig,  '&gt;').replace(/\n/ig,  '<BR/>');
	return text;
};

function htmlDecode(text) {
	if (text == null || text == '') {
		return text;
	}
	text = text.replace(/\&lt\;BR\/\&gt\;/ig,  '\n').replace(/\&amp\;/ig,  '&').replace(/\&lt\;/ig,  '<').replace(/\&gt\;/ig,  '>');
	text = text.replace(/\&quot\;/ig, '"').replace(/\&apos\;/ig, "'");
	return text;
};

function htmlEncode1(text) {
	if (text == null || text == '') {
		return text;
	}
	return text.replace(/\</ig,  '&lt;').replace(/\>/ig,  '&gt;').replace(/\n/ig,  '&lt;BR\/&gt;');
};

function htmlDecode1(text) {
	if (text == null || text == '') {
		return text;
	}
	return text.replace(/\&lt\;BR\/\&gt\;/ig,  '\n').replace(/\&lt\;/ig,  '<').replace(/\&gt\;/ig,  '>');
};


