jQuery(function(){

	var alert_message = "Please fill out the following required sections:\n";

	var validate = function(required_fields){
		var missing = [];
		var failed = [];
		jQuery(required_fields).each(function(){
			var $label = jQuery(this).prev('label');
			if(!$label.length)
				$label = jQuery(this).prev('span').prev('label');
			var label = $label.text();
			label = label.substr(0, label.length-1);
			if(!this.value)
				missing.push(label);
			else if((this.name=='ZipCode' && !validate.zip(this)) || (this.name=='Email' && !validate.email(this)))
				failed.push(label);
			return true;
		});
		if(missing.length){
			alert(alert_message + missing.join("\n"));
			return false;
		}
		if(failed.length){
			return false
		}
		return true;
	}

	validate.email = function (field){
		var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if(!filter.test(field.value)){
			alert('Please provide a valid email address');
			return false;
		}
		return true;
	}

	validate.zip = function(field) {
		var valid = "0123456789-";
		var hyphencount = 0;
		var value = field.value;

		if (value.length!=5 && value.length!=10) {
			alert("Please enter your 5 digit or 5 digit+4 zip code.");
			return false;
		}
		for (var i=0; i < value.length; i++) {
			var temp = "" + value.substring(i, i+1);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in your zip code.  Please try again.");
				return false;
			}
			if ((hyphencount > 1) || ((value.length==10) && "" + value.charAt(5) != "-")) {
				alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
				return false;
			}
		}
		return true;
	}

	jQuery('#lead-form-1 form').each(function(){
		this.required_fields = [this.FirstName, this.LastName, this.Email, this.ZipCode];
	});

	jQuery('#lead-form-2').each(function(){
		this.required_fields = [this.FirstName, this.LastName, this.Email, this.Phone, this.ZipCode, this.StreetAddress, this.PropertyType, this.AvgElectricityBill, this.CurrentProvider];
	});

	jQuery('#lead-form-1 form, #lead-form-2').submit(function(){
		return validate(this.required_fields);
	})
	.each(function(){
		jQuery(this.required_fields).each(function(){
			var $label = jQuery(this).prev('label');
			if(!$label.length)
				$label = jQuery(this).prev('span').prev('label');
			$label.text($label.text() + '*');
		})
	});



});

function moneyFormat(field) {
	var num = field.value;
	if(num)
		num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		field.value = '0';
	var sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	var cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	if(isNaN(num)){
		field.value = '0';
	}
	else{
		field.value = (((sign)?'':'-') + num + '.' + cents);
	}
}