﻿// JScript File
    

 document.onclick=CloseCalendars; 
   
  function CloseCalendars()
  {
     if($find('StartCalendar')!= null)
      $find('StartCalendar').hide();
    if($find('EndCalendar')!=null)
      $find('EndCalendar').hide();
  }
 


function OnSatelliteChanged()
{
    var satelliteCombo = document.Form1.Satellite;
    if(satelliteCombo != null && satelliteCombo.selectedIndex != -1)
    {  
        var satSystem = satelliteCombo.options[satelliteCombo.selectedIndex].value;
        LookAngleWebService.GetEphemeris(satSystem,EnableEphemeris);
       
    }
    OnCheckedChanged();
}

function EnableEphemeris(result,eventArgs)
{
  if(result == 'true')
  {
    document.Form1.cbEphemeris.disabled = false;
  }
  else
  {
   document.Form1.cbEphemeris.disabled = true;
  }
}

function OnCheckedChanged()
{

  var selected = document.Form1.cbEphemeris.checked;
  
  if(selected==true)
  {
    document.getElementById('btnStartDate').disabled = false;
    document.getElementById('btnEndDate').disabled = false;
    document.Form1.StartDate.disabled = false;
    document.Form1.EndDate.disabled = false;
    
    
    var satelliteCombo = document.Form1.Satellite;
    if(satelliteCombo != null && satelliteCombo.selectedIndex != -1)
    {  
        var satSystem = satelliteCombo.options[satelliteCombo.selectedIndex].value;
        LookAngleWebService.GetEndDate(satSystem,SetEndDate);
       
    }
   
  }
  else
  {
     document.Form1.StartDate.disabled = true;
     document.Form1.EndDate.disabled = true;
     document.Form1.StartDate.value = "";
     document.Form1.EndDate.value = "";
     document.getElementById('btnStartDate').disabled = true;
     document.getElementById('btnEndDate').disabled = true;
  }

}
//sets the start and end date text boxes
function SetEndDate(result,eventArgs)
{
  var value = result.split('@');
  if( value.length==4)
  {
   document.getElementById('StartDate').value=value[0];
   document.getElementById('EndDate').value=value[1];
   var start = new Date(value[0]);
   var end = new Date(value[1]);
  //updates the highlighted dates in the calendars
  $find('StartCalendar').set_selectedDate(start);
  $find('EndCalendar').set_selectedDate(end);
        
  }
   
}


function onCityChanged()
{
 var cityCombo = document.Form1.City;
 if (cityCombo != null && cityCombo.selectedIndex > 0)
    {
      var latlong = cityCombo.options[cityCombo.selectedIndex].value;
      LookAngleWebService.GetLatLong(latlong,SetLatLong);

    }
}

function SetLatLong(result,EventArgs)
{
  var value = result.split('@');
  if( value.length==2)
  {
    document.Form1.Latitude.value = value[0];
    document.Form1.Longitude.value = value[1];
    
  }
}


function onInputChanged()
{
    var inputRadios = document.Form1.Input;
    var countryCombo = document.Form1.Country;
    var cityCombo = document.Form1.City;
    var latTextBox = document.Form1.Latitude;
    var lonTextBox = document.Form1.Longitude;
    if(inputRadios[0].checked)
    {
        //city/country input selected
        countryCombo.disabled = false;
        cityCombo.disabled = (countryCombo.selectedIndex < 0);//enabled based on country selection
        //onCityChanged();//be sure to update the lat/lon fields
       latTextBox.disabled = true;
       lonTextBox.disabled = true;
    }
    else
    {
        //direct input selected
        countryCombo.disabled = true;
        cityCombo.disabled = true;
        latTextBox.disabled = false;
        lonTextBox.disabled = false;
    }
}



