var FrontEnd = true;

var FieldsArray = new Array();
var Form1   = new Array(
          'shippingaddress_line1',
          'shippingaddress_line2',
          'txt_ship_county',
          'shippingaddress_postcode',
          'shippingaddress_city',
          'shippingaddress_postcodereturncontent',
          'shippingaddress_ajaxaddresscontent',
          'shippingaddress_ajaxaddressinform'
          );

FieldsArray[1] = Form1;

var Form2   = new Array(
          'billingaddress_line1',
          'billingaddress_line2',
          'txt_bill_county',
          'billingaddress_postcode',
          'billingaddress_city',
          'billingaddress_postcodereturncontent',
          'billingaddress_ajaxaddresscontent',
          'billingaddress_ajaxaddressinform'
          );

FieldsArray[2] = Form2;

CurrentIndex = 1;

BaseUrl = "/ajax/cp/call-centre/";
cmAddress = new AjaxObject();

cmAddress.SetAjaxDest('ajaxaddressinform','ajaxaddresscontent');
cmAddress.SetResetFlag(false);
cmAddress.ChangeAjaxBase(BaseUrl + "customers_manage/addresses");
cmAddress.SetAjaxVar('username', '" . $this->UserObject->username . "');

autofillAddress = new AjaxObject();
autofillAddress.SetAjaxDest('ajaxaddressinform','postcodereturncontent');
autofillAddress.SetResetFlag(false);
autofillAddress.ChangeAjaxBase(BaseUrl + "customers_manage/postcodeautofill");

function FireGoogleAnalyticsEvent()
{
	if($F('GoogleAnalyticsPath'))
	{
		pageTracker._trackPageview($F('GoogleAnalyticsPath'));
	}
}

function UpdateShippingManually()
{
  //$('ShippingGroup1').appendChild($('ShippingGroup0'));
  
  $('UpdateShippingManuallyLink').style.display = 'none';
  $('ShippingGroup1').style.display = 'inline';
  //$('ShippingGroup0').style.display = 'none';
  $('ShippingToggle0').style.display = 'none';
  $('ShippingToggle1').style.display = 'inline';
  if ($('CountryPostcodeError1')) $('CountryPostcodeError1').style.display = 'none';
  if ($('CountryPostcodeError2')) $('CountryPostcodeError2').style.display = 'none';  
}

function UpdateShippingUsingFinder()
{
  //$('ShippingToggle0').parentNode.insertBefore($('ShippingGroup0'), $('ShippingToggle0'));

  $('UpdateShippingManuallyLink').style.display = 'inline';
  $('ShippingGroup0').style.display = 'inline';
  $('ShippingGroup1').style.display = 'none';
  $('ShippingToggle0').style.display = 'inline';
  $('ShippingToggle1').style.display = 'none';
}

function UpdateBillingManually()
{
  //$('BillingGroup1').appendChild($('BillingGroup0'));

  $('UpdateBillingManuallyLink').style.display = 'none';
  $('BillingGroup1').style.display = 'inline';
  //$('BillingGroup0').style.display = 'none';
  $('BillingToggle0').style.display = 'none';
  $('BillingToggle1').style.display = 'inline';
  if ($('CountryPostcodeError1')) $('CountryPostcodeError1').style.display = 'none';
  if ($('CountryPostcodeError2')) $('CountryPostcodeError2').style.display = 'none';  
}

function UpdateBillingUsingFinder()
{
  //$('BillingToggle0').parentNode.insertBefore($('BillingGroup0'), $('BillingToggle0'));
  $('UpdateBillingManuallyLink').style.display = 'inline';
  $('BillingGroup0').style.display = 'inline';
  $('BillingGroup1').style.display = 'none';
  $('BillingToggle0').style.display = 'inline';
  $('BillingToggle1').style.display = 'none';
}

function CheckPostCode(ShippingType, pFieldName, pCurrIndex)
{ 
   if($('CountryPostcodeError')) $('CountryPostcodeError').innerHTML = "";
   if(ShippingType == 'billing') CurrentIndex = 2;
   
   var success = CheckPostCodeReturnAddress(null,null,pFieldName,pCurrIndex,ShippingType);
   
  if(success)
  {
   showhide('paf_popup');
   if(ShippingType == 'billing')
   {
      $('UpdateBillingManuallyLink').style.display = 'none';
      $('BillingGroup1').style.display = 'inline';
      $('BillingToggle0').style.display = 'inline';   
   }
   else
   {
      $('UpdateShippingManuallyLink').style.display = 'none';
      $('ShippingGroup1').style.display = 'inline';
      $('ShippingToggle0').style.display = 'inline';
   }
  }
}

