/* adapted from AirportInfo.js for JPDC-1305 */

// Text weather javascript functions

function getGreatCircle(winLocBase){
 if (document.gc.station1.value == "" ||
     document.gc.station2.value == ""){
  alert("You must enter 2 stations for Great Circle Weather")
  return false
 }
 if (document.gc.station1.value.length < 3 ||
     document.gc.station2.value.length < 3){
  alert("You must use ICAO or IATA airport codes")
  return false
 }
  var winLoc = winLocBase + '?query=310&station1=' +
                 document.gc.station1.value +
                 '&station2=' + document.gc.station2.value;
  if(document.gc.gcalt1.value != ""){
        winLoc = winLoc + '&gcalt1=' + document.gc.gcalt1.value;}
  if(document.gc.gcalt2.value != ""){
        winLoc = winLoc + '&gcalt2=' + document.gc.gcalt2.value;}
  if(document.gc.gcalt3.value != ""){
        winLoc = winLoc + '&gcalt3=' + document.gc.gcalt3.value;}
        win=window.open(winLoc,'GreatCircleWx','menubar=1,scrollbars=yes,resizable=no,width=640,height=450');
        win.focus();
        return false;
}





var reportType = "longTAF_METAR";
function getAirportData(winLocBase){

	//alert(reportType);

 if (document.airportData.wxStation.value == ""){
  alert("Please enter at least one well formed weather station \n(e.g. KLAX).")
  return false
 }
 else{

 //examine wxStation list for validity
 var theList = document.airportData.wxStation.value;
 var notamFilterName = document.airportData.notamFilterName.value;
 if(reportType == "longTAF_METAR"){
	notamFilterName = "";
	//alert("NFN blocked");
 }
 theList = theList.split(" ");
 var ctr = 0;
 var stationCtr = 0;
 var newStations = "";
 while (ctr < theList.length) {
  if(theList[ctr] == "" || (theList[ctr].length > 2 && theList[ctr].length < 5)){
	if(theList[ctr] != ""){
	 stationCtr++;
	 newStations += " " + theList[ctr];
	}
  }else{
	alert('Input station: '+theList[ctr]+' appears to be malformed. \n Please use IATA or ICAO station codes separated by spaces and try again.');
	return false;
  }
  ctr++;
 }
//JPDC-319
// if(stationCtr > 6 && notamFilterName != ""){
//	alert('Sorry, the maximum number of stations that can be requested for NOTAMs with a Notam Filter is currently 6.  Please reduce the number of stations to 6 or fewer and try again.');
//	 return false;
// }

 var inputType = reportType;
 if(reportType == "sa"){
  inputType = reportType + document.airportData.SAhour.value;
 }
 if(reportType == "ft" && document.airportData.taf2Prev.checked){
  inputType += "1";
 }
 if(reportType == "longTAF_METAR" && document.airportData.taf4Prev.checked){
  inputType += "1";
 }

 if(reportType == "no"){
 	//reset inputType for notams
 	inputType = "";
 	//JPDC-319
// if(stationCtr > 6 && notamFilterName != ""){
//      alert('Sorry, the maximum number of stations that can be requested for NOTAMs with a Notam Filter is currently 6.  Please reduce the number of stations to 6 or fewer and try again.');
//       return false;
// }
 if(document.airportData.notamType1.checked){
 	//alert('1 is checked');
	if(document.airportData.notam1[0].checked){
		inputType = "noi";
	}else{
		inputType = "no";
	}
 }
// -- }else{
 if(document.airportData.notamType2.checked){
  	//alert('2 is checked');
	if(document.airportData.notam2[0].checked){
		if(inputType == ""){
		 inputType = "nofi";
		}else{
		 inputType = "nofi,"+inputType;
		}
	}else{
		if(inputType == ""){
		 inputType = "nof";
		}else{
		 inputType = "nof,"+inputType;
		}
	}
 }
 	//alert('inputType, '+inputType);
 }
 	
 //alert('reportType='+inputType);
 
  var winLoc = winLocBase + '?query=freewx&wxStation=' +
		 document.airportData.wxStation.value;
	winLoc += '&wxType=' + inputType;
	winLoc += '&notamFilterName='+notamFilterName;

   var winWidth = 640;
   if(reportType == "ua"){
       winLoc = "/jeppesen/jsp/weather/getFreeTxtWx.jsp?airports=" +
		document.airportData.wxStation.value + "&reports=PIREP";
	winWidth = 800;
   }
   if(reportType == "sa" && document.airportData.SAhour.value == "0"){
       winLoc = "/jeppesen/jsp/weather/getFreeTxtWx.jsp?airports=" +
                document.airportData.wxStation.value + "&reports=METAR";
        winWidth = 800;
   }
   if(reportType == "sa" && document.airportData.SAhour.value == "3"){
       winLoc = "/jeppesen/jsp/weather/getFreeTxtWx.jsp?airports=" +
                document.airportData.wxStation.value + "&reports=METAR3";
        winWidth = 800;
   }



        win=window.open(winLoc,'AirportData','menubar=1,scrollbars=yes,resizable=no,width='+winWidth+',height=450');
        win.focus();
  return false;
 }
}




