// Validates the elements of date time on a form. validate_time indicates whether to validate time or only date
function validateDateTimeCombo(combo_name , form_name)
{
  var result = true ;
  
  if (document.forms.namedItem(form_name).elements.namedItem(combo_name + "_YEAR").value == '0000') 
    result = false ;
  else if (document.forms.namedItem(form_name).elements.namedItem(combo_name + "_MONTH").value == '00') 
    result = false ;
  else if (document.forms.namedItem(form_name).elements.namedItem(combo_name + "_DAY").value == '00') 
    result = false ;
  
  return result ;
}

function selectAllEntries(chkbox_name) 
{
  var cur_form = document.forms[0] ; // There is only one form per page
  
  if (! isNaN(cur_form.elements[chkbox_name].length)) 	
    for (var i = 0 ; i < cur_form.elements[chkbox_name].length ; i++)
      cur_form.elements[chkbox_name][i].checked = true ;
  else // there is only one item in the list
    cur_form.elements[chkbox_name].checked = true ;
}

function deselectEntries(chkbox_name) 
{
  var cur_form = document.forms[0] ; // There is only one form per page
  // If there is only one item in the list then 'document.frm_jbasket_list.chk_jbasket.length' is undefined and the for loop does not work
  if (!isNaN(cur_form.elements[chkbox_name].length)) 
    for (i = 0 ; i < cur_form.elements[chkbox_name].length ; i++)
	{
      if (cur_form.elements[chkbox_name][i].checked == false)
        cur_form.elements[chkbox_name][i].checked = true ;
	  else
        cur_form.elements[chkbox_name][i].checked = false ;	
	}	
  else // there is only one item in the list
  {
    if (cur_form.elements[chkbox_name].checked == false)
      cur_form.elements[chkbox_name].checked = true ;
	else
      cur_form.elements[chkbox_name].checked = false ;	
  }	
}

// Prepares the ids of the selected entries and puts them in the hidden input which is necessary in list forms  
function presubmitListForm(chkbox_name , result_input_name , warning_msg) 
{
  var result = true ; // final result
  var selected_entries = "" ; // ids of the selected items
  var cur_form = document.forms[0] ; // There is only one form per page
  
  // Ask the warning msg before proceeding
  if (warning_msg.length != 0)
    if (! confirm(warning_msg)) 
	  return false ; 

  // If there is only one item in the list then 'document.frm_jbasket_list.chk_jbasket.length' is undefined and the for loop does not work
  if (! isNaN(cur_form.elements[chkbox_name].length)) 
  {
    for (i = 0 ; i < cur_form.elements[chkbox_name].length ; i++)
      if (cur_form.elements[chkbox_name][i].checked == true)
      {	
	    if (selected_entries != "")
	      selected_entries = selected_entries + "','" + cur_form.elements[chkbox_name][i].value ;
	    else
	      selected_entries = "'" + cur_form.elements[chkbox_name][i].value ;  
	  }
  }
  else // there is only one item in the list
  {
    if (cur_form.elements[chkbox_name].checked == true)
	  selected_entries = "'" + cur_form.elements[chkbox_name].value ;  
  }
	
  if (selected_entries == "")
  {
    alert ("Please select the items by clicking on the check box next to them.") ;
    result = false ;
  }
  else
    cur_form.elements[result_input_name].value = selected_entries + "'" ;
  
  return result ;
}

// Prepares the ids of the selected entries and puts them in the hidden input which is necessary in list forms  
function prepareSelectionList(chkbox_name , result_input_name , key_name) 
{
  var result = true ; // final result
  var selected_entries = "" ; // jbasket_ids of the selected items
  var cur_form = document.forms[0] ; // There is only one form per page
  
  // If this object does not exist at all do nothing  
  if (document.getElementById(chkbox_name) == null)
    return result ;  

  // If there is only one item in the list then 'document.frm_jbasket_list.chk_jbasket.length' is undefined and the for loop does not work
  if (! isNaN(cur_form.elements[chkbox_name].length)) 
  {
    for (i = 0 ; i < cur_form.elements[chkbox_name].length ; i++)
      if (cur_form.elements[chkbox_name][i].checked == true)
      {	
	    if (selected_entries != "")
	      selected_entries = selected_entries + "'&" + key_name + "[]='" + cur_form.elements[chkbox_name][i].value ;
	    else
	      selected_entries = key_name + "[]='" + cur_form.elements[chkbox_name][i].value ;  
	  }
  }
  else // there is only one item in the list
  {
    if (cur_form.elements[chkbox_name].checked == true)
	  selected_entries = key_name + "[]='" + cur_form.elements[chkbox_name].value ;  
  }
	
  if (selected_entries != "")
    cur_form.elements[result_input_name].value = selected_entries + "'" ;
  return result ;
}

