//this class is used to generate dinamic rows for a table
//strTableIDName - the table id
//arrRowsNames - this array contains the name of the fields that are generated when an item is added
//arrRowsTypes - this array contains the type of the fields
//arrRowsDisplay - this array contains the text that is displayed near the fields
//arrRowsMandatory - this array specifies for each field if it's mandatory
//arrRowsClassNames - this array contains the class for each field
//arrRowsDisplayClassNames - this array contains the class for each field's display
//strRemoveLinkClass - the css class of the "remove" link
//strFormName - the name of the form where the fields will be created
//arrRowsRestrictions - array containing the restrictions for each field (nonblank, goodemail, goodfloat)
//arrFieldEvents - the events associated with each field

function CDinamicRows(objName, strTableIDName, arrRowsNames, arrRowsTypes, arrRowsDisplay, arrRowsMandatory, arrRowsClassNames, arrRowsDisplayClassNames, iCellWidth, strRemoveLinkClass, strFormName, arrRowsRestrictions, arrFieldEvents, funcAfterRemove, funcAfterRemoveWithId, strImgSeparator)
{
	
	//properties
	this.objName = objName;
	this.strTableIDName = strTableIDName;
	this.arrRowsNames = arrRowsNames;
	this.arrRowsTypes = arrRowsTypes;
	this.arrRowsDisplay = arrRowsDisplay;
	this.arrRowsMandatory = arrRowsMandatory;
	this.arrRowsClassNames = arrRowsClassNames;
	this.arrRowsDisplayClassNames = arrRowsDisplayClassNames;
	this.strRemoveLinkClass = strRemoveLinkClass;
	this.strFormName = strFormName;
	this.arrRowsRestrictions = arrRowsRestrictions;
	this.arrFieldEvents = arrFieldEvents;
	this.arrAttributes = new Array();
	this.bHideCaption = false;
	
	this.funcAfterRemove = funcAfterRemove;
	this.funcAfterRemoveWithId = funcAfterRemoveWithId;
	
	this.arrTrIDS = new Array();
	this.arrAuxTrIDS = new Array();
	this.iIndexForArr = 0;
	
	if(!strImgSeparator)
		strImgSeparator = '../images/dot.gif';
	this.strImgSeparator = strImgSeparator;
	
	this.iCellWidth = iCellWidth;
	this.iIndexID = 0;
	this.BlocksNo = 0;
	
	//methods	
	this.addItem = addItem; //adds an item
	this.removeItem = removeItem; // removes an item
	this.removeAllItems = removeAllItems; // removes all items
	this.escapeDoubleQuotes = escapeDoubleQuotes; //replace " with \"
	this.replaceSpecialChars = replaceSpecialChars; //escapes special chars
	this.removeQuotes = removeQuotes; // removes the quotes from a string
	this.verifyObjectFields = verifyObjectFields; //verify the fields according to arrRowsRestrictions restrictions
	this.calculateSumOfFields = calculateSumOfFields; //adds the values from the specified fields
	this.concatenateFieldValues = concatenateFieldValues; //returns a string made by all the values of that field, separated by the separator	
	this.addCalendarSetup = addCalendarSetup;
}
function addItem(arrRowsValues)
{	
	var iAuxID = this.iIndexID;
	var sAttributes = "";
	var cellIndex = 0;
//	alert(arrRowsValues);
//	alert(this.arrRowsNames)
	if(this.arrRowsNames)
	{
		this.BlocksNo++;
		
//		eval("var newRow = document.getElementById('"+this.strTableIDName+"').insertRow(-1)");					
		var newRow = document.getElementById(this.strTableIDName).insertRow(-1);					
		
		var newCell = newRow.insertCell(0);
		newCell.className = this.arrRowsDisplayClassNames[i];
		newCell.vAlign = "top";				
		newCell.colSpan = "2"
		newCell.innerHTML = '<img src="' + this.strImgSeparator + '" width="100%" height="1" alt="" border="" />';				
		
		if(typeof(Builder) != 'undefined')
		{
			/**
			 * INSERTION OF THE AUTOCOMPLETER
			 * */
			//generate a unique id for each text box with name of the misc vendor
			var d = new Date();
			var milliseconds = d.getMilliseconds();
			//generate the Choices div
			var AutocompleteChoicesDiv = Builder.node('div', {id:'autocomplete_choices'+milliseconds, className:'autocomplete'});
		}
		
		
		for(var i=0; i<this.arrRowsNames.length; i++)
		{
//			eval("var newRow = document.getElementById('"+this.strTableIDName+"').insertRow(-1)");
			var newRow = document.getElementById(this.strTableIDName).insertRow(-1);

			sAttributes = this.arrAttributes[i];
			
			cellIndex = 0;
			if(this.bHideCaption == false){
				var newCell = newRow.insertCell(cellIndex);
				newCell.className = this.arrRowsDisplayClassNames[i];
				newCell.vAlign = "top";		
				newCell.width = this.iCellWidth;
				newCell.innerHTML = this.arrRowsDisplay[i];	
				cellIndex++;
			}							
			
			newCell = newRow.insertCell(cellIndex);
			newCell.className = this.arrRowsDisplayClassNames[i];			
			newCell.vAlign = "top";
			newCell.noWrap = true;	
			if(this.arrRowsTypes[i] == "text")
			{			
				if(sAttributes == "" || sAttributes == 'undefined'){
					sAttributes = 'maxlength="50"';
				}
				if (!arrRowsValues[i])
				{
					arrRowsValues[i] = '';
				}
				
				if (i === 0)
				{
					//misc vendor name cell
					newCell.innerHTML = '<input id="miscVendorName'+milliseconds+'" type=\"text\"' + sAttributes + '" class=\"'+this.arrRowsClassNames[i]+'\" name=\"'+this.arrRowsNames[i]+this.iIndexID+'\" value=\"'+this.replaceSpecialChars(arrRowsValues[i])+"\" "+this.escapeDoubleQuotes(this.arrFieldEvents[i])+'>';
					if(AutocompleteChoicesDiv) {
						newCell.appendChild(AutocompleteChoicesDiv);
					}
				}
				else if (i === 1)
				{
					//email cell
					newCell.innerHTML = '<input id="emailBox'+milliseconds+'" type=\"text\"' + sAttributes + '" class=\"'+this.arrRowsClassNames[i]+'\" name=\"'+this.arrRowsNames[i]+this.iIndexID+'\" value=\"'+this.replaceSpecialChars(arrRowsValues[i])+"\" "+this.escapeDoubleQuotes(this.arrFieldEvents[i])+'>';
				}
				else if(i === 3)
				{
					//phone cell
					newCell.innerHTML = '<input id="phoneBox'+milliseconds+'" type=\"text\"' + sAttributes + '" class=\"'+this.arrRowsClassNames[i]+'\" name=\"'+this.arrRowsNames[i]+this.iIndexID+'\" value=\"'+this.replaceSpecialChars(arrRowsValues[i])+"\" "+this.escapeDoubleQuotes(this.arrFieldEvents[i])+'>';
				}
				else
				{
					//amount cell
					newCell.innerHTML = '<input type=\"text\"' + sAttributes + '" class=\"'+this.arrRowsClassNames[i]+'\" name=\"'+this.arrRowsNames[i]+this.iIndexID+'\" value=\"'+this.replaceSpecialChars(arrRowsValues[i])+"\" "+this.escapeDoubleQuotes(this.arrFieldEvents[i])+'>';
				}
				
				//instantiate a new autocompleter object
				
				if(this.arrRowsMandatory[i] == 1)
				{
					newCell.innerHTML += '&nbsp;<font color=\"#FF0000\">*</font>';
				}
				if(i == this.arrRowsNames.length - 1)
				{
//					eval("newCell.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript: "+this.objName+".removeItem("+this.iIndexID+")\" class=\""+this.strRemoveLinkClass+"\">« remove</a>'");
					newCell.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;<a onClick=\"javascript: '+this.objName+'.removeItem('+this.iIndexID+')\" class=\"'+this.strRemoveLinkClass+'\">« remove</a>';
				}			
			}
			else if(this.arrRowsTypes[i] == "hidden")
			{
				if (!arrRowsValues[i])
				{
					arrRowsValues[i] = '';
				}
				
				newCell.innerHTML = '<input type=\"hidden\" name=\"'+this.arrRowsNames[i]+this.iIndexID+'\" value=\"'+this.replaceSpecialChars(arrRowsValues[i])+"\" "+this.escapeDoubleQuotes(this.arrFieldEvents[i])+'>';
				
				if(i == this.arrRowsNames.length - 1)
				{
//					eval("newCell.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript: "+this.objName+".removeItem("+this.iIndexID+")\" class=\""+this.strRemoveLinkClass+"\">« remove</a>'");
					newCell.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;<a onClick=\"javascript: '+this.objName+'.removeItem('+this.iIndexID+')\" class=\"'+this.strRemoveLinkClass+'\">« remove</a>';
				}
			}
			else if(this.arrRowsTypes[i] == "file")
			{				
				eval("newCell.innerHTML = '<input type=\"file\" maxlength=\"100\" class=\""+this.arrRowsClassNames[i]+"\" name=\""+this.arrRowsNames[i]+this.iIndexID+"\">'");					
				
				if(this.arrRowsMandatory[i] == 1)
				{
					eval("newCell.innerHTML += '&nbsp;<font color=\"#FF0000\">*</font>'");
				}
				if(i == this.arrRowsNames.length - 1)
				{
					eval("newCell.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;<a onClick=\"javascript: "+this.objName+".removeItem("+this.iIndexID+")\" class=\""+this.strRemoveLinkClass+"\">« remove</a><br>'");
				}
			}
			else if(this.arrRowsTypes[i] == "date")
			{
				eval("newCell.innerHTML  = '<input type=\"text\" maxlength=\"100\" class=\""+this.arrRowsClassNames[i]+"\" name=\""+this.arrRowsNames[i]+this.iIndexID+"\" id=\""+this.arrRowsNames[i]+this.iIndexID+"\">'");				
				eval("newCell.innerHTML += '&nbsp;<img id=\"btnDate"+this.arrRowsNames[i]+this.iIndexID+"\" style=\"position: relative\" src=\"../includes/cool_cal/calendar.gif\" align=\"top\" border=\"0\" width=\"34\" height=\"21\">'");																							
				
				if(this.arrRowsMandatory[i] == 1)
				{
					eval("newCell.innerHTML += '&nbsp;<font color=\"#FF0000\">*</font>'");
				}
				if(i == this.arrRowsNames.length - 1)
				{
					eval("newCell.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;<a onClick=\"javascript: "+this.objName+".removeItem("+this.iIndexID+")\" class=\""+this.strRemoveLinkClass+"\">« remove</a><br>'");
				}
				
				this.addCalendarSetup(this.arrRowsNames[i] + this.iIndexID, '%m/%d/%Y', "btnDate"+this.arrRowsNames[i]+this.iIndexID);						
			}
			else if(this.arrRowsTypes[i] == "textarea")
			{
				if(sAttributes == ""){
					sAttributes = 'rows=\"5\" cols=\"40\"';
				}
				
				eval("newCell.innerHTML = '<textarea " + sAttributes + " class=\""+this.arrRowsClassNames[i]+"\" name=\""+this.arrRowsNames[i]+this.iIndexID+"\">" + this.replaceSpecialChars(arrRowsValues[i]) + "</textarea>'");					
				
				if(this.arrRowsMandatory[i] == 1)
				{
					eval("newCell.innerHTML += '&nbsp;<font color=\"#FF0000\">*</font>'");
				}
				if(i == this.arrRowsNames.length - 1)
				{
					eval("newCell.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;<a onClick=\"javascript: "+this.objName+".removeItem("+this.iIndexID+")\" class=\""+this.strRemoveLinkClass+"\">« remove</a><br>'");
				}
			}
			else
			{
				newCell.innerHTML = '';
			}
			iAuxID++;
		}
		if(typeof(Autocompleter) != 'undefined'){
			var Au = new Autocompleter('miscVendorName'+milliseconds, 'phoneBox'+milliseconds, 'emailBox'+milliseconds, 'autocomplete_choices'+milliseconds, '../common_files/misc_vendors_autocomplete_request.php');
			Event.observe(Au.textBox, 'keyup', function(){Au.operate()})
		}
//		eval("var newRow = document.getElementById('"+this.strTableIDName+"').insertRow(-1)");					
		var newRow = document.getElementById(this.strTableIDName).insertRow(-1);					
		var newCell = newRow.insertCell(0);
		newCell.className = this.arrRowsDisplayClassNames[i];
		newCell.vAlign = "top";				
		newCell.colSpan = "2"
		newCell.innerHTML = '<img src="' + this.strImgSeparator + '" width="100%" height="1" alt="" border="" />';
		iAuxID += 2;
		
		this.arrAuxTrIDS[this.arrAuxTrIDS.length] = this.iIndexID;
		this.arrTrIDS[this.iIndexForArr] = this.iIndexID;
	
		this.iIndexID = iAuxID;
		this.iIndexForArr++;
	}
}


