﻿/**
 * Description: javascript actions and functions related to the module shoping cart.
 * User: pedro alfaiate
 * Date: 12/Sep/2011
 **/


/* Cart Actions */

var bol_returnCarForm = false;

function onLoadStep1Init(str_currentSet) {

  $( '#btnNextStep' ).click( function() {
    $( '#cartForm' ).submit();
  } );

  $( '.inputQuantity' ).keyup( function() {
    if ( parseInt( $( this ).val() ) >= 1 ) {
      window.location = "/" + str_currentSet + "&" + $( this ).attr( 'name' ) + "=" + $( this ).val();
    }
  } );

  $( '#receberAmostra' ).click( function() {
    if ( $( this ).is( ':checked' ) ) {
      $( '#' + str_promotionColgateName ).val( str_promotionColgateValue );
    }
    else {
      $( '#' + str_promotionColgateName ).val( '' );
    }
    //alert($('#'+str_promotionColgateName).val());
  } );
}

function onLoadStep2Init() {

  var obj_autoCompleteCountryPhoneCodes = new Array(),
      obj_countryPhoneCodesXml,
      str_optionsCountryPhoneCodes = '';

  $( '#EcomOrderDeliveryCountry' ).val( str_EcomOrderCustomerCountry ).change( function() {
    switchCartZipCodeSufix( $( this ).val() , 'delivery' );
  } );

  $( '#EcomOrderCustomerCountry' ).val( str_EcomOrderCustomerCountry ).change( function() {

    switchCartZipCodeSufix( $( this ).val() , 'customer' );
    switchCartZipCodeSufix( $( this ).val() , 'delivery' );

    $( obj_countryPhoneCodesXml ).find( 'code' ).each( function(i) {
      if ( $( this ).attr( 'country' ) == $( '#EcomOrderCustomerCountry' ).val() ) {
        $( "#customerCountryPhoneId" ).val( $( this ).text() );
        $( "#customerCountryCellId" ).val( $( this ).text() );
        $( "#body select" ).select_unskin().select_skin();
      }
      1
    } );
  } );

  
  if ( $( "#customerSurnames" ).val() == '' )
    $( "#customerSurnames" ).val( $( "#userLastName" ).html() );

  if ( $( "#EcomOrderCustomerRegion" ).val() == '' )
    $( "#EcomOrderCustomerRegion" ).val( $( "#userDistrict" ).html() );

  if ( $( "#customerZip2" ).val() == '' )
    $( "#customerZip2" ).val( $( "#userZipSufix" ).html() );

  if ( $( "#EcomOrderCustomerVatRegNumber" ).val() == '' )
    $( "#EcomOrderCustomerVatRegNumber" ).val( $( "#userVAT" ).html() );


  $.ajax( {
    type: "GET",
    url: "/Files/Templates/Designs/DeltaQ/PhoneCountryCode.xml",
    dataType: "xml",
    success: function(xml) {
      obj_countryPhoneCodesXml = xml;

      $( xml ).find( 'code' ).each( function(i) {
        if ( $( '#EcomOrderCustomerCountry' ).val() == '' ) {
          $( '#EcomOrderCustomerCountry' ).val( 'PT' );
          $( '#EcomOrderDeliveryCountry' ).val( 'PT' );
        }
        /*
         if($(this).attr('country') == $('#EcomOrderCustomerCountry').val()){
         $("#customerCountryPhoneId").val('+ '+$(this).text());
         $("#customerCountryCellId").val('+ '+$(this).text());
         }
         */
        obj_autoCompleteCountryPhoneCodes[i] = '+ ' + $( this ).text();

        if ( $( this ).attr( 'country' ) == $( '#EcomOrderCustomerCountry' ).val() ) {
          str_optionsCountryPhoneCodes += '<option title="' + $( this ).attr( 'country' ) + '" selected="selected" value="' + $( this ).text() + '">' + $( this ).text() + '</option>';
        }
        else {
          str_optionsCountryPhoneCodes += '<option title="' + $( this ).attr( 'country' ) + '" value="' + $( this ).text() + '">' + $( this ).text() + '</option>';
        }
      } );
      $( "#customerCountryPhoneId" ).html( str_optionsCountryPhoneCodes );
      $( "#customerCountryCellId" ).html( str_optionsCountryPhoneCodes );

      $( "#body select" ).select_unskin().select_skin();

      /*
       $("#customerCountryPhoneId").autocomplete({
       source: obj_autoCompleteCountryPhoneCodes
       });
       $("#customerCountryCellId").autocomplete({
       source: obj_autoCompleteCountryPhoneCodes
       });
       */
    }
  } );

  for ( var i = 0 ; i < obj_errorMessages.length ; i++ ) {
    if ( obj_errorMessages[i] != '' ) {
      str_allMessageError += obj_errorMessages[i] + '<br/>';//'\n';
    }
  }
  if ( str_allMessageError ) {
    alert( str_allMessageError , str_pleaseFillFields );
  }

  $( '#deliveryAddressCustomer' ).val( str_deliveryAddressCustomer ).change( function() {disableDeliveryFields()} );
  disableDeliveryFields();

  switchCartZipCodeSufix( $( '#EcomOrderCustomerCountry' ).val() , 'customer' );
  switchCartZipCodeSufix( $( '#EcomOrderCustomerCountry' ).val() , 'delivery' );


  $( "#body select" ).select_skin();
  /* helpdeskStart - nuno.b - 27/12/2011 @ 17:30 ( on contry select, enable the region) #4736 */
  $('#EcomOrderCustomerCountry').change(function() {
    if ($('#EcomOrderCustomerCountry :selected').val() == '') {
      $('#EcomOrderCustomerRegion').attr('disabled', 'enable');
    }
    else {
      $('#EcomOrderCustomerRegion').removeAttr('disabled');
    }
  });                                                                                    
  /* helpdeskEnd */
}
function onLoadStep3Init() {

  $( '#paymentMethod li' ).click( function() {
    $( '#paymentMethod li' ).removeClass( 'active' );
    $( this ).addClass( 'active' );
    $( '#EcomCartPaymethodID' ).val( $( this ).attr( 'id' ) );
  } );

  //alert($('#paymentMethod li').length);
  if ( $( '#paymentMethod li' ).length <= 1 ) {
    $( '#paymentMethod li' ).eq( 0 ).click();
  }

  $( '#shippingMethod li' ).click( function() {
    $( '#shippingMethod li' ).removeClass( 'active' );
    $( this ).addClass( 'active' );
    $( '#EcomCartShippingmethodID' ).val( $( this ).attr( 'id' ) );
  } );
  if ( $( '#shippingMethod li' ).length <= 1 ) {
    $( '#shippingMethod li' ).eq( 0 ).click();
  }


  $( '#btnNextStep' ).click( function() {
    if ( $( '#acceptTerms' ).is( ':checked' ) ) {
      $( '#submitUserMethod' ).submit();
    }
    else {
      alert( str_msgAcceptTerms );
    }
  } );

}

