jQuery(document).ready(function(){
	// fix ajax form submission for checkboxes with the same name - doesn't change name to use [] like PHP would
	jQuery.ajaxSettings.traditional = true;
	
	// animate nav mouseovers
	/*$('.nav a').mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:500})
		}).mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(0 -156px)"}, {duration:500})
		});*/
	
	//initialize login form toggle link
	$("#link_login").click(function () {
		//console.log("show login form");
		$(".login_form").slideToggle("slow");
		$(this).toggleClass("active");
		return false;
	});
	
	//initialize login form toggle link
	$("#login_above").click(function () {
		//console.log("show login form");
		$(".login_form").toggle();
		//$(this).toggleClass("active");
		return false;
	});
	
	// used to create breadcrumbs
	//$("#breadcrumb").jBreadCrumb();
	
	// set up jQuery UI elements when needed
	try {
		$("#tabs_search").tabs();
		$(".tabs").tabs();
	} catch(e) {
		//expose(e,document.getElementById("debugging_message"))
	}
	
	//modal dialogs in footer
	$(".modal").live("click", function () {
		var dialog_content = this.href;
		var dialog_title = this.title;
		if (!dialog_title || dialog_title == "") {
			dialog_title = $(this).text();
		}
		var modal_flag = "?modal=true";
		if (dialog_content.indexOf("?") >= 0) {
			modal_flag = "&modal=true";
		}
		$("#modal").load(dialog_content + modal_flag, {},
			function() {
				$("#modal").dialog({autoOpen: true, width: 920, modal: true});
				$("#modal").dialog('option', 'title', dialog_title);
				$("#modal").dialog("open");
				
				//initialize functions for modal content if needed - called after content loads
				accountCodeSelectFilterInit();
				formAjaxInit();
			});
		return false;
	});
	$(".link_modal_close").live("click", function () {
		$("#modal").dialog("close");
		return false;
	});
	
	//load link via ajax call if possible
	$(".link_ajax").live("click", function () {
		$(this).load(this.href);
		return false;
	});
	
	//instruction links
	$(".link_instructions").live("click", function () {
		var link_id = this.id;
		var div_id = link_id.replace("link_", "");
		$("#"+div_id).toggle('slow');
		return false;
	});
	$(".instructions").hide();	
	
	//show confirmation dialog when clicking delete links
	$(".link_delete").live("click", function () {
		return confirm('Are you sure you want to Delete ' + this.title + '\n\nClick OK to Delete or Cancel.')
	});
		
	formHelpersInit();
	formAjaxInit();
	filtersInit();
	accountCodeSelectInit();
	accountCodeSelectFilterInit();
	
	//request details links
	
	$(".link_request_details").live("click", function () {
		$('tr.current').toggleClass("current");								   
		$(this).closest('tr').toggleClass("current");
		var request_ID = this.id.replace("request_", "");
		$("#request_details").html('<p id="ajax_loading">Loading&hellip;<br /><img src="images/icons/loading_bar.gif" alt="" /></p>');
		$("#request_details").load("requests.cfc", 
			{
				method: "request_details",
				request_ID: request_ID
			},
			function() {
				accountCodeSelectInit();
				helpRatingInit();
				formAjaxInit();
			});
		return false;
	});
	
	$(".link_inventory_details").live("click", function () {
		$('tr.current').toggleClass("current");								   
		$(this).closest('tr').toggleClass("current");
		var inventory_ID = this.id.replace("inventory_", "");
		loadInventoryDetails(inventory_ID);
		return false;
	});
	
	//load link via ajax call if possible
	$(".link_confirm").live("click", function () {
		var inventory_ID = this.id.replace("inventory_confirm_", "");
		var confirmed_date = $(this).parent().next();
		confirmed_date.load("inventory.cfc", 
			{
				method: "inventory_location_confirm",
				function_context: "ajax",
				inventory_ID: inventory_ID
			});
		return false;
	});
	
	$(".link_account_code_favorite").click(function () {
		var favorite_account_code_ID = this.id.replace("favorite_account_code_ID_", "");
		
		//console.log("Account Code Favorite Function Fired");
		
		$.ajax({
				type: "POST",
				url: "account_codes.cfc?method=favorite_account_code&function_context=ajax",
				data: {account_code_ID: favorite_account_code_ID},
				success: function() {},
				dataType: "html"
		});
		
		$(this).toggleClass('favorite_active');
		
		return false;
	});
	
	//sortable init for approval steps
	approvalStepsInit();
	
	// help rating stars
	helpRatingInit();
		
	// fix IE CSS hover flicker
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	
});

