function buildSelectBox(name, id, values, currentValue)
{
  var selectBox = $(document.createElement("select"));
  selectBox.attr("name", name);
  selectBox.attr("id", id);

  var option = $(new Option("Please Select...", ""));
  option.text("Please Select...");
  option.appendTo(selectBox);

  $.each(values, function()
  {
    var option = $(new Option(this.name, this.value));
    option.text(this.name);
    
    if (currentValue && this.value == currentValue)
    	option.attr("selected", true);

    option.appendTo(selectBox);
  });

  return selectBox;
}

function createHiddenField(name, id, value)
{
  var hiddenField = $(document.createElement("input"));
  hiddenField.attr("type", "hidden");
  hiddenField.attr("name", name);
  hiddenField.attr("id", id);
  hiddenField.attr("value", value);
  
  return hiddenField;
}

function lookupCities(zipcode, communityId)
{
  var cityLookupUrl = new URL(CONTEXT_PATH + "/register/lookupLocalities");
  cityLookupUrl.addArg("zipCode", zipcode);
  cityLookupUrl.addArg("callback", "?");

  $.getJSON(cityLookupUrl.toAbsolute(), function (data)
  {
    if (data.status == "SUCCESS")
    {
      var results = data.results;
      
      if (results.length == 0)
      {
        $("#city").html("No Member Community found");
      }
      else if (results.length == 1)
      {
        var hiddenField = createHiddenField("selectedCommunity", "communityHidden", results[0].value);
    	  
        $("#city").text(results[0].name);
        $("#city").append(hiddenField);
      }
      else
      {
        var communityList = buildSelectBox("selectedCommunity", "communityList", results, communityId);
        $("#city").html(communityList);
      }

      $("#communityDiv").show("slow");
    }
  });
}

function lookupUtilities(zipcode, utilityId)
{
  var utilityLookupUrl = new URL(CONTEXT_PATH + "/register/lookupUtilities");
  utilityLookupUrl.addArg("zipCode", zipcode);
  utilityLookupUrl.addArg("callback", "?");

  $.getJSON(utilityLookupUrl.toAbsolute(), function (data)
  {
    if (data.status == "SUCCESS")
    {
      var results = data.results;

      if (results.length == 0)
      {
        $("#utility").html("No Utility found");
      }
      else if (results.length == 1)
      {
        var hiddenField = createHiddenField("selectedUtility", "utilityHidden", results[0].value);
        
        $("#utility").text(results[0].name);
        $("#utility").append(hiddenField);
      }
      else
      {
        var utilityList = buildSelectBox("selectedUtility", "utilityList", results, utilityId);
        $("#utility").html(utilityList);
      }

      $("#utilityDiv").show("slow");
    }
  });
}

function setupRegistrationPage(communityId, utilityId)
{
  var zipRegEx = new RegExp(/(^\d{5}$)/);
  
  $("#zipcode").each(function()
  {
	 var zipcode = this.value;
	 
     if (zipcode.match(zipRegEx))
     {
       lookupCities(zipcode, communityId);
       lookupUtilities(zipcode, utilityId);
     }
  });
  
  $("#zipcode").keydown(function(event)
  {
    var charCode = (event.which) ? event.which : window.event.keyCode;

    if (!(charCode == 8 || charCode == 46) && !(charCode >= 48 && charCode <= 57) && !(charCode >= 96 && charCode <= 105))
      return false;

    return true;
  });

  $("#zipcode").keyup(function(event)
  {
    var charCode = (event.which) ? event.which : window.event.keyCode;
    
    if (charCode == 8 || charCode == 46 || (charCode >= 48 && charCode <= 57) || (charCode >= 96 && charCode <= 105))
    {
      var zipcode = this.value;
      
      if (zipcode.match(zipRegEx))
      {
        lookupCities(zipcode);
        lookupUtilities(zipcode);
      }
      else
      {
    	$("#communityDiv").hide("slow");
    	$("#utilityDiv").hide("slow");
      }
    }
  });
  
}


