function convertIt(_a) {
  var _c = _a;
  var _x = _a.indexOf("$");
  if (_x == -1 ) _x = _a.indexOf(",");
  if (_x != -1) {
    var _p1 = _a.substr(0,_x);
	var _p2 = _a.substr(_x+1,_a.length);
    _c = convertIt(_p1+_p2);
  }
  return (_c);
}


$(document).ready(function(){
	$('a.dzemails').DZEmailDecryptomatic();
   

	if ($('.slideshow').length) {
		$('.slideshow').cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			timeout:  6000	
		});
	}


    $('.changemo').swapit('_over');
    
    $('a#go').click(function(){
		if ($('input#search-site').val() != '') {
			window.location = '?module=Search&keywords='+$('input#search-site').val();
			return false;
		}
	});

	$('input#search-site').focus(function() {
		$(this).val('');
	}).blur(function() { if ($(this).val() == '') { $(this).val('Search'); } });

    
    if ($('form#calverts-form').length) {
    
    	$('form#calverts-form input#sendmail').click(function(){
    		var _errors = new Array();
    		if ($('form#calverts-form input[name="name"]').val().length == 0) {
    			$('form#calverts-form input[name="name"]').addClass('formerror');
    			_errors.push("You must enter in your name.");
    		}
    		if ($('form#calverts-form input[name="address1"]').val().length == 0) {
    			$('form#calverts-form input[name="address1"]').addClass('formerror');
    			_errors.push("You must enter in your address.");
    		}
    		if ($('form#calverts-form input[name="city"]').val().length == 0) {
    			$('form#calverts-form input[name="city"]').addClass('formerror');
    			_errors.push("You must enter in your city.");
    		}
    		if ($('form#calverts-form input[name="zip"]').val().length == 0) {
    			$('form#calverts-form input[name="zip"]').addClass('formerror');
    			_errors.push("You must enter in your zip code.");
    		}
    		if ($('form#calverts-form input[name="phone1"]').val().length == 0) {
    			$('form#calverts-form input[name="phone1"]').addClass('formerror');
    			_errors.push("Please enter your phone number.");
    		}
    		if ($('form#calverts-form textarea#comments').val().length == 0) {
    			$('form#calverts-form textarea#comments').addClass('formerror');
    			_errors.push("Please provide the details of your project");
    		}
    		
    		if (_errors.length) {
    			alert("Error\n"+_errors.join("\n"))
    			return false;
    		}
    		
    		return true;
    	});
    }
    
    if ($('form#donate-form').length) {
    	$('form#donate-form input#donate-now').click(function(){
    		
    		var _errors = new Array();
    		
    		if (!$('form#donate-form input[name="total"]:checked').val()) {
    			_errors.push('You must select your donation amount');
    		} else {
    			if ($('form#donate-form input[name="total"]:checked').val() == 'other'){
    				var _amount = parseFloat(convertIt($('form#donate-form input[name="other-amount"]').val()));
    				if (isNaN(_amount)) {
    					_errors.push('You enter a valid amount for your donation');
    				} else {
	    				if (_amount < 1.00) {
	    					_errors.push('Donation amounts must be $1.00 or more.');
	    				}
    				}
    			}
    		}
    		
    		if ($('form#donate-form input[name="firstname"]').val().length == 0) {
    			$('form#donate-form input[name="firstname"]').addClass('formerror');
    			_errors.push("You must enter in your first name.");
    		}
    		
    		if ($('form#donate-form input[name="lastname"]').val().length == 0) {
    			$('form#donate-form input[name="lastname"]').addClass('formerror');
    			_errors.push("You must enter in your last name.");
    		}
    		
    		if ($('form#donate-form input[name="address1"]').val().length == 0) {
    			$('form#donate-form input[name="address1"]').addClass('formerror');
    			_errors.push("You must enter in your address.");
    		}
    		
    		if ($('form#donate-form input[name="city"]').val().length == 0) {
    			$('form#donate-form input[name="city"]').addClass('formerror');
    			_errors.push("You must enter in your city/township.");
    		}
    		
    		if ($('form#donate-form input[name="zip"]').val().length == 0) {
    			$('form#donate-form input[name="zip"]').addClass('formerror');
    			_errors.push("You must enter in your zip code.");
    		}
    		
    		if ($('form#donate-form input[name="email"]').val().length == 0 && 
    			$('form#donate-form input[name="phone1"]').val().length == 0) {
    			$('form#donate-form input[name="phone1"]').addClass('formerror');
    			$('form#donate-form input[name="email"]').addClass('formerror');
    			_errors.push("Please provide a way to contact you, either by phone and/or e-mail address.");
    		}
    		
    		if (_errors.length) {
    			alert("Error\n"+_errors.join("\n"))
    			return false;
    		}
    		
    		return true;
    	});
    }
    
    
    if ($('form#contact-form').length) {
    	$('form#contact-form input#sendmail').click(function(){
    		var _errors = new Array();
    		if ($('form#contact-form input[name="name"]').val().length == 0) {
    			$('form#contact-form input[name="name"]').addClass('formerror');
    			_errors.push("You must enter in your name.");
    		}
    		if ($('form#contact-form input[name="address1"]').val().length == 0) {
    			$('form#contact-form input[name="address1"]').addClass('formerror');
    			_errors.push("You must enter in your address.");
    		}
    		if ($('form#contact-form input[name="city"]').val().length == 0) {
    			$('form#contact-form input[name="city"]').addClass('formerror');
    			_errors.push("You must enter in your city.");
    		}
    		if ($('form#contact-form input[name="zip"]').val().length == 0) {
    			$('form#contact-form input[name="zip"]').addClass('formerror');
    			_errors.push("You must enter in your zip code.");
    		}
    		if ($('form#contact-form input[name="email"]').val().length == 0 && $('form#contact-form input[name="phone"]').val().length == 0) {
    			$('form#contact-form input[name="email"]').addClass('formerror');
    			$('form#contact-form input[name="phone"]').addClass('formerror');
    			_errors.push("You must enter in either your e-mail or phone #");
    		}
    		if ($('form#contact-form textarea#comments').val().length == 0) {
    			$('form#contact-form textarea#comments').addClass('formerror');
    			_errors.push("Please enter your questions or comments in the box provided.");
    		}
    		
    		if (_errors.length) {
    			alert("Error\n"+_errors.join("\n"))
    			return false;
    		}
    		
    		return true;
    	});
    }
    
    /**
     *	Cubic Feet Calculator
     */
    if ($('form.calc-form').length) {
    	
    	$('form.calc-form span.calc-display').text('');
    	
    	$('form.calc-form input#calc-submit').click(function() {
    	
    		var _calcdepth = parseFloat($('input[name="calc-depth"]').val());
    		var _calcwidth = parseFloat($('input[name="calc-width"]').val());
    		var _calcheight = parseFloat($('input[name="calc-height"]').val());
    		
    		var _errors = new Array();
    		
    		if (isNaN(_calcdepth) || isNaN(_calcwidth) || isNaN(_calcheight)) {
    			
    			if (isNaN(_calcdepth)) {
    				_errors.push('You forgot to enter the depth of your cargo space.');
    			}
    			if (isNaN(_calcwidth)) {
    				_errors.push('You forgot to enter the width of your cargo space.');
    			}
    			if (isNaN(_calcheight)) {
    				_errors.push('You forgot to enter the height of your cargo space.');
    			}
    			
    			$('form.calc-form span.calc-display').html('<h1 id="mainhead">Oops!</h1><p>'+_errors.join('<br />')+'</p>');
    			
    		} else {
    		
    			var _tail = String(((_calcwidth * _calcheight * _calcdepth) / 27)).split('.');
    				_tail[0] = Number(_tail[0]);
    				_tail[1] = Number(_tail[1].substr(0, 2));
    				if (_tail[1] > 50) {
    					_tail[0]++;
    					_tail.splice(1, 1);
    				} else {
    					_tail[1] = 5;
    				}
    				_tail = _tail.join('.');
    				_calcvolume = _tail;
    				
    			/**
    				_calcvolume = parseInt(Math.round(((_calcwidth) * (_calcheight) * (_calcdepth)) / 27));
    			 */
    			 
    			$('form.calc-form span.calc-display').html('<h1>Wow!</h1><p>You have '+_calcvolume+' cubic yard(s) of stuff!</p>');
    			
    		}
    		
    		return false;
    		
    	});
    
    }
    
});
