var steps = ["errorMessage", "propertyDescription", "propertyLocation", "contactInformation"];
var stepFoci = { "propertyDescription":"propertyUse", "propertyLocation":"locationStreetAddress", "contactInformation":"contactFirstName" };
var policies = ["policyTermsOfUse", "policyPrivacy", "policyAboutUs"];

var isSanDiegoShortSale =
      (location.hostname.toLowerCase().match(/sandiegoshortsalerealtors\.com$/) != null);

function goToStep(s) {
  if (s == 'firstStep') {
     if (isSanDiegoShortSale) {
        s = steps[3];
     } else {
        s = steps[1];
     }
  }
  
  var f = document.cmaForm;

  for (var i = 0; i < steps.length; i++) {
    var o = obj(steps[i]);
    if (o != null) {
      o.att("class", "hidden");
    }
  }

  obj(s).att("class", "");

  // Prefill the physical contact information with the most likely values since
  // this is the seller's primary residence.
  if (f.propertyUse.type != 'hidden'
      && f.propertyUse.options[f.propertyUse.selectedIndex].value == "primary") {
    f.contactStreetAddress.value = f.locationStreetAddress.value;

    if (f.locationUnitNumber && f.locationUnitNumber.value.length > 0) {
      f.contactStreetAddress.value += " #" + f.locationUnitNumber.value;
    }

    f.contactCity.value = defaultCity;

    for (var i = 0; i < f.contactState.options.length; i++) {
      if (f.contactState.options[i].value == defaultState) {
	f.contactState.options[i].selected = true;
      }
    }

    f.contactZipCode.value = f.locationZipCode.value;
  }

  var focusObj = obj(stepFoci[s]);

  if (focusObj != null) {
    focusObj.focus();
    if (focusObj.select) {
      obj(stepFoci[s]).select();
    }
  }

  if (s == "finish") {
    if (f.contactFirstName.value != "") {
      obj("finishFirstName").innerHTML = f.contactFirstName.value;
    }
  }
}

function togglePolicy(p) {
  var hidden = obj(p).att("class") == "hidden";

  for (var i = 0; i < policies.length; i++) {
    obj(policies[i]).att("class", "hidden");
  }

  obj(p).att("class", (hidden ? "" : "hidden"));
}

function handleParkingTypeNone(e) {
  if (e.type == 'hidden') {
     return;
  }
  
  var pps = obj("propertyParkingSpaces");

  // [!] Mozilla Firefox: If the user has already made a selection from
  // propertyParkingSpaces, setting pps.disabled to true will expand the
  // propertyParkingSpaces control, obscuring propertyPlanningToSell and
  // propertyListingStatus. Once propertyParkingSpaces is disabled, there is no
  // way to collapse the control.
  if (e[e.selectedIndex].value == "none") {
//     pps.disabled = true;
    pps.selectedIndex = pps.length - 1;
  } else if (e[e.selectedIndex].value == "") {
//     pps.disabled = false;
    pps.selectedIndex = 0;
  } else {
//     pps.disabled = false;
    if (pps.selectedIndex == pps.length - 1) {
      pps.selectedIndex = 0;
    }
  }

  return true;
}

function trimString(s) {
  var re = new RegExp("^\\s*(.*?)\\s*$");
  return s.replace(re, "$1");
}

function isValidNumber(n) {
  var re = new RegExp(",", "g");
  var vn = n.replace(re, "");
  vn = new Number(vn);
  return ! isNaN(vn);
}

function isPositive(n) {
  var re = new RegExp(",", "g");
  var vn = n.replace(re, "");
  vn = new Number(vn);
  return vn > 0;
}

function validatePropertyDescription() {
  var enStrings = {"propertyUse":"Current Use","propertyType":"Type of Property","propertyCondition":"Overall Condition","propertyLotSize":"Approximate Lot Size","propertySquareFeet":"Approximate Square Feet","propertyBedrooms":"Bedrooms","propertyBathrooms":"Bathrooms","propertyParking":"Type of Parking","propertyParkingSpaces":"Parking Spaces","propertyPlanningToSell":"Planning to Sell"};

  var msg = "";

  var propertyUse = obj("propertyUse");
  if (propertyUse[propertyUse.selectedIndex].value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.propertyUse;
  }

  var propertyType = obj("propertyType");
  if (propertyType[propertyType.selectedIndex].value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.propertyType;
  }

  var propertyCondition = obj("propertyCondition");
  if (propertyCondition[propertyCondition.selectedIndex].value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.propertyCondition;
  }

  var propertyLotSize = obj("propertyLotSize");
  if (propertyLotSize != null && propertyLotSize[propertyLotSize.selectedIndex].value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.propertyLotSize;
  }

  var propertySquareFeet = obj("propertySquareFeet");
  propertySquareFeet.value = trimString(propertySquareFeet.value);
  if (propertySquareFeet.value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.propertySquareFeet;
  }

  var propertyBedrooms = obj("propertyBedrooms");
  if (propertyBedrooms[propertyBedrooms.selectedIndex].value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.propertyBedrooms;
  }

  var propertyBathrooms = obj("propertyBathrooms");
  if (propertyBathrooms[propertyBathrooms.selectedIndex].value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.propertyBathrooms;
  }

  var propertyParking = obj("propertyParking");
  if (propertyParking[propertyParking.selectedIndex].value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.propertyParking;
  }

  var propertyParkingSpaces = obj("propertyParkingSpaces");
  if (propertyParkingSpaces[propertyParkingSpaces.selectedIndex].value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.propertyParkingSpaces;
  }

  var propertyPlanningToSell = obj("propertyPlanningToSell");
  if (propertyPlanningToSell[propertyPlanningToSell.selectedIndex].value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.propertyPlanningToSell;
  }

  if (msg) {
    msg = "The following required information has not been provided:\n\n" + msg;
  }

  if (propertySquareFeet.value != "" &&
    (! isValidNumber(propertySquareFeet.value) ||
      ! isPositive(propertySquareFeet.value))) {
    msg += (msg ? "\n\n" : "") + enStrings.propertySquareFeet + " should be a valid number greater than zero.";
  }

  if (msg) {
    alert(msg);
    return false;
  }

  return true;
}

