// $Id: webform_aux.js,v 1.0 2009/01/28 20:34:45 quicksketch Exp $

/**
 * Aux function for webform configuration and operation
 */

$(function(){
  // Check and configure the datepicker
  // Webforms with an unique date field
  if (document.getElementById('edit-submitted-date-datepicker-popup-0')!=null){
    $('#edit-submitted-date-datepicker-popup-0').datepicker({minDate: minDate, maxDate: maxDate, dateFormat: 'dd/M/yy',
       showOn: 'both', buttonImageOnly: true, buttonImage: '/egnos_ops/sites/all/modules/webform/calendar.gif',
       beforeShowDay: checkDates, firstDay: 1, dafaultDate: initialDate});

    $('#edit-submitted-date-datepicker-popup-0').attr('value',initialDate);
    $('#edit-submitted-date-datepicker-popup-0').attr('readOnly','true');
  }

  // Reports and Data-gaps webforms
  if (document.getElementById('edit-submitted-date1-datepicker-popup-0')!=null){
    $('#edit-submitted-date1-datepicker-popup-0').datepicker({minDate: minDate, maxDate: maxDate, dateFormat: 'dd/M/yy',
       showOn: 'both', buttonImageOnly: true, buttonImage: '/egnos_ops/sites/all/modules/webform/calendar.gif',
       firstDay: 1, dafaultDate: '-1M'});

    $('#edit-submitted-date2-datepicker-popup-0').datepicker({minDate: minDate, maxDate: maxDate, dateFormat: 'dd/M/yy',
       showOn: 'both', buttonImageOnly: true, buttonImage: '/egnos_ops/sites/all/modules/webform/calendar.gif',
       firstDay: 1, dafaultDate: '+0d'});

    $('#edit-submitted-date1-datepicker-popup-0').attr('readOnly','true');
    $('#edit-submitted-date1-datepicker-popup-0').attr('value',initialDate);
    $('#edit-submitted-date2-datepicker-popup-0').attr('readOnly','true');
    $('#edit-submitted-date2-datepicker-popup-0').attr('value',initialDate2);
  }

  // Custom graphics webform
  if (document.getElementById('edit-submitted-from-datepicker-popup-0')!=null){
    $('#edit-submitted-from-datepicker-popup-0').datepicker({minDate: minDate, maxDate: maxDate, dateFormat: 'dd/M/yy',
       showOn: 'both', buttonImageOnly: true, buttonImage: '/egnos_ops/sites/all/modules/webform/calendar.gif',
       beforeShowDay: checkDates, firstDay: 1, dafaultDate: initialDateFrom});

    $('#edit-submitted-to-datepicker-popup-0').datepicker({minDate: minDate, maxDate: maxDate, dateFormat: 'dd/M/yy',
       showOn: 'both', buttonImageOnly: true, buttonImage: '/egnos_ops/sites/all/modules/webform/calendar.gif',
       beforeShowDay: checkDates, firstDay: 1, dafaultDate: initialDateTo});

    $('#edit-submitted-from-datepicker-popup-0').attr('readOnly','true');
    $('#edit-submitted-from-datepicker-popup-0').attr('value',initialDateFrom);
    $('#edit-submitted-to-datepicker-popup-0').attr('readOnly','true');
    $('#edit-submitted-to-datepicker-popup-0').attr('value',initialDateTo);
    $('#edit-submit').click(function(){copyRIMS();});
//    $("'#"+checked+"'").attr('checked',true);
    $('#edit-submitted-type').attr('value',type);
  }

  // Check the RIMS map state
  if ((document.getElementById('edit-submitted-rims')!=null)&&(document.getElementById('edit-submitted-rims').value!='')){
    document.getElementById('rims-map-image').style.display = 'none';
    $('#toggleButton').attr('src','/egnos_ops/sites/all/images/maximize.png');
  }
});


/*
 * Aux function for checking valid dates in the calendar plugin
 */
function checkDates(date){
  date2check = date.getFullYear()+(((date.getMonth()+1)<10)?'0':'')+(date.getMonth()+1)+((date.getDate()<10)?'0':'')+date.getDate();

  if (jQuery.inArray(date2check, dates)<0){
    return [false, 'disable'];
  }
  else{
    return [true, ''];
  }
}

/*
 * Update the value of the selected RIMS when it is clicked on the map
 */
function setRim(rims){
  $("option[selected]").attr('selected',false);
  $("option[value='"+rims+"']").attr('selected',true);
  document.forms[0].submit();
}

function toggleImage(){
  $('#rims-map-image').toggle("slow");
}

/*
 * Hide or show the given div in order of the current state
 */
function toggleDiv(divId, toggleImage){
  $('#'+divId).slideToggle("slow");
  if (toggleImage)
  {
    if ($('#'+toggleImage).attr('src') == '/egnos_ops/sites/all/images/minimize.png')
      $('#'+toggleImage).attr('src','/egnos_ops/sites/all/images/maximize.png');
    else
      $('#'+toggleImage).attr('src','/egnos_ops/sites/all/images/minimize.png');
  }
}

/*
 * Aux function for the custom graphic section used for clearing the RIMS selection table
 */
function clearRIMS(){
 $('#rims-table td').removeClass("selected");
 $('#rims-table td').addClass("non-selected");
}

/*
 * Aux function for the custom graphic section used for (de)select a RIMS
 */
function toggleRIMS(id){
  if ($('#'+id).attr('className') == 'selected'){
    $('#'+id).attr('className','non-selected');
    index = jQuery.inArray(id, rimsList);
    rimsList.splice(index, 1);
  }else{
    if ($("#rb-dates").attr('checked') == true){
      clearRIMS();
      rimsList.shift();
    }
    rimsList.unshift(id);
    $('#'+id).attr('className','selected');
  }
}


function copyRIMS(){
  $('#edit-submitted-rimslist').attr('value',rimsList.toString());
}