// $Id: dyc_general.js,v 1.1 2009/05/25 09:17:44 hekj Exp $

// used for the 44o popup
var pagePrefix = '';

// handles all navigation within the DYC pages
// puts the carid and modelcode in the url, if necessary
// parameters:
//   page = page to redirect to
//   carid = a new carid to use, default = carid in url
function gotoPage(page, carid)
{
	if (!carid) carid = getParam('carid');
	var specgroupid = getParam('specgroup');
	var url = getCleanUrl();

	// setup 3rd party redirect if going to a 3rd party page
	if (page.indexOf('http') == 0) {
		page = '3rd_party.xml?carid='+carid+'&url='+page;
		
	// set the carid in the url if not going to entry page
	} else if (page.indexOf('dyc.shtml') < 0) {
		page += '?carid='+carid;
	}

	// if going into step 2, set the specgroup id
	if (page.indexOf('dyc_step2') >= 0) {
		// get the proper specgroup id if within step2
		if (url.indexOf('dyc_step2') >= 0) {
			var form = getForm('specgroup');
			if (form && form.specgroup) {
				specgroupid = form.specgroup.options[form.specgroup.selectedIndex].value;
			}
		}

		if (!specgroupid) specgroupid = 1;
		page += '&specgroup='+specgroupid;
	}
	
	// save the car state if within step 1 or 2
	if (url.indexOf('dyc_step') >= 0) {
		//saveState(carid);
	}
	document.location = page;
} // getoPage(page)


// initializes the left nav drop downs
// required js object:
//	 _models = multi dimensional array [caption][value][car_name_array]
// required js libraries:
//	 select.js, layer.js, functions.js
// required in html:
// 	 a form called quicknav
//   a selection box called model
//   a selection box called car 
// parameters:
//   action = init | update | submit, default = init
//var _models;
function dycQuickNav(action)
{
	if (!_models) return;
	var quicknav = getForm('quicknav');
	if (!quicknav || !quicknav.model || !quicknav.car) return;
	var carid = getParam('carid');
	
	if (!action || action == 'init') {
		// init
		// find the current model index and car index for this carid

		var model = 0;
		var car = 0;

		if (carid) {
			for(var i = 0; i < _models.length; i++) {
				for(var j = 0; j < _models[i][2].length; j++) {
					if (_models[i][2][j][1] == carid) {
						model = i;
						car = j;
						break;
					}
				}
			}
		}

		changeOptions(quicknav.model, _models, model);
		changeOptions(quicknav.car, _models[model][2], car);

	} else if (action == 'update') {
		changeOptions(quicknav.car, _models[quicknav.model.selectedIndex][2]);

	} else if (action == 'submit') {
		var carid = quicknav.car.options[quicknav.car.selectedIndex].value;
		gotoPage('dyc_step1.shtml', carid);
	}
} // dycQuickNav(action)


// prepends any page prefix to the form action's value
// used in 440 popup
function prepareForm(form,valid)
{
	if (valid == null) valid = true;

	if (valid && (pagePrefix != '') && (form.action.indexOf(pagePrefix) != 0)) {
		form.action = pagePrefix+form.action;
	}
	return valid;
}