//****** FUNCTIONS *********************************//
function formHelpersInit() {
	$('.date').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: "yy-mm-dd",
		showButtonPanel: true,
		yearRange: "-70:+10"
	});
	
	//use remit address checkbox in vendor add and edit
	var use_vendor_address = $("#use_vendor_address");
	if (use_vendor_address) {
		$("#use_vendor_address").change(updateRemitAddress).change();
		
		// assign change handlers for vendor address - if use_vendor_address is checked, copy value to remit address
		$("#remit_address").change(updateRemitAddress);
		$("#remit_address2").change(updateRemitAddress);
		$("#remit_city").change(updateRemitAddress);
		$("#remit_state_ID").change(updateRemitAddress);
		$("#remit_zip").change(updateRemitAddress);
	}
	
	//prevent double form submissions on forms with class .submit_once
	$('form.submit_once').preventDoubleSubmit();

	// IE chokes on autogrow function for some reason
	try {
		$('.text_grow').autogrow({
			maxHeight: 500,
			minHeight: 20,
			line_height: 20
		});
	} catch(err) {
  		//Handle IE error here
  	}
}

function filtersInit() {
	$('#report_type').change(function () {
		$("#report_filters").load("reports.cfc?method=reports_form", 
			{	report_type: $("#report_type").val(),
				account_ID: $("#account_ID").val(),
				district_ID: $("#district_ID").val(),
				school_ID: $("#school_ID").val(),
				user_ID: $("#user_ID").val(),
				request_type: $("#request_type").val(),
				spending_type: $("#spending_type").val(),
				dept_type: $("#dept_type").val(),
				conference_type: $("#conference_type").val(),
				reason_type: $("#reason_type").val(),
				approval_step: $("#approval_step").val(),
				category_type: $("#category_type").val(),
				date_start: $("#date_start").val(),
				date_end: $("#date_end").val()
			},
				function() {
					filtersInit();
					formHelpersInit()
					$('#account_ID').change();
				}
		);
		return false;
	});
	$('#account_ID').change(function () {
		$("#district_ID").load("filters.cfc?method=list_districts", 
			{	account_ID: $("#account_ID").val()
			},
				function() {
					$('#district_ID').change();
				}
		);
		return false;
	});
	$('#district_ID').change(function () {
		$("#school_ID").load("filters.cfc?method=list_schools", 
			{	district_ID: $("#district_ID").val()
			},
				function() {
					$('#school_ID').change();
				}
		);
		return false;
	});
	$('#school_ID').change(function () {
		$("#user_ID").load("filters.cfc?method=list_users", 
			{	school_ID: $("#school_ID").val()
			}
		);
		return false;
	});
	
	// filter account codes on account_codes.cfm
	$('#filter_expenditure_function_code_ID').change(function () {							   
		$("#filter_expenditure_object_code_ID").load("account_codes.cfc?method=list_account_code_objects", 
			{	school_ID: $("#school_ID").val(),
				function_ID: $("#filter_expenditure_function_code_ID").val(),
				object_ID: $("#filter_expenditure_object_code_ID").val(),
				account_ID: $("#filter_account_ID").val()
			}
		);
		return false;
	});
	
	// filter account codes on account_codes.cfm
	$('#filter_expenditure_function_code_ID').change(function () {							   
		$("#filter_expenditure_program_code_ID").load("account_codes.cfc?method=list_account_code_programs", 
			{	school_ID: $("#school_ID").val(),
				function_ID: $("#filter_expenditure_function_code_ID").val(),
				//program_ID: $("#expenditure_program_code_ID").val(),
				show_unused: 'false'
			}
		);
		return false;
	});
	
	// update function codes based on selection of fund code in account codes add form on account_code_add.cfm
	$('#add_account_code_fund_ID').change(function () {							   
		$("#add_account_code_function_ID").load("account_codes.cfc?method=list_account_code_functions", 
			{	fund_ID: $("#add_account_code_fund_ID").val(),
				//function_ID: $("#add_account_code_function_ID").val(),
				show_unused: 'true'
			}
		);
		return false;
	});

	// update object codes based on selection of function code in account codes add form on account_code_add.cfm
	$('#add_account_code_function_ID').change(function () {							   
		// load object codes and pass the function id to filter them
		$("#add_account_code_object_ID").load("account_codes.cfc?method=list_account_code_objects", 
			{	function_ID: $("#add_account_code_function_ID").val(),
				//object_ID: $("#add_account_code_object_ID").val(),
				show_unused: 'true'
			}
		);
		// load program codes and pass the function id to filter them
		$("#add_account_code_program_ID").load("account_codes.cfc?method=list_account_code_programs", 
			{	function_ID: $("#add_account_code_function_ID").val(),
				show_unused: 'true'
			}
		);
		return false;
	});
	
	// inventory details location update form
	try {
		$('#location_school_ID').change(function () {
		$("#location_user_ID").load("filters.cfc?method=list_users", 
			{	school_ID: $("#location_school_ID").val()
			}
		);
		return false;
	});
	} catch(e) {}
	
	// create section for help filters if needed, then add change functions
	var filters_help = $('#filters_help');
	if(filters_help.length) {
		//create dom nodes if needed
		var department_ID = $('#department_ID');
		if(!department_ID.length) {
			filters_help.html("<select name='department_ID' id='department_ID'></select><select name='help_topic_ID' id='help_topic_ID'>");
			$("#department_ID").load("filters.cfc?method=list_departments");
		} 
		// add change events
		filtersHelpInit();
	}
}