function isValidZipCode(s) {
  var re = new RegExp("^\\d\\d\\d\\d\\d$");
  return s.match(re) != null;
}

function validatePropertyLocation() {
  var enStrings = {"locationStreetAddress":"Street Address","locationCity":"City","locationZipCode":"Zip Code"};

  var msg = "";

  var locationStreetAddress = obj("locationStreetAddress");
  locationStreetAddress.value = trimString(locationStreetAddress.value);
  if (locationStreetAddress.value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.locationStreetAddress;
  }

  var locationCity = obj("locationCity");
  locationCity.value = trimString(locationCity.value);
  if (locationCity.value == "") {
     msg += (msg ? "\n" : "") + "* " + enStrings.locationCity;
  }
  
  var locationZipCode = obj("locationZipCode");
  locationZipCode.value = trimString(locationZipCode.value);
  if (locationZipCode.value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.locationZipCode;
  }

  if (msg) {
    msg = "The following required information has not been provided:\n\n" + msg;
  }

  if (locationZipCode.value != "" && ! isValidZipCode(locationZipCode.value)) {
    msg += (msg ? "\n\n" : "") + enStrings.locationZipCode + " should be a valid 5-digit zip code.";
  }

  if (msg) {
    alert(msg);
    return false;
  }

  return true;
}

function isValidEmailAddress(s) {
  var re = new RegExp("^.+@.+$");
  return s.match(re) != null;
}

function validateContactInformation() {
  var enStrings = {"contactFirstName":"First Name","contactLastName":"Last Name","contactStreetAddress":"Street Address","contactCity":"City","contactState":"State","contactZipCode":"Zip Code","contactEmailAddress":"E-mail Address","contactPhoneNumber":"Phone Number"};

  var msg = "";

  var contactFirstName = obj("contactFirstName");
  contactFirstName.value = trimString(contactFirstName.value);
  if (contactFirstName.value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.contactFirstName;
  }

  var contactLastName = obj("contactLastName");
  contactLastName.value = trimString(contactLastName.value);
  if (contactLastName.value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.contactLastName;
  }

  var contactStreetAddress = obj("contactStreetAddress");
  contactStreetAddress.value = trimString(contactStreetAddress.value);
  if (contactStreetAddress.value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.contactStreetAddress;
  }

  var contactState = obj("contactState");
  if (contactState[contactState.selectedIndex].value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.contactState;
  }

  var contactZipCode = obj("contactZipCode");
  contactZipCode.value = trimString(contactZipCode.value);
  if (contactZipCode.value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.contactZipCode;
  }

  var contactEmailAddress = obj("contactEmailAddress");
  contactEmailAddress.value = trimString(contactEmailAddress.value);
  if (location.hostname.toLowerCase().match(/sandiegoshortsalerealtors\.com$/) != null && contactEmailAddress.value == "") {
    msg += (msg ? "\n" : "") + "* " + enStrings.contactEmailAddress;
  }


  if (msg) {
    msg = "The following required information has not been provided:\n\n" + msg;
  }

  if (contactZipCode.value != "" && ! isValidZipCode(contactZipCode.value)) {
    msg += (msg ? "\n\n" : "") + enStrings.contactZipCode + " should be a valid 5-digit zip code.";
  }

  if (contactEmailAddress.value != "" && ! isValidEmailAddress(contactEmailAddress.value)) {
    msg += (msg ? "\n\n" : "") + enStrings.contactEmailAddress + " should be a valid e-mail address of the form <name>@<domain>.";
  }

  if (msg) {
    alert(msg);
    return false;
  }

  return true;
}


function initPage() {
  handleParkingTypeNone(obj("propertyParking"));
}

self.onload = initPage;
