

function formTextClear(){
		formClear("searchBox", "e1");
		formClear("suggestions", "e2");
		formClear("firstname", "e3");
		formClear("surname", "e4");
		formClear("contactNo", "e5");
		formClear("email", "e6");
		formClear("order", "e7");
		formClear("addressfirstline", "e8");
		formClear("addresssecondline", "e9");
		formClear("addressthirdline", "e10");
		formClear("postcode", "e11");
		formClear("availability", "e12");
		formClear("additionalInfo", "e13");	
}

function formClear (element, elementNo) {

	if (document.getElementById(element) != null){			
			elementNo = document.getElementById(element);
			
			if ((elementNo.defaultValue.substr(0,12) == "Please enter") || (elementNo.defaultValue.substr(0,14) == "Enter keywords")) {		
					if (navigator.userAgent.indexOf("MSIE")!=-1	) {
						elementNo.attachEvent('onfocus', function() {clearValue(elementNo); })
						elementNo.attachEvent('onblur', function() {defaultValue(elementNo); })
					} else {
						elementNo.addEventListener('focus', function() {clearValue(elementNo); }, false)
						elementNo.addEventListener('blur', function() {defaultValue(elementNo); }, false)
					}
			}
	}
}

function clearValue(e) {	
	if(e.value == e.defaultValue){
		e.value = '';
	}
}

function defaultValue(e) {
	if(e.value==''){
		e.value = e.defaultValue;
	}
}

function getElementsByClass( searchClass, domNode, tagName) {
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) {
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1)
			el[j++] = tags[i];
	}
	return el;
}


function displayProduce(){

	if (document.getElementById("order-onlinePage")){
		// add in instructions
		document.getElementById("instructions").innerHTML = " and click on '<strong>add to order</strong>' to add an item to <a href=\"#myOrder\">your order</a> beneath";
		
	
		// hide all products
		var descriptions = getElementsByClass('hideProduct');	
		for(i=0; i<descriptions.length; i++) {
			descriptions[i].style.display = 'none';
		}
		
		// display fruit onload
		document.getElementById("fruitTitle").style.color = "#606060";
		document.getElementById("fruitTitle").style.textDecoration = "none";
		document.getElementById("fruitDescription").style.display = "block";
		
		// add event listeners
		displayProduct("fruitTitle", "fruitDescription");
		displayProduct("breadTitle", "breadDescription");
		displayProduct("dairyTitle", "dairyDescription");
		displayProduct("deliTitle", "deliDescription");
		displayProduct("cupboardTitle", "cupboardDescription");
		displayProduct("frozenTitle", "frozenDescription");
		displayProduct("softdrinksTitle", "softdrinksDescription");
		displayProduct("wineTitle", "wineDescription");
		displayProduct("householdTitle", "householdDescription");
		displayProduct("toiletriesTitle", "toiletriesDescription");
		displayProduct("petTitle", "petDescription");
		displayProduct("teaTitle", "teaDescription");
		displayProduct("preservesTitle", "preservesDescription");
		displayProduct("stampsTitle", "stampsDescription");
		
	}
}

function displayProduct (element, description) {
	if (document.getElementById(element) != null){					
			if (navigator.userAgent.indexOf("MSIE")!=-1	) {
				document.getElementById(element).attachEvent('onclick', function() {showProduct(element, description); })
			} else {
				document.getElementById(element).addEventListener('click', function() {showProduct(element, description); }, false)
			}
	}
}

function showProduct(element, description) {	
	
	var active = getElementsByClass('active');	
	for(i=0; i<active.length; i++) {
		active[i].style.color = "#FC7C29";
		active[i].style.textDecoration = "underline";
	}
	
	var hideProduct = getElementsByClass('hideProduct');	
	for(i=0; i<hideProduct.length; i++) {
		hideProduct[i].style.display = 'none';
	}
	
	document.getElementById(element).style.color = "#606060";
	document.getElementById(element).style.textDecoration = "none";
	document.getElementById(description).style.display = "block";	
}