function ShowState(StateNotCounty, Address)
{
  if (true == StateNotCounty)
  {
    $(Address + '_state').style.display = 'block';
    $(Address + '_county').style.display = 'none';
  }
  else
  {
    $(Address + '_state').style.display = 'none';
    $(Address + '_county').style.display = 'block';
  }
}

function ToggleState(type, Address)
{
	if (type == 'state')
	{
	   document.getElementById(Address + '_state').style.display = 'block';
	   document.getElementById(Address + '_county').style.display = 'none';
	   document.getElementById(Address + '_countyList').style.display = 'none';
	   document.getElementById(Address + '_provinceList').style.display = 'none';
	}
	else if (type == 'province')
	{
	   document.getElementById(Address + '_state').style.display = 'none';
	   document.getElementById(Address + '_county').style.display = 'none';
	   document.getElementById(Address + '_countyList').style.display = 'none';
	   document.getElementById(Address + '_provinceList').style.display = 'block';
	}
	else if (type == 'county')
	{
	   document.getElementById(Address + '_state').style.display = 'none';
	   document.getElementById(Address + '_county').style.display = 'block';
	   document.getElementById(Address + '_countyList').style.display = 'none';
	   document.getElementById(Address + '_provinceList').style.display = 'none';
	}
	else if (type == 'countyList')
	{
	   document.getElementById(Address + '_state').style.display = 'none';
	   document.getElementById(Address + '_county').style.display = 'none';
	   document.getElementById(Address + '_countyList').style.display = 'block';
	   document.getElementById(Address + '_provinceList').style.display = 'none';
	}
	else if (type == 'none')
	{
	   document.getElementById(Address + '_state').style.display = 'none';
	   document.getElementById(Address + '_county').style.display = 'none';
	   document.getElementById(Address + '_countyList').style.display = 'none';
	   document.getElementById(Address + '_provinceList').style.display = 'none';
	}      	      		
}


function ChangeCountry(Address)
{	
	if ( Address == true ) { //error handling in case the wrong value is sent
		return;
	} else 
	if ( Address == false) {
		$('change_country_popup').style.display = 'none';
	} else
	{
		var dropdown = $('ddl_'+Address+'_country');
		if ('US' == dropdown.options[dropdown.selectedIndex].value)
		{
		ToggleState('state', Address);
		if(Address == 'ship'){
		UpdateShippingUsingFinder();
		$('btnFindShippingAddress').style.display = 'inline';
		}else{
		UpdateBillingUsingFinder();
		$('btnFindBillingAddress').style.display = 'inline';    
		}
		$('address_postcode_label_mand').style.display = 'block';
		$('address_postcode_label_norm').style.display = 'none';
		
		$('house_number1').show();
		$('house_number1_label').show();
		} else
		if ('CA' == dropdown.options[dropdown.selectedIndex].value)
		{
		ToggleState('province', Address);
		if(Address == 'ship'){
		UpdateShippingUsingFinder();
		$('btnFindShippingAddress').style.display = 'inline';
		}else{
		UpdateBillingUsingFinder();
		$('btnFindBillingAddress').style.display = 'inline';    
		}
		
		$('address_postcode_label_mand').style.display = 'block';
		$('address_postcode_label_norm').style.display = 'none';
		
		$('house_number1').hide();
		$('house_number1_label').hide();
		}
		else
		{    
		if('GB' === dropdown.options[dropdown.selectedIndex].value)
		{
		ToggleState('countyList', Address);
		$('address_postcode_label_mand').style.display = 'block';
		$('address_postcode_label_norm').style.display = 'none';
		  
		if(Address == 'ship'){
		 UpdateShippingUsingFinder();
		 $('btnFindShippingAddress').style.display = 'inline';
		}else{
		 UpdateBillingUsingFinder();
		 $('btnFindBillingAddress').style.display = 'inline';    
		}
		
		$('house_number1').hide();
		$('house_number1_label').hide();
		}
		else
		if('IE' === dropdown.options[dropdown.selectedIndex].value)
		{
		ToggleState('county', Address);
		if(Address == 'ship'){
		 UpdateShippingManually();
		 $('ShippingGroup0').style.display = 'inline';
		}else{
		 UpdateBillingManually();
		 $('BillingGroup0').style.display = 'inline';
		}
		
		if($('house_number1')) $('house_number1').hide();
		if($('house_number1_label')) $('house_number1_label').hide();
		
		$('address_postcode_label_mand').style.display = 'none';
		$('address_postcode_label_norm').style.display = 'block';
		
		}
		else
		{
		ToggleState('none', Address);
		if(Address == 'ship'){
		 UpdateShippingManually();
		 $('ShippingGroup0').style.display = 'inline';
		}else{
		 UpdateBillingManually();
		 $('BillingGroup0').style.display = 'inline';
		}
		
		if($('house_number1')) $('house_number1').hide();
		if($('house_number1_label')) $('house_number1_label').hide();
		
		$('address_postcode_label_mand').style.display = 'none';
		$('address_postcode_label_norm').style.display = 'block';
		
		}
		}
	}
}