function getWA(){
  if(document.Free.input.value == ""){
	alert("You must define your request");
	return false;
  }else{
	var c = '^';
        var winLoc = '/jeppesen/jpdcServlet?query=380&FPinput=wx' + c + document.Free.input.value + c + 'q' + c;
        win=window.open(winLoc,'02Free','menubar=1,scrollbars=yes,resizable=no,width=640,height=450');
        win.focus();
        //alert('submitting = '+winLoc);
        return false;
  }
}

/*
var notamType = 1;
function getaNotam(winLocBase){
 if(document.Notams.airport.value == "" ){
	alert ('Input at least one 3 or 4 letter airport code');
	return false;}

 //examine wxStation list for validity
 var theList = document.Notams.airport.value;
 var notamFilterName = document.Notams.notamFilterName.value;
 theList = theList.split(" ");
 var ctr = 0;
 var stationCtr = 0;
 var newStations = "";
 while (ctr < theList.length) {
  if(theList[ctr] == "" || (theList[ctr].length > 2 && theList[ctr].length < 5)){
	if(theList[ctr] != ""){
         stationCtr++;
         newStations += " " + theList[ctr];
	}
  }else{
        alert('Input station: '+theList[ctr]+' appears to be malformed. \n Please use IATA or ICAO station codes separated by spaces and try again.');
        return false;
  }
  ctr++;
 }
//JPDC-319
// if(stationCtr > 6 && notamFilterName != ""){
//      alert('Sorry, the maximum number of stations that can be requested for NOTAMs with a Notam Filter is currently 6.  Please reduce the number of stations to 6 or fewer and try again.');
//       return false;
// }

 //alert('reportType='+inputType);
 var inputType = "";
// -- if(notamType == 1){
 if(document.Notams.notamType1.checked){
	if(document.Notams.notam1[0].checked){
		inputType = "noi";
	}else{
		inputType = "no";
	}
 }
// -- }else{
 if(document.Notams.notamType2.checked){
	if(document.Notams.notam2[0].checked){
		if(inputType == ""){
		 inputType = "nofi";
		}else{
		 inputType = "nofi,"+inputType;
		}
	}else{
		if(inputType == ""){
		 inputType = "nof";
		}else{
		 inputType = "nof,"+inputType;
		}
	}
 }
// -- }
  var winLoc = winLocBase + '?query=325&wxStation=' +
                 document.Notams.airport.value;
        winLoc += '&wxType=' + inputType;
	winLoc += '&notamFilterName='+notamFilterName;
        win=window.open(winLoc,'NotamData','menubar=1,scrollbars=yes,resizable=no,width=640,height=450');
        win.focus();

 return false;
}
*/

function disableFIR(){
        document.Notams.notam1[0].disabled=false;
        document.Notams.notam1[1].disabled=false;
        document.Notams.notam2[0].disabled=true;
        document.Notams.notam2[1].disabled=true;
	document.Notams.notamType1.checked=true;
        document.Notams.notamType2.checked=false;
	notamType=1;
}
function disableAirport(){
        document.Notams.notam1[0].disabled=true;
        document.Notams.notam1[1].disabled=true;
        document.Notams.notam2[0].disabled=false;
        document.Notams.notam2[1].disabled=false;
        document.Notams.notamType1.checked=false;
	document.Notams.notamType2.checked=true;
	notamType=0;
}

function enableNotams(){
	document.airportData.notamType1.disabled=false;
	document.airportData.notam1[0].disabled=false;
	document.airportData.notam1[1].disabled=false;
	document.airportData.notamType2.disabled=false;
	document.airportData.notam2[0].disabled=false;
	document.airportData.notam2[1].disabled=false;
}
function disableNotams(){
	document.airportData.notamType1.disabled=true;
	document.airportData.notam1[0].disabled=true;
	document.airportData.notam1[1].disabled=true;
	document.airportData.notamType2.disabled=true;
	document.airportData.notam2[0].disabled=true;
	document.airportData.notam2[1].disabled=true;
}



