/* Global Javascript Functions */

/* Nav Menu Hover */
var registeredImages = new Array();
var imgOffDelay = new Array();

function imgReg(imgName,imgSrc) {
	registeredImages[imgName] = new Array( new Image(), new Image());
	registeredImages[imgName][0].src = imgSrc;
	registeredImages[imgName][1].src = imgSrc.replace("_off.","_on.");
}

function imgOn(imgName,selectCheck) {
	window.clearTimeout(imgOffDelay[imgName]);
	window.clearTimeout(imgOffDelay["movietag"]);
	document.images[imgName].src = registeredImages[imgName][1].src;
	/* hide select boxes on IE */
	if(navigator.appVersion.indexOf("MSIE") >= 0 && document.getElementsByTagName("SELECT").length > 0 && selectCheck == true)
		for (var i=0; i<document.getElementsByTagName("SELECT").length; i++)
			document.getElementsByTagName("SELECT")[i].style.visibility = "hidden";
	var galObj = document.getElementById("movietag");
	if (galObj) galObj.style.visibility = "hidden";
}

function imgOff(imgName,selectCheck) {
	imgOffDelay[imgName] = window.setTimeout("document.images['"+imgName+"'].src = registeredImages['"+imgName+"'][0].src",5);
	/* hide select boxes on IE */
	if(navigator.appVersion.indexOf("MSIE") >= 0 && document.getElementsByTagName("SELECT").length > 0 && selectCheck == true)
		for (var i=0; i<document.getElementsByTagName("SELECT").length; i++)
			document.getElementsByTagName("SELECT")[i].style.visibility = "visible";
	var galObj = document.getElementById("movietag");
	if (galObj) imgOffDelay["movietag"] = setTimeout('document.getElementById("movietag").style.visibility = "visible";',10);
}

/* GlobalNav Images */
imgReg("globalnav_vehicles","/mazda/images/nav_global_vehicles_off.gif");
imgReg("globalnav_shopping_tools","/mazda/images/nav_global_shopping_tools_off.gif");
imgReg("globalnav_owners","/mazda/images/nav_global_owners_off.gif");
imgReg("globalnav_mazdaspeed","/mazda/images/nav_global_mazdaspeed_off.gif");
imgReg("globalnav_inside_mazda","/mazda/images/nav_global_inside_mazda_off.gif");
imgReg("globalnav_zoomzoom","/mazda/images/nav_global_zoomzoom_off.gif");

//open window

function popup(url,winName,w,h) {
	newWin = window.open(url,winName,"width=" + w + ",height=" + h + ",menubar=no,scrollbars=yes,resizable=yes,toolbar=no")
}
function popup2(url,winName,w,h) {
	newWin = window.open(url,winName,"width=" + w + ",height=" + h + ",menubar=no,scrollbars=no,resizable=no,toolbar=no")
}
// w=width, h=height, m=menubar, s=scrollable, r=resizeable, t=toolbar
function popup3(url,winName,w,h,m,s,r,t) {
	newWin = window.open(url,winName,"width=" + w + ",height=" + h + ",menubar=" + m + ",scrollbars=" + s + ",resizable=" + r + ",toolbar=" + t + "")
}



/* submit button through link */
function formSubmit(thisForm,name,value) {
	if (name && value) {
		var hiddenField = document.createElement("input");
		hiddenField.setAttribute("type","hidden");
		hiddenField.setAttribute("name",name);
		hiddenField.setAttribute("value",value);
		thisForm.appendChild(hiddenField);
	}
	thisForm.submit();
}

/* formats a number with appropriate commas */
function commaNum(num) {
	var formattedNum = "";
	var stringNum = "" + num;
	var numPart = 0;
	var separator = "";
	for (var i = stringNum.length; i > 0; i=i-3) {
		numPart = ((i-3) < 0) ? 0 : (i-3);
		formattedNum = "" + stringNum.substring(numPart,i) + separator + formattedNum;
		separator = ",";
	}
	return formattedNum;
}

//pop up that takes different parameters
// this will center the window within the browser and bring the window to focus
var legendWin;
function createWin(url,width,height) {

	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	if(legendWin == null || legendWin.closed) {
		var props = "resizable,scrollbars=no,left=" + left + ",top=" + top + ",width=" + width + ",height=" + height;
		legendWin = window.open(url, "legend", props);
		legendWin.focus();
	} else {
		legendWin.close();
		var props = "resizable,scrollbars=no,left=" + left + ",top=" + top + ",width=" + width + ",height=" + height;
		legendWin = window.open(url, "legend", props);
		legendWin.focus();
	}
}


/* incentives popup */
function winincentivesopen() {
	var w = 540, h = 500;

	if (document.all || document.layers) {
	w = screen.availWidth;
	h = screen.availHeight;
	}

	var popW = 540, popH = 500;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	var myWindow;
	myWindow = window.open("displayRegionalIncentivesPopup.action", "regionalIncentives", "width=" + popW + ",height=" + popH + ",top=" + topPos + ",left=" + leftPos + ",resizable=no,status=no,scrollbars=yes");
	myWindow.focus();
}

function cleanWin(url,width,height) {
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	newWin = window.open(url,"popup","left=" + left + ",top=" + top + ",width=" + width + ",height=" + height + ",menubar=no,scrollbars=no,resizable=no,toolbar=no")
}

function safariSniff() {
	var appVer = navigator.appVersion;
	var safariVer = -1;
	if (appVer.indexOf("Safari")>0)
		safariVer = appVer.substr(appVer.indexOf("Safari")+7);
	if (safariVer > 0 && safariVer < 200)
		document.getElementById("globalnav_box").style.width = "523px";
}

function staticRedirect() {
	alert('Maintenance on MazdaUSA.com is complete.  You are being redirected to the MazdaUSA.com home page where all features are accessible.');
	if(window.location.href.indexOf("forward=false") < 0) window.location.href='http://www.mazdausa.com/MusaWeb/displayHomepage.action';
}
