/*
	Javascript Mac-esque Multi-Select
	Lech M. Boron

	Given a Select, replaces with multi-select table		
*/

function MultiInput(instance,el,tableClass){
	this.instanceName = instance;
	tbl = document.createElement("table");
	tbl.className = tableClass;
	this.tBody = document.createElement("tbody");
	tbl.appendChild(this.tBody);
	
	this.input = document.createElement("input");
	this.input.type = "text";
	this.input.name = el.name;
	this.input.setAttribute('class','email required');
	this.input.setAttribute('title','Please enter a valid email addres');
	values = el.value.split(/\n+/);

	this.addLink = document.createElement("a");
	this.addLink.className = "add";
	this.addLink.innerHTML = "<span>+</span>";
	this.addLink.href = "#";
	this.removeLink = document.createElement("a");
	this.removeLink.className = "remove";
	this.removeLink.innerHTML = "<span>&minus;</span>";
	this.removeLink.href = "#";
	this.tr = document.createElement("tr");
	this.tr.appendChild(document.createElement("td"));
	this.tr.appendChild(document.createElement("td"));
	this.tr.appendChild(document.createElement("td"));
	this.tr.childNodes[0].appendChild(this.input);
	this.tr.childNodes[1].appendChild(this.addLink);
	this.tr.childNodes[2].appendChild(this.removeLink);
	
	if(values.length > 0){
		for(var i = 0; i < values.length; i++){
			this.addRow(false,values[i]);
		}		
	}else this.addRow(false,false);
	
	el.parentNode.replaceChild(tbl,el);
}
MultiInput.prototype.tBody;
MultiInput.prototype.input;

MultiInput.prototype.removeRow = function(rowIndex){
	if(this.tBody.childNodes.length > 1){
		this.tBody.removeChild(this.tBody.childNodes[rowIndex]);
	}
	this.refreshNames();
}
MultiInput.prototype.addRow = function(insertAfter,value){
	tr = this.tr.cloneNode(true);
	if(value != false){
		tr.childNodes[0].childNodes[0].value = value;
	}
	if(insertAfter === false || this.tBody.childNodes.length==0 || insertAfter==(this.tBody.childNodes.length-1)){
		this.tBody.appendChild(tr);
	}
	else{
		this.tBody.insertBefore(tr,this.tBody.childNodes[insertAfter+1]);
	}
	this.refreshNames();
}
MultiInput.prototype.refreshNames = function(){
	for(var i = 0; i < this.tBody.childNodes.length; i++){
		var v = i;
	
/*		var v = 'n' + i;
		var val = this.tBody.childNodes[i].childNodes[0].childNodes[0].value;

		var re = /^\[(.*?)\] (.*)/
		var m =val.match(re);
		
		if (m) {
			var v = 'r' + m[1];
		//	this.tBody.childNodes[i].childNodes[0].childNodes[0].value = m[2];
		}*/
	
		this.tBody.childNodes[i].childNodes[0].childNodes[0].name = this.input.name+"["+v+"]";
		this.tBody.childNodes[i].childNodes[1].childNodes[0].onclick = new Function(this.instanceName+".addRow("+v+",false);return false;");
		this.tBody.childNodes[i].childNodes[2].childNodes[0].onclick = new Function(this.instanceName+".removeRow("+v+");return false;");
		
		
		// this probably wont work phil, so fix it! x
		this.form = $('referafriendForm');
		if("undefined" != typeof(valid8)) {
			valid8.initialize($('referafriendForm'),{errorClass: 'error', dateFormat: 'dd/MM/yyyy'});
		}
	}
}
