  function doSelectOptions(theValue,theField) {
  	
	var objForm = document.checkdeals;
	if (!objForm) objForm = document.view_step;
	for (i=0; i< objForm[theField].options.length; i++)
		if (objForm[theField].options[i].value == theValue) {
			objForm[theField].options[i].selected = true;
		}
  }
  
  function StabilesteAn(theForm, ty, tm, td)
  {
	a = parseInt(theForm.adate1.value) - 1;
	b = parseInt(theForm.adate2.value) + 0;

	c = parseInt(theForm.adate2.value);
	e = tm - 1;
	f = parseInt(td);

  	var today	= new Date(ty, e, td);
	//window.alert(today1.getFullYear()+'|'+ty+'|'+tm+'|'+td);

	var nextYear= new Date((ty+1), e, td);
	var outDate	= new Date(parseInt(theForm.ddate3.value), parseInt(theForm.ddate1.value)-1, parseInt(theForm.ddate2.value));
  	var inDate	= new Date(theForm.adate3.value, a, c);


	//window.alert(today.getFullYear()+'|'+ty+'|'+tm+'|'+td);

	if (inDate.getTime()  > nextYear.getTime()) { // Crossed the newyear boundary
  		inDate.setYear((inDate.getFullYear())-1);
  	}

  	if (inDate.getTime()  < today.getTime()) { // Crossed the newyear boundary
  		inDate.setYear((today.getFullYear())+1);
  	}

	outDate.setYear(inDate.getFullYear());
	if (outDate.getTime()  <= inDate.getTime()) { // Crossed the newyear boundary
  		outDate.setYear(inDate.getFullYear()+1);
  	}

	theForm.ddate3.value = (outDate.getFullYear());
	theForm.adate3.value = (inDate.getFullYear());

	//window.alert(outDate.getFullYear());
  }

  function calcBothDates(theForm, ty, tm, td)
  {
	 a = parseInt(theForm.adate1.value) -1;
	 b = parseInt(theForm.adate2.value) +2;

	 c = parseInt(theForm.adate2.value);
	 e = tm - 1;
	 f = parseInt(td);

	var today	= new Date(ty, e, td);
	var outDate	= new Date(theForm.adate3.value, a, b);
	var inDate	= new Date(theForm.adate3.value, a, c);

	if (inDate.getTime()  < today.getTime()) { // Crossed the newyear boundary
		inDate.setYear((today.getFullYear())+1);
		outDate.setYear((today.getFullYear())+1);
	}

	var today_next_year	= new Date(ty+1, e, td);
	if (inDate.getTime()  >= today_next_year.getTime()) { // Crossed the newyear boundary
		inDate.setYear((today.getFullYear()));
		outDate.setYear((today.getFullYear()));
	}

	outDate.setYear(inDate.getFullYear());
	if (outDate.getTime()  <= inDate.getTime()) { // Crossed the newyear boundary
		outDate.setYear((inDate.getFullYear())+1);
	}


	theForm.adate3.value	= (inDate.getFullYear());
	doSelectOptions(inDate.getMonth()+1, 'adate1');
	doSelectOptions(inDate.getDate(), 'adate2');

	theForm.ddate3.value	= (outDate.getFullYear());
	doSelectOptions(outDate.getMonth()+1, 'ddate1');
	doSelectOptions(outDate.getDate(), 'ddate2');


	var todayTemp = new Date();
	StabilesteAn(theForm, todayTemp.getFullYear(), todayTemp.getMonth(), todayTemp.getDate());
	//alert("year="+todayTemp.getFullYear()+", month="+todayTemp.getMonth()+", day="+todayTemp.getDate());
}
