// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
	var popup;
function ajax_dropdown(url, options) {
	options = $H({window: true, title: '', width: 400, height: 400, indicator: 'indicator', confirm: null}).merge(options).toObject();
	options.parameters.my_id = $(options.parameters.my_box).value;

	if ($(options.parameters.my_box).value != "" || url.indexOf("new") !== -1) {
  	if(options.confirm == null || (options.confirm && confirm(options.confirm))){
	  	if (options.window) {
		    if (popup) { popup.close(); };
			  popup = new Window({className: 'hoegger',
			  title: options.title,
			  width: options.width,
			  height: options.height,
			  destroyOnClose: true,
			  minimizable: false,
			  maximizable: false,
			  showEffectOptions: {duration: 0.5}});
			  popup.setAjaxContent(url,
		  			{parameters: options.parameters,
			  		asynchronous:true, evalScripts:true, 
				  	onComplete: function(request){Element.hide(options.indicator);}, 
			  		onLoading:function(request){Element.show(options.indicator);}});
		  			popup.setZIndex(25000);
	  				popup.showCenter();
  	 } else {
	  		new Ajax.Request(url,
		  		{parameters: options.parameters,
			 			asynchronous:true, evalScripts:true, 
			  		onComplete: function(request){Element.hide(options.indicator);}, 
				  	onLoading:function(request){Element.show(options.indicator);}});
			  	}
			 	return false;
		}
	}
}

function is_blank_date(value) {
  if(value == 'mm/dd/yyyy' || value == 'mm-dd-yyyy' || value == '') {
    return true;
  } else {
    return false;
  }
}

//Accepts date strings in the format: mm/dd/yyyy or mm-dd-yyyy
function days_between(from,to) {
  var from_parts = from.split(/[\/\-]/);
  var to_parts   = to.split(/[\/\-]/);
  var from_date = new Date();
  var to_date   = new Date();
  
  from_date.setMonth(from_parts[0]-1);
  from_date.setDate(from_parts[1]);
  from_date.setYear(from_parts[2]);
  
  to_date.setMonth(to_parts[0]-1);
  to_date.setDate(to_parts[1]);
  to_date.setYear(to_parts[2]);
  
  var day = 1000*60*60*24;
  var days = Math.ceil((to_date.getTime()-from_date.getTime())/day)+1;
  return (isNaN(days) ? '' : days);
}

// For dynamic list forms (subcontractor, equipment, etc)
// we want to show the previous add button upon removal,
// but the previous button may not be the previous index
// because we may have deleted that index.  This finds
// the first previous index that's available and shows it.
function show_prev_add(form,id,count) {
  //Only do this if we're on the last row (where the add button is visible)
  var add = form + '_add_' + id + '_' + count;
  if($(add)!=null && $(add).visible()) {
    for(var i = count-1; i >= 0; i--) {
      var e = form + '_add_' + id + '_' + i;
      if ($(e)) { $(e).show(); return; }
    }
  }
}
Array.prototype.sum = function(){
	for(var i=0,sum=0;i<this.length;sum+=this[i++]);
	return sum;
}
Array.prototype.max = function(){
	return Math.max.apply({},this)
}
Array.prototype.min = function(){
	return Math.min.apply({},this)
}

function toggle_sorters(selected_sorter,arr){
	for(i=0;i<arr.size();i++){
		$(arr[i]+"_desc_off").show();
		$(arr[i]+"_desc_on").hide();
		$(arr[i]+"_asc_off").show();
		$(arr[i]+"_asc_on").hide();
	}
	$(selected_sorter+"_on").show();
	$(selected_sorter+"_off").hide();


}
function toggle_goto_menu(id) {
 $('project_goto_'+id, 'goto_selector_container_'+id).invoke('toggle');
}
function unauthorized(){
	alert('You are unauthorized to view this page.');
}
function set_job_report_inprogress_days(id) {
  id = id || '';
  var posted_on = $('jobreport___posted_on_'+id).innerHTML;
  if(!is_blank_date(posted_on)) {
    $('jobreport_day_'+id).update(days_between($('project_start_date_'+id).innerHTML, posted_on));
  }
}