/**
 *  calendar.js
 *  Author: Oleg V. Marukhin (omarukhin@raduga.sarov.ru)
 */

  
  //vars used in date restriction selection
  var date_today = new Date();
  var date_tom = new Date(date_today.getTime()+24*60*60*1000); //add 1 day
  var date_yes = new Date(date_today.getTime()-24*60*60*1000); //sub 1 day
  var tmonth = date_tom.getMonth() + 1;
  var tday = date_tom.getDate();
  var tyear = date_tom.getFullYear();
  var ymonth = date_yes.getMonth() + 1;
  var yday = date_yes.getDate();
  var yyear = date_yes.getFullYear() - 1;
  var date_yest_str = ymonth + "/" + yday + "/" + date_yes.getFullYear();
  var date_future = tmonth + "/" + tday + "/" + tyear; //cals not allowed to pick dates in the future
  var date_year_ago = ymonth + "/" + yday + "/" + yyear; //most cals restricted to one year ago
  var yfyear = date_tom.getFullYear() + 1;
  var date_year_future = tmonth + "/" + tday + "/" + yfyear; //PO amended delivery date goes from today to 1 year from now
  var date_subro = "06/30/09"; //subro restricted to no earlier than 7/1/09 

  

  var cal1 = new CalendarPopup();
  cal1.showNavigationDropdowns();
  cal1.addDisabledDates(null, date_year_ago);
  cal1.addDisabledDates(date_future, null);
  cal1.setReturnFunction("showDate1");
  function showDate1(y,m,d) {
    document.forms[0].date1.value = m + '/' + d + '/' + y;
    getDate('date1');
  }
var cal1a = new CalendarPopup();
  cal1a.showNavigationDropdowns();
  cal1a.addDisabledDates(null, date_year_ago);
  cal1a.addDisabledDates(date_year_future, null);
  cal1a.setReturnFunction("showDate1a");
  function showDate1a(y,m,d) {
      document.forms[0].date1.value = m + '/' + d + '/' + y;
      getDate('date1');
  }
  var cal2 = new CalendarPopup();
  cal2.showNavigationDropdowns();
  cal2.addDisabledDates(null, date_year_ago);
  cal2.addDisabledDates(date_future, null);
  cal2.setReturnFunction("showDate2");
  function showDate2(y,m,d) {
      document.forms[0].date2.value = m + '/' + d + '/' + y;
      getDate('date2');
  }

  var cal2a = new CalendarPopup();
  cal2a.showNavigationDropdowns();
  cal2a.addDisabledDates(null, date_yest_str);
  cal2a.addDisabledDates(date_year_future, null);
  cal2a.setReturnFunction("showDate2a");
  function showDate2a(y,m,d) {
      document.forms[0].date2.value = m + '/' + d + '/' + y;
      getDate('date2');
  }
  var cal1b = new CalendarPopup();
  cal1b.showNavigationDropdowns();
  cal1b.addDisabledDates(null, date_yest_str);
  cal1b.addDisabledDates(date_year_future, null);
  cal1b.setReturnFunction("showDate1b");
  function showDate1b(y,m,d) {
      document.forms[0].date1.value = m + '/' + d + '/' + y;
      getDate('date1');
  }
  var cal2b = new CalendarPopup();
  cal2b.showNavigationDropdowns();
  cal2b.addDisabledDates(null, date_yest_str);
  cal2b.addDisabledDates(date_year_future, null);
  cal2b.setReturnFunction("showDate2b");
  function showDate2b(y,m,d) {
      document.forms[0].date2.value = m + '/' + d + '/' + y;
      getDate('date2');
  }
  var cal3 = new CalendarPopup();
  cal3.showNavigationDropdowns();
  cal3.addDisabledDates(null, date_year_ago);
  cal3.addDisabledDates(date_future, null);
  cal3.setReturnFunction("showDate3");
  function showDate3(y,m,d) {
      document.forms[0].date3.value = m + '/' + d + '/' + y;
      getDate('date3');
  }
  var cal3a = new CalendarPopup();
  cal3a.showNavigationDropdowns();
  cal3a.addDisabledDates(null, date_year_ago);
  cal3a.addDisabledDates(date_year_future, null);
  cal3a.setReturnFunction("showDate3a");
  function showDate3a(y,m,d) {
      document.forms[0].date3.value = m + '/' + d + '/' + y;
      getDate('date3');
  }

  var cal4 = new CalendarPopup();
  cal4.showNavigationDropdowns();
  cal4.addDisabledDates(null, date_year_ago);
  cal4.addDisabledDates(date_future, null);
  cal4.setReturnFunction("showDate4");
  function showDate4(y,m,d) {
      document.forms[0].date4.value = m + '/' + d + '/' + y;
      getDate('date4');
  }
   var cal5 = new CalendarPopup();
  cal5.showNavigationDropdowns();
  cal5.addDisabledDates(null, date_year_ago);
  cal5.addDisabledDates(date_future, null);
  cal5.setReturnFunction("showDate5");
  function showDate5(y,m,d) {
      document.forms[0].date5.value = m + '/' + d + '/' + y;
      getDate('date5');
  }

  var cal6 = new CalendarPopup();
  cal6.showNavigationDropdowns();
  cal6.addDisabledDates(null, date_year_ago);
  cal6.addDisabledDates(date_future, null);
  cal6.setReturnFunction("showDate6");
  function showDate6(y,m,d) {
      document.forms[0].date6.value = m + '/' + d + '/' + y;
      getDate('date6');
  }

  var cal7 = new CalendarPopup();
  cal7.showNavigationDropdowns();
  cal7.addDisabledDates(null, date_year_ago);
  cal7.addDisabledDates(date_future, null);
  cal7.setReturnFunction("showDate7");
  function showDate7(y,m,d) {
      document.forms[0].date7.value = m + '/' + d + '/' + y;
      getDate('date7');
  }

  var cal8 = new CalendarPopup();
  cal8.showNavigationDropdowns();
  cal8.addDisabledDates(null, date_year_ago);
  cal8.addDisabledDates(date_future, null);
  cal8.setReturnFunction("showDate8");
  function showDate8(y,m,d) {
      document.forms[0].date8.value = m + '/' + d + '/' + y;
      getDate('date8');
  }

  var cal9 = new CalendarPopup();
  cal9.showNavigationDropdowns();
  cal9.addDisabledDates(null, date_year_ago);
  cal9.addDisabledDates(date_future, null);
  cal9.setReturnFunction("showDate9");
  function showDate9(y,m,d) {
      document.forms[0].date9.value = m + '/' + d + '/' + y;
      getDate('date9');
  }
  var cal10 = new CalendarPopup();
  cal10.showNavigationDropdowns();
  cal10.addDisabledDates(null, date_year_ago);
  cal10.addDisabledDates(date_future, null);
  cal10.setReturnFunction("showDate10");
  function showDate10(y,m,d) {
      document.forms[0].date10.value = m + '/' + d + '/' + y;
      getDate('date10');
  }
  
  //11 and 12 are linked... when 11 changes, 12 changes to 5 days later
  //lots of extra date logic below for the subro cals
   var cal11 = new CalendarPopup();
  cal11.showNavigationDropdowns();
  cal11.addDisabledDates(null, date_subro);
  cal11.addDisabledDates(date_future, null);
  cal11.setReturnFunction("showDate11");
  function showDate11(y,m,d) {
      var ndate = new Date(y,m-1,d);
      
      var today = new Date();
      var yearago = new Date(today.getFullYear()-1,today.getMonth(),today.getDate());
      if(yearago > ndate){
        ndate = yearago;
      }
      else if(today < ndate){
        ndate = today;
      }
      d = ndate.getDate();
      m = ndate.getMonth()+1;
      y = ndate.getFullYear();
      document.forms[0].date11.value = m + '/' + d + '/' + y;
      getDate('date11');
      ndate.setDate(ndate.getDate()+5);
      	var d2 = ndate.getDate();
      	var m2 = ndate.getMonth()+1;
      	var y2 = ndate.getFullYear();
      	showDate12(y2,m2,d2);
  }
  var cal12 = new CalendarPopup();
  cal12.showNavigationDropdowns();
  cal12.addDisabledDates(null, date_subro);
  cal12.addDisabledDates(date_future, null);
  cal12.setReturnFunction("showDate12");
