function searchMap_link_replace(){
	if(placesMMIsSupportedBrowser()) {
		var mapLinks = getElementsByClassName('searchMap_link');
		for(i=0;i<mapLinks.length;i++){
			mapLinks[i].href = mapLinks[i].href.replace(/ssm=1/,"ssm=0");
			mapLinks[i].href = mapLinks[i].href.replace(/ssm=&/,"ssm=0&");
		}
		var hiddenInputs = document.getElementsByName('ssm');
		for(i=0;i<hiddenInputs.length;i++){
			hiddenInputs[i].value="0";
		}
	}
}


function autoSubmit(formEl){
	while (formEl.tagName.toLowerCase() != "form") {
		formEl = formEl.parentNode;
	}
	try{
		formEl.onsubmit(); // Ensure onsubmit event is done before submitting the form...
	}catch(err){}
	formEl.submit();
}

function changeClass(el){
	chosenOption = el.selectedIndex;
	chosenST = el.options[chosenOption].parentNode.label;
	if(chosenST=="Similar searches" || chosenST=="Similar search"){
		for(i=0;i<gimme('.changeClassST').length;i++){
			gimme('.changeClassST')[i].value="relatedclassification";
		}
	}
	if(chosenST=="Narrow search"){
		for(i=0;i<gimme('.changeClassST').length;i++){
			gimme('.changeClassST')[i].value="refinedclassification";
		}		
	}
}

function toolsPrep(){
	for(i=0;i<gimme('.startPoint').length;i++){
    if (!gimme('.startPoint')[i].value) {
      gimme('.startPoint')[i].value="e.g postcode";
    }
    gimme('.startPoint')[i].onfocus = function(){
			this.value="";
		};
	}
	for(i=0;i<gimme('.autoSubmit').length;i++){
		gimme('.autoSubmit')[i].onchange = function(){
			if(this.selectedIndex!==0){
				changeClass(this);
				changeLoc(this);
				autoSubmit(this);
			}else{
				return false;
			}
		};
	}
}

function changeLoc(el){
	chosenOption = el.selectedIndex;
	chosenST = el.options[chosenOption].parentNode.label;
	if(chosenST=="Expand location"){
		for(i=0;i<gimme('.changeLocationST').length;i++){
			gimme('.changeLocationST')[i].value="expandedlocation";
		}
	}
	if(chosenST=="Related location" || chosenST=="Related locations"){
		for(i=0;i<gimme('.changeLocationST').length;i++){
			gimme('.changeLocationST')[i].value="relatedlocation";
		}
	}
	if(chosenST=="Narrow location"){
		for(i=0;i<gimme('.changeLocationST').length;i++){
			gimme('.changeLocationST')[i].value="narrowerlocation";
		}
	}				
}


$(function(){
		searchMap_link_replace();
		toolsPrep();
});