$(document).ready(function() {		
	$('.delete_btn').bind('click', function() {
		var mid = $('#iid').val();
		if(mid == '') {
			callDialog("<p>Please select object(s) to delete.</p>", 270)
		} else {
			$("#dialog").html("<p>Are you sure you want to delete this object(s)?</p>");
			$( "#dialog" ).dialog({
				modal:true,
				resizable: false,
				width: 350,
				buttons: {
					"Yes": function() {
						$("#form").submit();
					},
					"No": function() {
						$( this ).dialog( "close" );
					}
				}
			});
		}
	});
	
	$("#save_btn").bind('click', function() {
		$('#action_container').removeClass("action_container_0 action_container_1").addClass("action_container");
		$('#alert').html('');
		var validator = 0;
		$('.required_container').remove();
		$('.required').each(function() {
			if($(this).val() == "") {
				var w = $(this).attr('id');
				validator = validator + 1;
				var x = $(this).parent().html();
				$(this).parent().html(x + '<div class="required_container">Required!</div>');
			}
		});
		if(validator == 0) {
			$('#myform').submit();
		}
	});
});

function updateTextArea() {
	var allVals = [];
	$('#dataGrid :checked').each(function() {
		allVals.push($(this).val());
	});
	$('#iid').val(allVals);
}
	
function submitForm(myform) {
	$('#action_container').removeClass("action_container_0 action_container_1").addClass("action_container");
	$('#alert').html('');
	var validator = 0;
	$('.required_container').remove();
	$('.required').each(function() {
		if($(this).val() == "") {
			validator = validator + 1;
			var x = $(this).parent().html();
			$(this).parent().html(x + '<div class="required_container">Required!</div>');
		}
	});
	if(validator == 0) {
		return true;
	}	
	return false;
}

function onFocus(element) {
	if (element.value == element.defaultValue) {
		element.value = '';
	}
}

function onBlur(element) {
	if (element.value == '') {
		element.value = element.defaultValue;
	}
} 
	
function callDialog(msg, width) {
	$("#dialog").html(msg);
	$( "#dialog" ).dialog({
		modal:true,
		resizable: false,
		width: width,
		buttons: {
			"Ok": function() {
				$( this ).dialog( "close" );
			}
		}
	});
}