function filtersHelpInit() {
	$('#request_type').change(function () {
		if($('#request_type').val() == "Help") {
			$("#department_ID").show();
			$("#help_topic_ID").show();
		} else {
			$("#department_ID").hide();
			$("#help_topic_ID").hide();
		}
		return false;
	});
	$('#department_ID').change(function () {
		$("#help_topic_ID").load("filters.cfc?method=list_help_topics", 
			{	department_ID: $("#department_ID").val()
			}
		);
		return false;
	});
	$('#request_type').change();
}

// ajax form submission function, generically handles ajax form submissions
function formAjaxInit() {
	$('.ajax_form').submit(function() {
		var form = $(this);
		var cfc = $(this).children('.ajax_action_cfc').val();
		var method = $(this).children('.ajax_action_method').val();
		var ajax_type = $(this).children('.ajax_action_type').val();	// save or load
		var callback = $(this).children('.ajax_callback').val();	// on success callback function
		var confirm_submit = $(this).children('.ajax_confirm_submit').val();	// show confirm alert before continuing
		var confirmed = true;
		if(confirm_submit && confirm_submit == "true") {
			confirmed = confirm('Are you sure you want to REJECT this request?\n\nClick OK to Reject or Cancel.');
		} 
		//console.log("cfc: " + cfc + " - method: " + method + " - ajax_type: " + ajax_type + " - callback: " + callback);
		if(confirmed == true) {
			if(ajax_type == "save") {
				var form_data = $(this).serialize();
				var ajax_status_icon = form.children('.ajax_status_icon');
				if (ajax_status_icon.length) {
					ajax_status_icon.attr("src", "images/icons/ajax_loader_circle.gif");
					ajax_status_icon.attr("alt", "Loading");
				} else {
					$(this).append('<img src="images/icons/ajax_loader_circle.gif" alt="Loading" class="ajax_status_icon">');
				}
				
				$.ajax({
					type: "POST",
					url: cfc + ".cfc?method=" + method + "&function_context=ajax",
					data: form_data,
					success: function(data) {
						form.children('.ajax_status_icon').attr("src", "images/icons/tick.png");
						form.children('.ajax_status_icon').attr("alt", "Completed");
						if(callback && callback != "") {
							window[callback](data); // use string from hidden form field as callback function
						}
					},
					dataType: "html"
				});
			} else {
				var form_data = $(this).serializeArray();
				var ajax_container = $(this).children('.ajax_container').val();
				//console.log("ajax_container: " + ajax_container);
				//console.log("$(this).serializeArray(): " + $(this).serializeArray());
				//console.log("form_data: " + form_data);
				$(ajax_container).html('<p id="ajax_loading">Loading&hellip;<br /><img src="images/icons/loading_bar.gif" alt="" /></p>');
				$(ajax_container).load(
					cfc + ".cfc?method=" + method + "&function_context=ajax&random=" + Math.random()*99999,
					form_data,
					function(data) {
						if(callback && callback != "") {
							window[callback](data);	// use string from hidden form field as callback function
						} 
					}
				);
			} // end if ajax_type
		} // end if confirmed
		return false;
	});
}

