/*
	The function updated the title and meta tags of the page depending on some conditions. Please refere to the requirements 
	for the functional knowledge.
*/
function updateMeta(currentAction, information){
    //debugMode = true;
	if(debugMode){ 
		alert("Entered function updateMeta()");
	}

   	if (document.getElementsByTagName) {
        var descriptionTagIndex;
		var keywordsTagIndex;
        var descriptionContent;
		var keywordContent;
        
		// The following loop gets all the meta tags in the page and fixds the index of tags "description" and "keywords"
		metatags = document.getElementsByTagName("meta");
		for (var cnt = 0; cnt < metatags.length; cnt++){
			var name = metatags[cnt].getAttribute("name");
						
			if (name!= null && name!='' && name == "description"){
				if(debugMode){ 
					alert("Got description tag, Index: "+cnt);
				}
				
				descriptionTagIndex = cnt;
			}else if (name!= null && name!='' && name == "keywords"){
				if(debugMode){ 
					alert("Got keywords tag, Index: "+cnt);
				}
				
				keywordsTagIndex = cnt;
			}
		}
		
		/* The first if loop is if the search is being done based on the zipcode 
		   Second loop handles the "city, state" search
		   Third loop handles the "driving directions" input page
		   Fourth and the last loop handles the "driving direction results" page
		*/
		if(currentAction == 'zipcode'){
			if(debugMode){ 
				alert("In zipcode section.");
			}
			
			document.title = dealerAssets["dealerLocator.searchResults.titleCommon"]+' '+dealerAssets["dealerLocator.searchResults.zipCodeTitle"]+' '+information;
			
			descriptionContent = dealerAssets["dealerLocator.searchResults.meta.descPart1"]+' '+information+' '+dealerAssets["dealerLocator.searchResults.meta.descPart2"];
			keywordContent = dealerAssets["dealerLocator.searchResults.meta.keyPart1"]+' '+information+', '+dealerAssets["dealerLocator.searchResults.meta.keyPart2"];
			
			if(debugMode){ 
				alert("New description: "+descriptionContent);
				alert("New keywords: "+keywordContent);
			}
			

			metatags[descriptionTagIndex].setAttribute("content", descriptionContent);
			metatags[keywordsTagIndex].setAttribute("content", keywordContent);

			if(debugMode){ 
				alert("Leaving zipcode section.");
			}
		}else if(currentAction == 'cityState'){
			if(debugMode){ 
				alert("In city&state section.");
			}
			
			document.title = dealerAssets["dealerLocator.searchResults.titleCommon"]+' '+information+' '+dealerAssets["dealerLocator.searchResults.cityStateTitlePart"];	

			descriptionContent = dealerAssets["dealerLocator.searchResults.meta.descPart1"]+' '+information+' '+dealerAssets["dealerLocator.searchResults.meta.descPart2"];
			keywordContent = dealerAssets["dealerLocator.searchResults.meta.keyPart1"]+' '+information+', '+dealerAssets["dealerLocator.searchResults.meta.keyPart2"];
			
			if(debugMode){ 
				alert("New description: "+descriptionContent);
			alert("New keywords: "+keywordContent);
			}
			
			metatags[descriptionTagIndex].setAttribute("content", descriptionContent);
			metatags[keywordsTagIndex].setAttribute("content", keywordContent);
			
			if(debugMode){ 
				alert("Leaving city&state section.");
			}
		}else if(currentAction == 'drivingDirections'){
			if(debugMode){ 
				alert("In driving directions section.");
			}
			
			document.title = dealerAssets["dealerLocator.searchResults.titleCommon"]+' '+positionedDealer["name"]+', '+positionedDealer["city"]+', '+positionedDealer["state"]+', '+positionedDealer["postalCode"]+' '+dealerAssets["dealerLocator.drivingDirections.commonTitle"];	

			descriptionContent = dealerAssets["dealerLocator.drivingDirections.meta.descPart1"]+' '+positionedDealer["name"]+', '+positionedDealer["city"]+', '+positionedDealer["state"]+', '+positionedDealer["postalCode"]+' '+dealerAssets["dealerLocator.drivingDirections.meta.descPart2"];
			keywordContent = dealerAssets["dealerLocator.drivingDirections.meta.keyPartCommon1"]+' '+positionedDealer["name"]+', '+positionedDealer["city"]+', '+positionedDealer["state"]+', '+positionedDealer["postalCode"]+', '+dealerAssets["dealerLocator.drivingDirections.meta.keyPartCommon2"]+dealerAssets["dealerLocator.drivingDirections.meta.keyPart2"];
			
			if(debugMode){ 
				alert("New description: "+descriptionContent);
				alert("New keywords: "+keywordContent);
			}
			
			metatags[descriptionTagIndex].setAttribute("content", descriptionContent);
			metatags[keywordsTagIndex].setAttribute("content", keywordContent);
			
			if(debugMode){ 
				alert("Leaving driving directions section.");
			}
		}else if(currentAction == 'drivingDirectionResults'){
			if(debugMode){ 
				alert("In driving directions results section.");
			}
			
			document.title = dealerAssets["dealerLocator.searchResults.titleCommon"]+' '+positionedDealer["name"]+', '+positionedDealer["city"]+', '+positionedDealer["state"]+', '+positionedDealer["postalCode"]+' '+dealerAssets["dealerLocator.drivingDirections.commonTitle"]+' - '+dealerAssets["dealerLocator.drivingDirectionResults.title"];	

			descriptionContent = dealerAssets["dealerLocator.drivingDirectionResults.meta.descPart1"]+' '+positionedDealer["name"]+', '+positionedDealer["city"]+', '+positionedDealer["state"]+', '+positionedDealer["postalCode"]+' '+dealerAssets["dealerLocator.drivingDirectionResults.meta.descPart2"];
			keywordContent = dealerAssets["dealerLocator.drivingDirections.meta.keyPartCommon1"]+' '+positionedDealer["name"]+', '+positionedDealer["city"]+', '+positionedDealer["state"]+', '+positionedDealer["postalCode"]+', '+dealerAssets["dealerLocator.drivingDirections.meta.keyPartCommon2"];
			
			if(debugMode){ 
				alert("New description: "+descriptionContent);
				alert("New keywords: "+keywordContent);
			}
			
			metatags[descriptionTagIndex].setAttribute("content", descriptionContent);
			metatags[keywordsTagIndex].setAttribute("content", keywordContent);

			if(debugMode){ 
				alert("Leaving driving directions results section.");
			}
		}
	}

	if(debugMode){ 
		alert("Leaving function updateMeta()");
	}
	//debugMode = false;
}

