function isAnInteger (possibleNumber) {

    return (parseInt (possibleNumber, 10) == possibleNumber);
}

function toSeconds(duration)
{
    var hour, min, sec;
    for (var i = 0; i < 3; i++) {
        var tmpStr = duration.substring (0, duration.indexOf (':'));
        duration = duration.substring (duration.indexOf (':') + 1, duration.length);
        if (i == 0) hour = parseInt (tmpStr, 10);
            else
        if (i == 1) min = parseInt (tmpStr, 10);
            else
        if (i == 2) sec = parseInt (duration, 10);
    } 
    dursec = hour*3600 + min*60 + sec;
    return dursec;
}

function plotWindow () {

// Validate the data
// Check the duration

    var duration = document.menuform.duration.value;
    var hour, min, sec;
    for (var i = 0; i < 3; i++) {
        var tmpStr = duration.substring (0, duration.indexOf (':'));
        duration = duration.substring (duration.indexOf (':') + 1, duration.length);
        if (i == 0) hour = parseInt (tmpStr, 10);
            else
        if (i == 1) min = parseInt (tmpStr, 10);
            else
        if (i == 2) sec = parseInt (duration, 10);
    } 

    if (hour > 24) {
        alert ("Hour in duration is too big!");
    } 
    if ((min < 0) || (min > 59)) {
        alert ("Minute in duration is too big!");
        return (false);
    } 
    if ((sec < 0) || (sec > 59)) {
        alert ("Second in duration is too big!");
        return (false);
    } 
    if (isAnInteger (hour) && isAnInteger (min) && isAnInteger (sec)) {
        var timespan = hour * 3600 + min * 60 + sec;
        if (timespan > 86400) {
            alert ("Time span (duration) greater than twenty-four hours which is not allowed!");
            return (false);
        } /*else 
            if (timespan < 1) {
                alert ("Time span (duration) is invalid!");
                return (false);
            } */
    } else {
        alert ("Please use the correct format for duration time:  h:mm:ss");
        return (false);
    }

   /* Check for max time */
    maxdur = plotLayoutMaxTimes[document.menuform.satellite.selectedIndex];
    maxsec = toSeconds(maxdur);
    dursec = toSeconds(document.menuform.duration.value);
    if(dursec>maxsec) {
      alert("Duration is too big! (Max: "+ maxdur + ")");
      return false;
    }

// Check the start time

    var start_time = document.menuform.starttime_d.value;
    if (start_time.length < 12) {
        alert ("Start time should be in the format YYYY-DDD/HH:MM:SS!");
        return (false);
    }
    var year = start_time.substring (0, start_time.indexOf ('-'));
    if (isAnInteger (year)) year = parseInt (year, 10);

    if ((year < 1999) || (year > 2032)) {
        alert ("Year in start time is invalid!");
        return (false);
    }

    var day = start_time.substring (start_time.indexOf ('-') + 1, start_time.indexOf ('/'));
    if (isAnInteger (day)) day = parseInt (day, 10);
    if ((day < 1) || (day > 366)) {
        alert ("Day in start time is invalid!");
        return (false);
    }

    start_time = start_time.substring (start_time.indexOf ('/') + 1, start_time.length);
    for (var i = 0; i < 3; i++) {
        var tmpStr = start_time.substring (0, start_time.indexOf (':'));
        start_time = start_time.substring (start_time.indexOf (':') + 1, start_time.length);
        if (i == 0) hour = parseInt (tmpStr, 10);
            else
        if (i == 1) min = parseInt (tmpStr, 10);
            else
        if (i == 2) sec = parseInt (start_time, 10);
    }
    if (hour > 23) {
        alert ("Hour in start time is too big!");
        return (false);
    } 
    if ((min < 0) || (min > 59)) {
        alert ("Minute in start time is too big!");
        return (false);
    } 
    if ((sec < 0) || (sec > 59)) {
        alert ("Second in start time is too big!");
        return (false);
    } 
// Check if duration isn't longer than max time


// If we want multiple windows, change the target window

    if (document.menuform.autop.value == "autopop") {
        var temp = parseInt(document.menuform.num.value, 10);
        temp++;
        temp = temp + "M";
        document.menuform.num.value = temp;
        document.menuform.target=document.menuform.num.value;
        targetWindow = document.menuform.num.value;
    } else {
        targetWindow = "targetPlotWindow";
        document.menuform.target="targetPlotWindow";
    }
////////////////////////////////////////////////////////////
// Determine the size of the plot window                  //
////////////////////////////////////////////////////////////
    var plotsize = document.menuform.plotsize[document.menuform.plotsize.selectedIndex].text;
    var width = parseInt (plotsize.substring (0, plotsize.indexOf ('x')), 10);
    var height = parseInt (plotsize.substring (plotsize.indexOf ('x') + 1, plotsize.length), 10);
    var selectedPlot = document.menuform.satellite[document.menuform.satellite.selectedIndex].value;
    var appletPadding = 160;
    var noAppletPadding = 220;

    // Fix width for small plots
    if(width<=300)
       width=500;

    // Determine for 'Java enabled'
    if (document.menuform.javaon[1].checked) {
      // Determine for 'Contour'
      if(selectedPlot.search("spinpad")!=-1 && 
         selectedPlot!="C1-C4_spinpad") 
      {
        var tmpHeight = width + appletPadding;
        width = height;
        height = tmpHeight;
      }
      // Determine for 'Spectogram'
      else {
        height = height + appletPadding;
      }
      var paramStr = "toolbar=no,scrollbars=yes,resizable=yes,";
    }
    // Determine for 'Java disabled'
    else {
      // Determine for 'Contour'
      if(selectedPlot.search("spinpad")!=-1 && 
         selectedPlot!="C1-C4_spinpad") 
      {
        var tmpHeight = height + noAppletPadding;
        width = height + 20;
        height = tmpHeight;
        var paramStr = "toolbar=no,scrollbars=yes,resizable=yes,";
      }
      // Determine for 'Spectogram'
      else {
        width = width +20;
        height = height + noAppletPadding;
        var paramStr = "toolbar=yes,status=yes,scrollbars=yes,resizable=yes,";
      }
    }
    width = width+50;
    paramStr = paramStr+ "width=" + width + ",height=" + height;

    //alert(paramStr);
    //var paramStr = "toolbar=yes,scrollbars=yes,resizable=no,width=" + width + ",height=" + height;
    open ("", targetWindow, paramStr);
    return (true);
}                                

