/* 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;
	/* display globalnav dropdowns on IE */
	if(navigator.appVersion.indexOf("MSIE") >= 0
		&& document.getElementById(imgName+"_div")) {
		document.getElementById(imgName+"_div").style.visibility = "visible";
		document.getElementById(imgName+"_div").style.clear = "left";
	}
	/* IE7 subnav position hack (next two if statements) */
	if (navigator.appVersion.indexOf("MSIE")>=  0 && navigator.appVersion.indexOf("7.0")>= 0) {
		document.getElementById("globalnav_vehicles_div").style.position = "fixed";
		document.getElementById("globalnav_vehicles_div").style.top = "50px";
    /* FleetWeb menu is shorter and doesn't need this part of the hack*/
    if (window.location.href.indexOf('/FleetWeb/') == -1){
        document.getElementById("globalnav_vehicles_div").style.left = "232px";
    }
	}
	if (navigator.appVersion.indexOf("MSIE")>=  0 && navigator.appVersion.indexOf("7.0")>= 0) {
		// Check that div exists on Fleet
		if (document.getElementById("globalnav_shopping_tools_div")) {
			document.getElementById("globalnav_shopping_tools_div").style.position = "fixed";
			document.getElementById("globalnav_shopping_tools_div").style.top = "50px";
			document.getElementById("globalnav_shopping_tools_div").style.left = "308px";
		}
	}
	
	/* hide iframes on IE */
	if(navigator.appVersion.indexOf("MSIE") >= 0 && navigator.appVersion.indexOf("7.0") <= 0 && document.getElementsByTagName("IFRAME").length > 0 && selectCheck == true)
		for (var i=0; i<document.getElementsByTagName("IFRAME").length; i++)
			document.getElementsByTagName("IFRAME")[i].style.visibility = "hidden";
	var galObj = document.getElementById("movietag");
	if (galObj) galObj.style.visibility = "hidden";
}

function imgOff(imgName,selectCheck) {
	if(navigator.appVersion.indexOf("MSIE") >= 0)
		document.images[imgName].src = registeredImages[imgName][0].src;
	else imgOffDelay[imgName] = window.setTimeout("document.images['"+imgName+"'].src = registeredImages['"+imgName+"'][0].src",5);
	/* display globalnav dropdowns on IE */
	if(navigator.appVersion.indexOf("MSIE") >= 0 && document.getElementById(imgName+"_div"))
		document.getElementById(imgName+"_div").style.visibility = "hidden";

	/* show iframes on IE */
	if(navigator.appVersion.indexOf("MSIE") >= 0
		&& document.getElementsByTagName("IFRAME").length > 0
		&& selectCheck == true)
		for (var i=0; i<document.getElementsByTagName("IFRAME").length; i++)
			document.getElementsByTagName("IFRAME")[i].style.visibility = "visible";
	var galObj = document.getElementById("movietag");
	if (galObj) imgOffDelay["movietag"] = setTimeout('document.getElementById("movietag").style.visibility = "visible";',10);
}


//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 = 580, h = 600;

	if (document.all || document.layers) {
	w = screen.availWidth;
	h = screen.availHeight;
	}

	var popW = 580, popH = 600;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	var myWindow;
	myWindow = window.open("http://www.mazdaespanol.com/mazdausa/enes/24/_images_mazdausa_com/MusaWeb/javascript/displayRegionalIncentivesPopup.action", "regionalIncentives", "width=" + popW + ",height=" + popH + ",top=" + topPos + ",left=" + leftPos + ",resizable=no,status=no,scrollbars=yes");
	myWindow.focus();
}

