/**
 * Main Configurator JavaScript
 */

/*
from lexus.com
*/

/******************************************************************
 * Browser/OS Sniffer
 *****************************************************************/


// Read in HTTP_USER_AGENT from browser
var agt = navigator.userAgent.toLowerCase();

// Assign OS Variables
var is_mac = (agt.indexOf("mac") != -1);
var is_win = (agt.indexOf("win") != -1);

// Sniff for the two major browsers
var ie  = (document.all)? true:false
var ns6 = ((document.getElementById)&&(!ie))? true:false

// Sniff for Mac, used for IE Mac browsers
var mac = (navigator.userAgent.toUpperCase().indexOf("MAC") > -1)? true:false

// Sniff for AOL, used in certain cases where AOL won't pop open a window the size we want it
var aol = (navigator.userAgent.toUpperCase().indexOf("AOL") > -1)? true:false

var undef;
var bgXcoord = 0;
var HideText = "";











//**************************
//** Misc Functions START **
//**************************


//****************************************************
//** Popup function Fixes IE making popup windows   **
//** 20px higher than specified when menubar is on  **
//** Parameters can be passed in as necessary 	    **
//** returns a reference to the	window if needed    **
//****************************************************

function popup(popupurl,winName,w,h,params) {
   // Build parameter list manually.  This lets us override individual defaults as needed.

	var paramList = "";
	if (params == undef) {
		params = "";
	}
	
	params = params.toLowerCase();
	
	if (params.indexOf("menubar=no") != -1) {
		paramList = paramList + ",menubar=no";
	} else {
	    // default
		paramList = paramList + ",menubar=yes";
	    // IE adds to the height with a menubar; subtract to keep height the same.
		if (ie && !mac) {
			h = h - 20;
		}
	}
	
	if ((params.indexOf("scrollbars=no") != -1) || (params.indexOf("scrollbar=no") != -1)) {
		paramList = paramList + ",scrollbars=no";
	} else {
	    // default
		paramList = paramList + ",scrollbars=yes";
	    // IE needs extra space to allow for the phantom scrollbar, or we'll get horizontal scrollbars too.
	    // IE 6.x needs 4px more than IE 5.x, but IE 5.x's width needs vary a lot.  So, we'll set all
	    // IE browsers to use the wider IE 6.x size.
		if (ie) {
			w = w + 17;
		} else {
			w = w + 16;
		}
	}
	
	if (params.indexOf("resizable=yes") != -1) {
		paramList = paramList + ",resizable=yes";
	} else {
	    // default
		paramList = paramList + ",resizable=no";
	}
	
	if ((params.indexOf("toolbar=yes") != -1) || (params.indexOf("toolbars=yes") != -1)) {
		paramList = paramList + ",toolbar=yes";
	} else {
	    // default
		paramList = paramList + ",toolbar=no";
	}
	
	if ((params.indexOf("location=yes") != -1) || (params.indexOf("location=yes") != -1)) {
		paramList = paramList + ",location=yes";
	} else {
	    // default
		paramList = paramList + ",location=no";
	}
	
	t = 35;
	l = 130;
	if ((params.indexOf("top=") != -1) || (params.indexOf("left=") != -1)) {
		paramArray = params.split(",");
		for (i=0; i<paramArray.length; i++) {
			if (paramArray[i].indexOf("top=") != -1) {
				pEles = paramArray[i].split("=");
				t = pEles[1];
			}
			if (paramArray[i].indexOf("left=") != -1) {
				pEles = paramArray[i].split("=");
				l = pEles[1];
			}
		}
	}
	// Override window name
	if (winName == "grad") {
	winName = "gradprogram";
	} else if (winName == "gradprog") {
	winName = "gradprogram";
	}
	
	//alert(paramList);
	//window.open(popupurl,winName,"top=" + t + ",left=" + l + ",width=" + w + ",height=" + h + paramList);
	var theParams = "top=" + t + ",left=" + l + ",width=" + w + ",height=" + h + paramList;
	openTargetWindow(popupurl,winName,theParams);

}


//************************
//** Misc Functions END **
//************************



// end from lexus








/* Utility Functions */