function addToOrder() {
	var right = getElementsByClass('right');	
	for(i=0; i<right.length; i++) {
		product = right[i]

		if (product.title != ""){
			product.innerHTML = "[<a href=\"#myOrder\">add to order</a>]";
			if (navigator.userAgent.indexOf("MSIE")!=-1) {
				product.attachEvent('onclick', addItem)
			} else {
				product.addEventListener('click', addItem, false)
			}
		}
	}
}


function addItem(e) {	
	currentValue = document.getElementById("order").value;
	if (navigator.userAgent.indexOf("MSIE")!=-1) {
		newItem = "1 x " + e.srcElement.parentNode.title + "\n";
	} else {
		newItem = "1 x " + e.target.parentNode.title + "\n";
	}
	
	endMessage = "\n[Edit your shopping list and amounts here as appropriate]";

	if (currentValue == "Please enter your order...") {
		document.getElementById("order").value = newItem + endMessage;
	} else {
		document.getElementById("order").value = newItem + currentValue;
	}
}

function clearMyOrder() {
 if (document.getElementById("order-onlinePage")){
		document.getElementById("clearOrder").innerHTML = "<a href=\"#myOrder\">Clear my order</a>";
		
		if (navigator.userAgent.indexOf("MSIE")!=-1) {
			document.getElementById("clearOrder").attachEvent('onclick', clearOrder)
		} else {
			document.getElementById("clearOrder").addEventListener('click', clearOrder, false)
		}
	}
}

function clearOrder(e) {
	document.getElementById("order").value = "Please enter your order...";
}

function removeTooltip() {
  var cushyTags;
  cushyTags = getElementsByClass("cushycms-text");
  for(var i = 0; i < cushyTags.length; i++)
    cushyTags[i].title="";
	
  cushyTags = getElementsByClass("cushycms-html");
  for(var i = 0; i < cushyTags.length; i++)
    cushyTags[i].title="";
}

//Google maps
function initialize() {
	if (document.getElementById("about-usfind-usPage")){
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("mapCanvas"));
			map.setCenter(new GLatLng(51.613981136396, -0.596056580544), 15);
			map.setUIToDefault();
			var point = new GLatLng(51.613981136396, -0.596056580544);
			var marker = new GMarker(point);
			map.addOverlay(marker);

			
		}
	}
}

function toggleTransport() {

        if ((document.getElementById("time") != null) && (document.getElementById("or") != null)){	        
                document.getElementById("time").style.display = "none";
                document.getElementById("or").style.display = "none";
                
                if (document.getElementById("collection") != null){			
                        e14 = document.getElementById("collection");		
                                        
                                                if (navigator.userAgent.indexOf("MSIE")!=-1) {
                                                        e14.attachEvent('onclick', function() {displayCollection(); })
                                                } else {
                                                        e14.addEventListener('click', function() {displayCollection(); }, false)
        
                                                }
                }
                
                if (document.getElementById("delivery") != null){			
                        e15 = document.getElementById("delivery");		
                                        
                                                if (navigator.userAgent.indexOf("MSIE")!=-1) {
                                                        e15.attachEvent('onclick', function() {displayDelivery(); })
                                                } else {
                                                        e15.addEventListener('click', function() {displayDelivery(); }, false)
        
                                                }
                }
        }
}


function displayCollection() {
        document.getElementById("time").style.display = "block";
        document.getElementById("collectionTime").style.display = "block";
        document.getElementById("deliveryTime").style.display = "none";
}

function displayDelivery() {
        document.getElementById("time").style.display = "block";
        document.getElementById("deliveryTime").style.display = "block";
        document.getElementById("collectionTime").style.display = "none";
}


function init() {
        toggleTransport();
	formTextClear();
	displayProduce();
	addToOrder();
	clearMyOrder();
	initialize();
	removeTooltip();		
}

window.onload=init;
