// JavaScript Document

var xhr_transport = null;
function Transport(id, _class, state){
	this.id = id;
	this.state = state;
	this._class = _class;
	this.properties = new Array();
	this.elements = new Array();
	this.objects = new Array();
	this.setProperty = function(prop, value){
		for(var i=0;i<this.properties.length;i++){
			if(this.properties[i].property == prop){
				this.properties[i] = new TransportProperty(prop, value);	
				return;
			}
		}
		this.properties[this.properties.length] = new TransportProperty(prop, value);	
	}
	this.removeProperty = function(key){
		for(var i=0;i<this.properties.length;i++){
			if(this.properties[i].property == key){
				this.properties.splice(i, 1);
				return;
			}
		}
	}
	this.setElement = function(e){
		for(var i=0;i<this.elements.length;i++){
			if(this.elements[i]._class == e._class){
				this.elements[i] = e;
				return;
			}
		}
		this.elements[this.elements.length] = e;
	}
	this.setObject = function(key, o){
		for(var i=0;i<this.objects.length;i++){
			if(this.objects[i].property == key){
				this.objects[i] = new TransportObject(key, o);
				return;
			}
		}
		this.objects[this.objects.length] = new TransportObject(key, o);
	}
	this.toXML = function(){
		var obj = '<object class="ultrod.util.transport.Transport">';
		obj += '<void property="id"><string>'+this.id+'</string></void>';
		obj += '<void property="theclass"><string>'+this._class+'</string></void>';
		obj += '<void property="state"><string>'+this.state+'</string></void>';
		
		var strprops = '<object class="java.util.ArrayList">';
		for(var i=0;i<this.properties.length;i++){
			strprops += '<void method="add"><object class="ultrod.util.transport.TransportProperty">'+
			'<void property="property"><string>'+this.properties[i].property+'</string></void>'+
			'<void property="value"><string>'+this.properties[i].value.getDecimalEncryptedValue()+'</string></void></object></void>';
		}
		strprops += '</object>';		
		var strobjs = '<object class="java.util.ArrayList">';
		for(var i=0;i<this.objects.length;i++){
			var _obj = this.objects[i];
			strobjs += '<void method="add"><object class="ultrod.util.transport.TransportObject">'+
			'<void property="property"><string>'+_obj.property+'</string></void>';
			var list = "";
			if(_obj.object.constructor.toString().indexOf("Array") != -1){
				list = '<object class="java.util.ArrayList">';
				for(var c=0;c<_obj.object.length;c++){
					if(_obj.object[c].toXML){
						list += '<void method="add">' + _obj.object[c].toXML() + '</void>';	
					}
				}
				list += '</object>';
			}
			strobjs += '<void property="value">'+list+'</void></object></void>';
		}
		strobjs += '</object>';
		obj += '<void property="properties">'+strprops+'</void>';
		obj += '<void property="objects">'+strobjs+'</void>';
		obj += '</object>';
		return obj;

	}
	this.save = function(oncallback, onerror, next, do_reload){
		var xml = '<?xml version="1.0" encoding="UTF-8"?><java version="1.5.0_04" class="java.beans.XMLDecoder">' + this.toXML() + '</java>';
		var params = 'ajax=true&accion=Transport&Transport=' + xml;
		if(do_reload){
			params += "&reload=true";	
		}
		xhr_transport = null;
		xhr_transport = newXMLHttpRequest();
		xhr_transport.open("POST", "../servlets/SrvSession", true);
		xhr_transport.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr_transport.setRequestHeader("Content-length", params.length);
		xhr_transport.setRequestHeader("Connection", "close");
		if(oncallback != null){
			xhr_transport.onreadystatechange = function(){
					if (xhr_transport.readyState == 4) {
						var data = xhr_transport.responseText;
						if(data.indexOf('TIMEOUT') != -1){
							document.location.href = 'feedback.jsp';
							return;
						}
						if(data.indexOf("ERROR")==-1){
							if(oncallback){
								oncallback(next);
							}
						}else{
							if(onerror){
								onerror(data);	
							}
						}
					}
			}
		}
		xhr_transport.send(params);
	}
	this.getPropertyValue = function(prop, encrypt_type){
		for(var i=0;i<this.properties.length;i++){
			if(this.properties[i].property == prop){
				if(encrypt_type == Encrypter.Types.DEC){
					return this.properties[i].value.getDecimalEncryptedValue();
				}else if(encrypt_type == Encrypter.Types.HTML){
					return this.properties[i].value.getHTMLEncryptedValue();
				}else if(encrypt_type == Encrypter.Types.DECRYPTED){
					return this.properties[i].value.getDecryptedValue();
				}else{
					throw "Invalid encrypt_type";	
				}
			}
		}
		return null;
	}
	this.getElement = function(_class){
		for(var i=0;i<this.elements.length;i++){
			if(this.elements[i]._class == _class){
				return this.elements[i];	
			}
		}
		return null;
	}
	this.getObject = function(key, encrypt_type){
		for(var i=0;i<this.objects.length;i++){
			if(this.objects[i].property == key){
				if(encrypt_type==null){
					return this.objects[i].object;
				}else{
					if(encrypt_type == Encrypter.Types.DEC){
						return new Encrypter(this.objects[i].object).getDecimalEncryptedValue();
					}else if(encrypt_type == Encrypter.Types.HTML){
						return new Encrypter(this.objects[i].object).getHTMLEncryptedValue();
					}else if(encrypt_type == Encrypter.Types.DECRYPTED){
						return new Encrypter(this.objects[i].object).getDecryptedValue();
					}else{
						throw "Invalid encrypt_type";	
					}	
				}
			}
		}
		return null;
	}
}

