// JavaScript Document
function on_load_mystuff() {
    chkFrqntFldIndx();
}
function resetAccountPage(s_section){
 var a_idList = new Array;
 a_idList['login'] = ["login","loginEdit","loginLink"];
 a_idList['persInfo'] = ["persInfo","persInfoEdit","persInfoLink"];
 a_idList['billInfo'] = ["billInfo","billInfoEdit","billInfoLink"];
 a_idList['travPref'] = ["travPref","travPrefEdit","travPrefLink"];
 for(var s_aryKey in a_idList)
 {
   var b_idsValid = true;var i_onOff;var a_noneBlockList = new Array;var x = 0;
   for(var i=0; i < a_idList[s_aryKey].length; i++)
   {
     if(document.getElementById(a_idList[s_aryKey][i]) && b_idsValid)
     {
       if(s_aryKey == s_section) {i_onOff = (i == 0)?'0':(i == 1)?'1':(i == 2)?'0':'1'} else {i_onOff = (i == 1)?'0':(i == 0)?'1':(i == 2)?'1':'0'}
       a_noneBlockList[x]="'"+a_idList[s_aryKey][i]+"!"+i_onOff+"'";
       x++;
     } else {b_idsValid = false;}
   }
     if(b_idsValid){eval("none"+"Block("+a_noneBlockList.join(",")+");")}
 }
}

var i_frqntFlyrCnt = 1;
var i_frqntGstCnt = 1;
var i_frqntRntlCnt = 1;
var i_frqntCrusCnt = 1;
var i_maxFldAmt=5;
var b_enterState = false;

function chkFrqntFldIndx() {
  var a_FldTyp = new Array('Flyr','Gst','Rntl','Crus');
  for(var s_fld in a_FldTyp) {
	 for(var i=1; i < i_maxFldAmt;i++) {
	   eval('i_frqnt'+a_FldTyp[s_fld]+'Cnt = (document.getElementById("frqnt'+a_FldTyp[s_fld]+i+'").style.display == "none") ? i_frqnt'+a_FldTyp[s_fld]+'Cnt : i+1');	   
	 }
	 var o_link = eval('document.getElementById("frqnt'+a_FldTyp[s_fld]+'Lnk");');
	 var s_rtn = eval('(document.getElementById("frqnt'+a_FldTyp[s_fld]+i_maxFldAmt+'").style.display == "block")? disableLink(o_link) : ""');
  }
}

function addFrqntFunc(s_type){
	if(eval('i_'+s_type+'Cnt') <= i_maxFldAmt){
        var s_theCounter;
        var s_theType; 
        s_theCounter = eval('i_'+s_type+'Cnt');
        s_theType = s_type + s_theCounter;
        document.getElementById(s_theType).style.display = (document.all)?'block':'table-row';
        eval('i_'+s_type+'Cnt = (s_theCounter += 1)');    
        disableLink(document.getElementById(s_type+'Lnk'));
    }
  }

function getAccountInfo()
{
   document.forms[0].action = "MyStuffControllerInit.do?target_page=MyAccount";
   document.forms[0].submit();    
}

function cancelAccountInfo( formName, cancelLink )
{
  if( formName.errorType.value == 'UpdateLoginInfoFailure' ||
      formName.errorType.value == 'UpdateLoginMessaging' ||
      formName.errorType.value == 'UpdatePersonalInfoFailure' ||
      formName.errorType.value == 'UpdateBillingInfo' ||
      formName.errorType.value == 'UpdateTravelPreferences')
  {
    formName.action = cancelLink;
    formName.submit();
    return false;
  }
  else
  {
    resetAccountPage();
    return true;
  }
}

function cancelLink () {
  return false;
}
function disableLink (link) {
  if (link.onclick && !link.oldOnClick){ 
    link.oldOnClick = link.onclick;
  }
  link.onclick = cancelLink;
  if (link.style)
    link.style.cursor = 'default';
    link.style.color = '#ccc';
}
function enableLink (link) {
  link.onclick = link.oldOnClick ? link.oldOnClick : null;
  if (link.style)
    link.style.cursor = 
      document.all ? 'hand' : 'pointer';
    link.style.color = '';
}
function checkFldLen (s_fldVal,o_link,s_altFld) {
  var o_altFld = document.getElementById(s_altFld);
  if(o_altFld){
   b_altFldChk = (o_altFld.type == 'select-one') ?  (o_altFld.selectedIndex == 0) ? false : true : (o_altFld.value == '') ? false : true; 
  }
  s_rslt = (s_fldVal.length > 0 && b_altFldChk) ? enableLink(o_link) : disableLink(o_link);
}

