
  var login_form_options = {
	     dataType:  'json',
	     beforeSubmit: loginValidate,
	     success:  loginResponse
  };



	$(document).ready(function() {
		if(window.XMLHttpRequest == undefined) {
			//alert('XMLHttpRequest Disabled...');
		}
		$('#error_resume').mouseup(function() {
        	$('#login_form_error').fadeOut('normal', function() {
        	  	$('#login_form_compact').fadeIn('normal');
        	});
        });
		// JQUERY FORM SUBMIT ::

        if(typeof(window['authenticated']) != "undefined" && window['authenticated']) {
	  	  $("#authenticated_header").show(); // show the authenticated message.
		} else {
	  	  $("#login_form_compact").show();  // show the login form.
	    }

        $('#login_form').submit(function() {
          if ($('#account').val() == $('#account')[0].title || $('#secret').val() == $('#secret')[0].title) {
          	$("#login_form_compact").hide();
          	$("#login_form_error_text").text("Enter your Email and Password");
		    $("#login_form_error").fadeIn("slow");
		    return false;
          }
          $("#login_form_compact").fadeOut('normal', function() {
          	  $('#login_form').ajaxSubmit(login_form_options);
          });
          return false;
        });
        // DEFAULT INPUT VALUES CONTROL ::

	    $("input.dark").focus(function () {
	    	input_focus(this);
	    });
	    $("input.dark").blur(function () {
	    	input_blur(this);
	    });
	    $("input.dark").blur();  // Force the blur so the styles get applied.
	});


  function input_focus(obj) {
      if ($(obj).val() == $(obj)[0].title) {
          $(obj).removeClass("dark_default");
          $(obj).val("");
          if(obj.title == 'Password') { // if it's a password field by default
          	replace_input('focus', obj, 'password', '', $(obj).attr('class'));
          }
      }
  }

  function input_blur(obj) {
      if ($(obj).val() == "") {
          $(obj).addClass("dark_default");
          $(obj).val($(obj)[0].title);
          if(obj.type == 'password' && $(obj).attr('value') == "" || $(obj).attr('value') == "Password") { // if it's a password field and it's empty make it a text field to show the default value!
          	replace_input('blur', obj, 'text', 'Password', $(obj).attr('class'));
          }
      }
  }

  function replace_input(event, obj, type, value) {
	  // Sadly this is so it works in IE6/7/Safari(webkit)
		var newObj=document.createElement('input');
		objId = $(obj).attr('id');
		newObjId = obj.getAttribute('id') + '_new';
		newObj.setAttribute('id',newObjId);
		newObj.setAttribute('type',type);
		newObj.setAttribute('value',value);
		newObj.setAttribute('name',obj.getAttribute('name'));
		newObj.setAttribute('title',obj.getAttribute('title'));
		$(newObj).attr('class',$(obj).attr('class'));
		$(newObj).attr('style',$(obj).attr('style'));
		pNodeId = $(obj).parent().attr('id');
		document.getElementById(pNodeId).insertBefore(newObj, obj);
        $('#'+objId).each(function() {
    			$(this).remove();
    	});
        newObj.setAttribute('id',objId);
		if(event == 'focus') {
		  $(newObj).focus();
		}
	    $("input.dark").focus(function () {
	    	input_focus(this);
	    });
	    $("input.dark").blur(function () {
	    	input_blur(this);
	    });
	    return newObj;
  }

   // Validates the login form on this page.
   function loginValidate(formData, jqForm, login_form_options) {
    for (var i=0; i < formData.length; i++) {
        if (!formData[i].value) {
		    $("#login_form_error_text").text("Enter your Email and Password");
		    $("#login_form_error").fadeIn("slow");
		    return false;
        }
    }
	  $("#login_form_error_text").text(""); // If an error wasn't caught empty the message.
	  return true;
  }

   // Processes the login response
   function loginResponse(data,status) {
	 //$("#debug_div_json_login").text("Debug Response:" + data);  // FOR DEBUGGING
	 if(typeof(data) == 'object') {
       if(data.success) {
	    $("#login_form_compact").html(data.html);
	    $("#login_form_compact").fadeIn("slow");
	   } else if (data.error) {
	    $("#login_form_error_text").text(data.error);
	    $("#login_form_error").fadeIn("slow");
	   } else {
	     $("#login_form_error_text").text("The server did not respond as expected");
	     $("#login_form_error").fadeIn("slow");
	   }
	 } else {
	   $("#login_form_error_text").text("The server did not respond to the request");
	   $("#login_form_error").fadeIn("slow");
	 }
   }




function countdownEvent(varname, function1, function2) {
	// varname is the global name of the variable to store the countdown (you should have created before hand)
	// varname should be a whole number equal to the number of seconds you want the countdown to take.
	// the purpose of using a global variable is that the countdown can be modified at will by the anonymous functions.
	// function1 is an anonymous function that executes every cycle
	// function2 is an anonymous function that executes upon completion of the countdown.
	window[varname] = window[varname] - 1;
	function1(varname);
	if(window[varname] == undefined || window[varname] == NaN || window[varname] < 1) {
		function2(varname);
		return true;
	}
	setTimeout('countdownEvent(\''+varname+'\', '+function1+','+function2+');', 1000);
	return true;
}