function switchCartZipCodeSufix(str_countryCode , str_person) {
  if ( str_person == 'customer' ) {

    if ( str_countryCode == 'PT' || str_countryCode == 'AQ' || str_countryCode == 'AG' || str_countryCode == 'AI' || str_countryCode == 'PL' ) {

      $( '#customerZip2' ).css( {'display':'block'} );
      $( '#EcomOrderCustomerZip' ).addClass( 'width2' ).removeClass( 'width11' );

      if ( str_countryCode == 'PT' || str_countryCode == 'AQ' || str_countryCode == 'AG' ) {
        $( '#EcomOrderCustomerZip' ).attr( 'maxlength' , '4' );

        if ( $( '#EcomOrderCustomerZip' ).val().length > 4 ) {
          $( '#EcomOrderCustomerZip' ).val( '' );
        } else if ( $( '#EcomOrderCustomerZip' ).val().length == 4 ) {
          bol_returnCarForm = true;
        }
      } else if ( str_countryCode == 'PL' ) {
        $( '#EcomOrderCustomerZip' ).attr( 'maxlength' , '2' );
        if ( $( '#EcomOrderCustomerZip' ).val().length > 2 ) {
          $( '#EcomOrderCustomerZip' ).val( '' );
        } else if ( $( '#EcomOrderCustomerZip' ).val().length == 2 ) {
          bol_returnCarForm = true;
        }
      }
    }
    else {

      $( '#customerZip2' ).css( {'display':'none'} );
      $( '#EcomOrderCustomerZip' ).removeClass( 'width2' ).addClass( 'width11' );

      // todo removed because of an error    if(str_countryCode == 'DE' || str_countryCode == 'FR' || str_countryCode == 'FX' || str_countryCode == 'ES' || str_countryCode == 'LK' || str_countryCode == 'BT'){
      if ( str_countryCode == 'DE' || str_countryCode == 'FX' || str_countryCode == 'ES' || str_countryCode == 'FR' || str_countryCode == 'LK' || str_countryCode == 'BT' ) {

        $( '#EcomOrderCustomerZip' ).attr( 'maxlength' , '5' );
        if ( $( '#EcomOrderCustomerZip' ).val().length > 5 ) {
          $( '#EcomOrderCustomerZip' ).val( '' );
        } else if ( $( '#EcomOrderCustomerZip' ).val().length == 5 ) {
          bol_returnCarForm = true;
        }
      } else if ( str_countryCode == 'BE' || str_countryCode == 'NL' || str_countryCode == 'LU' || str_countryCode == 'CH' ) {
        $( '#EcomOrderCustomerZip' ).attr( 'maxlength' , '4' );
        if ( $( '#EcomOrderCustomerZip' ).val().length > 4 ) {
          $( '#EcomOrderCustomerZip' ).val( '' );
        } else if ( $( '#EcomOrderCustomerZip' ).val().length == 4 ) {
          bol_returnCarForm = true;
        }
      }
      else {
        $( '#EcomOrderCustomerZip' ).removeAttr( 'maxlength' );
      }
    }
  }
  else {

    if ( str_countryCode == 'PT' || str_countryCode == 'AQ' || str_countryCode == 'AG' || str_countryCode == 'AI' || str_countryCode == 'PL') {

      $( '#deliveryZip2' ).css( {'display':'block'} );
      $( '#EcomOrderDeliveryZip' ).addClass( 'width2' ).removeClass( 'width11' );

      if ( str_countryCode == 'PT' || str_countryCode == 'AQ' || str_countryCode == 'AG' ) {
        $( '#EcomOrderDeliveryZip' ).attr( 'maxlength' , '4' );
        if ( $( '#EcomOrderDeliveryZip' ).val().length > 4 ) {
          $( '#EcomOrderDeliveryZip' ).val( '' );
        } else if ( $( '#EcomOrderDeliveryZip' ).val().length == 4 ) {
          bol_returnCarForm = true;
        }
      } else if ( str_countryCode == 'PL' ) {
        $( '#EcomOrderDeliveryZip' ).attr( 'maxlength' , '2' );
        if ( $( '#EcomOrderDeliveryZip' ).val().length > 2 ) {
          $( '#EcomOrderDeliveryZip' ).val( '' );
        } else if ( $( '#EcomOrderDeliveryZip' ).val().length == 2 ) {
          bol_returnCarForm = true;
        }
      }
    }
    else {

      $( '#deliveryZip2' ).css( {'display':'none'} );
      $( '#EcomOrderDeliveryZip' ).removeClass( 'width2' ).addClass( 'width11' );

      //  todo removed because of an e    if(str_countryCode == 'DE' || str_countryCode == 'FR' || str_countryCode == 'FX' || str_countryCode == 'ES' || str_countryCode == 'LK' || str_countryCode == 'BT'){
      if ( str_countryCode == 'DE' || str_countryCode == 'FX' || str_countryCode == 'ES' || str_countryCode == 'LK' || str_countryCode == 'FR' || str_countryCode == 'BT' ) {
        $( '#EcomOrderDeliveryZip' ).attr( 'maxlength' , '5' );
        if ( $( '#EcomOrderDeliveryZip' ).val().length > 5 ) {
          $( '#EcomOrderDeliveryZip' ).val( '' );
        } else if ( $( '#EcomOrderDeliveryZip' ).val().length == 5 ) {
          bol_returnCarForm = true;
        }
      } else if ( str_countryCode == 'BE' || str_countryCode == 'NL' || str_countryCode == 'LU' || str_countryCode == 'CH' ) {
        $( '#EcomOrderDeliveryZip' ).attr( 'maxlength' , '4' );
        if ( $( '#EcomOrderDeliveryZip' ).val().length > 4 ) {
          $( '#EcomOrderDeliveryZip' ).val( '' );
        } else if ( $( '#EcomOrderDeliveryZip' ).val().length == 4 ) {
          bol_returnCarForm = true;
        }
      }
      else {
        $( '#EcomOrderDeliveryZip' ).removeAttr( 'maxlength' );
      }
    }
  }

}

