function removeSpecialCharacters (str) {
	  var re = /\$|\u00A3|\u20AC|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
	  return str.replace(re, "");
}
function addCommas(str) {
	str += '';
	x = str.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;
}
Homes = window.Homes || {};

Homes.tableManipulation = function(id) {

	var table = $,
		arr = [],
		arrX = 0,
		arrY = 0,
		i = 0,
		dynamicColId = 'JS_col',
		dynamicRow = $('<tr />')
			.addClass('dynamicRow'),
		dynamicSelect = $('<select />')
			.append($('<option />')
					.text('Select price')
					.attr('value', '0')
			),
		dynamicClassName = 'hasJS';
	if ((typeof id !== 'undefined') && (id !== undefined)) {
		table = $('#' + id);
	}
	if (table.length > 0) {
		$('tbody tr', table).each(function(i) {

			arr[i] = [];
			arrY = i + 1;
			$('td', $(this)).each(function(j) {
				arr[i][j] = $(this).text();
				arrX = j + 1;
			});
		});



		for (i = 1; i <= arrX; i++) {
			dynamicRow
				.append(
					$('<td />')
					.attr('id', dynamicColId + i)
					.text('')
				);
		}
		dynamicSelect
			.bind('change', function() {

				var currentIndex = parseInt($(':selected', this).val(), 10);

				if (currentIndex > 0) {
					$('#explain_' + dynamicColId + '1').text(arr[currentIndex - 1][0]);
					var typicalDeposit = arr[currentIndex - 1][1].replace("only ", "");
					var ourDeposit = arr[currentIndex - 1][2].replace("only ", "");
					var stampDuty = arr[currentIndex - 1][3].replace("only ", "");
					$('#explain_' + dynamicColId + '2').text(typicalDeposit);
					$('#explain_' + dynamicColId + '3').text(ourDeposit);
					$('#explain_' + dynamicColId + '4').text(stampDuty);
					for (var i = 2; i <= arrX; i++) {
						$('#' + dynamicColId + i).text(arr[currentIndex - 1][i - 1]);
					}
				} else {
					$('#explain_' + dynamicColId + '1').text(arr[0][0].replace("only ", ""));
					for (var i = 2; i <= arrX; i++) {
						$('#' + dynamicColId + i).text('');
						$('#explain_' + dynamicColId + i).text(arr[0][i - 1].replace("only ", ""));
					}

				}
				var url = parseInt(removeSpecialCharacters(arr[currentIndex - 1][0].replace("only ", "")), 10) / 1000;
				var siteName = 'Barratt_Homes';
				if (window.location.href.indexOf("dwh.") > 0) { siteName = 'David_Wilson_Homes'; }
				if (window.location.href.indexOf("ward.") > 0) { siteName = 'Ward_Homes'; }
				dcsMultiTrack('DCS.dcsuri', '/Campaigns/first-time-buyer/deposit_calculator/price/' + url, 'WT.ti', 'Setting out - Adjust deposit calculator', 'WT.cg_n', 'Campaigns', 'WT.cg_s', 'Setting Out', 'WT.sp', siteName);

			});
		for (i = 1; i <= arrY; i++) {
			dynamicSelect
				.append(
					$('<option />')
					.attr('value', i)
					.text(arr[i - 1][0])
				);
		}
		$('#' + dynamicColId + '1', dynamicRow)
			.append(dynamicSelect);
		$('body')
			.addClass(dynamicClassName);
		$('tbody', table)
			.append(dynamicRow);
	}
};
$(document).ready(function() {  
	Homes.tableManipulation('mortageDepositRequirements');

});

