/*
	(c)Copyright 2005 Ford Motor Company. All rights reserved.
*/

// eScape.js

document.write("<script type='text/javascript' src='/fordconnection/js/domain.js'> <\/script>");

// Global Variable to hold an array of image urls.
var arImagePaths;

// Global Variable to hold an array of image codes.  This is in sycnh with arImagePaths, and is used as a lookup for it.
var arImageCodes;

// cross-browser function to return an element
// supports IE5.0+, Netscape 6.0+ and Mozilla
function getElementRef(id) {
    if (document.all) {
        return document.all[id];
    }
    if (document.getElementById) {
        return document.getElementById(id);
    }
    return null;
}

// cross-browser function to return window width and height as an array
function getWindowSize() {
    var width, height;
    var test1 = document.body.scrollHeight;
    var test2 = document.body.offsetHeight;
    
    if(test1==0 && test2==0) {
    	width="100%";
    	height="100%";
    }
    else if (test1 > test2) {
        // all but Explorer Mac
        width = document.body.scrollWidth;
        height = document.body.scrollHeight;
    } else {
        // Explorer Mac;
        // would also work in Explorer 6 Strict, Mozilla and Safari
        width = document.body.offsetWidth;
        height = document.body.offsetHeight;
    }
    return [width, height];
}

// Universal test for undefined variables.
function isUndefined(value)
{
  var undef;
  return value == undef;
}

// Universal function to obtain the scroll position of the
// horizontal scrollbar.
function getScrollLeft() {
	if(window.opera) {
	    return window.pageXOffset;
	}
	else {
	    return document.body.scrollLeft;
	}
}

// Load images into the arImages global variable.
// Used by colour to hold the URLs of images
// of vehicles in numerous different colours.
//
// Usage:
//		setImages(new Array("a.jpg", "b.jpg", "c.jpg"));
//
function setImagePaths(arr) {
	arImagePaths = arr;
}

function setImageCodes(arr) {
	arImageCodes = arr;
}

function submitForm()
{
	showHourglassLayer();
}

// Used by bodyStyle.jsp and the colourSelected(...) function.
// Draws a box around a table cell.
// Parameters:
//		el		-	Table Element (TD tag)
//		state	-	Set to '0' to draw border around table cell.
function itemSelected(el,state)
{
	var hover = "_hover";
	var className = el.className;
	
	if (el.style.clickState!='set'){
		if (state==0 && className.substr(className.length-hover.length)!=hover) {
			el.className=el.className+hover;
		}
		else if(state==1 && className.substr(className.length-hover.length)==hover) {
			el.className=el.className.substr(0,className.length-hover.length);
		}
	}
}

// Draws a box around a colour chip.
// Also changes the colour of the vehicle image found in vehicleInfo.jsp.
// Parameters:
//		el			-	Table Element (TD tag)
//		state		-	Set to '0' to draw border around table cell.
//		lm			-	Indicates colour chip being 'hovered' over.
//						Index Number of colourchip on page, starting at 0.
//		selColour	-	The currently selected colourn (starting at 0).
function colourSelected(el, state, colourCode)
{
	itemSelected(el, state);
	var image = getElementRef("summaryImage");

	// if the image arrays are not present, then communicate with a 3D applet
	if (isUndefined(image) || isUndefined(arImagePaths) || isUndefined(arImageCodes) || arImagePaths.length < 1 || arImageCodes.length < 1) {

		// We need the vehicle configuration string.
	    if(isUndefined(currentconfig)) {
		    return false;
	    }

	    var colour_token    = "&Colour=";
	    var separator_token = "&";
	    
	    // CV 3Ds use lower case tokens
	    if(currentconfig.indexOf(colour_token) == -1) {
	        colour_token = "&colour=";
	    }
	    
	    var start = currentconfig.indexOf(colour_token) + colour_token.length;
	    var end   = currentconfig.indexOf(separator_token, start);
	    var newconfig = currentconfig.substring(0,start) + colourCode + currentconfig.substring(end);

	    currentconfig = newconfig;
	    
	    parent.images.setConfig(currentconfig);
	}
	else if (el.style.clickState != 'set') {
	    index = getIndex(arImageCodes, colourCode);
	    // if the colour code is not found, then return false
	    if (index >= 0) {
		    image.src = arImagePaths[index];
		} else {
		    return false;
		}
	}
	return true;
}