function validateCartForm() {
  switchCartZipCodeSufix( $( '#EcomOrderCustomerCountry option:selected' ).val() , 'customer' ),
  switchCartZipCodeSufix( $( '#EcomOrderDeliveryCountry option:selected' ).val() , 'delivery' );

  if ( !bol_returnCarForm )
    alert( str_zipCodeErrorMessageDigits , "Aviso" );

  return bol_returnCarForm;
}

function validateMethodForm() {
  var bol_formError = true;
  if ( $( '#EcomCartPaymethodID' ).val() == '' ) {
    alert( str_payMethod );
    bol_formError = false;
  } else if ( $( '#EcomCartShippingmethodID' ).val() == '' ) {
    alert( str_shippingMethod );
    bol_formError = false;
  }
  return bol_formError;
}

function disableDeliveryFields() {
  if ( $( '#deliveryAddressCustomer' ).val() != str_optionDelivery ) {
    $( '.deliveryFields' ).css( {'display':'block'} );
    customerFieldsToDelivery( false );
    switchCartZipCodeSufix( $( '#EcomOrderCustomerCountry' ).val() , 'delivery' );
  }
  else {
    customerFieldsToDelivery( true );
    $( '.deliveryFields' ).css( {'display':'none'} );
  }
}

function customerFieldsToDelivery(bol_goAction) {
  var str_inputCustomerID,
      str_inputDeliveryID;

  $( '.customerFields input' ).each( function(i) {
    str_inputCustomerID = $( this ).attr( 'id' );
    str_inputDeliveryID = str_inputCustomerID.replace( "Customer" , "Delivery" ).replace( "customer" , "delivery" );
    if ( bol_goAction == true ) {
      $( '#' + str_inputDeliveryID ).val( $( this ).val() );
      $( this ).change( function() {
        str_inputCustomerID = $( this ).attr( 'id' );
        str_inputDeliveryID = str_inputCustomerID.replace( "Customer" , "Delivery" ).replace( "customer" , "delivery" );
        $( '#' + str_inputDeliveryID ).val( $( this ).val() );

      } )
    }
  } );

  $( '.customerFields select' ).each( function(i) {
    str_inputCustomerID = $( this ).attr( 'id' );
    str_inputDeliveryID = str_inputCustomerID.replace( "Customer" , "Delivery" );
    if ( bol_goAction == true ) {
      $( '#' + str_inputDeliveryID ).val( $( this ).val() );
      $( this ).change( function() {
        str_inputCustomerID = $( this ).attr( 'id' );
        str_inputDeliveryID = str_inputCustomerID.replace( "Customer" , "Delivery" );
        $( '#' + str_inputDeliveryID ).val( $( this ).val() );
      } )
    }
  } );

}