function resetTAFbuttons(code){
  if(code == "0"){
        document.airportData.taf2Prev.disabled=true;
        document.airportData.taf3Prev.disabled=true;
        document.airportData.taf4Prev.disabled=true;
	document.airportData.SAhour.disabled=true;
        document.airportData.windLevel[0].disabled=true;
        document.airportData.windLevel[1].disabled=true;
	document.airportData.windLevel[2].disabled=true;
        	disableNotams();
 }
  if(code == "1"){
        document.airportData.taf2Prev.disabled=true;
        document.airportData.taf3Prev.disabled=true;
        document.airportData.taf4Prev.disabled=true;
        document.airportData.SAhour.disabled=true;
        document.airportData.windLevel[0].disabled=true;
        document.airportData.windLevel[1].disabled=true;
	document.airportData.windLevel[2].disabled=true;
        	disableNotams();
 }
  if(code == "2"){
        document.airportData.taf2Prev.disabled=false;
        document.airportData.taf3Prev.disabled=true;
        document.airportData.taf4Prev.disabled=true;
        document.airportData.SAhour.disabled=true;
        document.airportData.windLevel[0].disabled=true;
        document.airportData.windLevel[1].disabled=true;
	document.airportData.windLevel[2].disabled=true;
        	disableNotams();
 }
  if(code == "3"){
        document.airportData.taf3Prev.disabled=false;
        document.airportData.taf2Prev.disabled=true;
        document.airportData.taf4Prev.disabled=true;
        document.airportData.SAhour.disabled=true;
        document.airportData.windLevel[0].disabled=true;
        document.airportData.windLevel[1].disabled=true;
	document.airportData.windLevel[2].disabled=true;
        	disableNotams();
 }
  if(code == "4"){
        document.airportData.taf4Prev.disabled=false;
        document.airportData.taf2Prev.disabled=true;
        document.airportData.taf3Prev.disabled=true;
        document.airportData.SAhour.disabled=true;
        document.airportData.windLevel[0].disabled=true;
        document.airportData.windLevel[1].disabled=true;
	document.airportData.windLevel[2].disabled=true;
        	disableNotams();
 }
  if(code == "5"){
        document.airportData.taf2Prev.disabled=true;
        document.airportData.taf3Prev.disabled=true;
        document.airportData.taf4Prev.disabled=true;
        document.airportData.SAhour.disabled=false;
        document.airportData.windLevel[0].disabled=true;
        document.airportData.windLevel[1].disabled=true;
	document.airportData.windLevel[2].disabled=true;
        	disableNotams();
 }
  if(code == "6"){
        document.airportData.taf2Prev.disabled=true;
        document.airportData.taf3Prev.disabled=true;
        document.airportData.taf4Prev.disabled=true;
        document.airportData.SAhour.disabled=true; //false;
	document.airportData.windLevel[0].disabled=false;
        document.airportData.windLevel[1].disabled=false;
        document.airportData.windLevel[2].disabled=false;
        	disableNotams();
 }
}

/* deprecated */
function getAirportDataCCN(winLocBase){
	 if (document.airportData.wxStation.value == ""){
	  alert("Please enter at least one well formed weather station \n(e.g. KLAX).")
	  return false
	 }
	 else{
	 //examine wxStation list for validity
	 var theList = document.airportData.wxStation.value;
	 var notamFilterName = document.airportData.notamFilterName.value;
	 if(reportType == "longTAF_METAR"){
	        notamFilterName = "";
	        //alert("NFN blocked");
	 }
	 theList = theList.split(" ");
	 var ctr = 0;
	 var stationCtr = 0;
	 var newStations = "";
	 while (ctr < theList.length) {
	  if(theList[ctr] == "" || (theList[ctr].length == 4)) { //> 2 && theList[ctr].length < 5)){
	        if(theList[ctr] != ""){
	         stationCtr++;
	         newStations += " " + theList[ctr];
	        }
	  }else{
	        alert('Input station: '+theList[ctr]+' appears to be malformed. \n Please use ICAO station codes separated by spaces and try again.');
	        return false;
	  }
	  ctr++;
	 }

	 var inputType = reportType;
	 if(reportType == "no"){
	        //reset inputType for notams
	        inputType = "NOTAMI";
	 if(document.airportData.notamType1.checked){
	        //alert('1 is checked');
	        if(document.airportData.notam1[0].checked){
	                inputType = "NOTAMI";
	        }else{
	                inputType = "NOTAM";
	        }
	 }

	 if(document.airportData.notamType2.checked){
	        //alert('2 is checked');
	        if(document.airportData.notam2[0].checked){
	                if(inputType == ""){
	                 inputType = "NOTAMIF";
	                }else{
	                 inputType = "NOTAMIF,"+inputType;
	                }
	        }else{
	                if(inputType == ""){
	                 inputType = "NOTAMJF";
	                }else{
	                    inputType = "NOTAMJF,"+inputType;
	                   }
	           }
	    }
	           //alert('inputType, '+inputType);
	    }

	           var winLoc = winLocBase + '?icao=' +
	                    document.airportData.wxStation.value;
	           winLoc += '&rt=' + inputType;
	           //winLoc += '&notamFilterName='+notamFilterName;

	           var winWidth = 800;
	           win=window.open(winLoc,'CCNData','menubar=1,scrollbars=yes,resizable=yes,width='+winWidth+',height=450');
	           win.focus();
	     return false;
	    }
	   }


