jQuery(function(){
	jQuery('input[type=submit]').removeAttr('disabled');
	jQuery('form').submit(function(){
		if ( typeof isValidCWContactForm == 'undefined' || isValidCWContactForm )
			jQuery('input[type=submit]:not(.addParagraph,.ResetFlow2,.clear,[name=SignUp],.cw_submitLink,#cwLegSearchFormSubmit,#printBill,#cwMediaSearchFormSubmit,#submitBillSearch)', this).attr('disabled', true);
		else
			jQuery('input[type=submit]').removeAttr('disabled');
	});
	
	if (!FlashDetect.installed){
		jQuery('.cw_showFlash').hide();
		jQuery('.cw_noFlash').show();
	}
	//$('.issueSubContent').corners();
});

//Controls paragraph selection
function cwInsertParagraphSelector(siteID,paragraphID,field){
	var range = jQuery('textarea[name=' + field + ']').getSelection();
	var startContent = jQuery('textarea[name=' + field + ']').val();
	var endContent = jQuery('textarea[name=' + field + ']').val(); 
	var textID = '#cwParagraphSelector' + paragraphID;
	var paragraphContent = jQuery(textID).val();
	var newContent = '';
	
	if ( range.start == startContent.length && range.start != 0 )
		paragraphContent = '\n\n' + paragraphContent;
	
	startContent = startContent.substring(0, range.start);
	endContent = endContent.substring(range.start, endContent.length);
	
	newContent = startContent + paragraphContent + endContent;
	
	jQuery.post(cw_rootPath + '\/index.cfm?siteid=' + siteID + '&action=TakeAction.AppendParagraphSelected',{
		'paragraphID' : paragraphID,
		'field' : field,
		'newContent' : newContent
	},function(response){
		jQuery('textarea[name=' + response.CONTENT.FIELD + ']').val(response.CONTENT.NEWCONTENT);
	},'json');
	
	return false;
}

function enableSubmit(){
	jQuery('#congressweb form input[type=submit]:not(.addParagraph)').removeAttr('disabled');
}

var cw_app = {
	collectInputs: function(name){
		var litems = new Array();
		jQuery('input[name=' + name + ']').each(function(){
			if ( jQuery(this).val().length )
				litems.push(jQuery(this).val());
		});
		litems = litems.join(',');
		return litems;
	},
	
	checkFieldLen: function(field,label){
		var currVal = jQuery('#cw_' + field).val();
		if (currVal.length)
			return true;
		else {
			jQuery('#cw_' + field + 'Error').html(label + ' is required');
			enableSubmit();
			return false;
		}
	},
	
	submitSelectedState: function(){
		if ( jQuery('#congressweb_content select[name=selectedState] option:selected').val().length )
			jQuery('.cwebcontent_stateselector').submit();
	}
}

