// JavaScript Document

Connector = function(params){
	
	// menu div
	this.init(params);
	
}

Connector.prototype = {
	
	form : null,
	frame : null,
	params : null,
	formAction : null,
	formTarget : null,
	oldFormAction : null,
	oldFormTarget : null,
	inputsAllowed: "text",
	_dataLoadFunc : null,
	suffix : "",
	init : function(params){
		
		var c = this;
		
		this.config(params);
		
		while(document.getElementById('connector-form-iframe'+this.suffix)){
		
			if(this.suffix == "")
				this.suffix = 0;
		
			this.suffix += 1;
		
		}
		
		// create an invisible iframe
		try {
		  this.frame = document.createElement('<iframe id="connector-form-iframe'+this.suffix+'" name="connector-form-iframe'+this.suffix+'">');
		} catch (ex) {
		  this.frame = document.createElement('iframe');
		}
		this.frame.setAttribute("id","connector-form-iframe"+this.suffix);
		this.frame.setAttribute("name","connector-form-iframe"+this.suffix);
		this.frame.style.display = "none";
		this.frame.style.height = "0px";
		
		var myFunction = function(event){c.dataLoaded();};
		
		if (this.frame.addEventListener) {
			this.frame.addEventListener ("load",myFunction,false);
		} else if (this.frame.attachEvent) {
			this.frame.attachEvent ("onload",myFunction);
		} else {
			this.frame.onload = myFunction;
		}
		
		// add iframe to page
		document.body.appendChild(this.frame);
		
	},
	config : function(opt){
		
		if(opt){
			
			var o = eval(opt);
			for(var i in o){
				if(isNaN(o[i]))
					eval('this.'+i+' = "'+o[i]+'";');
				else
					eval('this.'+i+' = '+o[i]+';');
			}
		}
		
	},
	getForm : function(){
	
		return this.form;
	
	},
	connect : function(formName,url){
		
		// connect to this form
		
		if(formName != "")
			this.form = document.getElementById(formName);
		else
		{
			this.form = document.createElement("form");
			this.form.setAttribute('action',url);
			this.form.setAttribute('name','connector-form'+this.suffix);
			this.form.setAttribute('id','connector-form'+this.suffix);
			document.body.appendChild(this.form);
		}
		
		this.formAction = url;
		this.formTarget = this.frame.name;
		
	},
	onDataLoad : function(func){
		
		this._dataLoadFunc = func;
		
	},
	data : function(){
		
		return document.getElementById('connector-form-iframe'+this.suffix).contentWindow.document.body.innerHTML;
		
	},
	send : function(d){
		
		if(d){
		
			for(var i in d){
				
				var tmp = document.createElement("input");
				tmp.setAttribute("type","hidden");
				tmp.setAttribute("name",i);
				tmp.setAttribute("value",d[i]);
				this.form.appendChild(tmp);
				
			}
		
		}
		
		this.oldFormAction = this.form.action;
		this.oldFormTarget = this.form.target;
		
		this.form.setAttribute("action",this.formAction);
		this.form.setAttribute("target",this.formTarget);
		
		this.form.submit();
		
	},
	dataLoaded : function(e){
		
		if(this.form){
			this.form.setAttribute("action",this.oldFormAction);
			this.form.setAttribute("target",this.oldFormTarget);
		}
		
		if(this._dataLoadFunc)
			this._dataLoadFunc();
		
	},
	generateFormString : function(){
		
		var _str = "";
		
		// go through all the fields in the form and append them to the string
		
		var _formObjs = this.form.getElementsByTagName("input");
		
		for(var i=0;i<_formObjs.length;i++){
		
			// need to only include certain input types	
			if(this.inputsAllowed.indexOf(_formObjs[i].type) == -1)	
				continue;
		
			if(_str != "")
				_str += "&";

			_str += _formObjs[i].name+"="+_formObjs[i].value;	
		
		}
		
		return _str;
		
	}


}
