// ################  Rating Form Script ###################
/*
function calculateOverall(){
    var formName = document.yourRatingForm
    for(i=0; i<5; i++){
        if(formName.quality[i].checked) var quality = formName.quality[i].value;
        if(formName.price[i].checked) var price = formName.price[i].value;
        if(formName.location[i].checked) var location = formName.location[i].value;
        if(formName.social[i].checked) var social = formName.social[i].value;
        if(formName.landlord[i].checked) var landlord = formName.landlord[i].value;
        if(formName.parking[i].checked) var parking = formName.parking[i].value;
    }
    var overallValue = (parseInt(quality) + parseInt(price) + parseInt(location) + parseInt(social) + parseInt(landlord) + parseInt(parking)) / 6;
    overallValue = Math.round(overallValue*Math.pow(10,1))/Math.pow(10,1);
    overallValue = overallValue.toString();
    if (overallValue.length==1) overallValue = overallValue + ".0";
    document.getElementById('overallValue').innerHTML = overallValue;
}


function expand(id) {
    var comShortDiv=document.getElementById("comClip_"+id);
    var comLongDiv=document.getElementById("comFull_"+id);
    comShortDiv.style.display="none";
    comLongDiv.style.display="block";
}
*/

/* START FORM-RELATED VALIDATION FUNCTIONS */

function validate_form(parentform) {

  validated = true;
  
  if (!validate_captcha(parentform)) {
    validated = false;
  }

  if (!validate_exaggerations(parentform)) {
    validated = false;
  }

  if (!validate_categories(parentform)) {
    validated = false;
  }

  if (!validate_rental_term(parentform)) {
    validated = false;
  }

  return validated;

}

function validate_captcha(parentform) {
  if (parentform.captcha.value != 100) {
    alert('Please enter the correct answer for the spam fighter.');
    return false;
  } else {
    return true;
  }
}

function validate_exaggerations(parentform){ // Called by validate_form

    if (
      parentform.quality[0].checked &&
      parentform.price[0].checked &&
      parentform.location[0].checked &&
      parentform.social[0].checked &&
      parentform.landlord[0].checked &&
      parentform.parking[0].checked
      ) {

      alert("Come on, it can't be THAT bad.  Give them some credit on at least one category and we'll call it good.");
      return false;
    }

    if (
      parentform.quality[4].checked &&
      parentform.price[4].checked &&
      parentform.location[4].checked &&
      parentform.social[4].checked &&
      parentform.landlord[4].checked &&
      parentform.parking[4].checked
      ) {

      alert("Come on, it can't be THAT great.  Dock them on at least one category and we'll call it good.");
      return false;
    }

    return true;

}

function validate_rental_term(parentform){ // Called by validate_form

    var start_sn=parentform.start_sn.value;
    var start_yr=parentform.start_yr.value;
    var end_sn=parentform.end_sn.value;
    var end_yr=parentform.end_yr.value;

    if (start_yr>end_yr || (start_yr==end_yr && start_sn>end_sn)){

      var alert_text = 'The beginning date on your renting term must be prior to the ending date.';

      if (start_sn == 0 || end_sn == 0) {
        alert_text += '\nRemember, winter semester is at the beginning of the year.';
      }

      alert(alert_text);
      return false;
    }

    return true;

}

function validate_categories(parentform) { // Called by validate_form

  var alert_text = "The following items still need be rated:\n";
  var validated = true;

  if (!validate_radio_group(parentform.quality)) {
    alert_text += '- Quality\n';
    validated = false;
  }

  if (!validate_radio_group(parentform.price)) {
    alert_text += '- Price\n';
    validated = false;
  }

  if (!validate_radio_group(parentform.location)) {
    alert_text += '- Location\n';
    validated = false;
  }

  if (!validate_radio_group(parentform.social)) {
    alert_text += '- Social Life\n';
    validated = false;
  }

  if (!validate_radio_group(parentform.landlord)) {
    alert_text += '- Landlord\n';
    validated = false;
  }

  if (!validate_radio_group(parentform.parking)) {
    alert_text += '- Parking\n';
    validated = false;
  }

  if (validated == false) {
    alert(alert_text);
    return false;
  }

  return true;

}

function validate_radio_group(group_name) // Called by validate_categories
{
  // set var radio_choice to false
  var radio_was_chosen = false;

  // Loop from zero to the one minus the number of radio button selections
  for (counter = 0; counter < group_name.length; counter++) {

    // If a radio button has been selected it will return true
    // (If not it will return false)
    if (group_name[counter].checked) {
      radio_was_chosen = true;
    }

  }

  return radio_was_chosen;
}

/* END FORM-RELATED VALIDATION FUNCTIONS */

// ################  Image Viewer Script ###################
function activateUploadImg(){
   var selectedDiv=document.getElementById('UploadImg');
   if(selectedDiv.style.display=="none") selectedDiv.style.display="block";
}