// Causes the form to navigate to a specific page
function doPageNavigation(new_page_input_name,new_page)
{
  cur_form = document.forms[0] ;
  cur_form.elements[new_page_input_name].value = new_page ; // NEW_PAGE_NO is name of the hidden input that holds the new page no to navigate to
  cur_form.submit() ;
}

function doRecordNavigation(new_rec_input_name,new_rec)
{
  cur_form = document.forms[0] ;
  cur_form.elements[new_rec_input_name].value = new_rec ; //new_rec_input_name  is name of the hidden input that holds the new record no to navigate to
  cur_form.submit() ;
}

// When user clicks on an iem on a list kind of filter, this function is called to filter the form
function handleListClick(list_input_name,new_val)
{
  cur_form = document.forms[0] ;
  cur_form.elements[list_input_name].value = new_val ; 
  cur_form.submit() ;
}

function trim(sString) 
{
  while (sString.substring(0,1) == ' ')
    sString = sString.substring(1, sString.length);
  while (sString.substring(sString.length-1, sString.length) == ' ')
    sString = sString.substring(0,sString.length-1);
  return sString;
}

// fromats a number according to format string and returns the string
function formatNum(cur_num , new_format) 
{
  // For now only accept "99"
  if (new_format == "99")
  {
    result = cur_num.toString() ;
	if (result.length < 2)
	  result = "0" + result;
  }
  return result ;
}

// Handles group selection on the checkboxes at the side of the lists
function handleRowSelect(cur_check_box , chkbox_name)
{
  var start_index = -1 , end_index = -1 ; 
  var cur_idx,             // Index of the current checkbox being clicked
      before_idx = -1,     // Index of the closest check box before current one which is checked
      after_idx = -1 ;     // Index of the closest check box after current one which is checked
  var cur_form = document.forms[0] ; // There is only one form per page  

  if (event.shiftKey == true)
  {
    // Find index of current selection
	for (i = 0 ; i < cur_form.elements[chkbox_name].length ; i++)
      if (cur_form.elements[chkbox_name][i] == cur_check_box)
	  {
	    cur_idx = i ;
        break ; // break after finding current selection		
	  }
	  
    // Find index of nearest selection before current selection
	for (i = cur_idx - 1 ; i >= 0  ; i--)
      if (cur_form.elements[chkbox_name][i].checked == true)
	  {
	    before_idx = i ;
        break ; // break after finding 		
	  }
	  
    // Find index of nearest selection after current selection
	for (i = cur_idx + 1 ; i < cur_form.elements[chkbox_name].length  ; i++)
      if (cur_form.elements[chkbox_name][i].checked == true)
	  {
	    after_idx = i ;
        break ; // break after finding 		
	  }
	
	// Priority is with after_idx as selection usually moves down 
	if (after_idx != -1)
	{
	  start_index = cur_idx ;
	  end_index = after_idx ;
	}
    else
    {
	  start_index = before_idx ;
	  end_index = cur_idx ;
    }
    // Now do the selection only if both start and end were found. Prevent shifts + single row selecion
    if (start_index != -1 && end_index != -1) 	
      for (i = start_index ; i <= end_index ; i++)
        cur_form.elements[chkbox_name][i].checked = true ;
  } 
}

// Changes the value in the dependant objects when user changes a selet item (usually for show only)
function handleDependants(form_name , select_obj , dependant_objs) 
{ 
  var new_value = select_obj.value ; 
  var cur_form = document.forms[form_name] ;
  for (var i = 0 ; i < dependant_objs.length ; i++) 
  {
    target_input_name = dependant_objs[i] ;
    source_input_name = target_input_name + "_" + new_value ;
    cur_form.elements[target_input_name].value = cur_form.elements[source_input_name].value ;
  }	
}


//debug probabaly remove var cur_action ;

