
/*
 * This script checks against user's previous choices displays appropriate prompts.
 * Should be included in all of configurator's main JSPs where selections are involved.  
 */
function WMessage( code, message ) {
	this.code = code;
	this.message = message;
}
var cfgWarnMessages = new Array();
cfgWarnMessages[0] = new WMessage( 'zip', 'You may lose previously made selections in Trims, Exterior/Interior and Options pages.' );
cfgWarnMessages[1] = new WMessage( 'modelCode', 'You will lose previously made selections in the Interior Exterior Colors page.' );
cfgWarnMessages[2] = new WMessage( 'interiorCode', 'You will lose previously made selections in the Packages and Options page.' );
cfgWarnMessages[3] = new WMessage( 'exteriorCode', 'You will lose previously made selections in the Packages and Options page.' );
cfgWarnMessages[4] = new WMessage( 'packages', 'You will lose previously made selections in the following steps.' );
cfgWarnMessages[5] = new WMessage( 'options', 'You will lose previously made selections in the following steps.' );

function lookupWMessage( code ) {
	var msg = "";

	for ( var nI = 0; nI < cfgWarnMessages.length; nI++ ) {
		if ( code == cfgWarnMessages[ nI ].code ) {
			msg = cfgWarnMessages[ nI ].message;
			break;
		}
	}
	return msg;
}

/*
 * This array will be populated dynamically by the JSP
 */
var cfgChoices = new Array();

function ConfigChoice( code, value, otherCode ) {
	this.code = code;
	this.value = value;
	this.otherCode = otherCode; // dependent code.
	this.promptAccepted = false;
}

/*
 * This method needs to be called from following screens of configurator.
 * 1) When the Zip changes
 * 2) When the Trim selection changes
 * 3) When the exterior or interior codes change
 */
function onChoiceChanged( code, value, methodCallIfTrue, btnValue ) {

	var result = true;

	if ( cfgChoices != null ) {
		for ( var nI = 0; nI < cfgChoices.length; nI++ ) {
			if ( code == cfgChoices[ nI ].code ) {
				if ( cfgChoices[ nI ].value != null
						&& cfgChoices[ nI ].value != value
						&& cfgChoices[ nI ].promptAccepted == false ) {
					//
					// This variable in utilities_dhtml.js
					//
					var callbackArgs = new Array();

					callbackArgs[0] = cfgChoices[ nI ];
					callbackArgs[1] = methodCallIfTrue;
					callbackArgs[2] = btnValue;
					gCallbackEvent = new DialogBoxEvent( 0, callbackArgs );
					gCallbackEvent.callbackMethod = choiceCallbackMethod;
					//popupDialogBox( lookupWMessage( cfgChoices[ nI ].code ) );
					//
					// the callback method takes care of the rest
					//
					result = true;
					break;
				}
			}
		}
	}

	return result;
}

function choiceCallbackMethod( dlgEvent ) {

	var	choiceMade = dlgEvent.argA[ 0 ];

	if ( dlgEvent.usersChoice == true ) {
		choiceMade.promptAccepted = true;
	} else {
		result = false;
	}
	//
	// For color selection if either interior or exterior changes,
	//	don't prompt for the other change.
	//
	if ( choiceMade.promptAccepted == true
			&& choiceMade.otherCode != null ) {
		for ( var nJ = 0; nJ < cfgChoices.length; nJ++ ) {
			if ( choiceMade.otherCode == cfgChoices[ nJ ].code ) {
				cfgChoices[ nJ ].promptAccepted = true;
				break;
			}
		}
	}

	if ( dlgEvent.usersChoice == true && dlgEvent.argA[1] != null ) {
		dlgEvent.argA[1]( dlgEvent.argA[2] );
	}
}