var isNN = (navigator.appName.indexOf("Netscape") != -1);
function filterDigits(e)
{
  var keyCode = (isNN) ? e.which : e.keyCode;
  if ( ( keyCode > 47 && keyCode < 58 ) || keyCode == 8 || keyCode == 0 )
    return true;
  else
    return false;
}

function autoTab(input, len, e)
{
  function getIndex(input)
  {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
      if (input.form[i] == input)
        index = i;
      else
        i++;
    return index;
  }
  function containsElement(arr, ele)
  {
    var found = false, index = 0;
    while (!found && index < arr.length)
      if (arr[index] == ele)
        found = true;
      else
        index++;
    return found;
  }
  var keyCode = (isNN) ? e.which : e.keyCode;
  var filter = (isNN) ? [0, 8, 9, 16] : [0, 8, 9, 16, 17, 18, 37, 38, 39, 40, 46];
  if (input.value.length >= len && !containsElement(filter, keyCode))
  {
    input.value = input.value.slice(0, len);
    nextElement = input.form[(getIndex(input) + 1) % input.form.length];
    if ((nextElement.type == "text") || (nextElement.type == "textarea"))
      nextElement.select();
    nextElement.focus();
  }
  return true;
}

function validateState( country, state ) {
  if( ( country.value != "US" ) &&
      ( country.value != "CA" ) &&
      ( country.value != "AU" ) )
  {
    state.value='';
  }
}

function doDateReset( month, year ) {
  var currentDate = new Date();
  if ( currentDate.getMonth() < 8 )
  {
    month.value = ( "0" + ( currentDate.getMonth() + 2 ) );
    year.value = currentDate.getYear();
  }
  else if ( currentDate.getMonth() < 11 )
  {
    month.value = ( currentDate.getMonth() + 2 );
    year.value = currentDate.getYear();
  }
  else
  {
    month.value = "01";
    year.value = ( currentDate.getYear() + 1 );
  }
}