function getAirportDataCCNviaJPA(winLocBase){
	 if (document.airportData.wxStation.value == ""){
	  alert("Please enter at least one well formed weather station \n(e.g. KLAX).")
	  return false
	 }
	 else{
	 //examine wxStation list for validity
	 var theList = document.airportData.wxStation.value;
	 theList = theList.split(" ");
	 var ctr = 0;
	 var stationCtr = 0;
	 var newStations = "";
	 while (ctr < theList.length) {
		//JPDC-2594 allow 3 char FAA codes
	  if(theList[ctr] == "" || (theList[ctr].length == 4 || theList[ctr].length == 3)) { //> 2 && theList[ctr].length < 5)){
	        if(theList[ctr] != ""){
	         stationCtr++;
	         newStations += " " + theList[ctr];
	        }
	  }else{
	        alert('Input station: '+theList[ctr]+' appears to be malformed. \n Please use ICAO station codes separated by spaces and try again.');
	        return false;
	  }
	  ctr++;
	 }
	 
	 var inputType = "NOTAM";
	 if(document.airportData.formatOutput[0].checked){
	        //alert('1 is checked');
	                inputType += "J";
	 }else{
	                inputType += "I"
	 }

	 if(document.airportData.wxType[1].checked){
	       inputType += "F";
	 }
     //alert('inputType, '+inputType);

	           var winLoc = winLocBase + '?icao=' +
	                    document.airportData.wxStation.value;
	           winLoc += '&rt=' + inputType;

	           var winWidth = 825;
	           win=window.open(winLoc,'CCNData','menubar=1,scrollbars=yes,resizable=yes,width='+winWidth+',height=450');
	           win.focus();
	     return false;
	    }
	   }



function getAirportDataFreeAOC(winLocBase){
	var rt="";
	
	if(     document.airportData.wxTypeTAF.checked == false &&
			document.airportData.wxTypeMETAR.checked == false &&
			document.airportData.wxTypePIREP.checked == false &&
			document.airportData.wxTypeWAFD.checked == false){
		alert('You must check at least one report type.');
		return false;
	}
	//set report type
	if(document.airportData.wxTypeTAF.checked == true ){
		if(rt != "") rt += " ";
		rt += "TAF";
	}
	if(document.airportData.wxTypeMETAR.checked == true ){
		if(rt != "") rt += " ";
		rt += "METAR";
	}
	if(document.airportData.wxTypePIREP.checked == true ){
		if(rt != "") rt += " ";
		rt += "PIREP";
	}
	if(document.airportData.wxTypeWAFD.checked == true ){
		if(rt != "") rt += " ";
		rt += "WAFD";
	}
	
	 if (document.airportData.wxStation.value == ""){
		 alert('Please enter at least one well formed weather station \n(e.g. KLAX).')
		 return false
	 }else{
		 //examine wxStation list for validity
		 
		 var theList = document.airportData.wxStation.value;
		 theList = theList.split(" ");
		 var ctr = 0;
		 var stationCtr = 0;
		 var newStations = "";
		 while (ctr < theList.length) {
			 if(theList[ctr] == "" || (theList[ctr].length > 2 && theList[ctr].length < 5)){
				 if(theList[ctr] != ""){
					 stationCtr++;
					 newStations += " " + theList[ctr];
				 }
			 }else{
				 alert('Input station: '+theList[ctr]+' appears to be malformed. \n Please use IATA or ICAO station codes separated by spaces and try again.');
				 return false;
			 }
			 ctr++;
		 }
		 
	     var winLoc = winLocBase + '?icao=' +
	                    document.airportData.wxStation.value;
	         winLoc += '&rt=' + rt;

         var winWidth = 800;
         win=window.open(winLoc,'AirportData','menubar=1,scrollbars=yes,resizable=yes,width='+winWidth+',height=450');
         win.focus();
         return false;
	  }
}

function setAllFree(){
	if(document.airportData.selectAll.checked == true){
		document.airportData.wxTypeTAF.checked = true;
		document.airportData.wxTypeMETAR.checked = true;
		document.airportData.wxTypePIREP.checked = true;
		document.airportData.wxTypeWAFD.checked = true;
	}else{
		document.airportData.wxTypeTAF.checked = false;
		document.airportData.wxTypeMETAR.checked = false;
		document.airportData.wxTypePIREP.checked = false;
		document.airportData.wxTypeWAFD.checked = false;
	}
}

