// The Freecycle Network MessageMaker Javascript
// (c) The Freecycle Network
// Author: Richard Wallman

var error_count = 0;
var warning_count = 0;

// ----------------------------------------------------------------------------------------------------------------------------
function clear_the_form() {
  document.getElementById('subject').value = '';
  document.getElementById('location').value = '';
  document.getElementById('bodytext').value = '';
  document.getElementById('fop_agree').checked = 0;
  document.getElementById('post_type').selectedIndex = 0;
  update_bodytext_count();
  document.getElementById('send_yg').disabled = 0;
  document.getElementById('send_email').disabled = 0;
  nohighlight_input('subject');
  nohighlight_input('location');
  nohighlight_input('bodytext');
  document.getElementById('fop_agree_label').style.display = '';
  document.getElementById('fop_agree').disabled = false;
  document.getElementById('fop_agree').style.display = '';
  document.getElementById('fop_agree').checked = false;  
}

// ----------------------------------------------------------------------------------------------------------------------------
function update_bodytext_count() {
  var the_elem = document.getElementById('bodytext_count').innerHTML =
    "(" + (MAXIMUM_DESCRIPTION_LENGTH - document.getElementById('bodytext').value.length) + " characters remaining)";
}

// ----------------------------------------------------------------------------------------------------------------------------
function highlight_input(id) {
  var obj = document.getElementById(id).style.background = '#fcc';
}

// ----------------------------------------------------------------------------------------------------------------------------
function dehighlight_input(id) {
  var obj = document.getElementById(id).style.background = '';
}

// ----------------------------------------------------------------------------------------------------------------------------
function nohighlight_input(id) { // back to white
  var obj = document.getElementById(id).style.background = '#fff';
}

// ----------------------------------------------------------------------------------------------------------------------------
function capital_percentage(the_string) {
  // Find all the letters in the string
  var all_letters = the_string.match(/[A-Z]/gi);
  if ( all_letters == null) return 0;

  // And get the capitals from within it
  var cap_letters = the_string.match(/[A-Z]/g);
  if ( cap_letters == null ) return 0;

  // Return the percentage, 0 to 100
  return ( cap_letters.length * 100 ) / all_letters.length ;
}

// ----------------------------------------------------------------------------------------------------------------------------
function check_subject() {
  var the_subj = document.getElementById('subject').value;
  var the_label = document.getElementById('errors');
  var error_flag = 0;

  // Clear highlights
  dehighlight_input('subject');

  // Check for mere presence
  if ( FORCE_ITEM_PRESENT && !the_subj ) {
    highlight_input('subject');
    the_label.innerHTML += 'Error - you need to enter something for the short item description!<br />';
    error_count++;
    error_flag = 1;
  };

  // Check for length
  if ( WARN_FOR_ITEM_LENGTH && (the_subj.length > MAXIMUM_ITEM_LENGTH) ) {
    highlight_input('subject');
    the_label.innerHTML += 'Warning - the short item description must be shorter than ' + MAXIMUM_ITEM_LENGTH + ' characters<br />';
    error_count++;
    error_flag = 1;
  };

  // Check for capitals
  if ( WARN_FOR_ITEM_CAPITALS && (capital_percentage(the_subj) > MAXIMUM_ITEM_CAPS_PERCENTAGE) ) {
    highlight_input('subject');
    the_label.innerHTML += 'Warning - the short item description must be less than ' + MAXIMUM_ITEM_CAPS_PERCENTAGE + '% capitals<br />';
    error_count++;
    error_flag = 1;
  };

  // Return with our error status
  return error_flag;
}

// ----------------------------------------------------------------------------------------------------------------------------
function check_location() {
  var the_loc = document.getElementById('location').value;
  var the_label = document.getElementById('errors');
  var error_flag = 0;

  // Clear highlights
  dehighlight_input('location');

  // Check for mere presence
  if ( FORCE_LOCATION_PRESENT && !the_loc ) {
    highlight_input('location');
    the_label.innerHTML += 'Error - you need to enter something for your location!<br />';
    error_count++;
    error_flag = 1;
  };

  // Check for length
  if ( WARN_FOR_LOCATION_LENGTH && (the_loc.length > MAXIMUM_LOCATION_LENGTH) ) {
    highlight_input('location');
    the_label.innerHTML += 'Warning - your location must be shorter than ' + MAXIMUM_LOCATION_LENGTH + ' characters<br />';
    error_count++;
    error_flag = 1;
  };

  // Check for capitals
  if ( WARN_FOR_LOCATION_CAPITALS && (capital_percentage(the_loc) > MAXIMUM_LOCATION_CAPS_PERCENTAGE) ) {
    highlight_input('location');
    the_label.innerHTML += 'Warning - your location must be less than ' + MAXIMUM_LOCATION_CAPS_PERCENTAGE + '% capitals<br />';
    error_count++;
    error_flag = 1;
  };

  // Return with our error status
  return error_flag;
}