function showDate12(y,m,d) {
  	  var ndate = new Date(y,m-1,d);
  	  var d11 = document.forms[0].date11.value;
  	  var dateError = document.getElementById('dateError');
  	  if(d11==""){
  	    var today = new Date();
  	    var yearago = new Date(today.getFullYear()-1,today.getMonth(),today.getDate());
  	    if(yearago > ndate){
           ndate = yearago;
      	}
      	else if(today < ndate){
           ndate = today;
      	}
  	    d = ndate.getDate();
      	m = ndate.getMonth()+1;
      	y = ndate.getFullYear();
      	if(dateError != null && dateError != ""){
      		dateError.style.visibility = 'hidden';
      	}
  	  	document.forms[0].date12.value = m + '/' + d + '/' + y;
      	getDate('date12');
  	  }  
  	  else{
  	  	var split = d11.split("/");
  	  	var startdate = new Date(split[2], split[0]-1, split[1]);
  	  	var enddate = new Date(startdate.getTime()+14*24*60*60*1000);
  	  	if((ndate >= startdate) && (ndate <= enddate)){
  	  		today = new Date();
	        if(today < ndate){
	        	d = today.getDate();
	      		m = today.getMonth()+1;
	      		y = today.getFullYear();
	        }
	        if(dateError != null && dateError != ""){
	        	dateError.style.visibility = 'hidden';
	        }
  	  		document.forms[0].date12.value = m + '/' + d + '/' + y;
      		getDate('date12');
      	}else{
      		if(dateError != null && dateError != ""){
      			dateError.style.visibility = 'visible';
      		}
      	}
  	  }
      
  }

  function getDate(the_name){
    var el = document.getElementById(the_name + 'S');
    the_span = eval(el);
    if(the_span){
      var sel = document.getElementById(the_name);
      var date_var = eval(sel).value;
      if(date_var.length > 1){
          if (the_name == 'date9')  {
            document.getElementById("date9B").innerHTML = '';
          }
          if (the_name == 'date10')  {
            document.getElementById("date10B").innerHTML = '';
          }
          if (the_name == 'date11')  {
            document.getElementById("date11B").innerHTML = '';
          }
          if (the_name == 'date12')  {
            document.getElementById("date12B").innerHTML = '';
          }
          the_span.innerHTML = date_var;
      }                                     
      else{
        the_span.innerHTML = 'Please Choose Date';
      }
    }
  }

  function setDateDisabled(){
      document.forms[0].date1.disabled = true;
      document.forms[0].date2.disabled = true;
  }

  function setDateDisabled2(){
      document.forms[0].date3.disabled = true;
      document.forms[0].date4.disabled = true;
  }