var cal_deadline = null;
var cal_available = null;
var count;
var spinner = null;

Event.observe(window, 'load', initForm, false);

String.prototype.trim = function() {
	return this.replace(/^\s*/,"").replace(/\s*$/,"");
}

function initForm() {
  if($("vuForms")) {
    $("vuForms").reset();
    spinner = $("statusIndicator");
    spinner.style.visibility = "hidden";
    count = 0;
	cal_deadline = new calendar2(document.forms['vuForms'].elements['deadline']);
    cal_deadline.year_scroll = true;
    cal_deadline.time_comp = false;
    cal_available = new calendar2(document.forms['vuForms'].elements['availableDate']);
    cal_available.year_scroll = true;
    cal_available.time_comp = false;
    $("availabilityList").innerHTML = "";
    Event.observe('add', 'click', addDate, false);
  }
}

function addDate() {
	var id = "availability_" + count;
	$("availabilityList").innerHTML += "<div class='availability' id='" + id + "'>" + $("availableDate").value + " " + $("availableTime").value + "</div>";
	activateInLineRemoves("vuForms");
	count++;
	$("availableDate").value = "";
	$("availableTime").selectedIndex = 0;
}

function activateInLineRemoves(parent) {
  $A($$(".availability"), parent).each(
    function(item) {
      makeRemovable(item.id);
    }
  );
}

function makeRemovable(id) {
	Event.observe(id, 'click', function() {removeDate(id)}, false);
	Event.observe(id, 'mouseover', function() {showAsRemovable(id, false)}, false);
	Event.observe(id, 'mouseout', function() {showAsRemovable(id, true)}, false);
}

function showAsRemovable(id, clear) {
	if(!clear) {
		Element.addClassName($(id), 'removable');
	}
	else {
		Element.removeClassName($(id), 'removable');
	}
}

function removeDate(id) {
	var answer = confirm("Are you sure you want to remove this date/time?");
	if(answer) {
	  Element.remove(id);
	}
}

function validateForm() {
  var field;
  var pattern;

	// Full Name Validation
  field = $F("fullName").trim();
  if(field.length == 0) {
    alert("Please provide your full name.");
    $("fullName").activate();
    return false;
  }
  pattern = /^[a-zA-Z\-\.\s']*$/;
  if(!field.match(pattern)) {
    alert("Please use only spaces, letters, dashes, apostrophes, or periods in the contact name.");
    $("fullName").activate();
    return false;
  }
  // Email Address Validation
  field = $F("email").trim();
  if(field.length == 0) {
    alert("Please provide your email address.");
    $("email").activate();
    return false;
  }
  pattern = /^.*@[\w\-\.]+\.\w+$/;
  if(!field.match(pattern)) {
    alert("Please enter a valid email address.");
    $("email").activate();
    return false;
  }
  // Phone Validation
  if($F("phone").trim().length == 0) {
    alert("Please provde your phone number.");
    $("phone").activate();
    return false;
  }
  // Project Name
  if($F("projectName").trim().length == 0) {
    alert("Please provide a project name.");
    $("projectName").activate();
    return false;
  }
  // Project Description
  if($F("description").trim().length == 0) {
    alert("Please provide some description of the project.");
    $("description").activate();
    return false;
  }
  // Deadline
  if($F("deadline").trim().length == 0) {
    alert("Please provide a project due date.");
    $("deadline").activate();
    return false;
  }
  // Consultant
  if($("meet").selectedIndex < 1) {
    alert("Please select one of the consultant options.");
    $("meetChoose").focus()
    return false;
  }
  
  // Availability
  if($$(".availability")!= null) {
    var allDates = "";
    $A($$(".availability"), "vuForms").each(
      function(item) {
    	  if(allDates.length > 0) {
    	  	allDates += "; ";
    	  }
        allDates += item.innerHTML;
      }
    );
    $("allAvailability").value = allDates;
  }
  if($F("availabilityDescription").trim().length != 0) {
  	if($F("allAvailability").trim().length != 0) {
  		$("allAvailability").value += "; ";
  	}
  	$("allAvailability").value += $F("availabilityDescription").trim();
  }
  if($("allAvailability").value.trim().length == 0) {
    alert("Please provide one or more possible meeting dates/times.");
    $("availableDate").activate();
    return false;
  }
  //alert("Would normally submit.");
  //return false;
  $("apply").disabled = true;
  spinner.style.visibility = "visible";
  return true;
}