var attendeesTable = new TableObjectHandler('attendeesList');
attendeesTable.tableLoaded = true;
var cost = new costHandler();

/**
 *
 * @access public
 * @return void
 **/
function TableObjectHandler(tableName)
{
	this.tableLoaded = false;
	this.rowStartingNum = 1;
	this.tableName = tableName;
	this.rowCount = 1; //Stores the total number of rows

	/*
     * insertRowToTable
     * Insert and reorder
     */
	/*this.insertRowToTable() {
		if (this.tableLoaded) {
			var tbl = document.getElementById(this.tableName);
			var rowToInsertAt = tbl.tBodies[0].rows.length;
			this.addRowToTable(rowToInsertAt);
			this.reorderRows(tbl, rowToInsertAt);
		}
	}*/


	/**
 	 * addRowToTable
 	 * Inserts at row 'num', or appends to the end if no arguments are passed in.
	 * Should not receive empty strings.
     **/
	this.addRowToTable = function(num) {

		/**
		 * Method to create a text cell for for the table
		 **/
		this.addCellText = function(cellNum, txt) {
			var cell = this.row.insertCell(cellNum);
			var textNode = document.createTextNode(txt);
			cell.appendChild(textNode);
			return cell;
		}

		/**
		 * Method to create an input cell for for the table
		 **/
		this.addCellInput = function(cellNum, inputName, value) {
			if (value==null) {
				value = '';
			}
			var cell = this.row.insertCell(cellNum);
			var txtInput = document.createElement('input');
			txtInput.setAttribute('type', 'text');
			txtInput.setAttribute('id', inputName + '_' + (this.iteration-1));
			txtInput.setAttribute('name', 'attendee[' + (this.iteration-1) +']['+inputName+']');
			txtInput.setAttribute('size', '40');
			txtInput.setAttribute('value', value);
			cell.appendChild(txtInput);
			return cell;
		}

		/**
		 * Method to create an anchor cell (text cell with hyperlink) for for the table
		 **/
		this.addCellAnchor = function(cellNum, href, txt, onclickAction) {
	    	var cell = this.row.insertCell(cellNum);
			var anchor = document.createElement('a');
			anchor.setAttribute('href', href);
			anchor.onclick = onclickAction;
			//anchor.textContent = txt;
			var textNode = document.createTextNode(txt);
			anchor.appendChild(textNode);
			cell.appendChild(anchor);
			return cell;
		}

		/**
		 * Constructor
		 **/
		if (this.tableLoaded) {
			var tbl = document.getElementById(this.tableName);
			var nextRow = tbl.tBodies[0].rows.length;

			this.iteration = nextRow + this.rowStartingNum;
			//this.iteration = nextRow + this.rowStartingNum - 1;
			if (num == null) {
				num = nextRow;
			} else {
				this.iteration = num + this.rowStartingNum;
			}

			// add the row
			this.row = tbl.tBodies[0].insertRow(num);
			this.row.className = 'tableRow' + (this.iteration % 2);

			// This section can be configured
			// cell 0 - text
			var cell0 = this.addCellText(0, this.iteration);

			// cell 1 - input text
			var cell1 = this.addCellInput(1, 'attendee_name');

			// cell 2 - input text
			var cell2 = this.addCellInput(2, 'attendee_lastname');

			// cell 3 - input text
			var cell3 = this.addCellInput(3, 'attendee_email');

			// cell 4 - anchor action
			var cell4Action = function() {
				attendeesTable.deleteCurrentRow(this);
				return false;
			}
			var cell4 = this.addCellAnchor(4, '/', 'remove', cell4Action);

			this.rowCount++;
			cost.updateCosts(price);
		}
	}

	/**
	 * Method to delete current row
	 **/
	this.deleteCurrentRow = function(obj) {
		if (this.tableLoaded) {
			var delRow = obj.parentNode.parentNode;
			var tbl = delRow.parentNode.parentNode;
			var rIndex = delRow.sectionRowIndex;
			var rowArray = new Array(delRow);
			this.deleteRows(rowArray);
			this.reorderRows(tbl, rIndex);
		}
	}

    /**
	 * Method to delete several rows
	 **/
	this.deleteRows = function(rowObjArray) {
		if (this.tableLoaded) {
			var rIndex;
			for (var i=0; i<rowObjArray.length; i++) {
				rIndex = rowObjArray[i].sectionRowIndex;
				rowObjArray[i].parentNode.deleteRow(rIndex);
				this.rowCount--;
				cost.updateCosts(price);
			}
		}
	}

    /**
	 * Method to reorder names and some data rows
	 * Useful after a row is deleted or inserted
	 **/
	this.reorderRows = function(tbl, startingIndex) {
		if (this.tableLoaded) {
			if (tbl.tBodies[0].rows[startingIndex]) {
				var count = startingIndex + this.rowStartingNum;
				for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {
					var row = tbl.tBodies[0].rows[i];
					row.className = 'tableRow' + (count%2);
					//This section can be configured
					row.cells[0].firstChild.data = count;
					row.cells[1].firstChild.name = 'attendee['+(count-1)+'][attendee_name]';
					row.cells[2].firstChild.name = 'attendee['+(count-1)+'][attendee_lastname]';
					row.cells[3].firstChild.name = 'attendee['+(count-1)+'][attendee_email]';
					count++;
				}
			}
		}
	}
}

/**
 *
 * @access public
 * @return void
 **/
function costHandler()
{
	this.price;
	this.attendees;
	this.cost;
	this.gst;
	this.total;

	this.updateCosts = function(price) {

		this.price = price;
		this.attendees = attendeesTable.rowCount;
		this.cost = this.attendees*this.price;
		this.gst = this.cost*10/100;
		this.total = this.cost+this.gst;

		var attendees = document.getElementById('attendeesNo');
		attendees.value = this.attendees;

		var cost = document.getElementById('cost');
		cost.value = this.monetaryFormat(this.cost);

		var gst = document.getElementById('gst');
		gst.value = this.monetaryFormat(this.gst);

		var total = document.getElementById('total');
		total.value = this.monetaryFormat(this.total);
	}

	this.monetaryFormat = function(nStr) {
		nStr = nStr.toFixed(2);
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return '$'+(x1 + x2);
	}
}
