$(document).ready(function() {
	/*--------------------------------------------------------------------------------------------------------------------------
					ONLOAD FUNCTIONS
	--------------------------------------------------------------------------------------------------------------------------*/					   
	checkKey();
	/*--------------------------------------------------------------------------------------------------------------------------
					JQUERY FUNCTIONS
	--------------------------------------------------------------------------------------------------------------------------*/
	$("#Name").blur(function() {
		var raw = this.value;
		if (raw.indexOf(" ") > -1) {
			var nameArray = raw.split(" ");
			if (nameArray.length > 1) {
				for (var i=2;i<nameArray.length;i++) {
					nameArray[1] += " " + nameArray[i];	
				}
			}
			$("#Contact0FirstName").val(nameArray[0]);
			$("#Contact0LastName").val(nameArray[1]);
		} else {
			$("#Contact0FirstName").val(raw);
		}
	});
	/*--------------------------------------------------------------------------------------------------------------------------
					VALIDATOR PROGRAMMING
	--------------------------------------------------------------------------------------------------------------------------*/
	// HIDE ERROR DIV
	$('div.errors').hide();
	// SETUP VALIDATOR CALLBACK
	$.validator.setDefaults({
		submitHandler: function() { 
			$.post('includes/track.php');
			document.leadForm.submit();  
		},
		invalidHandler: function(f, v) {
			var errors = v.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You have 1 error to fix. It has been highlighted above in <strong>red</strong>'
					: 'You have ' + errors + ' errors to fix. They have been highlited above in <strong>red</strong>';
				$('div.errors p').remove();
				$('<p>'+message+'</p>').appendTo('div.errors');
				$('div.errors').show();
			} else {
				$('div.errors').hide();
			}
			$.post('includes/fail.php');
		}
	});
	// SETUP VALIDATOR	
	$("#leadForm").validate({
		rules: {
			Contact0FirstName: {
				required:true,
				minlength:2
			},
			Contact0LastName: {
				required:true,
				minlength:2
			},
			Contact0Email: {
				required: true,
				email: true
			},
			Contact0Phone1: {
				required:true,
				phone: true
			}
		},
		messages: {
			Contact0FirstName: {
				required:"Please enter your firstname",
				minlength:"First name must be at least 2 characters"
			},
			Contact0LastName: {
				required: "Please enter your lastname",
				minlength: "Last name must be at least 2 characters"
			},
			Contact0Email: "Please enter a valid email address",
			Contact0Phone1: "Please enter a valid phone number"
		}
	});
});
/*--------------------------------------------------------------------------------------------------------------------------
					STANDARD FUNCTIONS
--------------------------------------------------------------------------------------------------------------------------*/
function checkKey() {
	if (keyword != null ) { document.getElementById("keyword").value = keyword; }
	var utm = getCookie('__utmz');
	if (utm.length>0) {
		ctr_start = utm.indexOf("utmctr=");
		if (ctr_start != -1) {
			ctr_start = ctr_start + 7;
			ctr_end = utm.indexOf("|",ctr_start);
			if (ctr_end == -1) ctr_end = utm.length;
			document.getElementById("keyword").value = utm.substring(ctr_start,ctr_end);
		}
	}
}

function getCookie(c_name) {
if (document.cookie.length>0) {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1) {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}
/*--------------------------------------------------------------------------------------------------------------------------
					PROTOTYPE EXTENTIONS
--------------------------------------------------------------------------------------------------------------------------*/
String.prototype.clean = function(rmv) {
	if (rmv.constructor.toString().indexOf("Array") == -1) {
		var temp = rmv;
		delete rmv;
		var rmv = [temp];
	}
	var str = this;
	for (var i=0; i<rmv.length; i++) {
		rmvTgt = str.indexOf( rmv[i] );
		while (rmvTgt != -1) {
			str = str.replace( rmv[i],"" );
			rmvTgt = str.indexOf( rmv[i] );
		}
		rmvTgt = str.indexOf( rmv[i].toUpperCase() );
		while (rmvTgt != -1) {
			str = str.replace( rmv[i].toUpperCase(),"" );
			rmvTgt = str.indexOf( rmv[i].toUpperCase() );
		}
	}
	return str;
}