//2007-05-01, Sunny Walker, miraclesalad.com
(function($){
$(document).ready(function(){
	$('#group3').bind('submit',function(){return false});
	//macros
	$('#buttons').before('<input type="button" id="save" value="Save mask to button" />');
	$('#save').click(function(){
		if ($('#mask').val().length==0) return;
		var id=$('#buttons input').length+1;
		var name=hsc($('#mask').val().substring(0,12));
		var title=hsc($('#mask').val()).replace(/&/g,'&amp;');
		if (name.length < title.length) name += '&hellip;';
		$('#buttons').append('<input type="button" id="macro-'+id+'" value="'+name+'" title="'+title+'" class="macro" />');
		$('#macro-'+id).bind('click',function(){
			$('#mask').val(title.replace(/&quot;/g,'"').replace(/&lt;/g,'<').replace(/&gt;/g,'>'));
			checkSave();
		})
		.Tooltip({delay: 0, showURL: false, extraClass: 'code'});
	});
	$('#mask').bind('keydown',checkSave).bind('keyup',checkSave).bind('change',checkSave);
	checkSave();
	//add a sample
	$('#tooltext').append('<p><a id="samples" href="javascript:void(0)">Load sample data</a> to see it in action.</p>');
	$('#samples').bind('click',function(){
		$('#list').val("first_name\nlast_name\nemail\nphone");
		$('#mask').val("validateNotEmpty('{{LINE}}','{{LINE}} cannot be blank.'); //test #{{LINENUMBER}}");
		$('#save').removeAttr('disabled');
		$('#mode').val('ajax');
		$('#form1').submit();
		return false;
	});
	var opt = {
		beforeSubmit: function(formArray, jqForm) {
			$('#working').show();
			$('#Submit').attr('disabled','true');
			$('#mode').val('ajax');
		},
		success: function(data){
			$('#Submit').removeAttr('disabled');
			$('#working').hide();
			$('#results').val(data.replace(/\\'/g,"'").replace(/\\"/g,'"'));
			$('#mask').focus();
		}
	};
	//$('#Submit').bind('click',function(){$('#form1').ajaxSubmit(opt);return false;});
	$('#form1').ajaxForm(opt);
});
function checkSave(){
	if ($('#mask').val().length==0) $('#save').attr('disabled','true');
	else $('#save').removeAttr('disabled');
}
function hsc(t){return t.replace(/"/g,'&quot;').replace(/</g,'&lt;').replace(/>/g,'&gt;')}
})(jQuery);

