﻿
function isIE() {
  if (navigator.appName=="Microsoft Internet Explorer") return true;
  return false;
}

function getEl(el_id) {
  return document.getElementById(el_id);
}

/* ******************** */

function CalendarObj(id,return_function,popup) {
	// arguments set
	this.id = id;
  if (popup===true) {
    this.win_id = id;
  } else {
    this.div_id = id;
  }
  this.return_function = return_function;
  // settings
  this.first_day_offset = 1;
  this.day_names = new Array("Ne","Po","Ut","St","Št","Pi","So");
  this.month_names = new Array("Január","Február","Marec","Apríl","Máj","Jún","Júl","August","September","Október","November","December");
  // other variables
  this.current_date = null;
  this.set_date = null;
  this.html_code = "";
  this.disabled_dates_expression = "";
  // object reference
  if (!window.CalendarObjObjects) window.CalendarObjObjects = new Array();
  window.CalendarObjObjects[id] = this;
}

/**
 * display()
 */ 
CalendarObj.prototype.display = function() {
  // set current date
  if (this.set_date == null) current_date = new Date();
  else current_date = new Date(this.set_date);
  // calculate navigation previous month/year and next month/year
  var prev_month = current_date.getMonth() == 0 ? 12 : current_date.getMonth();
  var prev_year = prev_month == 12 ? current_date.getFullYear()-1 :  current_date.getFullYear();
  var next_month = current_date.getMonth()+1 == 12 ? 1 : current_date.getMonth()+2;
  var next_year = next_month == 1 ? current_date.getFullYear()+1 : current_date.getFullYear();
  // DIV 
  html_code = "<div class=\"cDivCalendar\">";
  // CLOSE ROW
  html_code += "<table class=\"cTblCalendar\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">";
  if (typeof(this.win_id)=="undefined") {
  html_code += "<tr>";
  html_code += "<td class=\"cTdNavigationClose\" colspan=\"7\">";
  html_code += "<a href=\"javascript:CalendarProcessEvent('"+this.id+"')\">&times;</a>";
  html_code += "</td>";
  html_code += "</tr>";
  }
  // NAVIGATION ROW
  html_code += "<tr>";
  html_code += "<td class=\"cTdNavigationPrev\">";
  if (typeof(this.win_id)=="undefined") {
    html_code += "<a href=\"javascript:CalendarProcessEvent('"+this.id+"','"+prev_year+"','"+prev_month+"')\">&laquo;</a>";
  } else {
    html_code += "<a href=\"javascript:window.opener.CalendarProcessEvent('"+this.id+"','"+prev_year+"','"+prev_month+"')\">&laquo;</a>";
  }  
  html_code += "</td>";
  html_code += "<td class=\"cTdNavigationText\" colspan=\"5\">";
  html_code += this.month_names[parseInt(current_date.getMonth())]+" "+current_date.getFullYear();
  html_code += "</td>";
  html_code += "<td class=\"cTdNavigationNext\">";
  if (typeof(this.win_id)=="undefined") {
    html_code += "<a href=\"javascript:CalendarProcessEvent('"+this.id+"','"+next_year+"','"+next_month+"')\">&raquo;</a>";
  } else {
    html_code += "<a href=\"javascript:window.opener.CalendarProcessEvent('"+this.id+"','"+next_year+"','"+next_month+"')\">&raquo;</a>";
  }   
  html_code += "</td>";
  html_code += "</tr>";
  //html_code += "</table>";
  //html_code += "<table class=\"cTblCalendar\"cellspacing=\"0\" cellpadding=\"0\" border=\"0\">"; 
  // DAY NAMES ROW
  html_code += "<tr>";
  for (i=0; i<7; i++) {
    tmp = i+this.first_day_offset; 
    if (tmp>6) tmp-=7;
    html_code += "<td class=\"cTdDayName\">" + this.day_names[tmp] +"</td>";
  }
  html_code += "</tr>";
  // CALENDAR ROWS
  var curr_date = new Date(current_date.getFullYear(), current_date.getMonth(), 1);
  tmp = parseInt(curr_date.getDay()-this.first_day_offset);
  if (tmp<0) tmp=7-(this.first_day_offset-curr_date.getDay());
  var prev_date = new Date(current_date.getFullYear(), current_date.getMonth(), 0);
  prev_date.setDate(parseInt(prev_date.getDate()-tmp+1));
  // get days from previous month
  html_code += "<tr>";
  while (prev_date.getMonth()!=current_date.getMonth()) {
    var day = prev_date.getDate();
    var disabled = this.isDisabledDate(prev_date.getFullYear(),parseInt(prev_date.getMonth()+1),prev_date.getDate());
    html_code += "<td class=\"cTdDay cTdPrevMonthDay "+(disabled ? "cTdDisabledDay" : "")+"\">";
    if (!disabled) {
      if (typeof(this.win_id)=="undefined") {
        html_code += "<a href=\"javascript:CalendarProcessEvent('"+this.id+"','"+prev_date.getFullYear()+"','"+parseInt(prev_date.getMonth()+1)+"','"+day+"')\">" + day + "</a>";
      } else {
        html_code += "<a href=\"javascript:window.opener.CalendarProcessEvent('"+this.id+"','"+prev_date.getFullYear()+"','"+parseInt(prev_date.getMonth()+1)+"','"+day+"')\">" + day + "</a>";
      }  
    } else {
      html_code += day;
    }
    html_code += "</td>";
    // disabled
    //this.html_code += "<td class=\"cTblCalendarTdPrewMonthDay\ cTblCalendarTdDisabledDay"><a href=\"\">" + day + "</a></td>";
    prev_date.setDate(parseInt(day+1));
  }
  // get days from current month
  while (curr_date.getMonth()==current_date.getMonth()) {
    var day = curr_date.getDate();
    var disabled = this.isDisabledDate(curr_date.getFullYear(),parseInt(curr_date.getMonth()+1),curr_date.getDate());
    html_code += "<td class=\"cTdDay cTdCurrentMonthDay "+(disabled ? "cTdDisabledDay" : "")+"\">";
    if (!disabled) {
      if (typeof(this.win_id)=="undefined") {
        html_code += "<a href=\"javascript:CalendarProcessEvent('"+this.id+"','"+curr_date.getFullYear()+"','"+parseInt(curr_date.getMonth()+1)+"','"+day+"')\">" + day + "</a>";
      } else {
        html_code += "<a href=\"javascript:window.opener.CalendarProcessEvent('"+this.id+"','"+curr_date.getFullYear()+"','"+parseInt(curr_date.getMonth()+1)+"','"+day+"')\">" + day + "</a>";
      }  
    } else {
      html_code += day;
    }  
    html_code += "</td>";
    curr_date.setDate(parseInt(day+1)); 
    // end row
    if (parseInt(curr_date.getDay()) == this.first_day_offset) { 
      html_code += "</tr>"; 
    }  
  }  
  // get days from next month
  while (parseInt(curr_date.getDay())!=this.first_day_offset) {
    var day = curr_date.getDate();
    var disabled = this.isDisabledDate(curr_date.getFullYear(),parseInt(curr_date.getMonth()+1),curr_date.getDate());
    html_code += "<td class=\"cTdDay cTdNextMonthDay "+(disabled ? "cTdDisabledDay" : "")+"\">";
    if (!disabled) {
      if (typeof(this.win_id)=="undefined") {
        html_code += "<a href=\"javascript:CalendarProcessEvent('"+this.id+"','"+curr_date.getFullYear()+"','"+parseInt(curr_date.getMonth()+1)+"','"+day+"')\">" + day + "</a>";
      } else {
        html_code += "<a href=\"javascript:window.opener.CalendarProcessEvent('"+this.id+"','"+curr_date.getFullYear()+"','"+parseInt(curr_date.getMonth()+1)+"','"+day+"')\">" + day + "</a>";
      }  
    } else {
      html_code += day;
    }  
    html_code += "</td>";
    curr_date.setDate(parseInt(day+1));
    // end row
    if (parseInt(curr_date.getDay()) == this.first_day_offset) {
      html_code += "</tr>"; 
    }  
  }
  html_code += "</table>";
  html_code += "</div>";
  if (typeof(this.win_id)!="undefined") {
    this.win_id.document.open();
    this.win_id.document.write("<style type=\"text/css\">body { margin:0px; padding:0px; }</style>");
    this.win_id.document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/calendar.css\" />");
    this.win_id.document.write(html_code);
    this.win_id.document.close();
  } else {  
    document.getElementById(this.div_id).innerHTML = html_code;
  }
}