function TransportProperty(property, value){
	this.property = property;
	this.value = new Encrypter(value);
}

function TransportObject(property, value){
	this.property = property;
	this.object = value;
}

function Encrypter(s){
	var original_value = s;
	original_value = decrypt();
	var dec_encrypted_value = null;
	var html_encrypted_value = null;
    	this.getDecryptedValue = function(){
		return original_value;
	}
	this.getDecimalEncryptedValue = function(){
		if(dec_encrypted_value == null){
			dec_encrypted_value = encrypt(Encrypter.Types.DEC);
		}
		return dec_encrypted_value;
	}
	this.getHTMLEncryptedValue = function(){
		if(html_encrypted_value == null){
			html_encrypted_value = encrypt(Encrypter.Types.HTML);
		}
		return html_encrypted_value;
	}
	this.getDecryptedValue = function(){
		return decrypt();
	}
	function encrypt(type){
		if(original_value.length > 0){
			if(type == Encrypter.Types.DEC){
				var encrypted = "";
				for(var i=0;i<original_value.length;i++){
					encrypted += original_value.charCodeAt(i) + ",";
				}
				return "DEC:ENCRYPTED:" + encrypted.substring(0, encrypted.length - 1);	
			}else if(type == Encrypter.Types.HTML){
				var encrypted = "";
				for(var i=0;i<original_value.length;i++){
					if(original_value.charCodeAt(i) == 10){
						encrypted += "<br />";
					}else{
						encrypted += "&#" + original_value.charCodeAt(i) + ";";
					}
				}
				return encrypted;
			}
		}else{
			return "";	
		}
	}
	function decrypt(){
		if(original_value.indexOf("ENCRYPTED") == -1){
			return original_value;	
		}else{
			var codes = original_value.replace("DEC:ENCRYPTED:", "");
			codes = codes.split(",");
			var decrypted = "";
			for(var i=0;i<codes.length;i++){
				decrypted += String.fromCharCode(codes[i]);
			}
			return decrypted;	
		}
	}
	this.isEmpty = function(){
		if(original_value.length > 100)return false;
		var codes = this.getDecimalEncryptedValue().replace('DEC:ENCRYPTED:', '');
		codes = codes.split(",");
		if(codes.length == 0)return true;
		for(var i=0;i<codes.length;i++){
			if(codes[i] != 160 && codes[i] != '' && codes[i] != 32){
				return false;	
			}
		}
		return true;	
	}
}
Encrypter.Types = function(){}
Encrypter.Types.DEC = 1;
Encrypter.Types.HTML = 2;
Encrypter.Types.DECRYPTED = 3;

Transport.parseObject = function(_t){
	var t = new Transport(_t.id, _t.theclass, _t.state);
	for(var i=0;i<_t.properties.length;i++){
		t.setProperty(_t.properties[i].property, _t.properties[i].DECEncryptedValue);
	}
	for(var i=0;i<_t.objects.length;i++){
		t.setObject(_t.objects[i].property, _t.objects[i].value);
	}
	for(var i=0;i<_t.elements.length;i++){
		var element = Transport.parseObject(_t.elements[i]);
		t.setElement(element);
	}
	return t;
}

Transport.parseList = function(list){
	var _list = new Array();
	for(var i=0;i<list.length;i++){
		_list[i] = Transport.parseObject(list[i]);	
	}
	return _list;
}