function setDateValues () 
{
    //document.dateForm.greg_date.value = '' + year + '-' + padout(month - 0 + 1) + '-' + padout(day);
    //document.dateForm.jul_date.value = convertDate (document.dateForm.greg_date.value);
    greg_date = '' + year + '-' + padout(month - 0 + 1) + '-' + padout(day);
    jul_date = convertDate (greg_date);
    var start_time = document.menuform.starttime_d.value;
    start_time = start_time.substring (start_time.indexOf ('/'), start_time.length);
    document.menuform.starttime_d.value = year + '-' + jul_date + start_time;
    cal_window.close();
}

function openbw (loc)
{
     /* Make window width and height 80% of screen size */
     win_h = window.screen.height * 0.60;
     win_w = window.screen.width * 0.80;
     open (loc,
           "M","menubar=yes,scrollbars=yes,resizable=yes,width=" + win_w + ",height=" + win_h);
}

function openExamples()
{
    //openbw('generated/choices.php')
    win_h = window.screen.height * 0.80;
    win_w = window.screen.width * 0.60;
    ex_window = window.open ("layouts/samples/generated/choices.php", "ex",
//    ex_window = window.open ("layout_tree/layout_tree.php", "ex",
        "menubar=no,resizable=yes,scrollbars=yes,width=" + win_w + ",height=" + win_h);
    //ex_window.location.href = 'generated/examples.php';
    if (ex_window.opener == null) ex_window.opener = self;
}

function openExamplesBeta()
{
    win_h = window.screen.height * 0.60;
    win_w = window.screen.width * 0.80;
    //ex_window = window.open ("layout_tree/layout_tree.php", "ex",
    ex_window = window.open ("LayoutBrowser/index.html", "ex",
        "menubar=no,resizable=yes,scrollbars=yes,width=" + win_w + ",height=" + win_h);
    if (ex_window.opener == null) ex_window.opener = self;
}

// Open specific plot layout
function openExamplesIDBeta(i)
{
    win_h = window.screen.height * 0.60;
    win_w = window.screen.width * 0.80;
    //ex_window = window.open ("layout_tree/layout_tree.php?pl_id="+i, "ex",
    ex_window = window.open ("LayoutBrowser/index.html?pl_id="+i, "ex",
        "menubar=no,resizable=yes,scrollbars=yes,width=" + win_w + ",height=" + win_h);
    if (ex_window.opener == null) ex_window.opener = self;
}

function checkC3Combo ()
{
    if ((document.menuform.satellite.options[document.menuform.satellite.selectedIndex].text== "C3_combo") ||
        (document.menuform.satellite.options[document.menuform.satellite.selectedIndex].text== "C1_P_and_W")) {
        document.dateForm.greg_date.value = "2001-08-16";
        document.dateForm.jul_date.value = convertDate (document.dateForm.greg_date.value);
        document.menuform.starttime_d.value = "2001-228/09:00:00";
        document.menuform.duration.value = "2:00:00";
    }
}

/* Initialize the plotsize */
function initialize_plotsize()
{
  selectItemFromList (document.menuform.plotsize, getCookie("ck_plotsize"));
}

/* Save the plot size to a cookie*/
function save_plotsize()
{
  setCookie("ck_plotsize", get_selected(document.menuform.plotsize));
}

/* Set the selected layout */
function set_layout(layout)
{
   LoadPlotLayoutFromCaption(layout)
}
