
// shandford 05/21/03 addition:
// added openSurveyWin function
// function appends the opener location to the end of the url parameter and then calls the standard openWin function
// this is done to avoid windows trying to reference each other across different domains - which does not work
//


//global var

var ns = navigator.appName == "Netscape"; 							
// Browser check
var canSwitchLang = true;

function switchLang () {

	if (canSwitchLang) {
		engDelimiter = 'english';
		freDelimiter = 'french';

		newURL = switchLangSearch(engDelimiter, freDelimiter, document.URL);
	
		engDelimiter = "lang=en";
		freDelimeter = "lang=fr";

		newURL = switchLangSearch(engDelimiter, freDelimeter, newURL);
	
		engDelimiter = "_code=en";
		freDelimeter = "_code=fr";

		newURL = switchLangSearch(engDelimiter, freDelimeter, newURL);
	
		if (newURL != 'null') {
			self.location = newURL;
			return;
		}
	} else {
		alert("Sorry, you can not switch languages on this page.");
	}
}
function switchLangSearch (engDelim, freDelim, docURL) {

	engPosition = 0;
	frePosition = 0;

	charStart = 0;
	charEnd = docURL.length + 1;

	engDelimiter = engDelim;
	freDelimiter = freDelim;

	do {

		var test = docURL.substr(charStart, engDelimiter.length);
		if (test == engDelimiter) { engPosition = charStart;}

		charStart++;

	}

	while (charStart < charEnd - engDelimiter.length);

	if (engPosition > 0) {
		docURL = docURL.substr(0,engPosition) + freDelimiter + docURL.substr((engPosition + engDelimiter.length),docURL.length);
	} else {
		charStart = 0;
		do {
			var test = docURL.substr(charStart, freDelimiter.length)
			if (test == freDelimiter) { frePosition = charStart;}
			charStart++;
		}
		while (charStart < charEnd - freDelimiter.length);
		if (frePosition > 0) {
			docURL = docURL.substr(0,frePosition) + engDelimiter + docURL.substr((frePosition + freDelimiter.length),docURL.length);
		}
	}
	
	return docURL;

}

function filterOutUnwantedParams(url) {

	// split off the anchor
	var temp1 = new Array();
	temp1 = url.split('#');

	var urlNoAnchor = temp1[0];

	var pos = urlNoAnchor.indexOf('?');
	if (pos < 0) {
		return urlNoAnchor;
	}
	
	filteredURL = urlNoAnchor.substr(0, pos+1);

	var queryString = urlNoAnchor.substr(pos+1);
	
	temp1 = new Array();
	temp1 = queryString.split('&');
	for (i = 0; i < temp1.length; ++i) {
		var nameValuePair = temp1[i];

		var temp3 = new Array();
		temp3 = nameValuePair.split('=');
		
		var name = temp3[0];

		if (name == "npDel" || name == "cfgVehDel" || name == "locVehDel" || name == "TooManyUPToggle") {
			continue;
		}
		
		if (i > 0) {
			filteredURL += '&';
		}
		
		filteredURL += nameValuePair;
	}
	
	return filteredURL;
}

// Pop-up 
	
	var ie5 = (document.all && document.getElementbyID) ? false : true;
	var isMac = (navigator.appVersion.indexOf("Mac") == -1) ? false : true;
	var win, win2;	

	function openWin(url, popWidth, popHeight, popScroll, popStatus) {
		var winName =  "popup";
		// popStatus is a new parameter; it is optional so that old calls will not cause an error
		var statusBar = popScroll; 
		
		if (popStatus != null) { 
			statusBar  = popStatus;
			// 'fix' for Mac IE's problem when including a status bar in the pop up window
			// - the browser chops off the bottom 18 pixels of the window to accomodate for the
			//   the status bar, and so we add 18 pixels to the height			
			if (!ie5 && !ns && isMac) { popHeight += 18; }
		}

		if(win) {
			if(!win.closed) {
				if(win.name == "popup") winName = "popup2";	
			  	win.close(); 
			}			
			win = null;	
		}
		win=window.open(url, winName, 'width=' + popWidth + ',height=' + popHeight + ',screenX=25,screenY=150,left=25,top=150,scrollbars=' + popScroll + ',resize=no,status=' + statusBar + '');
		
		if (win.opener == null) win.opener = self;
		win.focus();
	}
	
// end of pop-up

