/*************************************************************************

Name:	convert.js
Desc:	Converts data from PHP to JAVA to XML

TODO:
*************************************************************************/

var INDEX_PLACEHOLDER = 			'_index';
var KEY_PREFIX = 					'key_';
var XML_ROOT = 						'ROOT';
/*
//shortcut to ajax call
//callPHP == ajax_Request
//ajax_Request('admin.getData', this.config, this, 'refreshReturn');
function callPHP(action, args, target, func) {
	ajax_Request({	action:	action, 
					args:	args, 
					target:	target, 
					func:	func});
	
	
}
/**/

//converts string to UTF8 - converts or removes WORD special characters
//use CHARMAP to find special hex codes
//use when getting data from textfields, url_encode, etc
function stringToUTF8(value) {
	
	value = isset(value) ? value : '';
	
	//convert some frequently used
	value = value.replace(/(\u0000|\u000D)/g, '');		//0 (IE broked), 13 (carriage return extra),
	value = value.replace(/(\u201C|\u201D)/g, '"');		//double quote l/r
	value = value.replace(/(\u2018|\u2019)/g, '\'');	//single quote l/r
	value = value.replace(/\u2026/g, '...');			//ellipse
	value = value.replace(/\u2013/g, '-');				//long dash
	
	/*
	value = value.replace(/\u00BC/g, '1/4');
	value = value.replace(/\u00BD/g, '1/2');
	value = value.replace(/\u00BE/g, '3/4');
	value = value.replace(/\u00A9/g, '&copy;');
	value = value.replace(/\u00AE/g, '&reg;');
	value = value.replace(/\u00B2/g, '2');				//squared
	value = value.replace(/\u00B3/g, '3');				//cubed
	
	value = value.replace(/[\u00E0-\u00E5]/g, 'a');	//dashed chars
	value = value.replace(/\u00E6/g, 'ae');
	value = value.replace(/\u00E7/g, 'c');
	value = value.replace(/[\u00E8-\u00EB]/g, 'e');
	value = value.replace(/[\u00EC-\u00EF]/g, 'i');
	value = value.replace(/[\u00F2-\u00F6]/g, 'o');
	value = value.replace(/[\u00F9-\u00FC]/g, 'u');
	
	value = value.replace(/[\u00A0-\uFFFF]+/g, '');		//removes other undisplayable chars
	/**/
	
	//clear all characters OVER 8-bit
	value = value.replace(/[\u0100-\uFFFF]+/g, '?');
	
	return value;
}


//converts formatted string to an XML object
function stringToXML(textXML) {
	var xml;
	
	if (window.ActiveXObject) {	// code for IE
		xml = new ActiveXObject("Microsoft.XMLDOM");
		xml.async = "false";
		xml.loadXML(textXML);
	} else {					// code for Mozilla, Firefox, Opera, etc.
		var parser = new DOMParser();
		xml = parser.parseFromString(textXML, "text/xml");
	}
	
	return xml;
}



//used by XMLtoJAVARecurse below
function XMLtoJAVAValue(value) {
	value = decodeURI(value);
	if (value.substr(0, 1) == '"') {
		value = value.substr(1, value.length - 2);
	} else if (value == 'TRUE') {
		value = true;
	} else if (value == 'FALSE') {
		value = false;
	} else if (value == 'NULL') {
		value = null;
	} else {
		value = Number(value);
	}
	return value;
}


