(function() {
  //method="post" action="../assets/ctnyfamilylaw.php"
  var Ctlaw = {
    initContactForm: function() {
      var frm = $ID('form_consult');
      if ( !frm) return;
      var inp = [frm.name,frm.email,frm.phone,frm.issue];
			$ON( inp, 'focus,blur', Ctlaw.handleFocus);
			$ON( 'btn_submit', 'click', Ctlaw.handleSubmit);
    },
    handleFocus: function( aEvt) {
    	var tgt = aEvt.getTarget();
	    // Return if not textarea or input
	    if ( tgt.tagName != 'TEXTAREA' && tgt.tagName != 'INPUT') return;		// Just fields & textarea
	    if ( tgt.tagName == 'INPUT' && tgt.type != 'text') return;					// But just text fields
	    if ( aEvt.getType() == 'focus') {
		    //e += 'focus...';
		    if ( tgt.value == tgt.defaultValue) tgt.value = '';
	    }
	    else if ( aEvt.getType() == 'blur') {
		    //e += 'blur...';
//		    tgt.value = Neaux.String.trim( tgt.value);
        tgt.value = tgt.value.replace(/^\s+/,'').replace(/\s+$/,'');
		    if ( tgt.value == '') tgt.value = tgt.defaultValue;
	    }
	    //if ( e) window.status = e;
    },
    handleSubmit: function() {
      var frm = $ID('form_consult');
      var invFields = {};
      for ( var i=0; i<frm.length; i++)
        if ( frm[i].name && frm[i].value == frm[i].defaultValue) invFields[frm[i].name] = true;
      if ( !Ctlaw.renderInvalidFields(invFields))
        Neaux.Http.request('assets/ctnyfamilylaw.php','post',Ctlaw.handleReceive,$ID('form_consult'));
    },
    handleReceive: function( aEvt) {
      if ( !aEvt.isDone()) return;
      var theResponse = aEvt.getJson();
      if ( !theResponse)
        alert( aEvt.getText());
      else if ( theResponse.errors)
        Ctlaw.handleErrors( theResponse, aEvt.getText());
      else {
        var theArea = $ID('form_consult_instructions');
        theArea.innerHTML = 'Thank you for your inquiry. We will contact you shortly.';
        theArea.style.color = '#008040';
        $ID('form_consult_body').style.display = 'none';
      }
    },
    handleErrors: function( aRsp) {
      var invFields = {};
      for ( var i=0; i < aRsp.error.length; ++i)
        invFields[aRsp.error[i][0]] = true;
      Ctlaw.renderInvalidFields(invFields);
    },
    renderInvalidFields: function( aInvList) {
      var frm = $ID('form_consult');
      var errorsFound = false;
      for ( var i=0; i<frm.length; i++) {
        if ( frm[i].name) {
          if ( aInvList[frm[i].name]) {
            errorsFound = true;
            frm[i].className = 'invalid';
          }
          else
            frm[i].className = '';
        }
      }
      if ( errorsFound) {
        var theArea = $ID('form_consult_instructions');
        theArea.innerHTML = 'The highlighted fields are invalid - please make corrections and press Submit Form to re-submit';
        theArea.style.color = '#a02000';
      }
      return errorsFound;
    },
    start: function() {
      Ctlaw.initContactForm();
    },
    toString: function() { '[Ctlaw]' }
  }


  Neaux.onload( Ctlaw.start);
})();