// Resizable Pop-up

	function openResizableWin(url, popWidth, popHeight, popScroll, popStatus) {
		var winName =  "popup";
		// popStatus is a new parameter; it is optional so that old calls will not cause an error
		var statusBar = popScroll; 
		
		if (popStatus != null) { 
			statusBar  = popStatus;
			// 'fix' for Mac IE's problem when including a status bar in the pop up window
			// - the browser chops off the bottom 18 pixels of the window to accomodate for the
			//   the status bar, and so we add 18 pixels to the height			
			if (!ie5 && !ns && isMac) { popHeight += 18; }
		}

		if(win) {
			if(!win.closed) {
				if(win.name == "popup") winName = "popup2";	
			  	win.close(); 
			}			
			win = null;	
		}
		win=window.open(url, winName, 'width=' + popWidth + ',height=' + popHeight + ',screenX=25,screenY=150,left=25,top=150,scrollbars=' + popScroll + ',resizable=yes,status=' + statusBar + '');
		
		if (win.opener == null) win.opener = self;
		win.focus();
	}

// end of resizable pop-up


// Pop-up

	function openNamedWin(url, popWidth, popHeight, popScroll, popStatus, popWin) {
		var winName =  "popup";
		// popWin is a new optional parameter; for backwards compatibility popStatus MUST be
		// specified if popWin is to be used - ppoles 12/24/02
		if (popWin != null)
		{
			winName = popWin;
		}
		// popStatus is a new parameter; it is optional so that old calls will not cause an error
		var statusBar = popScroll;

		if (popStatus != null) {
			statusBar  = popStatus;
			// 'fix' for Mac IE's problem when including a status bar in the pop up window
			// - the browser chops off the bottom 18 pixels of the window to accomodate for the
			//   the status bar, and so we add 18 pixels to the height
			if (!ie5 && !ns && isMac) { popHeight += 18; }
		}

//	    if(win) {
//			if(!win.closed) {
//				if(win.name == "popup") winName = "popup2";
//			  	win.close();
//			}
//			win = null;
//		}

		win=window.open(url, winName, 'width=' + popWidth + ',height=' + popHeight + ',screenX=25,screenY=150,left=25,top=150,scrollbars=' + popScroll + ',resize=no,status=' + statusBar + '');

		if (win.opener == null) win.opener = self;
		win.focus();
	}

// end of pop-up

function ParseQueryString(searchStr) {
	var tempStr = window.location.search;
    var startOfString = tempStr.indexOf(searchStr);
    var result = "";
    if (startOfString != -1) {
    	var endOfString = tempStr.indexOf("&",startOfString+searchStr.length+1);
       	if (endOfString != -1) {               
           	result = tempStr.substring(startOfString+searchStr.length+1, endOfString);
    	}
    	else {
    		result = tempStr.substring(startOfString+searchStr.length+1, tempStr.length);
    	}
    }
	return result;
}


// helper Javascript for bookmark layer kn Oct 29/99


var prefix, suffix, layerName, xPos; 								// globals for bookmark layer methods, timer
var bm_showing = false;
var bm_count = 0;

if (ns) {															// constructor for DOM
	prefix = 'document.layers[\"';
	suffix = '\"]';
	left = '.left';
} 
else {
	prefix = 'document.all[\"';
	suffix = '\"].style';
	left = '.pixelLeft';
}


function bm_show (myLayer) {								// bookmark layer methods, which include DOM differences
	eval(prefix + myLayer + suffix + '.visibility = "visible"');
	bm_count = 0;
	bm_showing = true;
	bm_timeOut();
}
function bm_hide (myLayer) {
	eval(prefix + myLayer + suffix + '.visibility = "hidden"');
	bm_showing = false;
}


function bm_timeOut() {											//bookmark timeout close
	if (bm_showing) {
		bm_count++;
		if(bm_count > 10) { 
			bm_hide('bookmarks'); 
		}
		else { setTimeout("bm_timeOut()",3000); }
	}
}

function bm_refresh() {
	bm_count = 0;
}

														// Function and event capture code to workaround resizing
														// problem in Nav4. Trap the resize event and force the page
														// to reload. Not ideal, but better than nothing
function handleResize() {
        return false;
}

if (navigator.appName == "Netscape"){
  	window.captureEvents(Event.RESIZE);
  	window.onresize = handleResize;
	}




//************************************************************************************************************


