function metaCalculator(){

	//retrieve firm name
	var firmName = capitalizeThis($("input[name='firm']").val());

	//retrieve all 'Alternate Locations' and puts them into the tempGeo array. Then shuffles the array. 
	var tempGeo = new Array();
	$('#calculator .geolocations :input').each(function (el) {
		if(this.value!=''){tempGeo.push(capitalizeThis(this.value))};
	});
	
	//retrieves 'City', 'State', and one (random) 'Alternate Location' and puts them into the geoLocation array.
	var geoLocation = new Array(capitalizeThis($("input[name='city']").val()),capitalizeThis($("input[name='state']").val()),tempGeo[rand(tempGeo.length)]);

	//retrieves all 'Sub-key terms' and puts them into the keyTerms array. (name="keyterm'(1-9)'"
	var keyTerms = new Array();
	if($('input.pa1').is(':checked')){
		$('#calculator .pa1 :input.keyterms').each(function (el) {
			keyTerms.push(this.value);
		});
	}
	if($('input.pa2').is(':checked')){
		$('#calculator .pa2 :input.keyterms').each(function (el) {
			keyTerms.push(this.value);
		});
	}
	if($('input.pa3').is(':checked')){
		$('#calculator .pa3 :input.keyterms').each(function (el) {
			keyTerms.push(this.value);
		});
	}




	//retrieves the 'Additional Keywords' and puts them into the keywords array. Than converts the keywords array into a string full of the keywords, separated by commas.
	var keywords = new Array();
	$('#calculator .keywords :input').each(function (el) {if(this.value!=''){keywords.push(this.value)};});
	var keywordString = keywords.toString();var keywords = keywordString.replace(/,/gi, ", ");

	//retrieves the three main 'Practice Areas' and puts them into the practiceAreas array.
	var practiceAreas = new Array();
	$('#calculator :input.pagroup').each(function (el) {if(this.value!=''){practiceAreas.push(this.value)};});	
	
	var mainAreas = new Array;
	if($('input.pa1').is(':checked')){mainAreas.push(practiceAreas[0]);}
	if($('input.pa2').is(':checked')){mainAreas.push(practiceAreas[1]);}
	if($('input.pa3').is(':checked')){mainAreas.push(practiceAreas[2]);}
	mainAreas.push(keyTerms[rand(keyTerms.length)]);



	var attorney = new Array("attorneys","lawyers");
	attorney.shuffle();
		
	// Randomize the geographical locations and put them into a temp array (tempGeo)
	var tempGeo = new Array(geoLocation[rand(3)]);	
	
	while(tempGeo[1]==null){var x = geoLocation[rand(3)];if(x!=tempGeo[0]){tempGeo[1] = x;}}
	while(tempGeo[2]==null){var x = geoLocation[rand(3)];if(x!=tempGeo[0] && x!=tempGeo[1]){tempGeo[2] = x;}}
	
	var shuffledGeos = new Array();
	for(var x = 0; x <= geoLocation.length; x++){
		shuffledGeos.push(geoLocation[x]);
	}
	shuffledGeos.shuffle();
	
	this.title = function(){
		return capitalizeThis(shuffledGeos[0] + ' ' + mainAreas[0] + ' ' + attorney[0] + ' | '  + shuffledGeos[1] + ' ' + mainAreas[1] + ' ' + attorney[1] + ' | ' + firmName);
	}

	//Returns Description Meta Tags
	this.description = function(){
		keyTerms.shuffle();
		practiceAreas.shuffle();
		
		switch (mainAreas.length){
			case 4:
				return firmName + ' represents clients in ' + mainAreas[0] + ', ' + mainAreas[1] + ' and ' + mainAreas[2] + ' cases including ' + keyTerms[0] + ' and ' + keyTerms[1] + ' in ' + geoLocation[0] + ', ' + geoLocation[1];
				break;
			case 3:
				return firmName + ' represents clients in ' + mainAreas[0] + ' and ' + mainAreas[1] + ' cases including ' + keyTerms[0] + ' and ' + keyTerms[1] + ' in ' + geoLocation[0] + ', ' + geoLocation[1];
				break;
			case 2:
				return firmName + ' represents clients in ' + mainAreas[0] + ' cases including ' + keyTerms[0] + ', ' + keyTerms[1] + ' and ' + keyTerms[2] + ' in ' + geoLocation[0] + ', ' + geoLocation[1];
				break;
			default: alert('Error');
		}
	}
	//end Description function

	//Returns Keyword Meta Tags
	this.keyword = function(){		
		if(practiceAreas[0]!=undefined && practiceAreas[1]!=undefined && practiceAreas[2]!=undefined){
			var ifKeyWords = "";if(keywords != ""){ifKeyWords = ', ' + keywords;}
			return geoLocation[0] + ' ' + practiceAreas[0] + ' ' + attorney[0] + ', ' + geoLocation[1] + ' ' + practiceAreas[1]+ ' ' +  attorney[1] + ', ' + geoLocation[2] + ' ' + practiceAreas[2] + ' ' + attorney[0] + ', ' + geoLocation[0] + ' ' + practiceAreas[1] + ' ' + attorney[1] + ', ' + geoLocation[1] + ' ' + practiceAreas[2] + ' ' + attorney[0] + ', ' + geoLocation[2] + ' ' + practiceAreas[0] + ' ' + attorney[1] + ', ' + geoLocation[0] + ' ' + practiceAreas[2] + ', ' + geoLocation[1] + ' ' + practiceAreas[0] + ', ' + geoLocation[2] + ' ' + practiceAreas[1] + ifKeyWords;
		} else if(practiceAreas[0]!=undefined && practiceAreas[1]!=undefined) {
			var ifKeyWords = "";if(keywords != ""){ifKeyWords = ', ' + keywords;}
			return geoLocation[0] + ' ' + practiceAreas[0] + ' ' + attorney[0] + ', ' + geoLocation[1] + ' ' + practiceAreas[1] + ' ' + attorney[1] + ', ' + geoLocation[2] + ' ' + keyTerms[0] + ' ' + attorney[0] + ', ' + geoLocation[0] + ' ' + practiceAreas[1] + ' ' + attorney[1] + ', ' + geoLocation[1] + ' ' + keyTerms[0] + ' ' + attorney[0] + ', ' + geoLocation[2] + ' ' + practiceAreas[0] + ' ' + attorney[1] + ', ' + geoLocation[0] + ' ' + keyTerms[1] + ', ' + geoLocation[1] + ' ' + practiceAreas[0] + ', ' + geoLocation[2] + ' ' + keyTerms[0] + ifKeyWords;
		} else {
			var ifKeyWords = "";if(keywords != ""){ifKeyWords = ', ' + keywords;}
			return geoLocation[0] + " " + practiceAreas[0] + " " + attorney[0] + ", " + geoLocation[1] + " " + keyTerms[0] + " " + attorney[1] + ", " + geoLocation[2] + " " + keyTerms[1] + " " + attorney[0] + ", " + geoLocation[0] + " " + keyTerms[0] + " " + attorney[1] + ", " + geoLocation[1] + " " + keyTerms[1] + " " + attorney[0] + ", " + geoLocation[2] + " " + practiceAreas[0] + " " + attorney[1] + ", " + geoLocation[0] + " " + keyTerms[1] + ", " + geoLocation[1] + " " + practiceAreas[0] + ", " + geoLocation[2] + " " + keyTerms[0] + ifKeyWords;
		}
		//end Keyword function
		
	}
}

// Randomizes
function rand(n){return (Math.floor(Math.random()*n));}

// Shuffles
Array.prototype.shuffle = function() {function randOrd(){return (Math.round(Math.random())-0.5); }this.sort( randOrd );}
Array.prototype.last = function() {return this[this.length-1];}

// Whats ths do?
$.validator.setDefaults({
	errorPlacement: function(error, element) {
		$(element).attr({"title": error});
	},
	highlight: function(element){
    $(element).removeClass("textinput");
		$(element).addClass("errorHighlight");
	},
	unhighlight: function(element){
		$(element).removeClass("errorHighlight");
		$(element).addClass("textinput");
	}
});

// Capitalizes
function capitalizeThis(obj) {val = obj;newVal = '';val = val.split(' ');for(var c=0; c < val.length; c++) {if(c == (val.length-1)){newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length);} else {newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';}}return newVal;}