// ----------------------------------------------------------------------------------------------------------------------------
function check_bodytext() {
  var the_body = document.getElementById('bodytext').value;
  var the_label = document.getElementById('errors');
  var error_flag = 0;

  // Clear highlights
  dehighlight_input('bodytext');

  // Check for mere presence
  if ( FORCE_DESCRIPTION_PRESENT && !the_body ) {
    highlight_input('bodytext');
    the_label.innerHTML += 'Error - you need to enter something for your description!<br />';
    error_count++;
    error_flag = 1;
  };

  // Check for length
  if ( WARN_FOR_DESCRIPTION_LENGTH && (the_body.length > MAXIMUM_DESCRIPTION_LENGTH) ) {
    highlight_input('bodytext');
    the_label.innerHTML += 'Warning - your description must be shorter than ' + MAXIMUM_DESCRIPTION_LENGTH + ' characters<br />';
    error_count++;
    error_flag = 1;
  };

  // Check for capitals
  if ( WARN_FOR_DESCRIPTION_CAPITALS && (capital_percentage(the_body) > MAXIMUM_DESCRIPTION_CAPS_PERCENTAGE) ) {
    highlight_input('bodytext');
    the_label.innerHTML += 'Warning - your description must be less than ' + MAXIMUM_DESCRIPTION_CAPS_PERCENTAGE + '% capitals<br />';
    error_count++;
    error_flag = 1;
  };

  // Return with our error status
  return error_flag;
}

// ----------------------------------------------------------------------------------------------------------------------------
function form_is_okay() {
  error_count = 0;
  warning_count = 0;
  document.getElementById('errors').innerHTML = '';

  return !(check_subject() + check_location() + check_bodytext());
}

// ----------------------------------------------------------------------------------------------------------------------------
function get_formatted_subject_line() {
  var subj = document.getElementById('post_type').value + ": "
         + document.getElementById('subject').value;
  
  if ( document.getElementById('location').value ) {
    subj += " (" + document.getElementById('location').value + ")";
  };

  if (SHOW_FOP_SUBJECT && document.getElementById('fop_agree').checked == true ) {
    subj += " FOP";
  };

  return subj;
   
};

// ----------------------------------------------------------------------------------------------------------------------------
function get_formatted_bodytext() {
  var bodytext = document.getElementById('bodytext').value;
  
  if (SHOW_FOP_BODY && document.getElementById('fop_agree').checked == true) {
    bodytext += "\n";
    
    for(var i=0;i<=FOP_LINE.length-1;i++)
    {
    	if(FOP_LINE[i] != "")
    	{
		bodytext += "\n" + FOP_LINE[i];
    	}
    }
  }
  
  if (INCLUDE_CUSTOM_FOOTER == 1) {
    bodytext += "\n";
    
    for(var i=0;i<=FOP_LINE.length-1;i++)
    {
    	if(FOOTER_LINE[i] != null && FOOTER_LINE[i] != "")
    	{
		bodytext += "\n" + FOOTER_LINE[i];
    	}
    }
  }  

  return bodytext;
   
};

// ----------------------------------------------------------------------------------------------------------------------------
function send_via_yg() {
  if ( !form_is_okay() ) {
    return;
  };

  url  = 'http://groups.yahoo.com/group/' + (yahoo_group_name ? yahoo_group_name : groupNames[0]);
  url += '/post?subject=' + escape(get_formatted_subject_line())
  url += '&message=' + escape(get_formatted_bodytext());
  window.open(url);
  document.getElementById('send_yg').disabled = 1;
  document.getElementById('send_email').disabled = 1;
}

// ----------------------------------------------------------------------------------------------------------------------------
function send_via_email() {
  if ( !form_is_okay() ) {
    return;
  };

  url  = 'mailto:' + (email_address ? email_address : groupEmails[0]);
  url += '?subject=' + escape(get_formatted_subject_line())
  url += '&body=' + escape(get_formatted_bodytext());
  window.open(url);
  document.getElementById('send_yg').disabled = 1;
  document.getElementById('send_email').disabled = 1;
}

// -----------------------------------------------------------------------------------------------------------------------------
function apply_form_limits() {
  document.getElementById('subject').maxLength = MAXIMUM_ITEM_LENGTH;
  document.getElementById('location').maxLength = MAXIMUM_LOCATION_LENGTH;
}

// -----------------------------------------------------------------------------------------------------------------------------
function toggle_fop() {
  if ( document.getElementById('post_type').value.toLowerCase().indexOf('want') != -1) 
  {
  	//document.getElementById('fop_agree').disabled = true;
  	document.getElementById('fop_agree').checked = false;
  	document.getElementById('fop_agree').style.display = 'none';
  	document.getElementById('fop_agree_label').style.display = 'none';
  }
  else
  {
	//document.getElementById('fop_agree').disabled = false;
	document.getElementById('fop_agree').style.display = '';
	document.getElementById('fop_agree_label').style.display = '';
  }
}