// images on layers: uses names html elements
function imgRef(myLayer,myImage) { 
	if(document.layers) {
		obj = eval("document.layers['" + myLayer + "'].document.images['" + myImage + "']");
	}
	else if(document.all){
		obj = eval("document.all('" + myImage + "')");
	}
	else if(document.getElementById){
		obj = eval("document.getElementById('" + myImage + "')");
	}
	return obj;
}

var gallPre = "pop_model_";
var haloPre = "pop_model_";
var imgThmPre = "thm";


function imgAssign(){ // assigns the initial sources to the links using the convention outlined above
	for (var i = 1; i <= imgNum; i++){
		imgRef('thumbnails', 'thm' + i) .src = modelPre + gallPre + i + ".jpg"; 
	}
}

var shiftit = 1;													// Tracks the changes in thumbnail "position"

// We pretend to move the images in one direction byswapping them in order 
function imgShift(){
	var totalClix = (shiftit + imgNum)-1;
	if (imgMax > totalClix){			
		for (var i = 1; i <= imgNum; i++){
			imgRef('thumbnails', 'thm' + i) .src = modelPre + gallPre + (i + shiftit) + ".jpg"; 
		}
		shiftit++;
	}	
}


function imgShiftBack(){	// Then we let people move them back the same way
	if (shiftit > 1){	
		shiftit--;
		for (var i = imgNum; i >= 1; i--){
			imgRef('thumbnails', 'thm' + i) .src = modelPre + gallPre + (i+shiftit-1) + ".jpg"; 
		}
	}	
}

function changeHalo(shift){ /*argument 'shift' is the number used in reverse order in the image links -
							They appear to move Right but the H ref stays the same   
							"shiftit" is the modifer again that allows us to load different halo shots with the same h ref */
	imgRef('mainshot', 'halo') .src =  modelPre + haloPre + (shift + shiftit) + "_lrg.jpg";
}
	
	
var firstImg = window.location.search.substring(1);

function firstHalo(){
	if (firstImg >= 1 && firstImg <= imgNum){
		changeHalo(parseInt(firstImg));
	}
	else {
		changeHalo(0);
	}
}


// Refresh rollover script by skubie - Sep. 8/00


var ns2 = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
var ready = false;  //no error until onload had finished


var divModelNum = 4;  			// number of models shown on divisional sub-nav
var nameplateTxt = 'vehtxt'; 	// layer name of nameplates - HTML must terminate with number
var nameplateGrc = 'veh'; 		// layer name of switching photos - HTML must terminate with numbe


var currLang ; 					// language for proper path
var currDivision ;				// current division
var truncDivision ;				// truncated division one of {buic,cadi,chev,chtr,gmc,olds,pont,show}
var currModel = 0; 				// variable to keep track of current position
var absPath = ''; 				// absolute path for images set to blank for local testing


function findPath () {			// Finds current location
	var mylocation = document.location.toString();
	var mystart = mylocation.indexOf(".com/") + 5;
	var myend = mylocation.indexOf(".html");
	var mylength = myend - mystart;
	var mysection = mylocation.substr(mystart, mylength);
	var separateValues = mysection.split("/");
	currLang = separateValues[0];
	currDivision = separateValues[2];
	absPath = '/' + currLang + '/vehicles/' + currDivision + '/';
} 

/*
	Image Pre-loader for model photos on divisional page only
	- images should be named with proper convention
	- format - xxxx_div_modelphoto_y.gif 
		where:	xxxx	= four letter division
				y		= 1,2,3... numbered from top down to match nameplates in subnav
	- no need to pre-load nameplates as they are loaded in hidden layers
*/


function loadImages() {
	if (currDivision == "chevytrucks" ) { 
		truncDivision = "chtr"; 
	}
	else if (currDivision == "gmc" ) { 
		truncDivision = "gmc"; 
	}
	else { 
		truncDivision = currDivision.substr(0,4); 
	}
	for(var i = 1; i < divModelNum + 1; i++) {
		// for thumbnail photos
		eval(truncDivision + '_div_modelphoto_' + i + '= new Image()');   
		eval(truncDivision + '_div_modelphoto_' + i + '.src = \"' + absPath + 'img/' + truncDivision + '_div_modelphoto_' + i + '.gif\"');
	}
	ready = true;
}


