﻿$(document).ready(function () {
  $("#submittedFormName").change(function () {
    var val = $("#submittedFormName").val();
    switch (val) {
      case '':
        $("#emptyFormSection").css('display', 'block');
        $("#supportFormSection").css('display', 'none');
        $("#requestCourseFormSection").css('display', 'none');
        break;
      case 'Tiger Woods - My Swing':
        $(".swingExtHeader").show();
        $(".courseSearch").hide();
        //$(".updateMessage").hide();
        $("#emptyFormSection").css('display', 'none');
        $("#supportFormSection").css('display', 'block');
        $("#requestCourseFormSection").css('display', 'none');
        break;
      case 'Software Support':
      case 'General Questions':
      case 'Golfshot Android':
      case 'Baseball Gameplan with Jason Giambi Support':
      case 'Golfplan with Paul Azinger Support':
      case 'Golfscape':
      case 'Media Relations':
        $(".swingExtHeader").hide();
        $(".courseSearch").show();
        //$(".updateMessage").show();
        $("#emptyFormSection").css('display', 'none');
        $("#supportFormSection").css('display', 'block');
        $("#requestCourseFormSection").css('display', 'none');
        break;
      case 'Request a New Course':
        $("#emptyFormSection").css('display', 'none');
        $("#supportFormSection").css('display', 'none');
        $("#requestCourseFormSection").css('display', 'block');
        $("#newCourseMessage").css("display", "block");
        $("#problemCourseMessage").css("display", "none");
        $("#scorecardFileSection").css("display", "none");
        $("#courseResultsSection").html("");
        break;
      case 'Report a Course Problem':
        $("#emptyFormSection").css('display', 'none');
        $("#supportFormSection").css('display', 'none');
        $("#requestCourseFormSection").css('display', 'block');
        $("#newCourseMessage").css("display", "none");
        $("#problemCourseMessage").css("display", "block");
        $("#scorecardFileSection").css("display", "none");
        $("#courseResultsSection").html("");
        break;
    }
  });
  $("#submittedFormName").change();

  $("#courseState").change(function () {
    var state = $("#courseState").val();
    if (state != "") {
      var canadaProvinces = ",AB,BC,MB,NB,NL,NT,NS,NU,ON,PE,QC,SK,YT,";
      if (canadaProvinces.indexOf("," + state + ",") != -1) {
        $("#courseCountry").val("CA");
      }
      else {
        $("#courseCountry").val("US");
      }
    }
  });
  $("#courseCountry").change(function () {
    var country = $("#courseCountry").val();
    if (country != "CA" && country != "US") {
      $("#courseState").val("");
    }
  });

  $("#locateCourseButton").click(function () {
    var reportForm = $("#submittedFormName").val() == "Report a Course Problem";

    $("#courseResultsSection").html("");
    $("#scorecardFileSection").css("display", "none");
    $("#golfCourseIsReadySection").css("display", "none");

    if (validateCourseLocateSection()) {
      $("#locateCourseButtonContainer").css("display", "none");
      $("#locateSpinner").css("display", "block");
      $.get(locateCourseUrl, {
        name: $("#courseName").val(),
        city: $("#courseCity").val(),
        state: $("#courseState").val(),
        country: $("#courseCountry").val(),
        published: (reportForm ? "1" : "0")
      },
				function (data) {
				  $("#locateSpinner").css("display", "none");
				  $("#locateCourseButtonContainer").css("display", "block");
				  if (data.indexOf("NO RESULTS") != -1) {
				    $("#courseResultsSection").html(data);
				    $("#scorecardFileSection").css("display", "block");
				    $("#sorryMessage").css("display", "inline");
				    if (reportForm) {
				      $("#descriptProblemMessage").css("display", "block");
				      $("#newUpdateMessage").css("display", "none");
				    }
				    else {
				      $("#descriptProblemMessage").css("display", "none");
				      $("#newUpdateMessage").css("display", "block");
				    }
				  }
				  else {
				    $("#courseResultsSection").html(data);
				    $("#scorecardFileSection").css("display", "none");
				  }
				}
			);
    }
    else {
      $("#locateSpinner").css("display", "none");
      $("#locateCourseButtonContainer").css("display", "block");
      $("#scorecardFileSection").css("display", "none");
    }
  });

  $("#submitFormButton").click(function () {
    if (validateNameAndEmail() && validateComments()) {
      $("#submitFormSpinnerSection").css("display", "block");
      $("#submitFormButtonSection").css("display", "none");
      var contactForm = $("#contactUsForm");
      $("#submitAction").val("finalSubmit");
      contactForm.submit();
    }
  });

  $("#submitRequestButton").click(function () {
    if (validateNameAndEmail() && validateCourseLocateSection() && validateScorecardFiles()) {
      $("#submitRequestSpinnerSection").css("display", "block");
      $("#submitRequestButtonSection").css("display", "none");
      var contactForm = $("#contactUsForm");
      $("#submitAction").val("finalSubmit");
      contactForm.submit();
    }
  });

  $("#viewImageLink").click(function () {
    window.open($("#viewImageLink").attr("query"), "_blank", "width=955,height=768,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes,location=no");
  });

  //$(".updateMessage").click(function () {
    //$(".locationServices").slideToggle("slow");
  //});

  //$(".locationServices").click(function () {
    //$(".locationServices").slideToggle("slow");
  //});

  if (!supportsFileInput()) {
    $("#requestCourseFormSection").empty();
    $('#requestCourseFormSection').append('<p>' + noFileUploadMessage + '</p');
  }
});

