// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function setClass(ele,className) {
   if ( ( className != '' ) &&  !hasClassName(ele,className) ) {
      addClass(ele,className)
   }
}

function hasClassName(ele,className) {
   if (ele.className.indexOf(className) > -1) {
     return true;
   }
   return false;
}

// Add a Class to an HTML element
function addClass(ele,className) {
   ele.className += ' ' + className
}

// Remove Class from an HTML element
function removeClass(ele,className) {
   ele.className = ele.className.replace(new RegExp(className),"");
}

function switchClass(ele,oldClass,newClass) {
   removeClass(ele,oldClass)   
   setClass(ele,newClass)
}

function date2us(date) {
    if ( !date ) { return null }
    var month = date.getMonth() + 1; // js starts counting at zero
    return date.getFullYear() + '-' + month + '-' + date.getDate();
}

function add_months_to_date(start_date,months) {
	months = parseInt(months)
	var new_date   = start_date

	if ( start_date.getMonth()+months <= 11 ) {
		new_date.setMonth(start_date.getMonth()+months)
	} else {
		new_date.setMonth( (start_date.getMonth()+months%12) - 12)
		new_date.setFullYear(new_date.getFullYear()+parseInt((12+months)/12)) 
	}
	return new_date
}


function month_diff_end_date_start_date(start_date,end_date) {
	if ( start_date.getFullYear() == end_date.getFullYear() ) {
		return end_date.getMonth()-start_date.getMonth();
	} else {
		return 12-start_date.getMonth()+end_date.getMonth();
	}
}

// feed this function a us date: yyyy-mm-dd
function new_date_object(us_date) {
	var date_array = us_date.split("-")
   return new Date(parseInt(date_array[0]),parseInt(date_array[1]-1),parseInt(date_array[2]));
}

// check if end_date > start_date
function compare_dates(start_date, end_date) {
	var start_year  = start_date.getFullYear()
	var start_month = start_date.getMonth()
	var start_day   = start_date.getDate()

	var end_year  = end_date.getFullYear()
	var end_month = end_date.getMonth()
	var end_day   = end_date.getDate()

   if ( ( end_year > start_year ) ||
        ( ( end_year == start_year ) && (end_month > start_month) ) ||
        ( ( end_year == start_year ) && ( end_month == start_month ) && ( end_day > start_day ) ) ) {
	   return true
   } else {
	   return false
   }
}