function GetDeliveryOptions(locale)
{
    showAjaxProgress('ajax-progress-save-button');
    if ( $('btn_save_continue') ) $('btn_save_continue').style.display = 'none';
    
    new Ajax.Request('/ajax/' + locale + '/my-account/checkout/delivery', {
      onSuccess: function(transport) {
          $('delivery_details').innerHTML = transport.responseText;
          hideAjaxProgress('ajax-progress-save-button');
          if ( $('btn_save_continue') ) $('btn_save_continue').style.display = 'inline';
         // FireGoogleAnalyticsEvent();
      }
    });
}

function SetDeliveryOptions(CountryCode)
{  
   if(CountryCode)
   {
       new Ajax.Request('/ajax/' + locale + '/my-account/checkout/getdeliveryoptions', {
         method: 'post',
         parameters: {
           countrycode: CountryCode
         },
         onSuccess: function(transport) {
             $('delivery_details').innerHTML = transport.responseText;
         // FireGoogleAnalyticsEvent();
         }
       });
   }
}

function SetSideBasketDelivery()
{       
    showAjaxProgress('ajax-progress-sidebasket-general');   
    var shipping_option = $('shipping_option').value;
    new Ajax.Request('/ajax/' + locale + '/my-account/checkout/sidebasketdelivery', {
      method: 'post',
      parameters: {
        shipping_option: shipping_option
      },
      onSuccess: function(transport) {
          $('content_2').innerHTML = transport.responseText;
      }
    });
}

function RefreshForgotPassword()
{         
    new Ajax.Request('/ajax/' + locale + '/my-account/checkout/refreshforgotpassword', {
      method: 'post',
      onSuccess: function(transport) {
          $('forgot-password-holder').innerHTML = transport.responseText;
      }
    });
}

// Call this when made any changes to the checkout process and it'll update the side basket
function UpdateSideBasket()
{  
   showAjaxProgress('ajax-progress-sidebasket-general');    
    new Ajax.Request('/ajax/' + locale + '/my-account/checkout/updatesidebasket', {
      method: 'post',
      onSuccess: function(transport) {
          $('content_2').innerHTML = transport.responseText;
      }
    });
}

function showAjaxProgress(id)
{
   if($(id)) $(id).style.display = 'inline';
}

function hideAjaxProgress(id)
{
   if($(id)) $(id).style.display = 'none';
}

function ForgotPassword()
{   
   showAjaxProgress('ajax-progress-forgot-password');
   var email = $('txt_email').value;
  
    new Ajax.Request('/ajax/' + locale + '/my-account/forgot_password', {
      method: 'post',
      parameters: {
        txt_email: email,
        submit_requestpw: true
      },
      onSuccess: function(transport) {
          $('forgot-password-holder').innerHTML = transport.responseText;
          showhide('forgot_password');
      }
    });
}

function NotSameBillingAddress(locale)
{    
   showAjaxProgress('ajax-progress-billing-option');
   if($('PlaceOrderButtonActive')) $('PlaceOrderButtonActive').style.display = 'none';
   if($('PlaceOrderButtonInactive')) $('PlaceOrderButtonInactive').style.display = 'inline';   
    new Ajax.Request('/ajax/' + locale + '/my-account/checkout/unsetbilling', {
      method: 'post',
      parameters: {
        
      },
      onSuccess: function(transport) {
         hideAjaxProgress('ajax-progress-billing-option');
         AddAddressLink('billing');
         UpdateSideBasket();
      }
    });
}