function winincentivesopenCX7() {
	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("http://www.mazdaespanol.com/mazdausa/enes/24/_images_mazdausa_com/MusaWeb/javascript/displayRegionalIncentivesCX7Popup.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.mazdaespanol.com/mazdausa/enes/24/_images_mazdausa_com/MusaWeb/displayHomepage.action';
}

function staticRedirectMexico() {
	alert('El mantenimiento en Mazda Mexico está completo. Le están volviendo a dirigir a la página principal de Mazda Mexico donde están accesibles todas las características del sitio.');
	if(window.location.href.indexOf("forward=false") < 0) window.location.href='http://www.mazdaespanol.com/mazdausa/enes/24/_images_mazdausa_com/MazdaMexico/displayMxPage.action?pageParameter=mazdaMexico';
}

/* IE7 ActiveX Workaround */
function drawEmbed(embedType,embed,returnText) {
/* Usage:
var flash = new Array();
flash["src"] = "flash/home/p_home_base.swf";
flash["base"] = "flash/home/"
flash["FlashVars"] = "altContentURL=/MusaWeb/displayPage.action?pageParameter=blah";
flash["quality"] = "high";
flash["bgcolor"] = "#ffffff";
flash["width"] = "750";
flash["height"] = "417";
flash["name"] = "bod";
flash["align"] = "";
flash["scale"] = "noscale";
flash["menu"] = "0";
flash["wmode"] = "transparent";

drawEmbed("flash",homePageFlash);

*/
	var embedObj = new Array();
	embedObj["flash"] = new Array();
	embedObj["flash"]["type"] = "application/x-shockwave-flash";
	embedObj["flash"]["pluginspage"] = "http://www.macromedia.com/go/getflashplayer";

	embedObj["qt"] = new Array();
	embedObj["qt"]["type"] = "video/quicktime";
	embedObj["qt"]["pluginspage"] = "http://www.apple.com/quicktime/download/";

	var embedArray = embed;
	if (embedType.constructor == Array) embedArray.concat(embedType);
	else embedArray.concat(embedObj[embedType]);

	var embedHtml = "<embed ";
	for (var i in embedArray)
		embedHtml += " "+i+"=\""+embedArray[i]+"\"";
	embedHtml += "></embed>";

	if (!returnText) document.write(embedHtml);
	else return embedHtml;
}
/*
function drawObject(objType,obj,params,embed) {
	var obj = new Array();
	obj["flash"] = new Array();
	obj["flash"]["codebase"] = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,65,0";
	obj["flash"]["classid"] = "D27CDB6E-AE6D-11cf-96B8-444553540000";

	obj["qt"] = new Array();
	obj["qt"]["codebase"] = "http://www.apple.com/qtactivex/qtplugin.cab";
	obj["qt"]["classid"] = "02BF25D5-8C17-4B23-BC80-D3488ABDDC6B";

}
*/

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* SCR05240 - LIVE CHAT for flash garages */
function mazdaChat(s_linkName){
	s_linkTrackVars='eVar2';
	s_linkTrackEvents='None';
	s_linkType='o';
	s_linkName='Haz clic en el bot\363n de chat en vivo - ' + s_linkName;
	s_eVar2='Haz clic en el bot\363n de chat en vivo - ' + s_linkName;
	s_lnk=s_co(this);s_gs('mazdausacom');
	s_objectID='Bot\363n de chat en vivo'; 
	createCookie('liveChatUser','liveChatUser',0.4);
	window.open('http://www.mazdaespanol.com/mazdausa/enes/24/_images_mazdausa_com/MusaWeb/displayPage.action?pageParameter=chatUnavailable','_blank','resizable=yes,width=590,height=590');
}

// SCR06713: Add promo banner for loyalty offer - Overlays
function openOverlay(id) {
	var IEoffset = 0;

	if(navigator.appVersion.indexOf("MSIE") >= 0)
		IEoffset = 70;

	var defaultLength = 262;
	var defaultMargin = -105;
	var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
	var objPosY = document.getElementById('overlayPosition').offsetTop;
	var offsetY = objPosY - scrollY + IEoffset;

	if (offsetY < defaultLength)
		document.getElementById('overlay').style.marginTop = defaultMargin + (defaultLength-offsetY) + "px";
	else
		document.getElementById('overlay').style.marginTop = defaultMargin + "px";

	if (id == null) id = "revestimiento";
	document.getElementById('overlay').style.display = "block";
}

function closeOverlay(id) {
	if (id == null) id = "revestimiento";
	document.getElementById(id).style.display = "none";
}

function disclaimerPop(url) {
	var winFeatures = 'width=560,height=300,scrollbars=0';
	open(url,'aviso legal',winFeatures);	
}

function chatDisclaimerPop(url) {
	var winFeatures = 'width=590,height=550,scrollbars=0';
	open(url,'aviso legal',winFeatures);	
}