// JavaScript Document
//draggable, location, zoom
function initMap( x, y, zoom, canDrag, iconImage, adType){
		
		var options = {
				centerx : x,
				centery : y,				
				zoomLevel : zoom,
				useMouseWheelZooming : false				
		}
		var map = new GSMap("myMap", options)
		
		map.addControl(GSMap.MAP_CONTROL);					
		initCoord =new GSPoint(x, y);
		params = {"name" : "Move to the location of your farm",
				 "coordinate" : initCoord, draggable : canDrag, "dragToPan" : false};

		/*only activate the listener if the user can move points etc, ifthe user can drag the 
		icon then the listener should be on*/
		if(canDrag){
			map.addListener(window); 											
		}
		
		adLocation = new GSPointFeature(params)
		
		icon = createIcon(iconImage, adType);
		adLocation.setIcon(icon);
		adLocation.addListener(window);
		map.addFeature(adLocation);
		document.enterAdDetailsForm.adLocation.value = (adLocation.coordinate);	
		document.enterAdDetailsForm.zoomLevel.value = zoom;
	}

/**when the feature is moved adLocation is set to the new point
	and get the zoom level in case page has to be reloaded
	*/
	/**when the feature is moved homeLocation is set to the new point then 
	centres the map on that location*/
	function featureDragEnd(feature){
		document.enterAdDetailsForm.adLocation.value = feature.coordinate;	
		map.panTo(feature.coordinate);	
	}
	
	/*stores the zoom level when zoom is changed. Means that whatever the 
	zoom level is when a user reloads the page it is set*/
	function mapZoomed(map, oldZoomLevel, newZoomLevel){
		document.enterAdDetailsForm.zoomLevel.value = newZoomLevel;
	}
	
	/*Moves the adLocation feature to the point clicked and stores the location*/
	function mapDblClicked(map, location){
		adLocation.coordinate = location;
		document.enterAdDetailsForm.adLocation.value = location;	
	}

	
	function getIconName(feed, adType){
	feed = feed.toLowerCase();
	var useDefault = false;
	var iconName = "";
	if(feed != "" && adType != ""){
		switch(feed){
			case "brassica":
				iconName =  "Brassica";
				break;
			case "maize":
				iconName =  "Maize";
				break;				
			case "grass":
				iconName =  "Grass";
				break;
			case "swedes":
				iconName =  "Swedes";
				break;
			case "green feed oats":
				iconName =  "GreenFeedOats";
				break;
			case "hay":
				iconName =  "Hay";
				break;
			case "baleage":
				iconName =  "Baleage";
				break;
			case "silage":
				iconName =  "Silage";
				break;
			default:
				useDefault = true;
				
		}
		
			if(adType == "Wanted"){
				iconName = iconName + "W";	
			}
			else if(adType == "For Sale"){
				iconName = iconName + "S";
			}
			else{
				useDefault = true;
			}
		
		
		if(useDefault){
			iconName = "DefaultIcon"	
		}
		else{
			iconName = iconName + "O"
		}
	}
	else{
		iconName = "DefaultIcon"
	}
		return iconName + ".gif";
}

	/*used to create icon*/
	function createIcon(iconImage, adType){
		var icon = new GSIcon()
		icon.imageSrc = "Images/Icons/" + getIconName(iconImage, adType);
		//iconTemplate.alt = "Click to view details";			
		icon.imageSize = new GSDimension(31, 31);			
		/*when the html balloon apears it will be (x, y) pixels from top left of the icon*/ 
		icon.iconInfoWindowOffset = new GSPoint(24,4);
		/*set the icon to sit with the point on the exact point where the user selected*/
		icon.iconOffset = new GSPoint(-14,-30);
		
		return icon;
	}
	
	function changeIcon(iconImage, adType){
		icon = createIcon(iconImage, adType);		
		adLocation.setIcon(icon);
	}
	
	function wipeValue(textBox){
		if(textBox.value == '--Min--' || textBox.value == '--Max--'){
			textBox.value = '';					
		}		
	}
	
	function refilValue(textBox, replacement){
		if(textBox.value == ""){
			textBox.value = replacement; 	
		}
	}
	
	/*change the backgound of the min and max stock values when ticking and unticking the relevent checkBox, if the checkbox is unticked disable the textboxes*/
	function changeStockCheckBox(checkBox, minTextBox, maxTextBox){		
		if(checkBox.checked == true){
			minTextBox.style.backgroundColor = "#FFFFFF";
			maxTextBox.style.backgroundColor = "#FFFFFF";
			minTextBox.style.color = "#000000";
			maxTextBox.style.color = "#000000";	
			minTextBox.disabled = false;
			maxTextBox.disabled = false;
		}
		else if(checkBox.checked == false){
			minTextBox.style.backgroundColor = "#CCCCCC";
			maxTextBox.style.backgroundColor = "#CCCCCC";
			minTextBox.style.color = "#666666";
			maxTextBox.style.color = "#666666";	
			minTextBox.disabled = true;
			maxTextBox.disabled = true;
			minTextBox.value = "--Min--";
			maxTextBox.value= "--Max--";

		}
	}
	
	/*same as above but for the other stock type text box*/
	function changeOtherStockText(otherStockTextbox, minTextBox, maxTextBox){
		if(otherStockTextbox.value != ""){
			minTextBox.style.backgroundColor = "#FFFFFF";
			maxTextBox.style.backgroundColor = "#FFFFFF";
			minTextBox.style.color = "#000000";
			maxTextBox.style.color = "#000000";	
			minTextBox.disabled = false;
			maxTextBox.disabled = false;
		}
		else if(otherStockTextbox.value == ""){
			minTextBox.style.backgroundColor = "#CCCCCC";
			maxTextBox.style.backgroundColor = "#CCCCCC";
			minTextBox.style.color = "#666666";
			maxTextBox.style.color = "#666666";	
			minTextBox.disabled = true;
			maxTextBox.disabled = true;
			minTextBox.value = "--Min--";
			maxTextBox.value= "--Max--";

		}
	}
	
	/*counts the numbers in the text area s and if it is over 300 crops the value to 300. Then displays the nubmer of letters in the textbox inside the element with the id "letters" */
	function countLetters(s, maxLength){
		var txt = s.value;	
		var totalLetters = txt.length +1;		

		if(totalLetters > maxLength){					
			txt = txt.substring(0, maxLength);
			s.value = txt;
			totalLetters = txt.length;	
		}		
		
		var lettersElement = document.getElementById("letters");
		lettersElement.innerHTML = totalLetters;
	}
	