function SelectBillingAddress(billing_id, locale)
{
   if($('SelectAddressError')) $('SelectAddressError').style.display = 'none';
    showAjaxProgress('ajax-progress-address-'+billing_id);
    showAjaxProgress('ajax-progress-sidebasket-general');

    $('addbillingaddress').style.display = 'none';
    $('billingid').value = billing_id;
    if($('billing_address_'+billing_id))
    {
      $('billing_address_'+billing_id).value = 'Processing...';
    }    
    
    new Ajax.Request('/ajax/' + locale + '/my-account/checkout/billing', {
      method: 'post',
      parameters: {
        billingid: billing_id
      },
      onSuccess: function(transport) {
          $('billing_details').innerHTML = transport.responseText;
          //FireGoogleAnalyticsEvent();
          if($('PlaceOrderButtonActive')) $('PlaceOrderButtonActive').style.display = 'inline';
          if($('PlaceOrderButtonInactive')) $('PlaceOrderButtonInactive').style.display = 'none';          
          UpdateSideBasket();
      }
    });
}

function SelectShippingAddress(shipping_id, locale)
{
    showAjaxProgress('ajax-progress-address-'+shipping_id);
    showAjaxProgress('ajax-progress-sidebasket-general');
    showAjaxProgress('ajax-progress-save-button');
    if ( $('btn_save_continue') ) $('btn_save_continue').style.display = 'none';
    
    $('addshippingaddress').style.display = 'none';
    $('shippingid').value = shipping_id;
    $('shipping_address_'+shipping_id).value = 'Processing...';
    
    new Ajax.Request('/ajax/' + locale + '/my-account/checkout/shipping', {
      method: 'post',
      parameters: {
        shippingid: shipping_id
      },
      onSuccess: function(transport) {          
          $('shipping_details').innerHTML = transport.responseText;
          //FireGoogleAnalyticsEvent();
          GetDeliveryOptions(locale);          
          UpdateSideBasket();
      }
    });
}

function AddAddressLink(type){

   if($('SelectAddressError')) $('SelectAddressError').style.display = 'none';
   
   if(type == 'shipping'){
        $('checkout_register').shippingaddress_city.value = '';
        $('checkout_register').ddl_ship_country.value = '';
        $('checkout_register').txt_ship_county.value = '';
        $('checkout_register').shippingaddress_line1.value = '';
        $('checkout_register').shippingaddress_line2.value = '';
        $('checkout_register').shippingaddress_name.value = '';
        $('checkout_register').shippingaddress_postcode.value = '';
        $('checkout_register').	ddl_ship_state.value = '';
        
        if($('checkout_register').	shippingaddressedit_id)
        {
          $('checkout_register').	shippingaddressedit_id.value = '';
        }     
        UpdateShippingUsingFinder();
      $('shippingid').value = ''; 
      $('addshippingaddress').style.display = 'block'; 
      ShowShippingHouseNumber();
      
   }else{
        $('checkout_register').billingaddress_city.value = '';
        $('checkout_register').ddl_bill_country.value = '';
        $('checkout_register').txt_bill_county.value = '';
        $('checkout_register').billingaddress_line1.value = '';
        $('checkout_register').billingaddress_line2.value = '';
        $('checkout_register').billingaddress_name.value = '';
        $('checkout_register').billingaddress_postcode.value = '';
        $('checkout_register').	ddl_bill_state.value = '';
        
        if($('checkout_register').	billingaddressedit_id)
        {
          $('checkout_register').	billingaddressedit_id.value = '';
        }     
        UpdateBillingUsingFinder();   
      $('billingid').value = ''; 
      $('addbillingaddress').style.display = 'block'; 
      ShowBillingHouseNumber();
   }
}

  function ShowBillingHouseNumber()
  {
    if ('US' == $('ddl_bill_country').value) {
      if (null !== $('house_number1_label')) $('house_number1_label').show();
      if (null !== $('house_number1')) $('house_number1').show();
    }
    else
    {
      if (null !== $('house_number1_label')) $('house_number1_label').hide();
      if (null !== $('house_number1')) $('house_number1').hide();
    }
  }