// CR2300 "Add interior images to eSCAPE" (cmorga10 04/03/2005)
// CR2301 "Move interior images to Trim Tab" (cmorga10 03/06/2005)
// Draws a box around a trim swatch.
// Also changes the image of the vehicle interior, and the larger
// image and text associated with the selected trim swatch
// (these are to be found in trim.jsp).
// Parameters:
//		el			-	Table Element (TD tag)
//		state		-	Set to '0' to draw border around table cell.
//		trimCode	-	The currently selected trim (starting at 0).
//		swatchPath	-	The path of the large swatch image to be displayed.
//		swatchText	-	The text to be displayed with the large swatch image.
function trimSelected(el, state, trimCode, swatchPath, swatchText)
{
	itemSelected(el, state);

	// return false if the arrays are not valid
	if (isUndefined(arImagePaths) || isUndefined(arImageCodes) || arImagePaths.length < 1 || arImageCodes.length < 1) {
	    return false;
	}

	if (el.style.clickState != 'set') {
	    index = getIndex(arImageCodes, trimCode);
	    // if the trim code is not found, then return false
	    if (index >= 0) {
	    	var image = getElementRef("summaryTrimImage");
		    image.src = arImagePaths[index];
		    
		    var summarySwatchImage = getElementRef("summarySwatchImage");
		    summarySwatchImage.src = swatchPath;
		    
		    var summarySwatchLabel = getElementRef("summarySwatchLabel");
		    summarySwatchLabel.innerHTML = swatchText;
		   
		} else {
		    return false;
		}
	}
	return true;
}

// function to return the index of a value within an array
function getIndex(arr, value) {
    // if the values are not valid, return -1
    if (isUndefined(arr) || isUndefined(value) || arr == null || value == null) {
        return -1;
    }
    // search through the array to find the value
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == value) {
            return i;
        }
    }
    // if the value can't be found, return -1
    return -1;
}

// Used by the Vehicle Summary screen to print the
// current page.
// This will actually bring up the Print dialog box.
function printWindow()
{
	if (window.print) {
		window.top.focus();	// Required by IE	
	    window.top.print(); // top.print required by Netscape
	}
}

function printIFrame() {

	if(window.print) {
		var iframe = window.top.frames['configIframe'];

		if(!isUndefined(iframe)) {
			//window.top.frames['configIframe'].print(); WORKS IN NETSCAPE!!!
			iframe.focus();
			iframe.print(); 
		} else {
			printWindow();
		}
	}
}

/**
 * Opens a popup window for the vehicle specs page. Requires a var called vehicleSpecsURL to be set. Scrollbars will display if argumaent is not passed
 */
function openVehicleSpecsWindow(window_name, width, height, scrollbars)
{
	if (isUndefined(scrollbars)) {
	    scrollbars = 'yes';
	}
	if (isUndefined(vehicleSpecsURL) || vehicleSpecsURL == null || vehicleSpecsURL == "") {
	    return false;
	}
	var oWin = window.open(vehicleSpecsURL,window_name,'width=' + width + ',height='+ height + ',scrollbars=' + scrollbars + ',resizable=yes,menubar=no,statusbar=yes,toolbar=no');
	oWin.focus();
	return true;
}

/**
 * Opens a popup window for the current promotions page. Requires a var called promotionsURL to be set. Scrollbars will display if argumaent is not passed
 */
function openPromotionsWindow(window_name, width, height, scrollbars)
{
	if (isUndefined(scrollbars)) {
	    scrollbars = 'yes';
	}
	if (isUndefined(promotionsURL) || promotionsURL == null || promotionsURL == "") {
		document.location = "promotions.do";
	    return false;
	}
	var oWin = window.open(promotionsURL,window_name,'width=' + width + ',height='+ height + ',scrollbars=' + scrollbars + ',resizable=yes,menubar=no,statusbar=yes,toolbar=no');
	oWin.focus();
	return true;
}

/**
 * Opens a popup window for an external URL. Scrollbars will display if argumaent is not passed
 */
function openExternalWindow(url, window_name, width, height, scrollbars)
{
	if (isUndefined(scrollbars)) {
	    scrollbars = 'yes';
	}
	var oWin = window.open(url,window_name,'width=' + width + ',height='+ height + ',scrollbars=' + scrollbars + ',resizable=yes,menubar=no,statusbar=yes,toolbar=no');
	oWin.focus();
}

