//
// This script contains American Airlines specific methods.
//
// Copyright 2008 Liberty Mutual Group. All rights reserved.
//
// Author Carrie Ann Roberge
//

///////////////////////////////////////////////////////////////////////////
// AmericanAirlines AmericanAirlines AmericanAirlines AmericanAirlines   //
///////////////////////////////////////////////////////////////////////////

var SRC_ARRAY = new Array("aa_splashpage", "aa_enewsletter", "aa_gen");
var KEYCODE_ID_ARRAY = new Array("IDAA3900", "IEAAN000", "IDAA3900");

var WEB_ID = "0000009925";
var AFFINITY = "americanairlines";
var KEYCODE_ID = "IDAA5000";
var PHONE_NUM = "888-438-9092";

// Resetting form variables in landing page.
function init_gateway()
{
	var src_code = swfobject.getQueryParamValue("src");

	if (src_code.length > 0)
	{
		for (var index = 0; index < SRC_ARRAY.length; index++) 
		{
			if (src_code.toLowerCase().indexOf (SRC_ARRAY [index].toLowerCase()) == 0)
			{
				KEYCODE_ID = KEYCODE_ID_ARRAY [index];

				break;
			}
		}
	}
	
	document.forms.getAQuoteForm.MM_webID.value = WEB_ID;
	document.forms.getAQuoteForm.keyCode.value = KEYCODE_ID;

	var phoneElement = document.getElementById("phoneNumberText");
	phoneElement.innerHTML = "Or call 1-" + PHONE_NUM;
}

function getSrcBasedWebID()
{
	return WEB_ID;  
}

function cf_getAQuoteAASubmit(getAQuoteForm)
{
	var selectControl = getAQuoteForm.policyType.options;
	var index = getAQuoteForm.policyType.selectedIndex;
	var choice = selectControl[index].value;
	var keyCodeID = getAQuoteForm.keyCode.value;

	// disable keyCode if not available
	if (keyCodeID == "") 
	{
		getAQuoteForm.keyCode.disabled = true;
	} 
	else 
	{
		getAQuoteForm.keyCode.disabled = false;
	}
	
	if (choice == "Life")
	{
    	getAQuoteForm.action = rc.urlLookup ("LifeQuoteServiceStartQuote");
		getAQuoteForm.submit();
    }
	else if (choice == "Auto" || choice == "H3" || choice == "H4" || choice == "H6")
	{
		if ((cf_validateZIPCode(getAQuoteForm.zipCode)) && (cf_validateAAdvantage(getAQuoteForm.MemberID)))
		{
		    document.forms.getAQuoteForm.action = rc.urlLookup ("QuoteServiceStartQuote");
			document.forms.getAQuoteForm.submit();
		}
	}
}

function cf_getAQuoteAAEnter(getAQuoteForm)
{
	var keycode;
	
	if (window.event) 
	{
		keycode = window.event.keyCode;
	}

	if (keycode == 13)
	{
		cf_getAQuoteAASubmit(getAQuoteForm);
	}
}

function cf_validateAAdvantage(AAPin)
{

    var format = /^[^a-bOIZ]{6}[0|2|4|6|8]$/;
	var ok = (format.exec(AAPin.value) == null)? false: true;
	//alert(ok);

	if (ok == true)
	{
		//alert("I am true");
		var count = AAPin.value.replace(/\d/g, '').length;
		if (count > 3)
		{
			ok = false;
		}
	}

	if (ok == false)
	{
		alert("Please enter a valid AAdvantage number.");
		AAPin.focus();
	}

	return ok;
}