function removeItem(iToRemoveID)
{
	if(document.getElementById("iInProposal") && document.getElementById("iInProposal").value == 1)
	{
		leaveProposal = false;
	}
	if(!confirm("Are you sure you want to remove this?")){
		return;
	}
	eval("var mainTable = document.getElementById(\""+this.strTableIDName+"\")");
	if(this.arrRowsNames)
	{
		this.BlocksNo--;
		if(this.arrRowsNames[4])
		{
			eval("var iId = document."+this.strFormName+"."+this.arrRowsNames[4]+iToRemoveID+".value");
		}
		else
		{
			var iId = '';
		}
		for(var i=0; i<this.arrRowsNames.length; i++)
		{
			eval("document."+this.strFormName+"."+this.arrRowsNames[i]+iToRemoveID+".value = ''");
		}
	}
	for(var i=0; i<this.iIndexForArr; i++)
	{
		if(iToRemoveID == this.arrTrIDS[i])
		{
			var del_id = this.arrAuxTrIDS[i];
			
			for(var t=0; t<this.arrRowsNames.length + 2; t++)
			{
				mainTable.deleteRow(del_id);
				//mainTable.removeChild(del_id);
				//mainTable.childNodes[1].removeChild(mainTable.childNodes[1].childNodes[del_id])
			}
			
			for(j=i; j<this.iIndexForArr; j++)
			{
				this.arrTrIDS[j] = this.arrTrIDS[j+1];
			}
			this.iIndexForArr--;
			break;
		}
	}	
	if(this.funcAfterRemove != '')
	{		
		eval(this.funcAfterRemove);	
	}
	if(this.funcAfterRemoveWithId != '' && iId && iId != '')
	{
		if(iToRemoveID != 0){
			eval(this.funcAfterRemoveWithId+'('+iId+')');	
		}
	}
}

