/**************************************
snowi.js

SNOWI specific JavaScript functionality

Author: Scott Lewis
***************************************/

// clear_form
// Resets the "info" form to it's basic defaults

function clear_form()
{
	var frm, x, name, newval;

	// Get the form, return if it doesn't exist
	frm = document.getElementById('info');
	if (!frm) return;

	for (x = 0; x < frm.elements.length; x++)
	{
		if (frm.elements[x].nodeName == 'INPUT')
		{
			name = frm.elements[x].name;

			if (frm.elements[x].type == 'text')
			{
				if (name == 'start_time') newval = '00:00:00';
				else if (name == 'stop_time') newval = '23:59:59';
				else if (name == 'granule_limit') newval = '150';
				else if (name == 'time_limit') newval = '60';
				else if (name == 'country') newval = 'United States';
				else newval = '';

				frm.elements[x].value = newval;
			}
			else if (frm.elements[x].type == 'checkbox')
				frm.elements[x].checked = false;
		}
		else if (frm.elements[x].nodeName == 'SELECT')
		{
			name = frm.elements[x].name;

			if (name == 'user_aff_category')
			{
				for (var y = 0; y < frm.elements[x].length; y++)
				{
					if (frm.elements[x].options[y].value == 'University')
					{
						newval = y;
						break;
					}
				}
			}
			else
				newval = 0;

			frm.elements[x].selectedIndex = newval;
		}
		
	}
	update_dataset_title();
}



// update_dataset_versions
// Takes the value of the "dataset_id" element, and extracts the version 
// numbers.
// OPTIONAL PARAMETER: sel_version
//	If sel_version not supplied, will select most recent version
//	If sel_version supplied, will select chosen version; if version not found,
//    will choose most recent version and alert the user.

function update_dataset_versions(sel_version)
{
    // DEFAULT SELECTION
	sel_version = typeof(sel_version) != 'undefined' ? sel_version : '';

	var ds_box, ver_box, sn_box;
	var ds_val, ds_split;
	var ver_split, x, found_sel;

	ds_box = document.getElementById('dataset_title');
	ver_box = document.getElementById('dataset_version');

	// check to make sure the form elements exist
	if (!ds_box || !ver_box)
		return;

	while (ver_box.length > 0)
		ver_box.remove(0);		// remove all the current elements

	ds_val = ds_box.value;
	if (ds_val != '')
	{
		ds_split = ds_val.split(':::');
		if (ds_split.length < 4)
			return;
		ver_split = ds_split[3].split('|');
		found_sel = (sel_version == '');
		
		for (x = 0; x < ver_split.length; x++)
		{
			var ver_opt = document.createElement('option');
			ver_opt.text = ver_split[x];
			ver_opt.value = ver_split[x];
			try 
			{
				ver_box.add(ver_opt, null)
			}
			catch(ex)
			{
				ver_box.add(ver_opt);
			}
			
			if (ver_split[x] == sel_version)
			{
				ver_box.selectedIndex = ver_box.length - 1;
				found_sel = true;
			}
		}

		if (!found_sel)
		{
			alert('The version you entered is not available.\n' 
                  + 'Using latest available version instead.'
                 );
		}
		
	}

    // if no versions available, create a dummy one
    if (ver_box.length == 0)
    {
        var ver_opt = document.createElement('option');
        ver_opt.text = 'N/A';
        ver_opt.value = '';
        try
        {
            ver_box.add(ver_opt, null)
        }
        catch(ex)
        {
            ver_box.add(ver_opt);
        }
    }

	update_dataset_id();
}



// update_dataset_title
// Takes the value of the "dataset_id" element, and finds the corresponding 
// entry in the "dataset_title" dropdown element.