function updateUserInfo(locale)
{
  showAjaxProgress('ajax-progress-user-details');  
  var password = $('account-password').password.value;
  var password_retyped = $('account-password').password_retyped.value;
  var hear_about_us = $('account-password').hear_about_us.value;
  var gender = null;  
  var offline_mailing_list = 0;
  var third_party_mailing_list = 0;

   if($('account-password').offline_mailing_list.checked){
      offline_mailing_list = 1;
   }
   
   if($('account-password').third_party_mailing_list.checked){
      third_party_mailing_list = 1;
   }    
   
   for (var i=0; i < $('account-password').gender.length; i++){
      {
      if ($('account-password').gender[i].checked)
         {
         var gender = $('account-password').gender[i].value;
         }
      }
   }

    
  new Ajax.Request('/ajax/' + locale + '/my-account/checkout/updateuserdetails', {
    method: 'post',
    parameters: {
      password: password,
      password_retyped : password_retyped,
      hear_about_us : hear_about_us,
      gender : gender, 
      offline_mailing_list : offline_mailing_list,
      third_party_mailing_list : third_party_mailing_list          
    },
    onSuccess: function(transport) {
        $('account-info-update').innerHTML = transport.responseText;        
    }
  });

  
  
}

function SetOfferCode()
{
   if($('ajax-progress-sidebasket-offercode')) $('ajax-progress-sidebasket-offercode').style.display = 'inline';
    var offercode = $('txt_promo-code').value;
    
    new Ajax.Request('/ajax/' + locale + '/my-account/checkout/offercode', {
      method: 'post',
      parameters: {
        offercode: offercode
      },
      onSuccess: function(transport) {
          $('right-column-container').innerHTML = transport.responseText;
          GetDeliveryOptions(locale);
      }
    });
}

function EditShippingAddress(addressid, locale)
{
  showAjaxProgress('ajax-progress-address-'+addressid);
  new Ajax.Request('/ajax/' + locale + '/my-account/checkout/editaddress', {
    method: 'post',
    parameters: {
      addressid: addressid,
      locale: locale,    
      type: 'shipping'
    },
    onSuccess: function(transport) {
        $('shipping_details').innerHTML = transport.responseText;
        // FireGoogleAnalyticsEvent();
    }
  });
}

function EditBillingAddress(addressid, locale)
{
   showAjaxProgress('ajax-progress-address-'+addressid);    
  new Ajax.Request('/ajax/' + locale + '/my-account/checkout/editaddress', {
    method: 'post',
    parameters: {
      addressid: addressid,
      locale: locale,    
      type: 'billing'   
    },
    onSuccess: function(transport) {
        $('billing_details').innerHTML = transport.responseText;
         // FireGoogleAnalyticsEvent();
        //showhide('addbillingaddress');
    }
  });
}


function addShippingAddress(locale)
{
  showAjaxProgress('ajax-progress-address');    
  var city = $('checkout_register').shippingaddress_city.value;
  var countrycode = $('checkout_register').ddl_ship_country.value;
  var county = $('checkout_register').txt_ship_county.value;
  var line1 = $('checkout_register').shippingaddress_line1.value;
  var line2 = $('checkout_register').shippingaddress_line2.value;
  var name = $('checkout_register').shippingaddress_name.value;
  var postcode = $('checkout_register').shippingaddress_postcode.value;
  var state = $('checkout_register').	ddl_ship_state.value;
  var countyList = $('checkout_register').	ddl_ship_county.value;
  var province = $('checkout_register').	ddl_ship_province.value;
  var id;
  
  if($('checkout_register').	shippingaddressedit_id)
  {
    id = $('checkout_register').	shippingaddressedit_id.value;
  }  
    
  new Ajax.Request('/ajax/' + locale + '/my-account/checkout/addaddress', {
    method: 'post',
    parameters: {
      shippingaddress_id: id,
      shippingaddress_city: city,
      shippingaddress_countrycode: countrycode,
      shippingaddress_county: county,
      shippingaddress_line1: line1,
      shippingaddress_line2: line2,
      shippingaddress_name: name,
      shippingaddress_postcode: postcode,
      shippingaddress_state: state,
      shippingaddress_province: province,
      shippingaddress_countyList: countyList,      
      type: 'shipping'          
    },
    onSuccess: function(transport) {
        $('shipping_details').innerHTML = transport.responseText;
        //  FireGoogleAnalyticsEvent();
        GetDeliveryOptions(locale);    
        UpdateSideBasket();    
    }
  });

  
  
}

