//------------------------------------------------------------------
//	OBJECT: attachment; 
//	AUTHOR: Joshua Bolduc
//	DATE: June 29, 2006
//	DESCRIPTION: This object lets you upload multiple images at once  
//------------------------------------------------------------------
function addAttachment(targetName,fileName,num)
{
	var form_size = "35"; 
	if(targetName!=null) this.targetName = targetName; 
	if(fileName!=null) 	 this.fileName   = fileName; 
	if(this.fileName==null){
		alert("Please provide the fileName"); 
	}else if(this.targetName==null){
		alert("Please provide the targetName"); 
	}else{
		targetName 	  	  = this.targetName; 
		var tbl       	  = document.getElementById(targetName);
		var nextRow   	  = tbl.tBodies[0].rows.length;
		var iteration 	  = nextRow; 
		if(num==null) num = nextRow; 
		else iteration 	  = num;
	
		var row = tbl.tBodies[0].insertRow(num);
	
		var cell   = row.insertCell(0);
		var input  = document.createElement('input');
		input.type = 'file'; 
		input.name = this.fileName + iteration; 
		input.size = form_size; 
		 
		
		var removeLink  = document.createElement('a');
		removeLink.href = "javascript:"
		removeLink.style.paddingLeft = "5px"; 
		removeLink.onclick = function(){ 
			rowObj = this.parentNode.parentNode;
			
			rIndex = rowObj.sectionRowIndex; 	
		
		 	 
			tbl = document.getElementById(targetName);  
			tbl.deleteRow(rIndex); 
			
			
		} 
		text = document.createTextNode('remove'); 
		removeLink.appendChild(text); 
		
	
		cell.appendChild(input); 
		cell.appendChild(removeLink);
	}

}
	function attachment()
	{
		this.fileName   = "attach"; 
		this.targetName = "tblAttach"; 
		
	}
	attachment.prototype.setFileName = function(name)
	{
		this.fileName = name; 
	}
	attachment.prototype.setTargetName = function(name)
	{
		this.targetName = name; 
	}
	attachment.prototype.add = addAttachment; 

	
/*****************************************************
		link_attachment
****************************************************/
	function addLink(targetName,fileName,num){
	
	var form_size = "45"; 
	
	if(targetName!=null) this.targetName = targetName; 
	if(fileName!=null) 	 this.fileName   = fileName; 
	
	if(this.fileName==null){
		alert("Please provide the fileName"); 
	}else if(this.targetName==null){
		alert("Please provide the targetName"); 
	
	
	}else{
		targetName = this.targetName; 
		var tbl       = document.getElementById(targetName);
		var nextRow   = tbl.tBodies[0].rows.length;
		var iteration = nextRow; 
		if(num==null) num = nextRow; 
		else iteration = num;
	
		var row = tbl.tBodies[0].insertRow(num);
	
		var cell   = row.insertCell(0);
		
		
		var title   = document.createElement('input');
		title.type  = 'text'; 
		title.name  = this.fileName + iteration +"_title";
		title.id    = title.name; 
		title.size  = "25"; 
		title.value = "Link text. . ."; 
		title.style.marginRight = "5px";
		title.onfocus = function ()
		{
			if(this.value=="Link text. . .")
			{
				this.value = ""; 
			}
		}
		title.onblur = function()
		{
			if(this.value == "")
			{
				this.value = "Link text. . ."; 
			}
		}
		
		var input  = document.createElement('input');
		input.type = 'text'; 
		input.name = this.fileName + iteration+"_url"; 
		input.id   = input.name; 
		input.size = form_size; 
		input.value= "http://"; 
		
		
		
		my_self = this; 
		 
		//------------------------
		// Remove Link
		//-------------------------
		var removeLink  = document.createElement('a');
		removeLink.id   = this.fileName + iteration + "_id"; 

		removeLink.onRemove = delete_link; 
		
			
		
		removeLink.href = "javascript:"
		removeLink.style.paddingLeft = "5px";
		removeLink.onclick = function()
		{  
			my_self.value = this.value;   
			rowObj = this.parentNode.parentNode;
			rIndex = rowObj.sectionRowIndex; 	
			tbl   = document.getElementById(targetName);  
			this.onRemove(); 
			tbl.deleteRow(rIndex); 
		 
		} 
		
		var hidden = document.createElement('input'); 
		hidden.type = "hidden"; 
		hidden.name = this.fileName + iteration+"_link_id";
		hidden.id   = hidden.name; 
	
		
		text = document.createTextNode('remove'); 
		removeLink.appendChild(text); 
		cell.appendChild(hidden); 
		cell.appendChild(title); 
		cell.appendChild(input); 
		cell.appendChild(removeLink);
	}

}
	

	function link_attachment()
	{
		this.fileName   = "link"; 
		this.targetName = "links"; 
		
	}
	
	link_attachment.prototype.autoAttach = function(array)
	{
		for(i=0; i<array.length; i++)
		{
			this.add();
			for(varname in array[i])
			{
				attach = document.getElementById(this.fileName+i+"_"+varname);
				if(attach) attach.value = array[i][varname]; 
			}	
			//hidden
			hidden = document.getElementById(this.fileName+i+"_link_id"); 
			hidden.value = array[i].id; 
		}
	}
	
	link_attachment.prototype.setFileName = function(name)
	{
		this.fileName = name; 
	}
	
	link_attachment.prototype.setTargetName = function(name)
	{
		this.targetName = name; 
	}
	link_attachment.prototype.add 	   = addLink; 
	 
	
