// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var helper = {

	/**
	 * event listener.
	 *
	 * @param obj name of the object to be listened for.
	 * @param evType the event type.
	 * @param fn the function.
	 * @param useCapture boolean to enable event capture.
	 */
    addEvent: function(obj, evType, fn, useCapture) {
        if (useCapture == null)
            useCapture = true;

        if (obj.addEventListener) {
            obj.addEventListener(evType, fn, useCapture);
            return true;
        } else if (obj.attachEvent) {
            return obj.attachEvent("on" + evType, fn);
        } else {
            obj['on' + evType] = fn;
        }
    },

	/**
	 * trims whitespace.
	 *
	 * @param string to trim.
	 * @return the trimmed string.
	 */
    trim: function(value) {
        value = value.replace(/^\s+/, '');
        value = value.replace(/\s+$/, '');
        return value;
    }
}


// this is hack
function preparePage() {
  
  if (mapOnPage) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"), {mapTypes: [G_NORMAL_MAP]});
        
        map.setCenter(new GLatLng(-37.820497, 145.084076), 15);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        
        var address = "1 Norfolk Rd Surrey Hills VIC 3127";
        
        var geocoder = new GClientGeocoder();
        geocoder.getLatLng(
            address,
            function(point) {
              if (!point) {
                alert(address + " not found");
              } else {
                map.setCenter(point, 13);
                var marker = new GMarker(point);
                map.addOverlay(marker);
                marker.openInfoWindowHtml('<strong>Ross-Hunt Real Estate</strong><br/>' + address);
              }
            }
        );
    }
  }

  fixDividerHeight();
}

function fixDividerHeight () {
  // find the height of the main content thing
  var height = $("#normalPageContent").height();
  
  // find which is bigger, the height of the main content thing + delta, or 720
  // set the height of the divider div to whichever is bigger
  height += 87;
  if (height > 720) {
    $("#divider").height(height);
  }
}


// ----------------------------------------------------------------------
// Javascript form validation routines.
// Author: Stephen Poley
//
// Simple routines to quickly pick up obvious typos.
// All validation routines return true if executed by an older browser:
// in this case validation must be left to the server.
//
// Update Jun 2005: discovered that reason IE wasn't setting focus was
// due to an IE timing bug. Added 0.1 sec delay to fix.
//
// Update Oct 2005: minor tidy-up: unused parameter removed
//
// Update Jun 2006: minor improvements to variable names and layout
// ----------------------------------------------------------------------

var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread

// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// --------------------------------------------

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}


// --------------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocus(valfield)
{
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}


// --------------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// --------------------------------------------

var proceed = 2;  

function commonCheck    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text) return true;  // infofield is wrong type of node  

  if (emptyString.test(valfield.value)) {
    if (required) {
      msg (infofield, "error", "ERROR: required");  
      setfocus(valfield);
      return false;
    }
    else {
      msg (infofield, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}

// --------------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// --------------------------------------------
function validatePresent(valfield)
{
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;

  msg (infofield, "warn", "");
  return true;
}

// --------------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------
function validateEmailAlert(valfield)
{

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    return false;
  }

  return true;
}