function addBillingAddress(locale)
{
  showAjaxProgress('ajax-progress-address');  
  var city = $('checkout_register').billingaddress_city.value;
  var countrycode = $('checkout_register').ddl_bill_country.value;
  var county = $('checkout_register').txt_bill_county.value;
  var line1 = $('checkout_register').billingaddress_line1.value;
  var line2 = $('checkout_register').billingaddress_line2.value;
  var name = $('checkout_register').billingaddress_name.value;
  var postcode = $('checkout_register').billingaddress_postcode.value;
  var state = $('checkout_register').	ddl_bill_state.value;  
  var countyList = $('checkout_register').	ddl_bill_county.value;
  var province = $('checkout_register').	ddl_bill_province.value;
  var id;
  
  if($('checkout_register').	billingaddressedit_id )
  {
    id = $('checkout_register').	billingaddressedit_id.value;
  }  
  
  new Ajax.Request('/ajax/' + locale + '/my-account/checkout/addaddress', {
    method: 'post',
    parameters: {
      billingaddress_id: id,
      billingaddress_city: city,
      billingaddress_countrycode: countrycode,
      billingaddress_county: county,
      billingaddress_line1: line1,
      billingaddress_line2: line2,
      billingaddress_name: name,
      billingaddress_postcode: postcode,
      billingaddress_state: state,
      billingaddress_province: province,
      billingaddress_countyList: countyList,
      type: 'billing'          
    },
    onSuccess: function(transport) {    
        $('billing_details').innerHTML = transport.responseText;
        //  FireGoogleAnalyticsEvent();        
        UpdateSideBasket();
    }
  });

  
  
}

function DeleteBillingAddress(id){
  showAjaxProgress('ajax-progress-delete-address-'+id);  
  new Ajax.Request('/ajax/' + locale + '/my-account/checkout/deleteaddress', {
    method: 'post',
    parameters: {
      address_id: id,
      type: 'billing'          
    },
    onSuccess: function(transport) {
        $('billing_details').innerHTML = transport.responseText;    
        //  FireGoogleAnalyticsEvent();    
        UpdateSideBasket();
    }
  });
}

function DeleteShippingAddress(id){
 showAjaxProgress('ajax-progress-delete-address-'+id);   
  new Ajax.Request('/ajax/' + locale + '/my-account/checkout/deleteaddress', {
    method: 'post',
    parameters: {
      address_id: id,
      type: 'shipping'          
    },
    onSuccess: function(transport) {    
        $('shipping_details').innerHTML = transport.responseText;        
        //  FireGoogleAnalyticsEvent();
        UpdateSideBasket();        
    }
  });
}
/*
function deleteAddressPopup(AddressId){
   $('delete-address-details').innerHTML = '';
  new Ajax.Request('/ajax/' + locale + '/my-account/checkout/getaddressstring', {
    method: 'post',
    parameters: {
      address_id: AddressId     
    },
    onSuccess: function(transport) {
        $('delete-address-details').innerHTML = transport.responseText;        
    }
  });
   showhide('delete-address-popup');
}
*/

function PlaceOrderButtonReplace(){
   if($('PlaceOrderButtonInactive')) 
   {
	   $('PlaceOrderButtonInactive').style.display = 'inline';
   }
   if($('PlaceOrderButtonActive')) 
   {
	   $('PlaceOrderButtonActive').style.display = 'none';
	   $('ajax-progress-payment').style.display = 'block';
   }
}

function GenericAjaxPopup(Path){
  new Ajax.Request(Path, {
    method: 'post',
    onSuccess: function(transport) {
        $('generic_popup_content').innerHTML = transport.responseText;
        showhide('generic_popup');
    }
  });
}

function showBillingAddressSelectError(){
   if($('SelectAddressError')) $('SelectAddressError').style.display = 'block'; 
}

