/**
 * Initialisation
 **/
var errors = new Array();
var messageDone = true;

/**
 * Clear extension fields on focus
 **/
function clearInput(element)
{
  if(element.value == element.defaultValue)
  {
    element.value='';
  }
}
/**
 * Show extra fields for certain reasons
 **/
function checkExtra(element, id)
{
  if ((element.checked) || (element.value == "Other"))
  {
    $(id).slideDown(300);
  }
  else
  {
    $(id).slideUp(300);
  }
}

/**
 * Reset Gift Card Radios
 **/
function clearRadios()
{
  document.getElementById('giftoption1').checked = false;
  document.getElementById('giftoption2').checked = false;
  document.getElementById('giftoption3').checked = false;
  document.getElementById('giftoption4').checked = false;
  document.getElementById('giftoption5').checked = true;
  $('#gift').slideUp(400);
}

/**
 * Find where the array value is being kept
 **/
function getItemRow(elementName) {
  var thePosition=-1
  for (i=0;i<errors.length;i++) {
    if(errors[i]==elementName) {
      thePosition = i;
      break;
    }
  }
  return thePosition
}

/**
 * There's an error, make some fancy message effects
 **/
function resetMsg(text)
{
  if (messageDone == true)
  {
    $("#message").slideDown(400);
    messageDone = false;
    $("#messageText").fadeOut(150);
    setTimeout(function(){
      if (text == "Sent")
       {
        document.getElementById('messageText').style.backgroundColor = "#9bd37c";
        document.getElementById('messageText').style.color = "darkgreen";
        document.getElementById('messageText').style.border = "1px solid green";
        $("#messageText").html("Thank-you, your message has been sent.").fadeIn(300);
        $("#scheduleFormContainer").slideUp(500);
        $("#thank-you-message").html('<p style="padding-top:30px;text-align:center;font-weight:bold;color:green;">Thank you for scheduling with Skyline Reporting, we will call you by phone to confirm your proceeding.</p>');
       }
       else
       {
        document.getElementById('messageText').style.backgroundColor = "#fcc";
        document.getElementById('messageText').style.color = "#ca0000";
        document.getElementById('messageText').style.border = "1px solid #ca0000";
        $("#messageText").html(text).fadeIn(300);
       }
       messageDone = true;
    }, 150);
  }
}

/**
 * Validation for all fields, called on keyup
 **/
function validate(element, type, required)
{
  $.ajax({
    url: "scheduleFormProcess.php",
    data: element.name + "=" + element.value + "&type=" + type + "&theField=" + element.name + "&req=" + required,
    type: "POST",
    dataType: "html",
    success: function(data)
    {
      if (data == "true")
      {
        element.style.backgroundColor = "#FFF";
        if (getItemRow(element.name) > -1)
        {
          errors.splice(getItemRow(element.name), 1);
        }
      }
      else
      {
        element.style.backgroundColor = "#FCC";
        if (getItemRow(element.name) == -1)
        {
          errors.push(element.name);
        }
      }
    }
  });
}

/**
 * Checks for any empty required fields, called on form submission
 * Any invalid fields should be stored - so show a message and return false
 **/
function sendForm(form){
  var submitButton = document.getElementById('submit');
  submitButton.disabled = true;
  setTimeout(function(){submitButton.disabled = false}, 800);
  if (errors[0])
  {
    var text = "<span style='color:#ca0000;'>Please amend the fields highlighted in red before submitting!</span>";
    resetMsg(text);
    return false;
  }
  else
  {
    $.ajax({
     url: "scheduleFormProcess.php",
     data: $(form).serialize() + "&submit=on",
     type: "POST",
     dataType: "html",
     success: function(data)
     {
       var text = data;
        resetMsg(text);
     }
    });
  }
  return false;
}