function validate()
{
    var isValid = true;
   
     
    //inputs to validate
    var satellite = document.Form1.Satellite;
    var input = document.Form1.Input;
    var country = document.Form1.Country;
    var city = document.Form1.City;
    var latitude = document.Form1.Latitude;
    var longitude = document.Form1.Longitude;

    //validators
    var countryValidator = new getObj('CountryValidator');
    var cityValidator = new getObj('CityValidator');
    var latitudeValidator = new getObj('LatitudeValidator');
    var longitudeValidator = new getObj('LongitudeValidator');

    //necessary numbers
    //SYSTEM_NAME:SAT_NAME:SAT_LONG_NOM
    var slon = parseFloat(satellite.options[satellite.selectedIndex].value.split('@')[2]);
    var lat = parseFloat(latitude.value);
    var lon = parseFloat(longitude.value);


    if(input[0].checked)
    {
        //Country/City selected so validate the selections
        if(country.selectedIndex < 0)
        {
            countryValidator.obj.innerHTML = '(Required)';
            countryValidator.style.visibility = 'visible';
            isValid = false;
        }
        else
        {
            countryValidator.style.visibility = 'hidden';
        }

        if(city.selectedIndex <= 0)
        {
            cityValidator.obj.innerHTML = '(Required)';
            cityValidator.style.visibility = 'visible';
            isValid = false;
        }
        else
        {
            cityValidator.style.visibility = 'hidden';
        }

        //don't bother validating the format of the numbers because they are auto stuffed
        //but we do still need to validate the loose box around the satellite
        if(lat <= -85.0 || lat >= 85.0)
        {
            latitudeValidator.obj.innerHTML = '(Not visible from satellite)';
            latitudeValidator.style.visibility = 'visible';
            isValid = false;
        }
        else
        {
            latitudeValidator.style.visibility = 'hidden';
        }

        if(getLongitudeDistance(slon, lon) > 85.0)
        {
            longitudeValidator.obj.innerHTML = '(Not visible from satellite)';
            longitudeValidator.style.visibility = 'visible';
            isValid = false;
        }
        else
        {
            longitudeValidator.style.visibility = 'hidden';
        }
    }
    else
    {
        //Latitude/Longitude option is selected so we don't need to valide the country/city
        countryValidator.style.visibility = 'hidden';
        cityValidator.style.visibility = 'hidden';

        //validate the latitude and longitude
        if(isNaN(lat))
        {
            latitudeValidator.obj.innerHTML = '(Required)';
            latitudeValidator.style.visibility = 'visible';
            isValid = false;
        }
        else
        {
            if(lat <= -90.0 || lat >= 90.0)
            {
                latitudeValidator.obj.innerHTML = '(-90.0 < Latitude < 90.0)';
                latitudeValidator.style.visibility = 'visible';
            //     calculateValidator.obj.innerHTML = "False";//isValid = false;
            }
            else if(lat <= -85.0 || lat >= 85.0)
            {
                latitudeValidator.obj.innerHTML = '(Not visible from satellite)';
                latitudeValidator.style.visibility = 'visible';
                isValid = false;
            }
            else
            {
                //validation passes
                latitudeValidator.style.visibility = 'hidden';
            }
        }

        if(isNaN(lon))
        {
            longitudeValidator.obj.innerHTML = '(Required)';
            longitudeValidator.style.visibility = 'visible';
            isValid = false;
        }
        else
        {
            if(lon <= -360.0 || lon >= 360.0)
            {
                longitudeValidator.obj.innerHTML = '(-360.0 < Longitude < 360.0)';
                longitudeValidator.style.visibility = 'visible';
                isValid = false;
            }
            else if(getLongitudeDistance(slon, lon) >= 85.0)
            {
                longitudeValidator.obj.innerHTML = '(Not visible from satellite)';
                longitudeValidator.style.visibility = 'visible';
                isValid = false;
            }
            else
            {
                longitudeValidator.style.visibility = 'hidden';
                  
            }
        }
    }
    
    if(document.Form1.cbEphemeris.checked ==1)
    {
    //inputs to validate
    var startDate = document.Form1.StartDate;
    var endDate = document.Form1.EndDate;

    //validators
    var startValidator = new getObj('StartValidator');
    var endValidator = new getObj('EndValidator');

    //necessary numbers
    var start = Date.parse(startDate.value);
    var end = Date.parse(endDate.value);
    if(isNaN(start))
    {
        if(startDate.value == '')
            startValidator.obj.innerHTML = '(Required)';
        else
        startValidator.obj.innerHTML = '(Invalid Date Format)';
        startValidator.style.visibility = 'visible';
        isValid = false;
    }
    else
    {
        startValidator.style.visibility = 'hidden';
    }

    if(isNaN(end))
    {
        if(endDate.value == '')
            endValidator.obj.innerHTML = '(Required)';
        else
          endValidator.obj.innerHTML = '(Invalid Date Format)';
          endValidator.style.visibility = 'visible';
          isValid = false;
    }
    else
    {
               
        endValidator.style.visibility = 'hidden';
    }

   
    //since both dates were given
    if( isValid)
    {
        if(end < start)
        {
            startValidator.obj.innerHTML = '(Start Date > End Date)';
            startValidator.style.visibility = 'visible';
            isValid = false;
        }
        else
        {
            startValidator.style.visibility = 'hidden';
        }
    }
    }//end of date validation
  
  
    return isValid;
}


