/**
 * SelectionList = JS object.
 */

var SelectionList = {
	_COOKIE_NAME : "selection_list",
	rows : [], // array of all rows in Selection List
		
	/**
	 * initialize
	 */
	init : function() {
		if(this.loadCookie()) {
			this.renderSelectionListHTML();
		}
	},	
	
	/**
	 * update selection list
	 */
	update : function(id,name,value,replace_row) {
		if(this.rows[id] == null || replace_row == true) {
			if(name.length > 0 && value.length > 0) {
				this.rows[id] = new Array(name,value);
			}
		}
		else {
			delete this.rows[id];
		}
		this.renderSelectionListHTML();
		this.saveCookie();
	},
	
	/**
	 * load the cookie
	 */
	loadCookie : function() {
		var cookie_content = Cookie.read(this._COOKIE_NAME );
		if(cookie_content == null) {
			return false;
		}

		// cookies can't have semi-colons, so they were replaced when saving the cookie. 
		cookie_content = cookie_content.replace(/SEMICOLON/g,';');
		this.rows = unserialize(cookie_content);	
		
		for(row in this.rows) {
			var checkbox = document.getElementById(row); 
			if(checkbox != null) {
				checkbox.checked = true;
			}
		}
		return true;
	},

	/**
	 * save the cookie.
	 */
	saveCookie : function() {
		var selection_list_string = serialize(this.rows);
		// cookies can't have semi-colons, so replace them.
		selection_list_string = selection_list_string.replace(/\;/g,'SEMICOLON');
		Cookie.create(this._COOKIE_NAME,selection_list_string,365);
	},

	/**
	 * Render the SelectionList HTML.
	 */
	renderSelectionListHTML : function() {
	
		table_html  = '<div id="selection_list_placeholder"><table id="selection_list_table" border="0" cellpadding="4" cellspacing="0" width="100%">';

		for(row in this.rows) {
			if(this.rows[row][0] != null) {
				table_html += '<tr>';
				table_html += '<td>' + this.rows[row][0] + '</td>';
				table_html += '<td align="right">' + this.rows[row][1] + '</td>';
				table_html += '</tr>';
			}
		}
		
		table_html += '</table></div>';
		
		// Update table.
		$('selection_list_placeholder').replace(table_html);
	},
	
	
	calculatePoolMeasurements : function(formObject) {
		var pw = parseFloat(formObject.pool_width.value);
		var pl = parseFloat(formObject.pool_length.value);
		var sw = parseFloat(formObject.step_width.value);
		var sl = parseFloat(formObject.step_length.value);
		var ts = ((pw * pl) + (sw * sl));

		if(isNaN(ts)) {
			alert('Sorry.\n\n * Please check your length and width sizes. * \n\nYour measurements must be numbers. \nFor example: 2.15');	
			this.update('above_ground','','');
			this.update('pool_size','','');
			this.update('step_size','','');
			this.update('total_size','','');
			return false;
		}
		
		if(formObject.above_ground.checked == true) {
			this.update('above_ground','Is this pool is an above ground pool?','Yes',true);		
		} 
		else {
			this.update('above_ground','Is this pool is an above ground pool?','No',true);			
		}
		this.update('pool_size','Pool Size', pw + 'm x ' + pl + 'm',true);
		this.update('step_size','Step Size', sw + 'm x ' + sl + 'm',true);
		var rts = Math.round(ts*100)/100;
		this.update('total_size','<strong>Total Size</strong>','<strong>' + rts + ' m<sup>2</sup></strong>',true);
		}
}