/**
 * Opens a popup window for an internal page. Scrollbars will display if argumaent is not passed
 */
function openInternalWindow(popup, window_name, width, height, scrollbars)
{
	if (isUndefined(scrollbars)) {
	    scrollbars = 'yes';
	}
	var url='popup.do?popup=' + popup;
	var oWin=window.open(url, window_name, 'directories=0, location=0, menubar=0, status=0, toolbar=0, scrollbars=' + scrollbars + ', resizable=yes, width=' + width + ', height=' + height);
	oWin.focus();
}

/**
 * Opens a popup window for an internal page. Scrollbars will display if argumaent is not passed
 */
function openValueMyCarWindow(url, width, height, scrollbars)
{
	if (isUndefined(scrollbars)) {
	    scrollbars = 'yes';
	}
	
	var oWin = window.open(url,'glass','width=' + width + ',height='+ height + ',scrollbars=' + scrollbars + ',resizable=yes,menubar=no,statusbar=yes,toolbar=no');
	oWin.focus();
}

/**
 * Popup Windows need some special processing when using STRUTS.
 * Example Use:
 *
 *	<html:form name="popupForm" action="/popup">
 *		<html:hidden name="reqCode" value="displayInformation" />
 *		<input type="submit" value="Open Window" onclick="this.form.reqCode.value='displayPopup';return openpopup(this.form, '', '280', '150', event);">
 *	</html:form>
 *
 *
 * Parameters:
 *	form		-		The form to submit back to STRUTS server
 *	width		-		width of popup window
 *	height		-		height of popup window
 */
function sendFormToPopup(form, width, height)
{
	window.open('about:blank', 'popup', 'directories=0, location=0, menubar=0, status=0, toolbar=0, scrollbars=yes, resizable=yes, width=' + width + ', height=' + height);
	var target = form.target;
	form.target='popup';
	form.submit();
	form.target = target;

	return false;
}

/*
 * The following functions are used by the DIV/LAYERS functionality
 * employed in the notables.jsp file.
 */
function hideScrollbar()
{
	document.body.style.overflow="hidden";
}

function showScrollbar()
{
	document.body.style.overflow="";
}

// hide all layers, show the main layer and turn the scrollbars back on.
function restoreMainLayer() {
    var mainLayer   = getElementRef("MainLayer");
    var shaderDiv = getElementRef("shaderDiv");
    var infoLayer = getElementRef("infoLayer");

    if (infoLayer != null) {
        infoLayer.style.visibility = "hidden";
    }
    if (shaderDiv != null) {
        shaderDiv.style.visibility = "hidden";
    }
    if (mainLayer != null) {
        mainLayer.style.visibility = "visible";
    }

    showScrollbar();
}

// show info and shader layers and hide scrollbars
function showInfoLayer() {
    var shaderDiv = getElementRef("shaderDiv");
    var infoLayer = getElementRef("infoLayer");

    if (shaderDiv != null) {
        var dimensions = getWindowSize();
        resizeLayer(shaderDiv, "100%", dimensions[1]);
        shaderDiv.style.visibility = "visible";
    }
    if (infoLayer != null) {
        infoLayer.style.visibility = "visible";
    }
    hideScrollbar();
}

// show the sessionTimeout warning layer and hide scrollbars
function showSessionTimeoutWarningLayer() {

	window.scrollTo(0,0);
    var shaderDiv = getElementRef("shaderDiv");
    var sessionTimeoutWarningLayer = getElementRef("sessionTimeoutWarningLayer");
    var eProfileInnerLayer = getElementRef("eProfileInnerLayer");

    if (shaderDiv != null) {
        var dimensions = getWindowSize();
        resizeLayer(shaderDiv, "100%", dimensions[1]);
        shaderDiv.style.zIndex = 3;
        shaderDiv.style.visibility = "visible";
    }

    if (sessionTimeoutWarningLayer != null) {
        sessionTimeoutWarningLayer.style.visibility = "visible";
    }

	// hide the eprofile div
    if (eProfileInnerLayer != null) {
        eProfileInnerLayer.style.visibility = "hidden";
    }

    hideScrollbar();
}