function RegisterStepTwo(locale){
  var shippingaddress = new Array();
  
  showAjaxProgress('ajax-progress-save-button2');
  
  if($('checkout_register').email){
      var email = $('checkout_register').email.value;
      var title = $('checkout_register').title.value;
      var username = $('checkout_register').username.value;
      var firstname = $('checkout_register').firstname.value;
      var home_phone = $('checkout_register').home_phone.value;
      var house_number = $('checkout_register').house_number.value;
      var lastname = $('checkout_register').lastname.value;  
      var password = $('checkout_register').password.value;
      var mobile_phone = $('checkout_register').mobile_phone.value;  
      var submit_register_page = $('checkout_register').submit_register_page.value;      
  }

  var shippingaddress_city = $('checkout_register').shippingaddress_city.value;
  var shippingaddress_countrycode = $('checkout_register').ddl_ship_country.value;  
  var shippingaddress_line1 = $('checkout_register').shippingaddress_line1.value;
  var shippingaddress_line2 = $('checkout_register').shippingaddress_line2.value;
  var shippingaddress_name = $('checkout_register').shippingaddress_name.value;
  var shippingaddress_postcode = $('checkout_register').shippingaddress_postcode.value;
  var shippingaddress_state = $('checkout_register').ddl_ship_state.value;
  var shippingaddress_county = $('checkout_register').txt_ship_county.value;
  var shippingaddress_countyList = $('checkout_register').ddl_ship_county.value;
  var shippingaddress_province = $('checkout_register').ddl_ship_province.value;
  
  var shipping_option = null;
  if($('checkout_register').shipping_option) shipping_option = $('checkout_register').shipping_option.value;
  
  
  new Ajax.Request('/ajax/' + locale + '/my-account/checkout/registertwo', {
    method: 'post',
    parameters: {
      email: email,
      title: title,
      username: username,
      firstname: firstname,
      home_phone: home_phone,
      house_number: house_number,
      lastname: lastname,
      mobile_phone: mobile_phone,
      password: password,
      shippingaddress_city: shippingaddress_city,
      shippingaddress_countrycode: shippingaddress_countrycode,
      shippingaddress_county: shippingaddress_county,
      shippingaddress_countyList: shippingaddress_countyList,
      shippingaddress_line1: shippingaddress_line1,
      shippingaddress_line2: shippingaddress_line2,
      shippingaddress_name: shippingaddress_name,
      shippingaddress_postcode: shippingaddress_postcode,
      shippingaddress_state: shippingaddress_state,
      shippingaddress_province: shippingaddress_province,
      submit_register_page: submit_register_page,
      shipping_option: shipping_option
    },
    onSuccess: function(transport) {
        UpdateSideBasket();    
        $('content_1').innerHTML = transport.responseText;        
        CheckTopOfPage();
          FireGoogleAnalyticsEvent();
        
    }
  });

  
  
}

function CheckTopOfPage(){
   if($('FirstNameError')){
      if($('FirstNameError').innerHTML != ''){
         window.scrollTo(0,0);
         return;
      }
   }   
   if($('LastNameError')){
      if($('LastNameError').innerHTML != ''){
         window.scrollTo(0,0);
         return;
      }
   }      
   if($('EmailError')){
      if($('EmailError').innerHTML != ''){
         window.scrollTo(0,0);
         return;
      }
   }      
   if($('HomePhoneError')){
      if($('HomePhoneError').innerHTML != ''){
         window.scrollTo(0,0);
         return;
      }
   }   

}

function PrePopulateName(){
   if ( $('shippingaddress_name') && $('txt_lastname') && $('txt_firstname') ){
      $('shippingaddress_name').value = $('txt_firstname').value + ' ' + $('txt_lastname').value;
   }
}


var ccErrorNo = 0;
var ccErrors = new Array ()

ccErrors [0] = "Unknown card type";
ccErrors [1] = "No card number provided";
ccErrors [2] = "Credit card number is in invalid format";
ccErrors [3] = "Credit card number is invalid";
ccErrors [4] = "Credit card number has an inappropriate number of digits";

function postError(element, error)
{
	$(element).addClassName('form_highlight');
	$(element+'-span').update('&nbsp;&nbsp;'+error);
}
function removeError(element)
{
	if($(element).hasClassName('form_highlight'))
	{
		$(element).removeClassName('form_highlight');
	}
	if($(element+'-span'))
	{
		$(element+'-span').update('');
	}
}