/*	
	Show/hide model nameplates for divisional page
	- each nameplate has own layer
	- change layer names at top, making sure they terminate in x where x = 1,2,3,...etc from top
*/
function showModelName(layerNum) {	
	if (ready) {
		currModel = layerNum;
		if (ns2) { // Netscape dom
			eval('document.' + nameplateTxt + currModel + '.visibility = "hidden"');
    		eval('document.' + nameplateGrc + currModel + '.visibility = "visible"');
    	}
		else {  // IE dom
			eval(nameplateTxt + currModel + '.style.visibility = "hidden"');
    		eval(nameplateGrc + currModel + '.style.visibility = "visible"');
		}
		swapImages('vehicleshots1','photo',currModel); //div name and image name for thumbnail
	}
} 


function hideModel() {
	if (currModel == 0) return;
		if (ns2) { // Netscape dom
			eval('document.' + nameplateTxt + currModel + '.visibility = "visible"');
    		eval('document.' + nameplateGrc + currModel + '.visibility = "hidden"');
    		// return to default image
		}
	else {  // IE dom
		eval(nameplateTxt + currModel + '.style.visibility = "visible"');
    	eval(nameplateGrc + currModel + '.style.visibility = "hidden"');
	}
} 



/*
	Thumbnail photo to represent correct model being shown
	- should be appropriately named 
	- the default image labelled 0
*/
function swapImages(divName, imgName, imgStatus) {
	if (ns2) {	// Netscape DOM with DIV name passed
		document.layers[divName].document.images[imgName].src = eval(truncDivision + '_div_modelphoto_' + imgStatus + '.src');
	}
	else {		// IE DOM with DIV name passed
		document.images[imgName].src = eval(truncDivision + '_div_modelphoto_' + imgStatus + '.src');  
	}
}
function openPrivacy(privacyUrl)  {
       window.open(privacyUrl, 'privacy', 'width=600,height=400,screenX=25,screenY=190,left=25,top=190,scrollbars=yes,resize=no');
}


function accessorizer(domain, pSWCode, pVin, pLang, pMake, pOrigin, pDataKeys, pDomain) {
        if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.userAgent.indexOf("Mac") != -1)) {
                // Handle IE Mac differently because it doesn't support VBScript
                window.open(domain+"/media/gmaccessorizer/en/accessories/accessories/sw.html?pVin="+pVin+"&pOrigin="+pOrigin+"&pLang="+pLang+"&pMake="+pMake+"&pDataKeys="+pDataKeys+"&pSWCode="+pSWCode+"&pDomain="+pDomain,"","status=0,width=720,height=560");
        } else {
                window.open(domain+"/media/gmaccessorizer/en/accessories/accessories/sw.html?pVin="+pVin+"&pOrigin="+pOrigin+"&pLang="+pLang+"&pMake="+pMake+"&pDataKeys="+pDataKeys+"&pSWCode="+pSWCode+"&pDomain="+pDomain,"","status=0,width=720,height=560");
        }

}


// Survey pop-up
// see comments at top of file

	function openSurveyWin(url, popWidth, popHeight, popScroll, popStatus) {
		
		var isQuery; // does the present url already contain a query string
		var referrer;  // holds the location of the opener

		isQuery = url.indexOf("?");
		
		if(isQuery != -1){
			referrer = "&ref=" + window.document.location;
		} else {
			referrer = "?ref=" + window.document.location;			
		}
		
		url += referrer;
		
		openWin(url, popWidth, popHeight, popScroll, popStatus);
	
	}

// end of Survey pop-up

	function rewriteLayer (idOrPath, html) {
	    var p = idOrPath.indexOf('.');
	    var id = p != -1 ?
		      idOrPath.substring(idOrPath.lastIndexOf('.') + 1)
		      : idOrPath;
	    if (document.all)
	      document.all[id].innerHTML = html;
	    else {
	      var l = document.getElementById(id);
	      var r = document.createRange();
	      r.setStartAfter(l);
	      var docFrag = r.createContextualFragment(html);
	      while (l.hasChildNodes())
		l.removeChild(l.firstChild);
	      l.appendChild(docFrag);
	    }
	}

	function makeArray() {
		for (i = 0; i<makeArray.arguments.length; i++)
		this[i + 1] = makeArray.arguments[i];
	}

	var months = new makeArray('January','February','March','April','May',
	'June','July','August','September','October','November','December');
	var date = new Date();
	var day = date.getDate();
	var month = date.getMonth() + 1;
	var yy = date.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	
	function showDate() {
		return months[month] + " " + day + ", " + year;
	}