// hides the sessionTimeout layer and shows scrollbars
function hideSessionTimeoutLayer() {
    var shaderDiv = getElementRef("shaderDiv");
    var infoLayer = getElementRef("infoLayer");
    var sessionTimeoutLayer = getElementRef("sessionTimeoutLayer");

    // sets the shader bar back to the state it was previously in
    if (shaderDiv != null) {
        shaderDiv.style.zIndex = 1;
        shaderDiv.style.visibility = infoLayer == null ? "hidden" : "visible";
    }

    if (sessionTimeoutLayer != null) {
        sessionTimeoutLayer.style.visibility = "hidden";
    }

    // if there wasn't an infoLayer already showing, then show the scrollbars again
    if (infoLayer != null && infoLayer.style.visibility == "hidden") {
        showScrollbar();
    }
}

// resizes the layer to the hieght and width of the total window size (including offscreen area)
function resizeLayer(layer, width, height) {
    if (layer != null) {
        layer.style.width = width;
        layer.style.height = height;
    }
}

function showShaderDiv() {
    getScrollPosition();
	showHourglassLayer();
}

// Redirect the browser to the URL after waiting
// a number of miliseconds.
function redirect(url, milliseconds) {
	window.setTimeout("window.location.replace('" + url + "');", milliseconds);
}

// Function which will display some kind of notification to the user
// when a tab, etc, is clicked - this ensures that the user is aware that
// the system is doing something.
// Just like the hourglass in Windows, for instance.
function showHourglassLayer() {
	var shaderDiv = getElementRef("shaderDiv");

    if (shaderDiv != null) {
        var dimensions = getWindowSize();
        resizeLayer(shaderDiv, "100%", dimensions[1]);
//        shaderDiv.style.top=document.body.scrollTop;
        shaderDiv.style.zIndex = 5;
        shaderDiv.style.visibility = "visible";
    }

	document.body.style.cursor = "wait";

    // just in case
    hideScrollbar();
}

// Function to remove spaces from the left-hand side of a string
function ltrim(s)
{
	return s.replace( /^\s*/, "" );
}

// Function to remove spaces from the right-hand side of a string
function rtrim(s)
{
	return s.replace( /\s*$/, "" );
}

// Function to remove spaces from either side of a string.
// Combine the rtrim() and ltrim() functions to make the trim() 
// function, which just wraps both calls together:
function trim(s)
{
	return rtrim(ltrim(s));
}

//Performs a simple javascript test to determine if the user has Java installed.
//The function must be cross-browser compatible.
//
//If Java is installed, the "hasJavaURL" is loaded into the browser.
//If Java is not installed, the "noJavaURL" is loaded into the browser.
function onJava(hasJavaURL, noJavaURL) 
{
	if (navigator.javaEnabled() != 1) {
		document.location=noJavaURL;
	}
	else {
		document.location=hasJavaURL;
	}
	return false;
}

var hourglassHandle;

//Hides the loading message 
function hideLoading() {
	clearTimeout(hourglassHandle);
	if (document.all && document.all.divLoading) document.all.divLoading.style.visibility='hidden';
	if (document.layers && document.divLoading) document.divLoading.visibility='hidden';
	if (document.getElementById && document.getElementById('divLoading')) document.getElementById('divLoading').style.visibility='hidden';
	document.body.style.cursor = "auto";
}

// display a loading message
function showLoading() {
	if (document.all && document.all.divLoading) document.all.divLoading.style.visibility='visible';
	if (document.layers && document.divLoading) document.divLoading.visibility='visible';
	if (document.getElementById && document.getElementById('divLoading')) document.getElementById('divLoading').style.visibility='visible';
}

function move(distance)
{
	var toplink = getElementRef("top_link");
	var bottomlink = getElementRef("bottom_link");
	var el = getElementRef("scrollContent");

	if(isUndefined(el.style.top) || el.style.top.length==0) {
		el.style.top="0px";
	}

	var top=parseInt(el.style.top);
	var height = el.offsetHeight;

	if(top+distance>0-height && top+distance<=0) {
		el.style.top = top + distance + "px";
		toplink.style.visibility = "visible";
		bottomlink.style.visibility = "visible";

		if(-top == distance)
		{
			toplink.style.visibility = "hidden";
		}
	}
	else if(top == 0) {
		toplink.style.visibility = "hidden";
	}
	else {
		bottomlink.style.visibility = "hidden";
	}
}

function setBlankImage(imageObj){
	imageObj.src="/fordconnection/images/single_option_img.jpg";
	imageObj.parentNode.onclick = function(){};
}