// file upload forms
function uploadInit() {
	
	//file upload
	$('#uploadify').fileUpload ({
		'uploader'  : 'uploadify/uploader.swf',
		'script'    : 'file_upload.cfm',
		'cancelImg' : 'images/icons/cross.png',
		'auto'      : true,
		'folder'    : 'uploads',
		'fileDesc'	: 'Allowed File Types: JPEG, JPG, GIF, PNG, BMP',
		'fileExt'	: '*.jpg;*.gif;*png;*.bmp;',
		'scriptData': {
			'user_ID'	:	$("#user_ID").val()
		},
		'onAllComplete'	: function(event, queueID, fileObj, response, data) {
			$("#resume_file").load("file.cfc", 
				{	method: "file_get",
					user_ID: $("#user_ID").val(),
					size: "medium",
					return_type: "img"
				},
				function() {
					$("#file_upload").toggle('slow');
					$("#link_file_edit").toggle('slow');
					$("#link_file_cancel").toggle('slow');
				}
			);
		},
		'onError' : function(event, queueID, fileObj, errorObj) {
			$("#resume_file").html("span class='highlight'>There was a problem uploading your file. We've been notified and are working on the problem.</span>");
		}
	});
	$('#link_file_edit').click(function () {
		$("#file_upload").toggle('slow');
		$("#link_file_edit").toggle('slow');
		$("#link_file_cancel").toggle('slow');
		return false;
	});
	$('#link_file_cancel').click(function () {
		$("#file_upload").toggle('slow');
		$("#link_file_edit").toggle('slow');
		$("#link_file_cancel").toggle('slow');
		return false;
	});

	$("#file_upload").hide();
	$("#link_file_cancel").hide();
		
}

function helpRatingInit() {
	//try {
		$("#help_rating_status").hide();
		$("#help_rating_submit").hide();
		// Create stars
		$("#help_rating_stars").stars({
			inputType: "select",
			callback: function(ui, type, value) {
				var help_request_ID = $("#help_request_ID").val();
				$("#help_rating_status").show().html('<img src="images/icons/ajax_loader_circle.gif" alt="Loading" class="ajax_status_icon"> Saving');
				$.ajax({
					type: "POST",
					url: "requests.cfc?method=help_rating_update&function_context=ajax&random=" + Math.random()*99999,
					data: {
					   request_ID: help_request_ID, 
					   rating: value
					},
					success: function() {
						$("#help_rating_status").html('<img src="images/icons/tick.png" alt="" class="ajax_status_icon"> Saved');
					},
					error: function() {
						$("#help_rating_status").html('<img src="images/icons/error.png" alt="" class="ajax_status_icon"> Not Saved');
					},
					dataType: "html"
				});
			},
			cancelShow: false
		});
	/*} catch(e) {
		txt="There was an error on this page in helpRatingInit.\n\n";
		txt+="Error description: " + e.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);
	}*/
}

