// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function toggle( e )
{
  // apply style to hide or show list elements
  if( e.className == 'expand' )
    e.className = 'hide';
  else
    e.className = 'expand';
}

// prevent a click on a child list element from reaching the
// parent.  
function cancel( evt )
{
  // stop event from bubbling
  if( window.event )
    window.event.cancelBubble = true;  // ie
  else if (evt.stopPropagation) 
    evt.stopPropagation();  // firefox
}

function clear_login() 
{
	if ($('username').value == "Email Address")
	{
		$('username').value = '';
		$('password').value = '';
	}
}

function validate_required(field,alerttxt)
{
	alert("Made it here!");
	if ($(field).value == null || $(field).value == "")
 	{
		alert("Made it here!");
		alert(alerttxt);
		return false;
	}
	else 
	{
		return true;
	}
}

function validate_email(field,alerttxt)
{
	apos=$(field).value.indexOf("@");
	dotpos=$(field).value.lastIndexOf(".");
	if (apos<1||dotpos-apos<2) 
  	{
		alert(alerttxt);
		return false;
	}
	else 
	{
		return true;
	}
}

function validate_form()
{
	if($('comment_full_name').value == null || $('comment_full_name').value == "")
	{
		alert("Please fill in your name before submission.");
		return false;
	}
	if($('comment_address').value == null || $('comment_address').value == "")
	{
		alert("Please fill in your address before submission.");
		return false;
	}
	if($('comment_city').value == null || $('comment_city').value == "")
	{
		alert("Please fill in your city before submission.");
		return false;
	}
	if($('state').value == null || $('state').value == "")
	{
		alert("Please fill in your state before submission.");
		return false;
	}
	if($('comment_zip').value == null || $('comment_zip').value == "")
	{
		alert("Please fill in your zip before submission.");
		return false;
	}
	if($('comment_email').value == null || $('comment_email').value == "")
	{
		alert("Please fill in your email before submission.");
		return false;
	}
	if($('comment_message').value == null || $('comment_message').value == "")
	{
		alert("Please fill in your message/comments before submission.");
		return false;
	}

}
var billing = {
    
  showBilling: function(type) {
    billing_types = $$('.billing_types');
    billing_types.each(Element.hide);
    Element.show(type + '_billing');
  }
    
}

function hidePagination() {
	$$('.pagination').each(function(element) {
	   Element.hide(element);
	});
}

Ajax.Responders.register({
  onCreate: function() {
      if($('progress') && Ajax.activeRequestCount>0)
          jQuery('#progress_bak').stop().fadeTo(1000, 0.75).css('display','block').fadeTo(1000, 0.75);
          jQuery('#progress').stop().fadeTo(0, 0).css('display','block').fadeTo(1000, 1);
      },
  
  onComplete: function() {
      if($('progress') && Ajax.activeRequestCount==0)
          jQuery('#progress, #progress_bak').stop().fadeOut();
      }
 });

function display_search_notice()
{
	jQuery('#progress_bak').stop().fadeTo(1000, 0.75).css('display','block').fadeTo(1000, 0.75);
    jQuery('#progress').stop().fadeTo(0, 0).css('display','block').fadeTo(1000, 1);
	if(jQuery.browser.msie)
	{
		jQuery('#loading_graphic').remove();
		jQuery('#progress').append('<div><center><img src="/images/ajax-loader.gif"></center></div>');
	}
	if(jQuery('#form_submit').length >= 1)
	{	
		jQuery('#form_submit').hide();
	}
}

function follup_up_county_criminal_search() {
	jQuery(document).ready(function() {
	  jQuery('.ccbutton').click(function(event) {
			var error = false;
			var $button = jQuery(this);
			var $form = jQuery(this).parent();
			var subject = $form.find('input#subject').val();
			var $submonth = jQuery('#ccsubject' + subject + ' input[name=month]');
			var $subday = jQuery('#ccsubject' + subject + ' input[name=day]');
			var $subyear = jQuery('#ccsubject' + subject + ' input[name=year]');

			event.preventDefault();
			
			$submonth.removeClass('inputerror');
			$subday.removeClass('inputerror');
			$subyear.removeClass('inputerror');
			
			if( !/^\d{1,2}$/.test($submonth.val()) ) {
				$submonth.addClass('inputerror').focus();
				alert("Subject's Month of Birth must be a 1 or 2 digit number");
				error = true;
			}
			else if( !($submonth.val() >= 1 && $submonth.val() <= 12) ) {
				$submonth.addClass('inputerror').focus();
				alert("Subject's Month of Birth must be a number from 1 to 12");
				error = true;
			}
			else if( !/^\d{1,2}$/.test($subday.val()) ) {
				$subday.addClass('inputerror').focus();
				alert("Subject's Day of Birth must be a 1 or 2 digit number");
				error = true;
			}
			else if( !($subday.val() >= 1 && $subday.val() <= 31) ) {
				$subday.addClass('inputerror').focus();
				alert("Subject's Day of Birth must be a number from 1 to 31");
				error = true;
			}
			else if( !/^\d{4}$/.test($subyear.val()) ) {
				$subyear.addClass('inputerror').focus();
				alert("Subject's Year of Birth must be a 4 digit number");
				error = true;
			}

			if (!error) { //Fill in the forms dob with the dob inputs
				$button.attr("disabled",true);
				$button.attr("value", "Submitting Search Please Wait...");
				$form.find('input[name=sinput[dobmonth]]').val($submonth.val());
				$form.find('input[name=sinput[dobday]]').val($subday.val());
				$form.find('input[name=sinput[dobyear]]').val($subyear.val());
				
				jQuery.ajax({
					type: "POST",
					data: $form.serialize(),
					url: $form.attr("action"),
					timeout: 120000,
					cache: false,
					beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");},
					error: function() {
						$button.attr("value", "Search Failed to Submit");
						$button.removeAttr('disabled');
				  },
					success: function(r) {
						$form.html(r);
					}
				}); 
			}
		});
	});
}