function getLongitudeDistance(satLon, lon)
{
    //get both in the [0, 360) range
    if (satLon < 0)
        satLon += 360.0;

    if (lon < 0)
        lon += 360.0;

    //now determine the distance between the points directly
    //and wrapping around the 0.0/360.0 longitude
    var straight = 0.0;
    var wrap = 0.0;
    if (lon < satLon)
    {
        straight = satLon - lon;
        wrap = (360.0 - satLon) + lon;
    }
    else
    {
        straight = lon - satLon;
        wrap = (360.0 - lon) + satLon;
    }

    //return the smaller value
    return straight < wrap ? straight : wrap;
}

//latitude/longitude auto-format function
function formatNumber(sender, precision)
{
    if(sender != null)
    {
        var num = (parseFloat(sender.value)).toFixed(precision);
        if(!isNaN(num)) sender.value = num;
    }
}
function onBlurLatitude()
{
    formatNumber(document.Form1.Latitude, 2);
}
function onBlurLongitude()
{
    formatNumber(document.Form1.Longitude, 2);
  
}

function OpenTimeZoneWindow()
{
    win = window.open('TimeZone.aspx', 'TimeZoneWindow_OSI', 'resizable=0,width=445,height=460');
    win.focus();
    return false;
}

function UpdateTimeZone()
{
    __doPostBack('TimeZoneLink','')
}

function OpenHelpWindow()
{
    win = window.open('LookAngleHelp.htm', 'MainHelpWindow_OSI', 'resizable=1,scrollbars=1,width=450,height=400');
    win.focus();
    return false;
}

function OpenTermsOfUseWindow()
{
    win = window.open('TermsOfUse.htm', 'TermsOfUseWindow_OSI', 'resizable=1,scrollbars=1,width=450,height=425');
    win.focus();
    return false;
}

var DHTML = (document.getElementById || document.all || document.layers);
function getObj(name)
{
    if (document.getElementById)
    {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
    }
    else if (document.all)
    {
        this.obj = document.all[name];
        this.style = document.all[name].style;
    }
    else if (document.layers)
    {
        this.obj = document.layers[name];
        this.style = document.layers[name];
    }
}

//add handlers to the extenders 
function pageLoad(sender, args)
{
   var casc = $find("cityddl");
   casc.add_populated(onPopulated);
        
}
    
  

 /*KNOWN AJAX BUG(WORKITEM #12189) :CascadingDropDown not obeying
   * disabled state of DropDownList, hence setting it forcefully */
   function onPopulated()
   {   
       $get("City").disabled = $get("Country").disabled
   }
   /*Issue with calendar extender: does not update the highlighted date in the calendar
   * if date entered in text box is not in its default format.Hence converting user
   * entered date to correct format for both the start and the end date textboxes*/
   
   function onBlurStartDate()
   {
      var startDate = document.Form1.StartDate;
      var start = Date.parse(startDate.value);
      
      if(isNaN(start))
      {
         if(startDate.value != '')
         {
            var startValidator = new getObj('StartValidator');
            startValidator.obj.innerHTML = '(Invalid Date Format)';
            startValidator.style.visibility = 'visible';
         }
      }
     else
     { //check dates validity and return it in the format "dd MMM yyyy HH:mm:ss"
       LookAngleWebService.CheckDateValidity(startDate.value,GetStartValidity);
     } 
   }
   
   function GetStartValidity(result)
   {    
        document.getElementById('StartDate').value= result;
        var start = new Date(result);
        $find('StartCalendar').set_selectedDate(start);
   }
  
  function onBlurEndDate()
  {
      var endDate = document.Form1.EndDate;
      var end = Date.parse(endDate.value);
     if(isNaN(end))
     {
       if(endDate.value != '')
       {
          var endValidator = new getObj('EndValidator');
          endValidator.obj.innerHTML = '(Invalid Date Format)';
          endValidator.style.visibility = 'visible';
       }
     }
     else
     {
       //check dates validity and return it in the format "dd MMM yyyy HH:mm:ss"
       LookAngleWebService.CheckDateValidity(endDate.value,GetEndValidity);
     } 
   }
   
   function GetEndValidity(result)
   {
        document.getElementById('EndDate').value= result;
        var end = new Date(result);
        $find('EndCalendar').set_selectedDate(end);
   }
  