function XMLtoJAVARecurse(xml) {
	
	var node;
	var name;
	var count;
	var text;
	var is_arr;
	var value;
	
	var placeholder = INDEX_PLACEHOLDER;
	var placeholder_len = String(placeholder).length;
	
	var key = KEY_PREFIX;
	var key_len = String(key).length;
	
	
	var length = xml.childNodes.length;
	
	if (length > 0) {
		name = xml.childNodes[0].nodeName;	//get first node to test if array
		name = (name.substr(key_len, name.length - key_len));
		
		is_arr = (name.substr(0, placeholder_len) == placeholder);
		if (is_arr) {
			var obj = new Array();
		} else {
			var obj = new Object();
		}
		
		for (var i=0; i<length; i++) {
			//get item
			node =		xml.childNodes[i];
			count =	node.childNodes.length;
			fullname =	node.nodeName;
			name = 		(fullname.substr(key_len, fullname.length - key_len));
			
			//sort out the value - if node has child then that is value
			if (node.childNodes.length > 0) {
				text = (node.firstChild.nodeName == '#text');	//true if node contains text rather than children
				if (text) {
					//decode the text
					//if (name == 'data') {
						//alert("DECODE " + name + ' = ' + node.firstChild.nodeValue);
					//	document.write("DECODE " + name + ' = ' + node.firstChild.nodeValue);
					//}
					//try {
					value = XMLtoJAVAValue(node.firstChild.nodeValue);
					
					
					//} catch (e) {
					//	document.write("FAIL " + name + ' = ' + node.firstChild.nodeValue);
					//}
				} else if (count > 0) {
					//check if the child is CDATA
					if (node.childNodes[0].nodeName == '#cdata-section') {
						value = XMLtoJAVAValue(node.childNodes[0].nodeValue);
					} else {
						value = XMLtoJAVARecurse(node);
					}
				}
			//no child, get value from attribute
			} else {
				value = node.attributes['value'];
			}
			
			//is array
			if (is_arr) {
				//strip prefix off array items
				name = name.substr(placeholder_len, name.length - placeholder_len);
				//push item onto array
				obj.push(value);
			//is object
			} else {
				//insert keyed item
				obj[name] = value;
			}
		}
		return obj;
		
	} else {
		return null;
		
	}
}

//converts XML to Array
function XMLtoJAVA(xmldoc) {
	var root;
	if (xmldoc.childNodes.length > 0) {
		root = xmldoc.childNodes[xmldoc.childNodes.length - 1];
		obj = XMLtoJAVARecurse(root);
		//alert(objectToString(obj));
	} else {
		obj = new Array();
	}
	return obj;
}














//convert array to string for alerts
function objectToString(source, indent) {
	var output = '';
	var obj;
	if (indent == null) {indent = 0;}
	
	for (var key in source) {
		obj = source[key];
		
		for (var i=0; i<indent; i++) {
			output += '\t';
		}
		
		switch (typeof(obj)) {
			case 'object':
				output += '[' + key + ']\n' + objectToString(obj, indent + 1) + '\n';
				break;
			case 'number':
			case 'string':
				output += '[' + key + '] = "' + obj + '"\n';
				break;
		}
	}
	
	return output;
}


//creates a formatted PHP array from JAVA object args
function JAVAtoPHP(args) {
	var output = '';

	if (args == 'undefined') {alert('JAVAtoPHP Error: undefined params');}
	
	//process the object
	if (args == null) {
		output = 'NULL';
	} else if (is_object(args)) {
		//keyed array - create a=>x,b=>y,c=>z
		var i=0;
		for (var key in args) {
			if (i > 0) {output += ', ';}
			output += '"' + key + '" => ' + JAVAtoPHP(args[key]);
			i++;
		}
		output = 'array(' + output + ')';
		
	} else if (is_array(args)) {
		//arranged array - create x,y,z
		var count = args.length;
		for (var i=0; i<count; i++) {
			if (i > 0) {output += ', ';}
			output += JAVAtoPHP(args[i]);
		}
		output = 'array(' + output + ')';
		
	} else {
		if (is_boolean(args)) {
			//use keywords for boolean
			output = (args) ? 'true' : 'false';
		} else if (is_number(args)) {
			//use direct number for numbers
			output = args;
		} else {
			//escape the slashes and quotes for strings
			output = String(args).replace(/\\/g, '\\\\');
			output = String(output).replace(/"/g, '\\"');
			output = '"' + output + '"';
		}
	}
	
	return output;
}