function arrayContains( anArray, anObject ) {
	if ( typeof(anArray) == "undefined" ) return false;
	for ( var i=0; i++; i<anArray.length) {
		if ( anArray[i] == anObject ) return true;
	}
	return false;
}

function currencyFormat( num ) {
	if (isNaN(num)) num = "0";
	var sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	var cents = num%100;
	num = Math.floor(num/100).toString();
	if (cents<10)
	cents = "0" + cents;
	if (cents<1)
	cents = "00";

	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+',' + num.substring(num.length-(4*i+3));
	}

	return (((sign)?'':'-') + '$' + num) + "." + cents;
}

function toUpperAndLower(someString) {
	if (someString.substring(0,1) >="0" && someString.substring(0,1) <= "9")
		return someString.substring(0,2).toUpperCase() + someString.substring(2).toLowerCase();
	else
		return someString.substring(0,1).toUpperCase() + someString.substring(1).toLowerCase();
}

function setHtml( pageItem, value ) {
	if ( ( typeof(pageItem) == "undefined" ) ||
		( pageItem == null ) ||
		( typeof(pageItem.innerHTML) == "undefined" ) )
	{
		return;
	}
	if ( value == null ) return;
	pageItem.innerHTML = value;
}

function setImgSrc( image, src ) {
	if ( typeof(image) == "undefined" ) return;
	if ( ( image == null ) || ( src == null ) ) return;
	image.src = src;
}

function setStyleClass( pageItem, className ) {
	if ( ( typeof(pageItem) == "undefined" ) || ( typeof(className) == "undefined" ) ) return;
	pageItem.className = className; 
}

/* Window Handling Functions */

function openSpecWindow(URL) {
	var day = new Date();
	var windowName = "" + day.getTime();
	window.open(URL, windowName, "toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=575,height=500");
}

function openFeatures(featuresLink) {
        popup(featuresLink,"standardFeatures",625,470,"scrollbars=yes,resizable=yes,menubar=yes");

}



function openGlossaryWindow(glossaryLink) {
        popup(glossaryLink,"glossaryWin",525,470,"scrollbars=yes,resizable=yes,menubar=yes");
}


function openNoteWindow(noteLink) {
        popup(noteLink,"note",525,470,"scrollbars=yes,resizable=yes,menubar=yes");
}

function openApplyForCreditWindow(applyForCreditLink) {
        openTargetWindow(
                applyForCreditLink,
                "tfsca",
                "height=572,width=780,scrollbars=yes,resizable=yes,menubar=yes"
        );
}
/* GM MODIDY
function openEstimatorWindow(estimatorLink) {
        openTargetWindow(
                estimatorLink,
                "PaymentEstimator",
		"height=588,width=781,toolbar=yes,menubar=yes,status=yes,scrollbars=yes,resizable=yes"
        );
}
*/
function openEstimatorWindow(estimatorLink) {
        openTargetWindow(
                estimatorLink,
                "PaymentEstimator",
                "height=572,width=780,scrollbars=yes,resizable=yes,menubar=yes"
        );
}
function openDealerMapWindow(dealerMapLink) {
        openTargetWindow(
                dealerMapLink,
                "dealerMap",
                "height=500,width=542,toolbar=no,resizable=yes,menubar=yes,scrollbars=yes"
        );
}

function openQuoteWindow(quoteLink) {
        openTargetWindow(
                quoteLink,
                "quoteRequest",
                "height=768,width=1024,scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes"
        );
}

function openEmailWindow(emailLink) {
	openTargetWindow(
		emailLink,
		"emailAFriend",
		"toolbar=no,scrollbars=yes,status=no,menubar=yes,resizable=yes,width=542,height=450"		
	);
}

function openPromoWindow(promoLink) {
	openTargetWindow(
		promoLink,
		"promo",
		"width=525,height=465,scrollbars=yes,resizable=yes,menubar=yes,toolbar=no,location=no"
	);
}

function openTargetWindow(linkObject, windowName, windowProperties) {
	if ( linkObject != null ) {
		linkObject.target = windowName;
	}
	var targetWindow = window.open(linkObject, windowName, windowProperties);
	if ( targetWindow != null ) {
		targetWindow.focus();
	}
}

