/**
*
* @author Andrzej Du�
*
*/

/* BEGIN - Text used in scripts */
var textWrongDateFormat = "Nieprawid\u0142owy format daty dd.mm.yy.";
var textWrongMonthFormat = "Podany miesi\u0105c jest nieprawid\u0142owy.";
var textWrongDayFormat = "Podany dzie\u0144 jest nieprawid\u0142owy.";
var textWrongYearFormat = "Podany rok jest nieprawid\u0142owy.";
var textWrongDate = "Podana data jest nieprawid\u0142owa.";
var textDateDiff = "Wylot i powr\u00F3t sa w tym samym dniu.";
var textDateDiffLess = "Data wylotu jest p\u00F3\u017Aniejsza od daty powrotu.";
/* END - Text used in scripts */

// Declaring valid date character, minimum year and maximum year
var dtCh= ".";
var minYear=00;
var maxYear=99;

/* BEGIN - Helper functions for isDate() function */
function isInteger(s)
{
  var i;
  for (i = 0; i < s.length; i++)
  {
    // Check that current character is number.
    var c = s.charAt(i);
    if (((c < "0") || (c > "9"))) return false;
  }
  // All characters are numbers.
  return true;
}

function stripCharsInBag(s, bag)
{
	var i;
  var returnString = "";
  // Search through string's characters one by one.
  // If character is not in bag, append to returnString.
  for (i = 0; i < s.length; i++)
  {
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

function daysInFebruary (year){
  // February has 29 days in any year evenly divisible by four,
  // EXCEPT for centurial years which are not also divisible by 400.
  return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
/* END - Helper functions for isDate() function */

/* Function checks if dtStr string is in propper format. Date delimiter is set in global variable dtCh */
function isDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1)
    strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1)
    strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
  if (dtStr.length == 0)
  {
    return true;
  }
  if (pos1==-1 || pos2==-1)
  {
		alert(textWrongDateFormat)
		return false
	}
	if (strMonth.length<1 || month<1 || month>12)
  {
		alert(textWrongMonthFormat)
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
  {
		alert(textWrongDayFormat)
		return false
	}
	if (strYear.length != 2 || year<minYear || year>maxYear)
  {
		alert(textWrongYearFormat)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
  {
		alert(textWrongDate)
		return false
	}
return true
}


/* Function checks if dtStrStart isn't bigger or equal then dtStrEnd */
function compareDates(dtStrStart, dtStrEnd)
{
  var dateStart = new Date();
	var pos1=dtStrStart.indexOf(dtCh)
	var pos2=dtStrStart.indexOf(dtCh,pos1+1)
	var strDayStart=dtStrStart.substring(0,pos1);
	var strMonthStart=dtStrStart.substring(pos1+1,pos2)-1;
	var strYearStart=parseInt(dtStrStart.substring(pos2+1))+2000;
	//alert(strMonthStart);
	dateStart.setFullYear(strYearStart, strMonthStart, strDayStart);

  var dateEnd = new Date();
	var pos1=dtStrEnd.indexOf(dtCh)
	var pos2=dtStrEnd.indexOf(dtCh,pos1+1)
	var strDayEnd=dtStrEnd.substring(0,pos1);
	var strMonthEnd=dtStrEnd.substring(pos1+1,pos2)-1;
	var strYearEnd=parseInt(dtStrEnd.substring(pos2+1))+2000;
	//alert(strMonthEnd);
	dateEnd.setFullYear(strYearEnd, strMonthEnd, strDayEnd);
  var i = 0;
//  alert(dateStart.getDate()+"."+dateStart.getMonth()+"."+dateStart.getFullYear());
//  alert(dateEnd.getDate()+"."+dateEnd.getMonth()+"."+dateEnd.getFullYear());
  timeDiffrence = dateEnd.getTime() - dateStart.getTime();
//  alert("tD: "+timeDiffrence);
  var minutes = 1000*60;
  var hours = 60*minutes;
  var day = 24*hours;
  if(timeDiffrence < 0)
  {
//    alert("mniejsze < 0 tD:" + timeDiffrence)
    alert(textDateDiffLess);
    return false;
  }
  var days = timeDiffrence / day;
  if( days >= 0 && days < 1 )
  {
    alert(textDateDiff);
    return false;
  }
  //alert(timeDiffrence / day);
  return timeDiffrence / day;
}

/* Function is used to increment source_date_input field by day_incremetation days and put int into destination_date_field */
function setNewDate( source_date_input, destination_date_input, day_incrementation )
{
  dtStr = document.getElementById(source_date_input).value;

	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
/*  if( strDay.substring(0,1) == '0' )
  {
    strDay=strDay.substring(1,2);
  }
  strDay=parseInt(strDay);*/

  var minutes = 1000*60;
  var hours = 60*minutes;
  var day = 24*hours;
//  alert(strMonth);
  var newDate = new Date();
  newDate.setFullYear(strYear, strMonth-1, strDay);
  newTime = newDate.getTime()+day_incrementation*day;
  newDate.setTime(newTime);

//  alert(strDay);

  strDay=newDate.getDate();
  if(strDay < 10 )
    strDay = "0"+strDay;

  strMonth=newDate.getMonth()+1;
  if(strMonth < 10 )
    strMonth = "0"+strMonth;

  var strYear=newDate.getFullYear();
  if(strYear < 10 )
    strYear = "0"+strYear;

//  alert(strYear);
  var new_date = strDay+dtCh+strMonth+dtCh+strYear;
  
  
  document.getElementById(destination_date_input).value = new_date;
}

/* Checks form */
function ValidateForm()
{
  var dt = new Array();
  dt[0] = document.step1.startflight;
	dt[1] = document.step1.endflight;
	var dh;
  for( i = 0; i < 2; i++ )
  {
    if( dt[i] == null )
      continue;

    // is this field a propper date??
    if (isDate(dt[i].value)==false)
    {
      dt[i].focus();
		  return false;
    }
  }
  //alert("dt1: "+dt[0].value.length+"dt2: "+dt[1].value.length);
  if( document.getElementById("endflight") != null )
  {
    if( dt[0].value.length != 0 && dt[1].value.length != 0 )
    {
      // isn't dt[0] bigger then dt[1]
      if( ( dh=compareDates(dt[0].value, dt[1].value) )==false)
      {
        return false;
      }
  }
  }
  //return true;
  document.step1.submit();
 }
