// FLIFOS.js
// -------------------------------------------------------------------------- \\

// FLIFOS Constructor
// -------------------------------------------------------------------------- \\
function FLIFOS () {
  // attributes
  this.alreadySubmited = false;

  // methods
  this.check      = FLIFOS_check;
  this.fillForm   = FLIFOS_fillForm;
  this.pageOnLoad = FLIFOS_pageOnLoad;
}
var FLIFOS = new FLIFOS();

// FLIFOS_check
// ------------------------------------------------------------------------- \\

function FLIFOS_check () {
  if (!this.alreadySubmited) {
    this.alreadySubmited = true;

	// remove previously added error messages
    WDSError.init();

	  // Check all fields
	      // from and to
	      var airline =  $('airline', 'flight_info');
    	  var airlineCode=airline.getCode();
	      if (airlineCode.length != 2) {
	        WDSError.add(WDSMessage.getMessage(10015), document.flightInfoSearchForm.AIRLINE_CODE);
	      } else {
	    	  document.getElementById('flight_info_airline_code').value = airlineCode;
	      }
	      var flightnum=WDSCommon.getTagValue(document.flightInfoSearchForm.FLIGHT_NUMBER);
	      if (flightnum == "") {
	        WDSError.add(WDSMessage.getMessage(117659), document.flightInfoSearchForm.FLIGHT_NUMBER);
	      }
	
	  // Display in case of error found
	  if (WDSError.hasError()) {
	    WDSError.show();
     	this.alreadySubmited = false;
	  } else {
	    this.fillForm();
	    document.flightInfoSearchForm.submit();
      	WDSWaitingImage.pleaseWait();
	  }
  }
}

function FLIFOS_fillForm () {
  var datStart = FLIFOSDateList.getDateTimeComposedString(FLIFOSDateList.grpStart);
  WDSCommon.updateTag(document.flightInfoSearchForm,"B_DATE",datStart);
  WDSCommon.updateTag(document.flightInfoSearchForm,"B_Month",datStart.substr(0,6));
  WDSCommon.updateTag(document.flightInfoSearchForm,"B_Day",datStart.substr(6,2));

  var bTime = WDSCommon.getTagValue(document.flightInfoSearchForm.B_Time);
  WDSCommon.updateTag(document.flightInfoSearchForm,"B_Time",bTime);
  if (bTime == "ANY")
    WDSCommon.updateTag(document.flightInfoSearchForm,"B_ANY_TIME","TRUE");
  else
    WDSCommon.updateTag(document.flightInfoSearchForm,"B_ANY_TIME","FALSE");
  
}

 function FLIFOS_pageOnLoad () {
   LocalErrorInit();
   DateListsInit();
   setPrintButton();
   removeStr();
}

// -------------------------------------------------------------------------- \\
// removeStr 
// -------------------------------------------------------------------------- \\
// Remove the '-' char at the end of the string
// -------------------------------------------------------------------------- \\
function removeStr(){
	var tds = document.getElementsByTagName('td');
	var str = '-';
	for (i=0; i<tds.length; i++){
		if(tds[i].className == 'removeStr'){
			var lastIndex = tds[i].innerHTML.lastIndexOf(str);
			var outstr = ((lastIndex != -1) ? tds[i].innerHTML.substring(0,lastIndex) : tds[i].innerHTML);
			tds[i].innerHTML = outstr;
		}
	}
}

// -------------------------------------------------------------------------- \\
// capitalize 
// -------------------------------------------------------------------------- \\
// Set the first letter of given string to uppercase
// -------------------------------------------------------------------------- \\
function capitalize(str){
	var firstLetter = ((str != null) ? str.substring(0,1) : '').toUpperCase();
	var outstr = firstLetter + ((str != null) ? str.substring(1,str.length) : '');
	document.write(outstr);
}

// -------------------------------------------------------------------------- \\
// setPrintButton 
// -------------------------------------------------------------------------- \\
// Set the print feature on button
// -------------------------------------------------------------------------- \\
function setPrintButton(){
	jQuery('a#flight-status-print-btn').click(function(){
		jQuery.jPrintArea('#fs-result');
		return false;
	});
}
