// Dependencies:
//	Implementation of function getDriveCode() : string
//	Progressive agent utilities at: 
//      http://www.progressiveagent.com/include/p_drive_banner_util.js
//		Note: If the utilities at the above script URL or 
//            the "quote landing page" and/or corresponding query string 
//            parameters change, the simulator will not work and will 
//            have to be re-written to compensate for the changes.

var DEFAULT_ZIP_CODE = "45840";
var ProductCodes = {
	"AUTO" : "AU",		// Auto Insurance
	"MOTO" : "MC-M",	// Motorcycle Insurance
	"ATV"  : "MC-A",	// ATV Insurance
	"BOAT" : "BT"		// Boat/Personal Watercraft Insurance
};

function ValidateAndSendToProgressive(prodCodeKey)
{ // Simulates user interaction at the form displayed by the script at this URL:
  //   http://www.progressiveagent.com/include/p_drive_qt_sl_banner_lg.js
  // Precondition:
  //    The function getDriveCode() returns an agent code.
  //    prodCodeKey is a valid product code key defined in ProductCodes dictionary.
  // Postcondition:
  //    Always returns false to prevent execution of the hyperlink or button
  //    invoking this method.
  //    The user is asked to type in their ZIP code. 
  //    The ZIP code is validated before any action is taken.

	var url = "http://www.progressiveagent.com/quote/quote-landing.aspx";
	var agentCode = getDriveCode();
	var qType = "agentbanner";
	url = AppendUrlParameter( url, "agt_cd", agentCode );
	url = AppendUrlParameter( url, "qtype", qType );

	var zip = AskForZipCode();
	if( zip )
		url = AppendUrlParameter( url, "zip_cd", zip );
	else
		return false;

	var productCode = ProductCodes[ prodCodeKey ];
	if( !productCode )
	{
		alert( "Invalid product key: " + prodCodeKey );
		return false;
	}
	else if(productCode.indexOf("-") > -1)
	{
		var prodCodeParts = productCode.split("-");

		url = AppendUrlParameter( url, "prod_cd", prodCodeParts[0] );
		url = AppendUrlParameter( url, "veh_typ", prodCodeParts[1] );
	}
	else
	{
		url = AppendUrlParameter( url, "prod_cd", productCode );
	}

	if( !window.open(url) )
		location.href = url;
	return false;		
}

function AskForZipCode()
{
	// Continually ask for the ZIP code until the user cancels or enters a valid zip code
	var zip = null;
	var zipIsValid = false;
	do
	{
		zip = prompt( "Please enter your 5-digit ZIP code:", DEFAULT_ZIP_CODE );
		if( isEmpty( zip ) )
		{
			// User canceled or entered no value
			zip = null;
		}
		else
		{
			if( (zip.length != 5) || (!isInteger( zip )) )
			{
				alert("ZIP code must be 5 digits long and consist of numerical digits.\n\nZIP+4 codes are not supported.");
			}
			else
			{
				zipIsValid = true;
			}
		}
	} while( zip != null && !zipIsValid );
	
	return zip;
}