$(document).ready(function()
{

  /*** Toggle AGB Container ***/

    $(".toggleAGB").click(function(){
      $("#agbContainer").toggle();
      return false;
    });

  /*** Code for Service create formular ***/

    // code templates
    var linkTemplate    = $.template( '&lt;a href="${href}" ${target}&gt;${logo}&lt;/a&gt;' );
    var dialogTemplate  = $.template( '&lt;iframe src="${href}" width="385" height="230" marginwidth="0" marginheight="0" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;' );
    var logoA           = '&lt;img src="/logoA" width="" height="" alt="" border="0" /&gt;';
    var logoB           = '&lt;img src="/logoB" width="" height="" alt="" border="0" /&gt;';

    // reset form
    $("#tx_nplinkservice_pi1_reset").click(function()
    {
      $("#tx_nplinkservice_pi1_preselection_c").attr('checked','checked');
      $("#tx_nplinkservice_pi1_stage_b").attr('checked','checked');
      $("#tx_nplinkservice_pi1_window_target_a").attr('checked','checked');
      $("#tx_nplinkservice_pi1_stage_address").val('Fischergasse 10');
      $("#tx_nplinkservice_pi1_city").val('Salzburg');
      $("#tx_nplinkservice_pi1_result_code").html('');
    });


    // generate code
    $("#tx_nplinkservice_pi1_run_for_code").click(function()
    {
      // set defaults
      var user_session_id = $("#tx_nplinkservice_pi1_user_session_id").val();
      var user_email      = $("#tx_nplinkservice_pi1_user_email").val();
      var user_url        = $("#tx_nplinkservice_pi1_user_url").val();
      var hash            = '';
      var post_url        = '/fileadmin/scripts/np_linkservice/requests.php?sid='+user_session_id+'&user_email='+escape(user_email)+'&user_url='+escape(user_url);

      // form validation
      // -> used for email / url validation
      var formOptions =
      {
        errorElement: "div",
        focusInvalid: true,
        event: "blur",
        rules:
        {
          "tx_nplinkservice_pi1[user][email]":  {
            required: true,
            email: true
          },
          "tx_nplinkservice_pi1[user][url]":  {
            required: true,
            url: true
          },
          "tx_nplinkservice_pi1[accept_agb]": {
            required: true
          }
        },
        messages:
        {
          "tx_nplinkservice_pi1[user][email]":  {
            required: "Bitte geben Sie Ihre E-Mail Adresse ein",
            email: "Bitte pr&uuml;fen Sie Ihre E-Mail Adresse"
          },
          "tx_nplinkservice_pi1[user][url]":  {
            required: "Bitte geben Sie Ihre Webseite an",
            email: "Bitte pr&uuml;fen Sie Ihre Webseiten Adresse"
          },
          "tx_nplinkservice_pi1[accept_agb]": {
            required: "Bitte akzeptieren Sie unsere AGB"
          }
        },
        submitHandler: function(form) {
        				return false;
        			}
      };
      var valid = $("#linkservice").validate(formOptions).form();
      if ( valid == false ) {
        return false;
      }

      // post request
      $.post( post_url, '', function(html)
      {
        hash  = html;

        // set defaults
        var codeType  = $("#tx_nplinkservice_pi1_type").val();
        var href      = 'http://www.svv-info.at/index.php?id=288&no_cache=1&amp;';
        var logo      = '';

        // set destination or origin
        // set destination
        if ( $("#tx_nplinkservice_pi1_preselection_a").attr('checked') )
        {
          var requestType = 'destination';
          var href  = href
                    + 'tx_nplinkservice_pi1%5Bplace_destination%5D='                // e.g. Munich, Salzburg
                    + escape( $("#tx_nplinkservice_pi1_city").val() )
                    + '&amp;tx_nplinkservice_pi1%5Bname_destination%5D='            // Streetname or Stage
                    + escape( $("#tx_nplinkservice_pi1_stage_address").val() )
                    + '&amp;search_type=destination'
                    + '&amp;';
        }
        // set origin
        else if ( $("#tx_nplinkservice_pi1_preselection_b").attr('checked') )
        {
          var requestType = 'origin';
          var href  = href
                    + 'tx_nplinkservice_pi1%5Bplace_origin%5D='
                    + escape( $("#tx_nplinkservice_pi1_city").val() )
                    + '&amp;tx_nplinkservice_pi1%5Bname_origin%5D='
                    + escape( $("#tx_nplinkservice_pi1_stage_address").val() )
                    + '&amp;search_type=origin'
                    + '&amp;';
        }

        // set origin / destination type (address|stop)
        if ( $("#tx_nplinkservice_pi1_stage_a").attr('checked') ) {
          var href  = href+'tx_nplinkservice_pi1%5Btype_'+requestType+'%5D=stop&amp;';
        }
        else if ( $("#tx_nplinkservice_pi1_stage_b").attr('checked') ) {
          var href  = href+'tx_nplinkservice_pi1%5Btype_'+requestType+'%5D=address&amp;';
        }
        else if ( $("#tx_nplinkservice_pi1_stage_c").attr('checked') ) {
          var href  = href+'tx_nplinkservice_pi1%5Btype_'+requestType+'%5D=poi&amp;';
        }

        // add name
        var href  = href+'tx_nplinkservice_pi1%5Bcustomer_name%5D='+$("#tx_nplinkservice_pi1_name").val()+'&amp;';

        // add session id (required = 0)
        var href  = href+'tx_nplinkservice_pi1%5BsessionID%5D=0&amp;';

        // add user hash
        href  = href + 'hash=' + hash + '&amp;';

        switch (codeType)
        {
          case "dialog":
          {
            // set window target
            if ( $("#tx_nplinkservice_pi1_window_target_b").attr('checked') ) {
              var href  = href + 'tx_nplinkservice_pi1%5Btarget%5D=1';
            }
            else {
              var href  = href + 'tx_nplinkservice_pi1%5Btarget%5D=0';
            }

            $("#tx_nplinkservice_pi1_result_code").html( dialogTemplate, {
                                                          href:    href,
                                                          target:  target,
                                                          logo:    logo
                                                        }
            );
          }
          break;

          case "link":
          {
            // set window target
            if ( $("#tx_nplinkservice_pi1_window_target_b").attr('checked') ) {
              var target  = 'tx_nplinkservice_pi1%5Btarget%5D="_blank"';
            }
            else {
              var target  = 'tx_nplinkservice_pi1%5Btarget%5D="_self"';
            }

            // set selected vars
            if ( $("#tx_nplinkservice_pi1_logo_A").attr('checked') ) {
              var logo  = logoA;
            }
            else if ( $("#tx_nplinkservice_pi1_logo_B").attr('checked') ) {
              var logo  = logoB;
            }

            $("#tx_nplinkservice_pi1_result_code").html( linkTemplate, {
                                                          href:    href,
                                                          target:  target,
                                                          logo:    logo
                                                        }
            );
          }
          break;

        }


      }, 'json');


    });


  /*** Code for the small Service dialog form ***/

    $("#linkedDates").datepicker(
    {
        /*minDate: new Date(2001, 1 - 1, 1),
        maxDate: new Date(2010, 12 - 1, 31),*/
        defaultDate:      new Date(),
        speed:            0,
        beforeShow:       readLinked,
        onSelect:         updateLinked,
        showOn:           "both",
        buttonImage:      "/fileadmin/lib/jquery/plugins/datepicker/calendar.gif",
        buttonImageOnly:  true,
        firstDay:         1,
        dateFormat:       'yymmdd',
        duration:         0,
        changeYear:       false,
        changeMonth:      false
    });
    $("#selectDay, #selectMonth, #selectYear").change(checkLinkedDays);

});


// Prepare to show a date picker linked to three select controls
function readLinked()
{
    $('#linkedDates').val($('#selectMonth').val() + '/' +
        $('#selectDay').val() + '/' + $('#selectYear').val());
    return {};
}

// Update three select controls to match a date picker selection
function updateLinked(date) {
    $('#selectMonth').val(date.substring(4,6));
    $('#selectDay').val(date.substring(6,8));
    $('#selectYear').val(date.substring(0,4));
}

// Prevent selection of invalid dates through the select controls
function checkLinkedDays()
{
    var daysInMonth = 32 - new Date($('#selectYear').val(),
    $('#selectMonth').val() - 1, 32).getDate();
    $('#selectDay option').attr('disabled', '');
    $('#selectDay option:gt(' + (daysInMonth - 1) +')').attr('disabled', 'disabled');
    if ($('#selectDay').val() > daysInMonth) {
        $('#selectDay').val(daysInMonth);
    }
}
