// JavaScript Document
var PriceMatch = new Class({
    Extends: Popup,
    initialize: function(adoptElement){
        this.parent({'title': 'Price Match', 'width': 455, height: 450, 'adoptElement': adoptElement});
    },
    
    createPopup: function() {
      this.parent();
      $('pr-submit').addEvent('click', function() {
        this.validateAndSend();
      }.bindWithEvent(this));
    },
    
    validateAndSend: function() {
      var valid = true;
      var fields = [$('pr-where'), $('pr-price'), $('pr-email'), $('pr-telephone')];
      fields.each(function(item) {
        item.removeClass('invalid');
        if (item.get('value') == '') {
          valid = false;
          item.addClass('invalid');
        }
      });
      if (!valid) {
        return;
      }
      var jSonRequest = new Request.JSON({url: 'sendpricematch.php', onComplete: function() {
        $('priceMatchForm').set('html', '<p>Thank you for submitting your price match, we\'ll get back to you as soon as possible.</p>');
      }}).post({'query': {'email': $('pr-email').value, 'where': $('pr-where').value, 'price': $('pr-price').value, 'product_model': $('pr-model').value, 'cust_name': $('pr-name').value, 'cust_telephone': $('pr-telephone').value, 
    	  'pr-link': $('pr-link').value, 'pr-id': $('pr-id').value, 'pr-product_link': $('pr-product_link').value}});
    }
});