function removeAllItems()
{
	eval("var mainTable = document.getElementById(\""+this.strTableIDName+"\")");
	
//	if(this.arrRowsNames)
//	{
//		this.BlocksNo--;
//		
//		for(var i=0; i<this.arrRowsNames.length; i++)
//		{
//			for(var j=0; j<this.iIndexForArr; j++)
//			{
//				eval("document."+this.strFormName+"."+this.arrRowsNames[i]+j+".value = ''");
//			}
//		}
//	}
	
	for(var i=0; i<this.iIndexForArr; i++)
	{
			del_id = this.arrAuxTrIDS[i];
			
			for(var t=0; t<this.arrRowsNames.length + 2; t++)
			{
				mainTable.deleteRow(del_id);
			}
			
			for(j=i; j<this.iIndexForArr; j++)
			{
				this.arrTrIDS[j] = this.arrTrIDS[j+1];
			}
			this.iIndexForArr--;
			break;
	}	
	
	if(this.funcAfterRemove != '')
	{		
		eval(this.funcAfterRemove);	
	}
}
function removeQuotes(str)
{
	var i;
	while(str.indexOf('&quot;') != -1)
	{
		str = str.replace("&quot;","\'");
	}
	return str;
}

function escapeDoubleQuotes(str)
{	
	while(str.indexOf('"') != -1)
	{
		str = str.replace('"', "&quot;");
	}
	while(str.indexOf('&quot;') != -1)
	{
		str = str.replace('&quot;', '\"');
	}
	return str;
}