function supportsFileInput() {
	var dummy = document.createElement("input");
	dummy.setAttribute("type", "file");
	return dummy.disabled === false;
}

function validateNameAndEmail()
{
	var name = $.trim($("#contactName").val());
	var emailAddress = $.trim($("#contactEmailAddress").val());
	var validName = name != "";
	var validEmailAddress = emailAddress.indexOf('@') != -1 && emailAddress.indexOf('.') != -1;
	if (!validName || !validEmailAddress)
	{
		alert("Please enter a your name and a valid email address.");
		if (!validName) $("#contactName").focus();
		else if (!validEmailAddress) $("#contactEmailAddress").focus();
		return false;
	}
	return true;
}

function validateComments()
{
	var comments = $.trim($("#comments").val());
	var validComments = comments != "";
	if (!comments)
	{
		alert("Please enter some comments.");
		$("#comments").focus();
		return false;
	}
	return true;
}

function validateScorecardFiles()
{
	var scorecardFront = $.trim($("#scorecardFront").val());
	var scorecardBack = $.trim($("#scorecardBack").val());
	var golfCourseWebsite = $.trim($("#golfCourseWebsite").val());
	var noWebsiteChecked = $("#noWebsite").is(':checked');
	if (scorecardFront == "" || scorecardBack == "")
	{
		alert("Please upload a scan or digital photograph of the front and back of the course scorecard.");
		return false;
	}
	if (golfCourseWebsite == "" && !noWebsiteChecked)
	{
		alert("Please enter the website for the golf course.");
		return false;
	}
	return true;
}

function validateCourseLocateSection()
{
	var courseName = $.trim($("#courseName").val());
	var courseCity = $.trim($("#courseCity").val());
	var courseState = $.trim($("#courseState").val());
	var courseCountry = $.trim($("#courseCountry").val());

	var validState = ((courseCountry == "US" || courseCountry == "CA") && courseState != "") || ((courseCountry != "US" && courseCountry != "CA") && courseState == "");
	if (courseName == "" || courseCountry == "" || !validState)
	{
		alert(typeof courseNameAndLocationMessage != 'undefined' ? courseNameAndLocationMessage : 'Please enter the name and location of your golf course.');
		return false;
	}
	return true;
}

function chooseCourse(radioId, google)
{
	var radio = document.getElementById(radioId);
	var idx = radioId.substring('courseResult'.length).split('_');
	var reportForm = ($("#submittedFormName").val() == "Report a Course Problem");
	if (idx.length == 1 || reportForm)
	{
		$("#scorecardFileSection").css("display", "block");
		if (idx.length == 1)
		{
			$("#sorryMessage").css("display", "inline");
		}
		else
		{
			$("#sorryMessage").css("display", "none");
		}
		if (reportForm)
		{
			$("#descriptProblemMessage").css("display", "block");
			$("#newUpdateMessage").css("display", "none");
		}
		else
		{
			$("#descriptProblemMessage").css("display", "none");
			$("#newUpdateMessage").css("display", "block");
		}
		$("#golfCourseIsReadySection").css("display", "none");
	}
	else
	{
		$("#descriptProblemMessage").css("display", "none");

		var key = idx[1];
		if (key == 'GSA' || key == 'GS')
		{
			$("#golfCourseIsReadySection").css("display", "block");
			$("#scorecardFileSection").css("display", "none");

			if (google != null && google != "")
			{
				$("#viewImageSection").css("display", "block");
				$("#viewImageLink").attr("query", google);
			}
			else
			{
				$("#viewImageSection").css("display", "none");
			}
		}
		else
		{
			$("#scorecardFileSection").css("display", "block");
			$("#sorryMessage").css("display", "none");
			$("#golfCourseIsReadySection").css("display", "none");
		}
	}
}