function validateCC()
{
	//assume that there's no error to begin with.
	var Error = false;
	
	//check the credit card
	if(!checkCreditCard($F('txt_card-no'), $F('ddl_card_name')))
	{
		Error = true;
		postError('txt_card-no', ccErrors[ccErrorNo]);
	}
	else
	{
		removeError('txt_card-no');
	}
	
	//check name
	if($F('txt_name').length == 0)
	{
		Error = true;
		postError('txt_name', 'Please set the cardholders name');
	}
	else
	{
		removeError('txt_name');
	}
	
	//check cvv code
	if($F('txt_security-co').length == 0)
	{
		Error = true;
		postError('txt_security-co', 'Please set the CVV Code');
	}
	else
	{
		removeError('txt_security-co');
	}
	
	//check expiry month
	if($F('paymentdetails_expiry_month') == '00' || $F('paymentdetails_expiry_year') == '00')
	{
		Error = true;
		$('paymentdetails_expiry_month').addClassName('form_highlight');
		postError('paymentdetails_expiry_year', 'Please set a Valid Expiry Date');
	}
	else
	{
		removeError('paymentdetails_expiry_month');
		removeError('paymentdetails_expiry_year');
	}
	
	if($('terms-and-conditions').checked)
	{
		$('terms-and-conditions_span').hide();
	}
	else
	{
		Error = true;
		$('terms-and-conditions_span').show();
	}
	
	if(Error)
	{
		return false;
	}
	else
	{
		return true;
	}
}


function checkCreditCard (cardnumber, cardname) {
  // Array to hold the permitted card characteristics
  var cards = new Array();

  // Define the cards we support. You may add addtional card types.
  
  //  Name:      As in the selection box of the form - must be same as user's
  //  Length:    List of possible valid lengths of the card number for the card
  //  prefixes:  List of possible prefixes for the card
  //  checkdigit Boolean to say whether there is a check digit
  
  cards [0] = {name: "Visa", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};
  cards [1] = {name: "Delta", 
               length: "16", 
               prefixes: "4",
               checkdigit: true};
  cards [2] = {name: "MasterCard", 
               length: "16", 
               prefixes: "51,52,53,54,55",
               checkdigit: true};
  cards [3] = {name: "AmEx", 
               length: "15", 
               prefixes: "34,37",
               checkdigit: true};
  cards [4] = {name: "Switch", 
               length: "16,18,19", 
               prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
               checkdigit: true};
  cards [5] = {name: "Maestro", 
               length: "12,13,14,15,16,18,19", 
               prefixes: "5018,5020,5038,6304,6759,6761",
               checkdigit: true};
               
  // Establish card type
  var cardType = -1;
  for (var i=0; i<cards.length; i++) {

    // See if it is this card (ignoring the case of the string)
    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
      cardType = i;
      break;
    }
  }
  
  // If card type not found, report an error
  if (cardType == -1) {
     ccErrorNo = 0;
     return false; 
  }
   
  // Ensure that the user has provided a credit card number
  if (cardnumber.length == 0)  {
     ccErrorNo = 1;
     return false; 
  }
    
  // Now remove any spaces from the credit card number
  cardnumber = cardnumber.replace (/\s/g, "");
  
  // Check that the number is numeric
  var cardNo = cardnumber
  var cardexp = /^[0-9]{13,19}$/;
  if (!cardexp.exec(cardNo))  {
     ccErrorNo = 2;
     return false; 
  }
       
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    var calc;
    for (i = cardNo.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardNo.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 3;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.
  var LengthValid = false;
  var PrefixValid = false; 
  var undefined; 

  // We use these for holding the valid lengths and prefixes of a card type
  var prefix = new Array ();
  var lengths = new Array ();
    
  // Load an array with the valid prefixes for this card
  prefix = cards[cardType].prefixes.split(",");
      
  // Now see if any of them match what we have in the card number
  for (i=0; i<prefix.length; i++) {
    var exp = new RegExp ("^" + prefix[i]);
    if (exp.test (cardNo)) PrefixValid = true;
  }
      
  // If it isn't a valid prefix there's no point at looking at the length
  if (!PrefixValid) {
     ccErrorNo = 3;
     return false; 
  }
    
  // See if the length is valid for this card
  lengths = cards[cardType].length.split(",");
  for (j=0; j<lengths.length; j++) {
    if (cardNo.length == lengths[j]) LengthValid = true;
  }
  
  // See if all is OK by seeing if the length was valid. We only check the 
  // length if all else was hunky dory.
  if (!LengthValid) {
     ccErrorNo = 4;
     return false; 
  };   
  
  // The credit card is in the required format.
  return true;
}

/*============================================================================*/
