var lock = false;
var tmpShortcut = "";
/*
 * Ajax Funktion
 */

function getProductNumberOfLicenceForm(shortcut, text)
{
	/**
	 * Parameter für die AJAX abfrage
	 */
	var params = {
	    'shortcut': shortcut
    };

    jQuery.post(
            '/form/getProductNumberOfLicenceForm',
            params,
            function(html){
              $('#licence').append(
                 '<tr class="'+shortcut+'">'
                   + '<td width="320" class="grey_background_left">'
                   + text
                   + '</td>'
                   + ' '
                   + '<td class="grey_background_right">'
                   + html
                   + '</td>'
                   + '</tr>'
               );
            },
            'html'
    );

    jQuery('.ajaxLoaderLicence').ajaxComplete(function(request, settings){
        lock = false;
        jQuery(this).hide();
    });

    return false
}

function getPartner()
{
  var country   = $('#quote_me_country_region option:selected').val();
  var shortcuts = new Array();
  $('#quote_me_products option:selected').each(function(){
     shortcuts.push($(this).val());
  });
  if(shortcuts && country)
  {
    var params = {
        'shortcuts[]': shortcuts,
        'country'  : country
      };

        jQuery.post(
              '/form/getPartnerForm',
              params,
              function(html){
                $('#quote_me_partner_tr').remove();
                $('#partner').append(
                       '<tr id="quote_me_partner_tr">'
                     + '<td>'
                     + html
                     + '</td>'
                     + '</tr>'
                 );
              },
              'html'
      );
  }
  return false
}

var init = function(){
  tmpShortcut = "ACAD";
  jQuery('#quote_me_products').change(function(){
    if(!lock)
    {
      lock = true;
      jQuery('.product_version').show();

      var shortcut = jQuery('#quote_me_products option:selected').val();
      var name     = jQuery('#quote_me_products option:selected').html();
      if(tmpShortcut)
      {
        jQuery('.' + tmpShortcut ).remove();
      }
      tmpShortcut = shortcut;

      if(shortcut)
      {
          jQuery('.ajaxLoaderLicence').show();
          jQuery('.ajaxLoaderVersion').show();
          getProductNumberOfLicenceForm( shortcut, name );
          getPartner();
      }
      else
      {
          lock = false;
      }

    }
  });

  jQuery('#quote_me_country_region').change(function(){
    getPartner();
  });

  jQuery('.asmListItemRemove').livequery(function(){
    jQuery(this).click(function(){
      getPartner();
      jQuery('.'+ getShortcut(jQuery(this).parent().attr('rel')) ).remove();
    });
  });

};

