﻿function formOnSubmit() {
	//update content of all rt editors
	$('.elrteEditorSelector').each(function () {
		$(this).elrte('updateSource');
	});

	//perform validation
	if(typeof Page_Validators == 'undefined') return;
	hideValidators();

	//declare arrai of validation groups
	var validationGroups = new Array();

	//check each validator
	for(var i = 0; i < Page_Validators.length; i++) {
		var validator = Page_Validators[i];

		if(!validator.isvalid) {
			//highlight invalid control
			$('#' + validator.controltovalidate).addClass('invalidFormInput');

			//get validation group name
			var group = validator.validationGroup ?
				validator.validationGroup : 'validationSummary';

			//push validation group name into array
			if($.inArray(group, validationGroups) < 0) {
				validationGroups.push(group);
			}
		}
	}

	//show validation summaries for all invalid groups
	for(var j = 0; j < validationGroups.length; j++) {
		$('.' + validationGroups[j]).each(function () {
			$(this).slideDown('normal');
		});
	}
}

function hideValidators() {
	//hide all validation summaries
	$('.validationSummary').each(function () {
		$(this).slideUp('normal');
	});

	//hide all invalid controls
	$('.invalidFormInput').each(function () {
		$(this).removeClass('invalidFormInput');
	})
}

function confirmDelete() {
	return confirm('Are you sure you want to delete record?');
}

function rebuildStyles() {
	//styleButtons();
	styleGrids();
}

function styleButtons() {
	$('.cmdAdd').each(function () {
		$(this).button({
			text: true,
			icons: {
				primary: 'ui-icon-plus'
			}
		});
	});

	/*$('.cmdEdit').each(function () {
		$(this).button({
			text: false,
			icons: {
				primary: 'ui-icon-pencil'
			}
		});
	});

	$('.cmdDelete').each(function () {
		$(this).button({
			text: false,
			icons: {
				primary: 'ui-icon-close'
			}
		});
	});

	$('.cmdAccept').each(function () {
		$(this).button({
			text: false,
			icons: {
				primary: 'ui-icon-check'
			}
		});
	});

	$('.cmdCancel').each(function () {
		$(this).button({
			text: false,
			icons: {
				primary: 'ui-icon-close'
			}
		});
	});*/
}

function styleGrids() {
	//var oldColor;
	$('.gridRow').mouseover(function () {
				$(this).addClass('gridRowHover');
	//    oldColor = $(this).css("background-color");
	  //  $(this).css("background-color", "GrayText");
	  //  $(this).css("color", "white");
	});

	$('.gridRow').mouseout(function () {
				$(this).removeClass('gridRowHover');
	//	$(this).css("background-color", oldColor);
//		$(this).css("color", "black");
	});

//	$('.gridRow').unbind('click').click(function () {
//		eval($(this).find('#hidEdit').val());
//	});
		}

		function setAutoComplete(target, emptyText, serviceUrl, minLength, data, callback) {
			if(emptyText) target.emptyText(emptyText);
			var results = null;

			$(target).autocomplete({
				source: function (request, response) {
					if (results != null) {
						response(results);
					} else {
						$.ajax({
							url: serviceUrl,
							data: data(request.term),
							dataType: 'json',
							type: 'POST',
							contentType: 'application/json; charset=utf-8',
							dataFilter: function (data) { return data; },
							success: function (data) {
								results = $.map(data.d, function (item) {
									return { label: item.Item1, value: item.Item2 }
								}
								);
								response(results);
							}
						});
					}
				},

				minLength: minLength,

				select: function (event, ui) {
					$(this).val(ui.item.label);
					callback(ui.item.value);
					return false;
				},

				focus: function (event, ui) {
					$(this).val(ui.item.label);
					return false;
				}
			});

			$(target).click(function () {
				if ($(this).val() == '') return;
				$(this).autocomplete('search', $(this).val().substr(0, minLength));
			});

			$(target).keydown(function () {
				results = null;
			});

			if (minLength == 0) {
				$(target).focus(function () {
					$(this).autocomplete('search', $(this).val().substr(0, minLength));
				});
			}
		}
