// JavaScript Document - common JS functions and initialisation for pages in Suzuki.com.au

// page variables
var dropMenuId = "nav_cars";
var externalLinkClass = "external_link"; // className applied to links which are to be opened in a new window


// page initialisation
window.onload = function () { init(); }

// anything in this function will be called when window.onload is dispatched by the browser
function init() {
	
	// IE6 fix for drop-down menus
	if (window.attachEvent){
		sfHover();	
	}
	
	// find all anchor tags with the class "external_link"
	// and configure them to open in a new window
	setupExternalLinks(externalLinkClass);	
	
}



// ------------- functions -----------------

function setupExternalLinks(externalLinkClass) {
	
	// find all  anchor tags  with the className externalLinkClass
	// and add an onclick handler to open their target urls in a new window.
	
	var anchors = document.getElementsByTagName("a");
	var count = anchors.length;
	for (var i=0; i<count; i++) {
		var thisAnchor = anchors[i];
		var className = getElementAttribute(thisAnchor,"class");
		if (className != undefined && className.indexOf(externalLinkClass) > -1) {
			thisAnchor.onclick = function() { return launchExternal(this) } ;
		}
	}
	
}


function launchExternal(linkElement) {
	// interrupts the normal cilck event on an anchor tag to open the link in a new window.
	var target = getElementAttribute(linkElement,"href");
	window.open(target);
	return false;
}

// apply nested X.x style numbering to nested ordered lists
// doesn't seem to be an easy way to do this without javascrip
// used on terms and conditions page, but flexiable for reuse 
// in other places
function formatNumberedList(list,limit,startNum) {
	var children = list.childNodes;
	var count = children.length;
	var currentNum = 0;
	var startNum = startNum ? startNum : 0;
	var limit = limit ? limit : 0 

	for (var i=0; i<count; i++) {
		var thisItem = children[i];
		switch (thisItem.nodeName) {
			case "LI":
				currentNum++;
				var thisNum = (startNum == 0) ? String(currentNum + " ") : String (startNum + "." + currentNum + " ");
				thisItem.innerHTML = thisNum + thisItem.innerHTML;
								
				if (thisItem.getElementsByTagName("li").length > 0 && startNum < limit) {
					formatNumberedList(thisItem,limit,currentNum)
				}
			break;
			
			case "OL":
				formatNumberedList(thisItem,limit,startNum)
			break;
							
		}
	}	
}


// apply support for hover on the menu items in the cars drop-down list for Internet Explorer
function sfHover() {
	if(document.getElementById(dropMenuId))
	{
		var sfEls = document.getElementById(dropMenuId).getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}

function embedWorldLinkFlash(targetDiv,model,swfPath) {
	
	var fileName = "world_link_" + model  + ".swf";
	
	var so = new SWFObject(swfPath + fileName, "world_link_flash", "447", "290", "8", "#222");
	//YUCK
	if(model=='swift'){model += '_s'}
	so.addVariable("model", model);
	so.addParam("wmode", "transparent");
	so.write(targetDiv);
}

function embedColourMovieFlash(targetDiv,model,swfPath) {
	
	var fileName = model + "_colorator.swf";
	
	var so = new SWFObject(swfPath + fileName, "colour_movie_link_flash", "730", "254", "8", "#222");
	so.addVariable("model", model);
	so.addParam("wmode", "transparent");
	so.write(targetDiv);
}


function validatePostcode(form) {
		
	// make sure a valid australian postcode has been entered in the dealer locator
	// return true if the postcode is valid, and not a QLD postcode
	// return false, but offer to redirect if a QLD postcode has been entered
	// return false if invalid data has been entered
			
	var isValid = false
	var goQLDSite = false;
	var value = form.postcode.value;
	var invalidMessage = "Please enter a valid Australian Postcode"
	var qldMessage =  "You have entered a Queensland Postcode.";

	
	if (value.length!=4 || isNaN(value*1)) {
		alert(invalidMessage);
		form.postcode.focus();
	} else if (checkQueenslandPostcode(value)) {
		// if a queensland postcode was entered,
		// offer an option to go to the qld website
		offerExternalQLDSite(qldMessage);
	} else {
		isValid = true;
	}
			
	return isValid
}

function queenslandStateClicked() {
	// when the queensland state is clicked on the dealer locator map
	// offer to take the visitor through to the external site.
	return offerExternalQLDSite();
}

function offerExternalQLDSite(introText) {
	
	var QLDSite = "http://www.suzukiautoco.com"
	var qldMessage ="";

	if (introText != undefined) {
		qldMessage += introText + "  ";
	};
	
	qldMessage += "To find a Suzuki dealer in Queensland, you need to visit www.suzukiautoco.com."
	qldMessage += "  Would you like to go there now?";
	
	if (confirm(qldMessage,"Yes","No")) {
		window.location = QLDSite;
		return true;
	}
	
	return false	
}
		
		
function queenslandStatePriceClicked() {
	// when the queensland state is clicked on the dealer locator map
	// offer to take the visitor through to the external site.
	return offerExternalPriceQLDSite();
}

function offerExternalPriceQLDSite(introText) {
	
	var QLDSite = "http://www.suzukiautoco.com"
	var qldMessage ="";

	if (introText != undefined) {
		qldMessage += introText + "  ";
	};
	
	qldMessage += "For Queensland customers to find out further information, you need to visit www.suzukiautoco.com.au."
	qldMessage += "  Would you like to go there now?";
	
	if (confirm(qldMessage,"Yes","No")) {
		window.location = QLDSite;
		return true;
	}
	
	return false	
}
		
		
function checkQueenslandPostcode(value) {
	// return true if value is a qld postcode
	// return false if value is not a qld postcode
	// if postcode is between 4000->4999 or 9000->9999, it is a queensland postcode.	
	// called from the validatePostcode function
	var valueNum = parseInt(value,10);			
	return ((valueNum >= 4000 && valueNum <= 4999) || (valueNum >= 9000 && valueNum <= 9999));
}

function getElementAttribute(element,attributeName) {
	var value;
	if (element.attributes[attributeName] ) {
		value = element.attributes[attributeName].value;
	} else {
		value =  element.getAttribute(attributeName);
	}	
	return value;
}

function setElementAttribute(element,attributeName,value) {

	if (element.attributes[attributeName] ) {
		element.attributes[attributeName].value = value;
	} else {
		element.setAttribute(attributeName,value);
	}	
	return value;
}
