

function JSONUtil() {
	
}


JSONUtil.sampleJSON2Form= function(formId,obj) {
	
	function trim(s) {
		var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
   		return (m == null) ? "" : m[1];
	}
	
	var ps = Object.keys(obj);
	ps.each(
		function(name){
			
			var formElement = $(name);
			var s = obj[name];
			
			if ( formElement != null) {
				 
				 if (s == null) {
				 	 s = "";
				 } else {
				 	s = trim( ""+s )
				 }
				 
				 //alert("try to set " + name + " to " + s);
				 
				 try {
				 	Form.Element.setValue(name, s);
				 } catch (e) {
				 	alert(e.message);
				 }
			} 
			
			// add innerHTML
			var view = $('view_' + name);
			var s = obj[name];
			
			if ( view != null) {
				 
				 if (s == null) {
				 	 s = "";
				 } else {
				 	s = trim( ""+s )
				 }
				 
				 try {
				 	view.update(s);
				 } catch (e) {
				 	alert(e.message);
				 }
			} 
			
			
		});
	
	return;
	
	/**
	var inputs = $(formId).getInputs();
	var selects = $(formId).getElements('select');
		
	inputs = inputs.concat(selects);
	alert(inputs.length);
	
	inputs.each(
		function(input){
			
			var id = input.identify();
			
			try {
				var value = obj[id];
				
				if (value != null ) {    
					var s = ""+value;         
					
					
				}
			} catch(e) {
				alert(e.message);
			}
		}
	);*/

}


JSONUtil.form2JSONString= function(formId) {
	
	return Object.toJSON( JSONUtil.form2JSON(formId) );

}

JSONUtil.form2JSON = function(formId) {

	var binds = $A( $(formId).getElementsByClassName('bind') );
	
	var json = {};
	binds.each(
		function(bind){
			
			var index = bind.name.indexOf('.');
			
			if (index > -1 ) { // is a object property
				
				var beanName = bind.name.substr(0,index);
				var propertyName = bind.name.substr(index+1,bind.name.length);;
				
				if (json[beanName] == null) { // first
					json[beanName] = {};
				}
				json[beanName][propertyName] = bind.value;
			} else {
				json[bind.name] = bind.value;
			}
		}
	);
	
	
	return json;

}

JSONUtil.JSON2form = function(source) {
	
	alert("call JSON2form");
	
	function expandObject(result,pre,obj,isElement) {
		
		function Property(name,value,type) {
			this.name = name;
			this.value = value;
			this.type = type;
			
			this.toString = function() {
				
				return this.name + "=" + this.value + " " + this.type;
			}
		}
		
		
		if (Object.isArray(obj)) {
			
			for (var i=0;i<obj.length;i++) {
				
				expandObject(result,pre + "[" + i + "]",obj[i],true); 
			
			}
		
		} else {
			
			var properties = Object.keys(obj);
		
			for (var i = 0, length = properties.length; i < length; i++) {
	      
	      		var property = properties[i], value = source[property];
	      		
	      		 if (Object.isFunction(value) == false) {
	      		 	
	      			if ( typeof(value) == 'object' ) {
	      			
	      				if (value == null) {
	      					expandObject(result,property,{}); 	
	      				} else {
	      					expandObject(result,property,value); 
	      				}
	      				
	      			} else {
	      			 
	      				var type = "";
	      				if (isElement == true) {
	      					type = 'element';
	      				} 
	      			
	      				if (pre == '') {
	      					result.push( new Property(property,value,type) );
	      				} else {
	      					result.push( new Property(pre + "." + property,value,type) );
	      				}
	      			}
	      			
	      		 }
	      	}
		
		}
		
	}
	
	var result = [];
	expandObject(result,'',source)
	
	var msg = '';
	for (var i=0;i<result.length;i++) {
		
		
		
		var property = result[i];
		
		alert(property);
		
		var objs = document.getElementsByName(property.name);
		
		if (property.type == 'element') {
			
			var name = property.name;
			var start = name.indexOf('[');
			var end = name.indexOf(']');
			var index = name.substr(start+1,end-start-1) * 1;
			var relName = name.replace("["+index+"]","");
			
			var items = document.getElementsByName(relName);
			
			//alert(relName +" index = " + index + " size = " + items.length);
			
			if (items.length >= (index+1) ) {
				items[index].checked = true;
			}
		
		} else {
			
			if (objs.length == 1) { // text,select
			
			var obj = objs[0];
			if (obj.type == 'textarea') {
				
			} else {
				obj.value = property.value.trim();
			}
					
			} else { // checkbox,radio
				
				for (var j=0;j<objs.length;j++) {
				
					var obj = objs[j];
					
					if (obj.value == property.value) {
						obj.checked = true;
					} else {
						obj.checked = false;
					}
				}
			}
		}
		
		
		
		msg = msg + result[i] + "\n";
	}
      
        
	alert(msg);
	
	/**
	var binds = $A( $(formId).getElementsByClassName('bind') );
	binds.each(
		function(bind){
		}
	);*/
	
	
	//return Object.toJSON(json);

}