function update_dataset_title()
{
	var sn_box, ds_box;
	var sn_name, sn_ver;
	var ds_val;
	var x, found_sel;

	sn_box = document.getElementById('dataset_id');
	ds_box = document.getElementById('dataset_title');

	// check to make sure both form elements exist
	if (!sn_box || !ds_box)
		return;			

	if (sn_box.value.indexOf('-') >= 0)
	{
		var sn_split = sn_box.value.split('-');
		sn_name = sn_split[0];
		sn_ver = sn_split[1];
	}
	else if (sn_box.value.indexOf('NISE') == 0)
    {
        sn_name = 'NISE';
        if (sn_box.value.length > 4)
            sn_ver = sn_box.value.substring(4);
    }
    else
	{
		sn_name = sn_box.value;
		sn_ver = '';
	}

	found_sel = false;

	for (x = 0; x < ds_box.length; x++)
	{
		var ds_split = ds_box.options[x].value.split(':::');
		ds_val = ds_split[0];
		if (ds_val.toLowerCase() == sn_name.toLowerCase())
		{
			ds_box.options[x].selected = true;
			found_sel = true;;
		}
	}

	if (!found_sel)
	{
		alert('The Short Name / ID you have entered does not match a '
              + 'valid Dataset!');		
		ds_box.options[0].selected = true;
	}

	update_dataset_versions(sn_ver);
}


// update_dataset_id
// Takes the value of the "dataset_title" and "dataset_version" elements, and 
// extracts the corresponding values to put in the "dataset_id" element

function update_dataset_id()
{
	var sn_box, ds_box;

	sn_box = document.getElementById('dataset_id');
	ds_box = document.getElementById('dataset_title');
	ver_box = document.getElementById('dataset_version');

	// check to make sure both form elements exist
	if (!sn_box || !ds_box || !ver_box)
		return;

	if (ds_box.value != '')
	{
		sn_box.value 
            = ds_box.options[ds_box.selectedIndex].value.split(':::')[0] + '-'
              +	ver_box.value;

        sn_box.value = sn_box.value.replace(/NISE-/, 'NISE');
	}
	else
	{
		sn_box.value = '';
	}
}


// toggle_ftp_push
// Displays or hides the FTP Push information, depending on the parameter
// "showit" is a boolean that is true if it should be displayed, false if not.

function toggle_ftp_push(showit)
{
	var showclass;
	var numftp, x;

	if (showit)
		showclass = 'table-row';
	else
		showclass = 'none';
	
	numftp = document.getElementById('num_ftppush_rows').value;

	for (x = 1; x <= numftp; x++)
	{
		document.getElementById('ftppush_row_'+x).style.display = showclass;
	}
}



// toggle_granule_list_checks
// Will toggle all the granule_list checkboxes to ON or OFF

function toggle_granule_list_checks() {
    
    this.newtoggle = !this.newtoggle;

    var ids = document.getElementsByName('granule_list');

    for (var x = 0; x < ids.length; x++) {
        if (ids[x].type == 'checkbox')
            ids[x].checked = this.newtoggle;
    }
}



// toggle_order_info
// Will toggle the Order Info fields (mostly for Search & Order)

function toggle_order_info(showorder) {

    if (!document.getElementById('orderinfo'))
        return;

    if (showorder)
        document.getElementById('orderinfo').style.display = '';
    else
    {
        var o_m = document.getElementById('order_method');

        if (o_m) {
            for (x = 0; x < o_m.options.length; x++) {
                if (o_m.options[x].value == 'FtpPull') {
                    o_m.options[x].selected = true;
                }
            }
            toggle_ftp_push(false);
        }
        
        document.getElementById('orderinfo').style.display = 'none';        
    }

}



// form_init
// Calls various form initialization functions, mostly for triggering actions
// that are called when a select box change is made.

function form_init()
{
    if (document.getElementById('dataset_title')) {
        update_dataset_title();
    }
    if (document.getElementById('search_and_order')) {
        toggle_order_info(
            document.getElementById('search_and_order').value == 'Y');
    }
    if (document.getElementById('order_method')) {
        toggle_ftp_push(
            document.getElementById('order_method').value == 'FtpPush');
    }
}
