var personalization;
var webServiceBaseURL = "/ws/personalization/default.asp";
var profile;
var language = "en";
var doUpdateProfile;
var vehicle;
var inftrim;
var trim;
var configID;
var tmpl;
var enhancedPage = false;

function initPage() {
	doUpdateProfile = false;
	personalization = new XMLHandlerCollection();
}

function drawPage(){
	tmpl = new HTMLTemplate();
}

function doPage(){
	initPage();
	if( enhancedPage ){
		initThisPage();
	}
	/******************************************************
	* On Infiniti sites "trim" is defined as a function   *
	* in global.js and this messes us up. So we test for  *
	* this and use another variable, inftrim, instead     *
	******************************************************/
	var mytrim;
	if( inftrim ){
		mytrim = inftrim;
	}else if( typeof( trim ) != "function"){
		mytrim = trim;
	}
	profile = new ProfileXMLHandler( doUpdateProfile, vehicle, mytrim, configID );
	personalization.language = language;
	personalization.addHandler( profile )
	personalization.loadDoc( webServiceBaseURL );
	drawPage();
	if( enhancedPage ){
		drawThisPage();
	}
}

// This is the XMLHandler implementation for the
// special offers data
function SpecialOffersXMLHandler(){
	that = new XMLHandler( 'specialOffers' );
	that.offerID = -1;
	that.loadResponse = function( doc ){
		var soElem = doc.getElementsByTagName('context')[0].getElementsByTagName('specialoffers')[0];
		this.offerType = soElem.attributes.getNamedItem("type").value;
		this.offerID = soElem.getElementsByTagName('offerid')[0].childNodes[0].nodeValue
		if( this.offerType == "regional" ){
			this.province = soElem.getElementsByTagName('province')[0].childNodes[0].nodeValue
			this.modelCode = soElem.getElementsByTagName('modelcode')[0].childNodes[0].nodeValue
			this.modelYear = soElem.getElementsByTagName('modelyear')[0].childNodes[0].nodeValue
			this.seriesName = soElem.getElementsByTagName('seriesname')[0].childNodes[0].nodeValue
			this.modelName = soElem.getElementsByTagName('modelname')[0].childNodes[0].nodeValue
			if( soElem.getElementsByTagName('showlease')[0].childNodes.length > 0 ){
				this.showLease = 'Y';
			}else{
				this.showLease = '';
			}
			if( soElem.getElementsByTagName('showfinance')[0].childNodes.length > 0 ){
				this.showFinance = 'Y';
			}else{
				this.showFinance = '';
			}
			if( soElem.getElementsByTagName('showmsrp')[0].childNodes.length > 0 ){
				this.showMSRP = 'Y';
			}else{
				this.showMSRP = '';
			}
		}else{
			this.description = soElem.getElementsByTagName('description')[0].childNodes[0].nodeValue
		}
		this.primaryOffer = soElem.getElementsByTagName('primaryoffer')[0].childNodes[0].nodeValue
	};
	return that;
}

// This is the XMLHandler implementation for the
// events data
function EventsXMLHandler(){
	that = new XMLHandler( 'events' );
	that.loadResponse = function( doc ){
		var evElem = doc.getElementsByTagName('context')[0].getElementsByTagName('events')[0];
		var evHTMLNode = evElem.getElementsByTagName('a')[0];
		if( evHTMLNode ){
			this.eventHTML = printNode( evHTMLNode, true );
		}
	};
	return that;
}

// This is the XMLHandler implementation for the
// garage data
function GarageXMLHandler( dotrigger ){
	that = new XMLHandler( 'garage' );
	that.trigger += ( dotrigger ? 'updateGarage=1&' : '' );
	that.loadResponse = function( doc ){
		// this is used only to trigger an update of Garage data
		// the only implementations that subscribe to the XML are Flash
	};
	return that;
}

// This allows you to recursively extract a node containing HTML nodes to string variable
// It only sees elements, text nodes and attributes
// Use printMe = false if the top level is not HTML but contains HTML
// This is supposed to replace the IE xml property of the node object in a cross-brwoserly fashion
function printNode( htmlNode, printMe ){
	var html = "";
	if( printMe ){
		html += "<" + htmlNode.nodeName;
		var attribs = htmlNode.attributes;
		for( var i = 0; i < attribs.length; i++ ){
			html += " " + attribs[i].nodeName + "='" + attribs[i].nodeValue + "'";
		}
		html += ">";
	}
	var children = htmlNode.childNodes;
	for( var i = 0; i < children.length; i++ ){
		if( children[i].nodeType == 1 ){
			html += printNode( children[i], true );
		}else{
			if( children[i].nodeType == 3 ){
				html += children[i].nodeValue;
			}
		}
	}
	if( printMe ){
		html += "</" + htmlNode.nodeName + ">";
	}
	return html;
}

// This sets a menu (select object) by selecting the option
// with a given value
function setMenu(theMenu, value, override){
	var theOptions = theMenu.options;
	var currentlySelected = theMenu.selectedIndex;
	if( value && (override || !theOptions[currentlySelected].value || theOptions[currentlySelected].value == 0) ){
		for( var i=0; i < theOptions.length; i++ ){
			if( theOptions[i].value == value ){
				theOptions[i].selected = true;
				break;
			}
		}
	}
}

// This is the XMLHandler implementation for the
// application (tools) data
function ApplicationXMLHandler( appID ){
	that = new XMLHandler( 'applications' );
	that.trigger += ( appID ? 'appID=' + appID + '&' : '' );	
	//alert( that.trigger );
	that.loadResponse = function( doc ){
	};
	return that;
}

// This is the XMLHandler implementation for the
// profile data which includes location and dealer information
function ProfileXMLHandler( dotrigger, vehicle, trim, configID ){
	that = new XMLHandler( 'profile' );
	that.trigger += ( dotrigger ? 'updateProfile=1&' : '' );
	that.trigger += ( vehicle ? 'vehicle=' + vehicle + '&' : '' );
	that.trigger += ( trim ? 'trim=' + trim + '&' : '' );
	that.trigger += ( configID ? 'configID=' + configID + '&' : '' );	
	that.loadResponse = function( doc ){
		//alert( "loading profile" );
		var profileElem = doc.getElementsByTagName('context')[0].getElementsByTagName('profile')[0];
		//this.salutation = profileElem.getElementsByTagName('firstname')[0].childNodes[0].nodeValue
		if( profileElem.getElementsByTagName('location')[0]
			&& profileElem.getElementsByTagName('location')[0].getElementsByTagName('provincecode')[0]
			&& profileElem.getElementsByTagName('location')[0].getElementsByTagName('provincecode')[0].childNodes[0] ){
			this.province = profileElem.getElementsByTagName('location')[0].getElementsByTagName('provincecode')[0].childNodes[0].nodeValue;
		}
	};
	this.addTrim = function( trimCode ) {
		that.trigger += ( trimCode ? 'trimCode=' + trimCode + '&' : '' );
	};	
	this.addCfg = function( cfgID ) {
		that.trigger += ( cfgID ? 'cfgID=' + cfgID + '&' : '' );
	};	
	return that;
}