function on_load_DisplayState()
{
	setStateFocus('false');
	setAltStateFocus('false');
	setBillingStateFocus('false');
	setAltBillingStateFocus('false');
}
function displayAlternateDeliveryAddress()
  {
    document.PersonalInformationForm.displayAltDeliveryAddress.value = "true";
  }
  function setStateFocus(setFocus)
  {
	var countryCode = document.PersonalInformationForm.countryCode.value;
	showStates(countryCode);
	if (setFocus == 'true' )
	{
		if(countryCode == 'CA')
		{
			document.PersonalInformationForm.elements['stateCanada'].focus()
		}
		else if(countryCode == 'US')
		{
			document.PersonalInformationForm.elements['stateUS'].focus()
		}
		else if(countryCode == 'AU')
		{
			document.PersonalInformationForm.elements['stateAus'].focus()
		}
	}

  }
  function setAltStateFocus(setFocus)
  {
	var countryCode = document.PersonalInformationForm.alternateCountryCode.value;
	showAltStates(countryCode);
	if (setFocus == 'true' )
	{
		if(countryCode == 'CA')
		{
			document.PersonalInformationForm.elements['altStateCanada'].focus()
		}
		else if(countryCode == 'US')
		{
			document.PersonalInformationForm.elements['altStateUS'].focus()
		}
		else if(countryCode == 'AU')
		{
			document.PersonalInformationForm.elements['altStateAus'].focus()
		}
	}
		
  }
  function showStates(country)
  {
    if  ( country == 'CA')
    {
      noneBlock('showCanadaStates!3','showUSStates!0','showAustraliaStates!0');
    }
    else if ( country == 'US')
    {
      noneBlock('showCanadaStates!0','showUSStates!3','showAustraliaStates!0');
    }
    else if ( country == 'AU')
    {
      noneBlock('showCanadaStates!0','showUSStates!0','showAustraliaStates!3');
    }
    else 
    {
      noneBlock('showCanadaStates!0','showUSStates!0','showAustraliaStates!0');
    }
  }

  function showAltStates(country)
  {
    if  ( country == 'CA')
    {
      noneBlock('showAltCanadaStates!3','showAltUSStates!0','showAltAustraliaStates!0');
    }
    else if ( country =='US')
    {
      noneBlock('showAltCanadaStates!0','showAltUSStates!3','showAltAustraliaStates!0');
    }
    else if ( country == 'AU')
    {
      noneBlock('showAltCanadaStates!0','showAltUSStates!0','showAltAustraliaStates!3');
    }
    else 
    {
      noneBlock('showAltCanadaStates!0','showAltUSStates!0','showAltAustraliaStates!0');
    }
  }

  function doAddressReset( param, addressId )
  {
    if ( param == 'pri' )
    {
      document.PersonalInformationForm.countryCode.value = 'US';
      document.PersonalInformationForm.companyName.value = '';
      document.PersonalInformationForm.deliveryAddress1.value = '';
      document.PersonalInformationForm.deliveryAddress2.value = '';
      document.PersonalInformationForm.city.value = '';
      document.PersonalInformationForm.stateUS.value = '';
      document.PersonalInformationForm.stateCanada.value = '';
      document.PersonalInformationForm.stateAus.value = '';
      document.PersonalInformationForm.zipCode.value = '';
      document.PersonalInformationForm.deleteAddressId.value = addressId;
    }
    if ( param == 'alt' )
    {
      document.PersonalInformationForm.alternateCountryCode.value = 'US';
      document.PersonalInformationForm.alternateCompanyName.value = '';
      document.PersonalInformationForm.alternateDeliveryAddress1.value = '';
      document.PersonalInformationForm.alternateDeliveryAddress2.value = '';
      document.PersonalInformationForm.alternateCity.value = '';
      document.PersonalInformationForm.alternateZip.value = '';
      document.PersonalInformationForm.deleteAlternateAddressId.value = addressId;
      document.PersonalInformationForm.altStateCanada.value = '';
      document.PersonalInformationForm.altStateUS.value = '';
      document.PersonalInformationForm.altStateAus.value = '';
    }
  }
  function displayAltCard()
  {
    document.UpdateBillingInformationForm.disAltCard.value="true";
  }
  function setBillingStateFocus(setFocus)
  {
	var countryCode = document.UpdateBillingInformationForm.billingCountry.value;
	showBillingStates(countryCode);
	if (setFocus == 'true' )
	{
		if(countryCode == 'CA')
		{
			document.UpdateBillingInformationForm.elements['billingStateCA'].focus()
		}
		else if(countryCode == 'US')
		{
			document.UpdateBillingInformationForm.elements['billingStateUS'].focus()
		}
		else if(countryCode == 'AU')
		{
			document.UpdateBillingInformationForm.elements['billingStateAU'].focus()
		}
	}

  }
  function setAltBillingStateFocus(setFocus)
  {
	var countryCode = document.UpdateBillingInformationForm.altBillingCountry.value;
	showAltBillingStates(countryCode);
	if (setFocus == 'true' )
	{
		if(countryCode == 'CA')
		{
			document.UpdateBillingInformationForm.elements['altBillingStateCA'].focus()
		}
		else if(countryCode == 'US')
		{
			document.UpdateBillingInformationForm.elements['altBillingStateUS'].focus()
		}
		else if(countryCode == 'AU')
		{
			document.UpdateBillingInformationForm.elements['altBillingStateAU'].focus()
		}
	}
		
  }
  function showBillingStates( country )
  {
    if ( country == 'CA' )
    {
      noneBlock('showBillingStatesCA!3','showBillingStatesUS!0','showBillingStatesAU!0');
    }
    else if ( country == 'US' )
    {
      noneBlock('showBillingStatesCA!0','showBillingStatesUS!3','showBillingStatesAU!0');
    }
    else if ( country == 'AU' )
    {
      noneBlock('showBillingStatesCA!0','showBillingStatesUS!0','showBillingStatesAU!3');
    }
    else 
    {
      noneBlock('showBillingStatesCA!0','showBillingStatesUS!0','showBillingStatesAU!0');
    }
  }

  function showAltBillingStates( country )
  {
    if ( country == 'CA' )
    {
      noneBlock('showAltBillingStatesCA!3','showAltBillingStatesUS!0','showAltBillingStatesAU!0');
    }
    else if ( country == 'US' )
    {
      noneBlock('showAltBillingStatesCA!0','showAltBillingStatesUS!3','showAltBillingStatesAU!0');
    }
    else if ( country == 'AU' )
    {
      noneBlock('showAltBillingStatesCA!0','showAltBillingStatesUS!0','showAltBillingStatesAU!3');
    }
    else 
    {
      noneBlock('showAltBillingStatesCA!0','showAltBillingStatesUS!0','showAltBillingStatesAU!0');
    }
  }

  function doCardReset( param, creditCardId )
  {
    var currentDate = new Date();
    if( param == 'pri' ) //reset primary card details
    {
      document.UpdateBillingInformationForm.creditCardType.value = 'ik';
      document.UpdateBillingInformationForm.creditCardNumber.value = '';
      document.UpdateBillingInformationForm.creditcardOwnerName.value = '';
      document.UpdateBillingInformationForm.companyName.value = '';
      document.UpdateBillingInformationForm.billingCountry.value = 'US';
      document.UpdateBillingInformationForm.billingAddress1.value = '';
      document.UpdateBillingInformationForm.billingAddress2.value = '';
      document.UpdateBillingInformationForm.billingCity.value = '';
      document.UpdateBillingInformationForm.billingStateUS.value = '';
      document.UpdateBillingInformationForm.billingStateCA.value = '';
      document.UpdateBillingInformationForm.billingStateAU.value = '';
      document.UpdateBillingInformationForm.billingZipCode.value = '';
      document.UpdateBillingInformationForm.creditCardExpirationMonth.value = '1';
      document.UpdateBillingInformationForm.creditCardExpirationYear.value = currentDate.getYear();
      if(creditCardId == "0")creditCardId = "-1";
      document.UpdateBillingInformationForm.deleteCreditCardId.value=creditCardId;
    }
    if( param == 'alt' ) //reset alternate card details
    {
      document.UpdateBillingInformationForm.altCreditCardType.value = 'ik';
      document.UpdateBillingInformationForm.altCreditCardNumber.value = '';
      document.UpdateBillingInformationForm.altCreditcardOwnerName.value = '';
      document.UpdateBillingInformationForm.altCompanyName.value = '';
      document.UpdateBillingInformationForm.altBillingCountry.value = 'US';
      document.UpdateBillingInformationForm.altBillingAddress1.value = '';
      document.UpdateBillingInformationForm.altBillingAddress2.value = '';
      document.UpdateBillingInformationForm.altBillingCity.value = '';
      document.UpdateBillingInformationForm.altBillingStateUS.value = '';
      document.UpdateBillingInformationForm.altBillingStateCA.value = '';
      document.UpdateBillingInformationForm.altBillingStateAU.value = '';
      document.UpdateBillingInformationForm.altBillingZipCode.value = '';
      document.UpdateBillingInformationForm.altCreditCardExpirationMonth.value = '1';
      document.UpdateBillingInformationForm.altCreditCardExpirationYear.value = currentDate.getYear();
      if(creditCardId == "0")creditCardId = "-1";
      document.UpdateBillingInformationForm.deleteAltCreditCardId.value=creditCardId;
    }
  }
  function redirect( redirectLink )
  {
    //location.href=("/travelocity/mystuff/MyStuffControllerInit.do?target_page=MyAccount");
    //window.location = "MyStuffControllerInit.do?target_page=MyAccount";
    window.location = redirectLink;
  }
  function setStateFocusForAssociate()
  {
	var countryCode = document.AddAssociatedTravelerForm.country.value;
	showStatesForAssociate(countryCode);
	if(countryCode == 'CA')
	{
		document.AddAssociatedTravelerForm.elements['stateCA'].focus()
	}
	else if(countryCode == 'US')
	{
		document.AddAssociatedTravelerForm.elements['stateUS'].focus()
	}
	else if(countryCode == 'AU')
	{
		document.AddAssociatedTravelerForm.elements['stateAU'].focus()
	}
  }
  function showStatesForAssociate( country )
  {
    if ( country == 'CA' )
    {
      noneBlock('showStatesCAForAssociate!3','showStatesUSForAssociate!0','showStatesAUForAssociate!0');
    }
    else if ( country == 'US' )
    {
      noneBlock('showStatesCAForAssociate!0','showStatesUSForAssociate!3','showStatesAUForAssociate!0');
    }
    else if ( country == 'AU' )
    {
      noneBlock('showStatesCAForAssociate!0','showStatesUSForAssociate!0','showStatesAUForAssociate!3');
    }
    else 
    {
      noneBlock('showStatesCAForAssociate!0','showStatesUSForAssociate!0','showStatesAUForAssociate!0');
    }
  }
  function setEmailFormat(format)
  {
	document.PersonalInformationForm.hidEmailFormat.value = format;
  }
