var rowCounter;
//JQuery extentions
(function ($) {
		
$.fn.dintable = function(options) {
	var options = $.extend({}, $.fn.dintable.defaults, options);
	
	return this.each(function () {
		var table = this;
		table.dintable = options;
		table.currentRowNumber = 0;
		addButton = $(this).find('.'+options.addButton);
		addButton.each(function (){ this.dintable = table});
		addButton.click(function(){return $.fn.dintable.addRow(this);}); //function() {console.log(this)}
	});
}


//Add button onclick handler
$.fn.dintable.addRow = function(button) {
	$table     = button.dintable; // - pointer to the table, witch contains (this) button
	$tableRow  = $table.dintable.row;             // - pointer to the row, witch should be added to the table
	$callback  = $table.dintable.callback;
	//Now all cells with dinamic data (like "name" attribute) should be set up
    var index = $.fn.dintable.__getNextRowNumber($table);
	if ($.fn.dintable.__canAdd($table)) {
		var result = $($table).append($tableRow);
		// replace '%d' with indexes
		$('.to_replace', result)
			.each(function () {
				$(this).attr({'name' : $(this).attr('name').replace('%d', index)});
			})
			.removeClass('to_replace');
		$.fn.dintable.__assignRemoveButton($table);
		//if callback is defined - call it
		if ($callback) {
			$callback();
		}
		return result;
	} else {
		return $($table);		
	}
};

//Remove button onclick handler
$.fn.dintable.removeRow = function(button) {
	$tableRow = button.dintable;
	$($tableRow).remove();
}

//Check, if new row can be added in table.
$.fn.dintable.__canAdd = function(table) {
	var canAdd = true;
	//Check, if row count of row in table doesn't exceed maxium available value
	if (canAdd) {
		if ((table.dintable.maxRowCount !=0) && ($('tr', table).length > table.dintable.maxRowCount)) {
			canAdd = false;	
		}
	}
	
	return canAdd;	
}

$.fn.dintable.__assignRemoveButton = function(table) {
	$('.'+table.dintable.removeButton+':last', table).each(function () {
		$(this).unbind('click');
		$(this).click(function (){return $.fn.dintable.removeRow(this)});
		this.dintable = $('tr:last',table)[0];
	});
}

$.fn.dintable.__getNextRowNumber = function (table) {
	return (table.currentRowNumber++);
}

$.fn.dintable.defaults = 
	{
		'addButton' : "addButton",
		'removeButton' : "removeButton",
		'maxRowCount'  : 5,
		'callback'     : null
	};
}) (jQuery)

function application() {
	//Initialization for dinamik table (add, remove rows)
	this.init_din_table = function(tableName, tableRow, callback) {
		$(document).ready(function () {
			$('#'+tableName).dintable({
				'row' : tableRow,
				'callback' : callback 
			});
		});	
	};
	
	//Initialization for on submit file types check
	this.init_file_input = function(formId, classId, extentions) {
		function ckeckFileExt(classId) {
			var allowSubmit = true;
			$('.'+classId).each(function(num) {
				file = this.value;
				rightFile = false;
				if (!file) {
					rightFile=false;
				} else {
					ext = file.match(/\.[\w]*$/)[0];
					if (typeof ext == 'string')  {
						ext = ext.toLowerCase();	
					}
					for (var i = 0; i < extentions.length; i++) {
						if (extentions[i] == ext) { rightFile = true; break; }
					}
				}
				if (rightFile) {
					allowSubmit = allowSubmit && true; 
				} else {
					num++;
					alert("File #"+num+" has illegal type. Please only upload files that end in types:  " 
					+ (extentions.join("  ")) + "\nPlease select a new "
					+ "file to upload and submit again.");
					allowSubmit =  allowSubmit && false;
				}
				return rightFile;
			});
			return allowSubmit;
		};
		$(document).ready(function (){
			$('form#'+formId).submit(function() {
				return ckeckFileExt(classId);
			});
		});
	};
	//Initialization for form inputs validation
	this.init_form_validation = function () {
		$.validator.addMethod('atleast', function(value, element) {
			return !(($('#RequestCellphone').val() == '') && ($('#RequestPhone').val() == ''));
		}, $.validator.messages['atleast']);
		$.validator.addMethod('atrequest', function(value, element) {
			return !(($('#RequestInvestigationCellphone').val() == '') && ($('#RequestInvestigationPhone').val() == ''));
		}, $.validator.messages['atleast']);
		$.validator.addMethod('atinvoice', function(value, element) {
			return !(($('#RequestInvoicePhone').val() == '') && ($('#RequestInvoiceCellphone').val() == ''));
		}, $.validator.messages['atleast']);
	}
}

App = new application;