var popupWindowNames =
	[ "standardFeatures", "glossaryWin", "note", "tfsca", "PaymentEstimator", "dealerMap",
	  "quoteRequest", "emailAFriend", "promo"
	];

function openTimeOutWindow(timeOutUrl, startPageUrl) {
	var windowName = window.name;
	var popup = false;
	if ( ( typeof(windowName) != "undefined" ) && ( windowName != null ) ) {
		for (var i=0; i < popupWindowNames.length; i++) {
			if ( popupWindowNames[i] == windowName ) {
				popup = true;
				break;
			}
		}
	}
	if ( popup ) {
		if ( ( typeof(window.opener) != "undefined" ) && ( window.opener != null ) ) {
			window.opener.location = startPageUrl;
		}
		else {
			mainWindow = window.open(startPageUrl, "_blank");
		}
		window.close();
	}
	else {
		var timeoutWindow = openMessageWindow( timeOutUrl, 525, 490 );
		if ( timeoutWindow != null ) timeoutWindow.focus();
	}
}

var messageWindow = null;

function openMessageWindow(theURL, width, height) {
	if ( ( messageWindow != null ) &&
	     ( typeof(messageWindow.closed) != "undefined" ) &&
	     ( ! messageWindow.closed ) )
	{
		messageWindow.close();
	}
	messageWindow = popup(theURL,"configuratorMessage",width,height,"toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,top=100,left=100");

	return messageWindow;		
}

/* Vehicle Info Box Functions */

function Vehicle( basePrice, deliveryFee, packagePrice, accessoriesPrice ) {

	function totalPrice() {
		if ( this.basePrice == null ) return 0;
		return this.basePrice + this.deliveryFee + this.packagePrice + this.accessoriesPrice;
	}

	function initDisplay() {
		this.display = new VehicleDisplay( this );
		this.display.update();
	}

	function updateVehicle( updateObject ) {
		for ( var x in updateObject ) {
			if ( typeof(this[x]) != "undefined" ) this[x] = updateObject[x];
		}
		if ( ( typeof(this.display) != "undefined" ) && ( this.display != null ) ) {
			this.display.update();
		}
	}

	this.basePrice = basePrice;
	this.deliveryFee = deliveryFee;
	this.packagePrice = packagePrice;
	this.accessoriesPrice = accessoriesPrice;

	this.modelImageSrc = null;
	this.packageName = null;

	this.display = null;

	this.totalPrice = totalPrice;
	this.initDisplay = initDisplay;
	this.update = updateVehicle;

	return this;
}

function VehicleDisplay( vehicleObject ) {

	function updateDisplay() {
		setHtml( this.basePrice, currencyFormat( this.vehicle.basePrice ) );
		setHtml( this.deliveryFee, currencyFormat( this.vehicle.deliveryFee ) );
		setHtml( this.packagePrice, currencyFormat( this.vehicle.packagePrice ) );
		setHtml( this.accessoriesPrice, currencyFormat( this.vehicle.accessoriesPrice ) );
		setHtml( this.totalPrice, currencyFormat( this.vehicle.totalPrice() ) );
		setImgSrc( this.vehicleImage, this.vehicle.modelImageSrc );
		setHtml( this.packageName, this.vehicle.packageName );
	}

	this.vehicle = vehicleObject;
	this.basePrice = document.getElementById("vehicleBasePrice");
	this.deliveryFee = document.getElementById("vehicleDeliveryFee");
	this.packagePrice = document.getElementById("vehiclePackagePrice");
	this.accessoriesPrice = document.getElementById("vehicleAccessoriesPrice");
	this.totalPrice = document.getElementById("vehicleTotalPrice");
	this.update = updateDisplay;

	this.packageName = document.getElementById("packageName");
	this.vehicleImage = document.images["vehicleImage"];

	return this;
}

function vehicleDisplayInitialized() {
	if ( typeof(vehicle) != "undefined" && vehicle == null &&
	     typeof(vehicle.display) != "undefined" || vehicle.display != null )
	{
		return true;
	}
	return false;
}

function initVehicleDisplay() {
	if ( typeof(vehicle) != "undefined" ) {
		vehicle.initDisplay();
	}
}