//select account code in modal dialog
function accountCodeSelectInit() {
	try {
		//replace form with link and text
		$(".update_account_code_form").each(function() {
			var update_account_code_form = $(this);
			var school_ID = update_account_code_form.find('[name=school_ID]').val();
			var request_ID = update_account_code_form.find('[name=request_ID]').val();
			var item_ID = update_account_code_form.find('[name=item_ID]').val();
			var item_total = update_account_code_form.find('[name=item_total]').val();
			var current_check_number = update_account_code_form.find('[name=current_check_number]').val();
			var current_credit_card_ID = update_account_code_form.find('[name=current_credit_card_ID]').val();
			var new_credit_card_ID = update_account_code_form.find('[name=new_credit_card_ID]').val();
			var credit_card = update_account_code_form.find('[name=new_credit_card_ID] option:selected').text();
			var current_account_code_ID = update_account_code_form.find('[name=current_account_code_ID]').val();
			var new_account_code_ID = update_account_code_form.find('[name=new_account_code_ID]').val();
			var current_account_code_name = update_account_code_form.find('[name=new_account_code_ID] option:selected').text();
			var credit_card_name = update_account_code_form.find('[name=new_credit_card_ID] option:selected').text();
			var credit_card_type = update_account_code_form.find('[name=credit_card_type]').val();
			var credit_card_number = update_account_code_form.find('[name=credit_card_number]').val();
			var missing_payment_info = "";
			
			//console.log('school_ID: ' + school_ID);
			//console.log('request_ID: ' + request_ID);
			//console.log('item_ID: ' + item_ID);
			//console.log('item_total: ' + item_total);
//			console.log('current_check_number: ' + current_check_number);
//			console.log('current_credit_card_ID: ' + current_credit_card_ID);
//			console.log('credit_card_type: ' + credit_card_type);
//			console.log('credit_card_number: ' + credit_card_number);
//			console.log('current_account_code_ID: ' + current_account_code_ID);
//			console.log('current_account_code_name: ' + current_account_code_name);
//			console.log('new_account_code_ID: ' + new_account_code_ID);
//			console.log('credit_card: ' + credit_card);
			
			var link_edit = "<br /><a href='account_codes_forms.cfc?method=select_account_code&request_ID=" + request_ID + "&school_ID=" + school_ID + "&item_ID=" + item_ID + "&item_total="  + item_total + "&current_check_number=" + current_check_number + "&account_code_ID=" + current_account_code_ID + "&current_credit_card_ID=" + current_credit_card_ID + "' class='link_edit modal'>Update Account Code &amp; Check Number</a>";
						
			// update text based on whether specified
			if(current_account_code_name != "Choose Funding Account") {
				current_account_code_name = "Account Code: " + current_account_code_name;
			} else if (current_account_code_name == "Choose Funding Account" && item_ID == "0") {
				current_account_code_name = "Choose Funding Account - All Items";
			}
			if(current_check_number != "") {
				current_check_number = "<br />Check Number: " + current_check_number;
			}
			if(current_credit_card_ID != 0) {
				credit_card = "<br />Credit Card: " + credit_card_type + " - " + credit_card_number;
			} else {
				credit_card = "";	
			}
			if(current_check_number == "" && current_credit_card_ID == 0) {
				missing_payment_info = "<br />No Payment Information Attached";
			}
			// add info in place of form
			update_account_code_form.after(current_account_code_name + current_check_number + credit_card + missing_payment_info + link_edit).remove();
		});
		// attach click event to link
		
		$("#filter_account_codes_request_details").hide();
	
	} catch(e) {
		txt="There was an error on this page in accountCodeSelectInit\n\n";
		txt+="Error description: " + e.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);
	}
}

//filter account code in modal dialog
function accountCodeSelectFilterInit() {
	try {
		//on submit of filters form catch submission and pass variables to cfc and keep modal open
		$("#form_filter_account_codes").submit(function() {
			//console.log("filtering account codes");
			var form_filter_account_codes = $(this);
			var expenditure_function_code_ID = form_filter_account_codes.find('[name=expenditure_function_code_ID]').val();
			var expenditure_object_code_ID = form_filter_account_codes.find('[name=expenditure_object_code_ID]').val();
			var school_year = form_filter_account_codes.find('[name=school_year]').val();
			var favorites = form_filter_account_codes.find('[name=favorites]').val();
			
			//modal dialogs in footer			
			var dialog_content = form_filter_account_codes.attr("action") + "&expenditure_function_code_ID=" + expenditure_function_code_ID + "&expenditure_object_code_ID=" + expenditure_object_code_ID + "&favorites=" + favorites + "&school_year=" + school_year;
			var dialog_title = "Update Account Code";
			var modal_flag = "&modal=true";
			$("#modal").load(dialog_content + modal_flag, {},
				function() {
					$("#modal").dialog({autoOpen: true, width: 920, modal: true});
					$("#modal").dialog('option', 'title', dialog_title);
					$("#modal").dialog("open");
					
					//initialize functions for modal content if needed - called after content loads
					accountCodeSelectFilterInit();
					formAjaxInit();
				});
			
			return false;
		});
		// .submit
	
	} catch(e) {
		txt="There was an error on this page in accountCodeSelectFilterInit.\n\n";
		txt+="Error description: " + e.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);
	}
}