/**
 * show
 */
CalendarObj.prototype.show = function(id) {
  var calendarObj = window.CalendarObjObjects[id];
  if (typeof(this.win_id)!="undefined") {
    var height = 130;
    var width = 160;
    this.win_id = window.open("about:blank",id,'top='+(screen.availHeight/2-height/2)+',left='+(screen.availWidth/2-width/2)+',width=150,height=150,scrollbars=no,status=no,menubar=no,resizable=no');
    this.win_id.focus();
  } else {  
    document.getElementById(this.div_id).style.display = "block";
  }
  calendarObj.display();
}

/**
 * hide
 */
CalendarObj.prototype.hide = function(div_id) {
  if (typeof(this.win_id)!="undefined") {
    this.win_id.close();
  } else {
    document.getElementById(div_id).style.display = "none";
  }  
}

/**
 * setCurrentDate(full_year,month,day)
 */
CalendarObj.prototype.setCurrentDate = function (y,m,d) {
  this.set_date = new Date();
  this.set_date.setDate(d);
  this.set_date.setMonth(parseInt(m-1));
  this.set_date.setFullYear(y);
}

/**
 * isDisabledDate(full_year,month,day)
 */
CalendarObj.prototype.isDisabledDate = function(y,m,d) {
  var date = "" + y + (m<10 ? "0"+m : m) + (d<10 ? "0"+d : d);
  return eval(this.disabled_dates_expression);
}

