var calculator;
var total;
var available;
var values = [];

PrayaboutUtils = { 
  form_submitted: false,
  recalculate:function(evt) {
    var idx = PrayaboutUtils.getIndex(this.id);
    
    // don't allow the form to become invalid at any point.
    if (this.value.match(/^\d*$/)) {
      var delta = new Number(values[idx] - new Number(this.value));
      var new_available = new Number(available.value) + delta;
      if (new_available >= 0) {
        values[idx] = new Number(this.value);
        available.value = new_available;
      } else {
        this.value = values[idx];
      }
    } else {
      this.value = values[idx];
    }
  },

  getIndex:function(id) {
    return id.substr("points_".length) - 1;
  },

  hideLightBox:function() {
    $('simple-lightbox').hide();
    $('simple-overlay').hide();
  },
  
  showFileUploadFields:function() {
    Element.show('fileUploadFields'); Element.hide('uploadButton');
  }

}

// Events are attached here:
Event.observe(window, "load", function() {
    // This disables submit form buttons after user clicks them, to prevent
    // double submits.
    $$("input[type=submit]").each ( function(submit_button) { 
      if (submit_button.getAttribute('value') == "Join and Submit") {
        return; 
      };
	  if (submit_button.getAttribute('value') == "Make Request") {
        return; 
      };
	  if (submit_button.getAttribute('value') == "Create") {
        return; 
      };
      Event.observe(submit_button, "click", function() {
        if (submit_button.visible() ) {
          if (submit_button.getAttribute("value") == "search") {
            new Insertion.After(submit_button, "<span id='searching-span' style='font-weight:bold;'>Searching...</span>");
          } else {
            new Insertion.After(submit_button, "<span id='submitting-span' style='font-weight:bold;'>Submitting...</span>");
          }
         submit_button.hide();
        }
      });
    });

  // don't let users do doublt submissions
/*  if ($("prevent_double_submit") != null) {
      Event.observe("prevent_double_submit", "submit", function() {
        if (PrayaboutUtils.form_submitted) {
            Form.disable("prevent_double_submit");
            alert( "Your contribution has already been submitted." );
            return false;
        };
        PrayaboutUtils.form_submitted = true;
      });
  };
  */
//Element.hide("_login");
//Element.hide("anonymous");

  // Detects if a prayer request or response form is on the page. Both of these
  // forms have controls for anonymous users and for optional login. 
  if ($("_login")) {   
    Element.hide("anonymous");
    Element.hide("_login");

    Event.observe("member_true", "click", function() {
      if ($('prevent_double_submit').hasAttribute("onsubmit")) {
        $('prevent_double_submit').removeAttribute("onsubmit");
      }
      Element.hide("anonymous");
      if ($("light-candle-input")) {
        Element.show("light-candle-input");
      }
      Element.show("_login");
    });

    Event.observe("member_false", "click", function() {
      Element.hide("_login");
      Element.show("anonymous");
      if ($("light-candle-input")) {
        Element.hide("light-candle-input");
      }
      // Change the action attribute of the form
      // this is for the ajax for at requests;browse because the ajax onsubmit
      // interfers with the above
      if ($('prevent_double_submit').hasAttribute("onsubmit")) {
        $('prevent_double_submit').removeAttribute("onsubmit");
      }
      //$("prevent_double_submit").setAttribute("onsubmit" , "new Ajax.Request('/account/create', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;");
      $("prevent_double_submit").setAttribute("action" , "/account/create");
//      $("prevent_double_submit").setAttribute("action" , "/account/create");
      //document.forms['post-and-join'].
    });
  }
  
	$$('a[name="delete-reply"]').each(function (el) {
		return $(el).observe('click', function(ev) {
			var response_id = el.id.substring(13);//delete-reply-(response_id)
			if(confirm("Are you sure you want to delete this response?")){
			}else{
				Event.stop(ev);
			}
		});
	});
  
	$$('form[name="prayer-comment-form"]').each(function (el) {
	    return $(el).observe('submit', function(ev) {
	    	Event.stop(ev); // stop the form from submitting
	    	var response_id = encodeURIComponent(el.response_id.value);
	    	var comment_body = encodeURIComponent(el.body.value);
			var container = 'comments-for-response-'+response_id;
			var url = "/response/comment/ajax_new.php";
			new Ajax.Request(
				//container, 
				url, {
				asynchronous: true,
				method: 'post',
				parameters:"submit=1&response_id="+response_id+"&body="+comment_body,
				onComplete: function(transport) {
					if(transport.status >= 200 && transport.status <= 300){
						$(container).update($(container).innerHTML + transport.responseText);
						$('textarea-for-comment-for-reply-'+response_id).clear();
				    	$("button-to-comment-on-reply-"+response_id).show();
				    	
				    	$('comment-form-for-reply-'+response_id).hide();
					}else{
						var error_string = "Connection error, please try again.";
						$('submitting-span').update(error_string);
						alert(error_string);
					}
					$('submitting-span').remove();
			    	$$("input[type=submit]").each ( function(submit_button) {
			    		submit_button.show();
			    	});
				},
				//onFailure:errFunc,
				insertion: Insertion.Bottom
			});
			return false;
	    });
	    
	    function errFunc(){
	    	alert("There was an error adding new comment. Please try again.");
	    	location.reload();
	    };
	});

});

//reCaptcha options:
var RecaptchaOptions = {
	theme : 'clean',
  	tabindex : 2
};