//callback to close model and refresh request detials on account code save in modal dialog, data = response from server (Success, Validation Error)
function accountCodeSelectSaveCallback(data) {
	try {
		//console.log("called account code update. Response = " + data);
		if($.trim(data) == "Success") {
			$("#modal").dialog("close");
			//old method for pulling request_ID, but doesn't work on request.cfm. Needs to work on request.cfm because emailed requests link to request.cfm instead of requests.cfm
			//var request_ID = $("tr.current .link_request_details").attr("id").replace("request_", "");
			
			// Updated the way we pull the request_ID for calling back request details because the .link_request_details isn't there on request.cfm
			var request_ID = $("#account_code_update_request_ID").val();
			
			$("#request_details").html('<p id="ajax_loading">Loading&hellip;<br /><img src="images/icons/loading_bar.gif" alt="" /></p>');
			$("#request_details").load("requests.cfc", 
				{
					method: "request_details",
					request_ID: request_ID
				},
				function() {
					accountCodeSelectInit();
					helpRatingInit();
				});
		} else {
			// shows validation error
			//console.log("updating bulk_update_account_code_modal_status = " + data);
			$(".ajax_status_icon").remove();
			$("#bulk_update_account_code_modal_status").html(data);	
		}
	} catch(e) {
		txt="There was an error on this page in accountCodeSelectSaveCallback.\n\n";
		txt+="Error description: " + e.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);
	}
}

function approvalStepsInit() {
	try {
		$("#item_list").sortable({ 
			handle : '.item_name', 
			update : function () { 
				var order = $('#item_list').sortable('serialize'); 
				var order = order.replace(/\[\]/g, "")	// replace [] with "" globally in string
				//console.log("reordered steps: " + order);
				$("#info").load("approval_steps.cfc?method=reorder&"+order); 
			} 
		});	
		
		// handle changing final approval step
		$(".approval_step_final_approval").change(function () {
			var approval_step_ID = $(this).val();												
			$("#info").load("approval_steps.cfc?method=final_approval_update&approval_step_ID="+approval_step_ID);										
		});
	} catch(e) {
		//console.log("sortable error: " + e);
	}
}

function loadInventoryDetails(inventory_ID) {
	// Set the optional inventory_ID parameter if needed
	if (inventory_ID != undefined ) {
		$("#inventory_details").html('<p id="ajax_loading">Loading&hellip;<br /><img src="images/icons/loading_bar.gif" alt="" /></p>');
		$("#inventory_details").load("inventory.cfc", 
			{
				method: "inventory_details",
				inventory_ID: inventory_ID
			},
			function() {
				inventoryDetailsInit();
			});	
	} else {
		// just reinit functions as a callback
		inventoryDetailsInit();
	}
}

// called after loading inventory details content
function inventoryDetailsInit() {
	filtersInit();	
	formAjaxInit();
}


// update select based on chosen category
function updateRemitAddress() {
	if ($("#use_vendor_address").is(":checked")) {
		$("#remit_attention").val($("#attention").val());
		$("#remit_address").val($("#address").val());
		$("#remit_address2").val($("#address2").val());
		$("#remit_city").val($("#city").val());
		$("#remit_state_ID").val($("#state_ID").val());
		$("#remit_zip").val($("#zip").val());
		
		//console.log("checkbox value:" + $("#use_vendor_address").val());
		//console.log("check box is checked")
		
		$("#remit_attention").attr("disabled","disabled");
		$("#remit_address").attr("disabled","disabled");
		$("#remit_address2").attr("disabled","disabled");
		$("#remit_city").attr("disabled","disabled");
		$("#remit_state_ID").attr("disabled","disabled");
		$("#remit_zip").attr("disabled","disabled");
	} else {
		$("#remit_attention").removeAttr("disabled");
		$("#remit_address").removeAttr("disabled");
		$("#remit_address2").removeAttr("disabled");
		$("#remit_city").removeAttr("disabled");
		$("#remit_state_ID").removeAttr("disabled");
		$("#remit_zip").removeAttr("disabled");
		
		//console.log("checkbox value:" + $("#use_vendor_address").val());
		//console.log("check box is not checked")
	}
}

//limit the number of characters in a textarea
function limitChars(textid, limit, infodiv) {
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit) {
		$('#' + infodiv).html('You cannot write more then '+limit+' characters.');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	} else {
		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	}
}

jQuery.fn.slowEach = function(interval, callback, callback2) {
	var items = this, i = 0;
	if(!items.length) return;
	function next() {
		(callback.call(items[i], i, items[i]) !== false && ++i < items.length) ? setTimeout(next, interval) : callback2 && callback2.call(items, i, items);
	}
	next();
};

//prevent double form submissions
jQuery.fn.preventDoubleSubmit = function() {
	jQuery(this).submit(function() {
		if (this.been_submitted) {
			return false;
		} else {
			this.been_submitted = true;
		}
	});
};