/**
 * addDisableDates(start_full_year,start_month, start_day,end_full_year,end_month,end_day)
 */
CalendarObj.prototype.addDisabledDates = function(start_y,start_m,start_d,end_y,end_m,end_d) {
  start_y = parseFloat(start_y); start_m = parseFloat(start_m); start_d = parseFloat(start_d); 
  end_y = parseFloat(end_y); end_m = parseFloat(end_m); end_d = parseFloat(end_d); 
  if (arguments.length==3) { 
    end_y = start_y; 
    end_m = start_m; 
    end_d = start_d; 
  }
	if (!start_y>0 && !start_m>0 && !start_d>0 && !end_y>0 && !end_m>0 && !end_d>0) { 
    return; 
  }
	if (this.disabled_dates_expression!="") { 
    this.disabled_dates_expression+= "||"; 
  }
	if (start_y>0 && start_m>0 && start_d>0) { 
	  start_m = start_m<10 ? "0"+start_m : start_m;
    start_d = start_d<10 ? "0"+start_d : start_d;
    start = ""+start_y+start_m+start_d;
  }
	if (end_y>0 && end_m>0 && end_d>0) {
	  end_m = end_m<10 ? "0"+end_m : end_m;
    end_d = end_d<10 ? "0"+end_d : end_d;
    end = ""+end_y+end_m+end_d;
  } 
	if (!start_y>0 && !start_m>0 && !start_d>0) { 
    this.disabled_dates_expression+="(date<="+end+")"; 
  }
	else if (!end_y>0 && !end_m>0 && !end_d>0) { 
    this.disabled_dates_expression+="(date>="+start+")"; 
  }
	else { 
    this.disabled_dates_expression+="(date>="+start+"&&date<="+end+")"; 
  }
  //alert(this.disabled_dates_expression);
}

/**
 * resetDisabledDates
 */
CalendarObj.prototype.resetDisabledDates = function() {
  this.disabled_dates_expression = "";
}

/**
 * setDayNames
 */
CalendarObj.prototype.setDayNames = function(sun, mon, tue, wed, thu, fri, sat) {
  this.day_names = new Array(sun,mon,tue,wed,thu,fri,sat);
}

/**
 * setMonthNames
 */
CalendarObj.prototype.setMonthNames = function(jan, feb, apr, mar, may, jun, jul, aug, sep, oct, nov, dec) {
  this.month_names = new Array(jan, feb, apr, mar, may, jun, jul, aug, sep, oct, nov, dec);
}

/**
 * setFirstDay
 * 0-sunday, 1-monday, 2-thursday ... 6-saturday
 */
CalendarObj.prototype.setFirstDay = function(first_day) {
  if (first_day>6) first_day = 0;
  this.first_day_offset = first_day;
}

/**
 * constructor 
 */ 
function Calendar(id,return_function,popup) {
  var calendar_obj = new CalendarObj(id, return_function, popup);
  return calendar_obj;
}

/**
 * CalendarProcessEvent
 */ 
function CalendarProcessEvent() {
  var obj_index = arguments[0];
  var calendarObj = window.CalendarObjObjects[obj_index];
  // hide calendar
  if (arguments.length==1) {
    calendarObj.hide(obj_index);
  } else {  
    if (arguments[1]) var year = arguments[1];
    if (arguments[2]) var month = arguments[2];
    if (arguments[3]) var day = arguments[3];
    // go to another month  
    if (arguments.length<4) {
      calendarObj.setCurrentDate(year,month,1);
      calendarObj.display();
    } 
    else {
      // hide calendar and return
      calendarObj.hide(calendarObj.div_id);
      eval(calendarObj.return_function+"("+parseFloat(year)+","+parseFloat(month)+","+parseFloat(day)+");");
    }
  }  
  return;
}