function replaceSpecialChars(str)
{	
	while(str.indexOf("'") != -1)
	{
		str = str.replace("'", "&#39;");
	}	
		
	
	return str;
}

function verifyObjectFields()
{
	for(var i=0; i<this.iIndexID; i++)
	{		
		var element = eval("document."+this.strFormName+"."+this.arrRowsNames[0]+i);
		
		if(element && element.parentNode && element.parentNode.parentNode && element.parentNode.parentNode.parentNode)
		{
			for(var j=0; j<this.arrRowsNames.length; j++)
			{				
				if(this.arrRowsRestrictions[j] == "nonblank")
				{
					if(!nonBlank(eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i), "Field \""+this.arrRowsDisplay[j]+"\" cannot be blank!"))
					{
						return false;
					}
				}
				if(this.arrRowsRestrictions[j] == "goodemail")
				{
					if(!goodEMail(eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i), "Field \""+this.arrRowsDisplay[j]+"\" must be a valid e-mail address!"))
					{
						return false;
					}
				}
				if(this.arrRowsRestrictions[j] == "blank||goodemail") {					
					if(eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i)) {
						if(eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i + ".value")) {
							if(!goodEMail(eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i), "Field \""+this.arrRowsDisplay[j]+"\" must be a valid e-mail address!")) {
								return false;
							}
						}
					}					
				}
				if(this.arrRowsRestrictions[j] == "goodfloat")
				{
					if(!goodFloat(eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i), "Field \""+this.arrRowsDisplay[j]+"\" must be a valid amount!"))
					{
						return false;
					}
				}
				if(this.arrRowsRestrictions[j] == "blank||goodfloat")
				{
					if(eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i)) {
						if(eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i + ".value"))
						if(!goodFloat(eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i), "Field \""+this.arrRowsDisplay[j]+"\" must be a valid amount!")){
							return false;
						}
					}
				}
				if(this.arrRowsRestrictions[j] == "gooddate") {
					if(!goodDate('mm/dd/yyyy', eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i), "Field \""+this.arrRowsDisplay[j]+"\" must be a date in this format: mm/dd/yyyy!")) {
						return false;
					}
				}
				if(this.arrRowsRestrictions[j] == "blank||gooddate") {
					if(eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i)) {
						if(eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i + ".value"))
							if(!goodDate('mm/dd/yyyy', eval("document."+this.strFormName+"."+this.arrRowsNames[j]+i), "Field \""+this.arrRowsDisplay[j]+"\" must be a date in this format: mm/dd/yyyy!")) {
								return false;
						}
					}
				}
			}
		}
	}
	return true;
}

function calculateSumOfFields(strNameOfField)
{	
	var dSum = 0;
	for(var i=0; i<this.iIndexID; i++)
	{
		if(eval("document."+this.strFormName+"."+strNameOfField+i))
		{
			if(eval("document."+this.strFormName+"."+strNameOfField+i+".value"))
			{
				dSum += parseFloat(eval("document."+this.strFormName+"."+strNameOfField+i+".value"));
			}
		}
	}
	return dSum;
}

function concatenateFieldValues(strNameOfField, strSeparator)
{
	var dSum = "";
	for(var i=0; i<this.iIndexID; i++)
	{
		if(eval("document."+this.strFormName+"."+strNameOfField+i))
		{
			if(eval("document."+this.strFormName+"."+strNameOfField+i+".value"))
			{
				if(dSum != "")
				{
					dSum += strSeparator;
				}
				dSum += eval("document."+this.strFormName+"."+strNameOfField+i+".value");
			}
		}
	}
	return dSum;
}

function addCalendarSetup(strFieldId, strIf, strBtnId) {	
	Calendar.setup(
		{
		  inputField  : strFieldId,      // ID of the input field
		  ifFormat    : strIf,    				            // the date format
		  button      : strBtnId                    // ID of the button
		}
	  );
}


