
var replaced = false;

/* Adam Vandenberg's script to parse the query string.
  Version 1.3
  28 May 2008
  
  License (Simplified BSD):
  http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
  this.params = {};
  
  if (qs == null) qs = location.search.substring(1, location.search.length);
  if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
  qs = qs.replace(/\+/g, ' ');
  var args = qs.split('&'); // parse out name/value pairs separated via &
  
// split out each name=value pair
  for (var i = 0; i < args.length; i++) {
    var pair = args[i].split('=');
    var name = decodeURIComponent(pair[0]);
    
    var value = (pair.length==2)
      ? decodeURIComponent(pair[1])
      : name;
    
    this.params[name] = value;
  }
}

Querystring.prototype.get = function(key, default_) {
  var value = this.params[key];
  return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
  var value = this.params[key];
  return (value != null);
}

function display_quote_form(){
  $('#message').parent().hide();
  $('#proposal').fadeIn();
  if (!replaced) {
    sIFR.replace(futurac, {
      selector: '#proposal h3',
      wmode : 'opaque',
      css : [
        '.sIFR-root {color: #3e92c4;}'
      ],
      antiAliasType : 'advanced',
      sharpness: -70,
      thickness: 20
    });
  }
  replaced = true;
}

function textfield_reset(that){
  var label = that.siblings('label');
  that.val(label.text());
}
function textarea_reset(that){
  var description = that.siblings('.field-description');
  that.val(description.text());
}
function form_reset(){
  $('input[type=text]').each(function(){
    textfield_reset($(this));
  });
  $('textarea').each(function(){
    textarea_reset($(this));
  });
}

function form_init(){
  $('input[type=text]').each(function(){
    if (!this.defaultValue) {
      textfield_reset($(this));
    }
  });
  $('textarea').each(function(){
    if (!this.defaultValue) {
      textarea_reset($(this));
    }
  });
}

$(function(){
  $('input[type=text],textarea').each(function(){
    var label = $(this).siblings('label');
    var description = $(this).siblings('.field-description');
    description.hide();
    label.hide();
  });
  form_init();
  $('input[type=reset]').replaceWith('<a id="clear-form-link" href="#">clear form</a>');
  $('#clear-form-link').click(function(e){
    e.preventDefault();
    form_reset();
  });
  jQuery('input[type=text]').each(function(){
    jQuery(this).focus(function(){
      var label = $(this).siblings('label');
      if (this.value == label.text()) {
        this.value = '';
      }
    }).blur(function(){
    var label = $(this).siblings('label');
      if (!this.value.length) {
        this.value = label.text();
      } 
    });
  });
  jQuery('textarea').each(function(){
    jQuery(this).focus(function(){
      var description = $(this).siblings('.field-description');
      if (this.value == description.text()) {
        this.value = '';
      }
    }).blur(function(){
    var description = $(this).siblings('.field-description');
      if (!this.value.length) {
        this.value = description.text();
      } 
    });
  });
  
  // Check the query string for "quote"
  var qs = new Querystring();
  if (qs.contains("quote")) {
    $('#topic').val('quote');
  }
  
  // in case the form is being refreshed
  if ($('#topic').val() == 'quote') {
    display_quote_form();
  } else {
    $('#proposal').hide();
  }
  
  $('#topic').change(function(){
    // If the proposal stuff is not selected yet
    if ($(this).val() == 'quote') {
      display_quote_form();
    } else {
      $('#proposal').fadeOut(function(){
        $('#message').parent().show();
      });
    }
  });
  
  
  // AJAX Submission of the form
  $('#the-form').submit(function(){ 
    var params = {};
    that = jQuery(this);
    that.find("input[type='text'],input[type='hidden'],textarea,select").each(function(){
      params[this.name] = this.value;
    });
    $('#user-messages').fadeOut().remove();
    $('#main p:first').after('<ul id="user-messages" style="display: none;"><li>One moment please...</li></ul>');
    $('#user-messages').fadeIn();
    jQuery.post('/includes/contact_ajax.php',params,function(data){
      var d = jQuery(data);
      var result = d.find('result').text();
      if (result != 'success') {
        $('#user-messages').fadeOut().remove();
        $('#main p:first').after('<ul id="user-messages" class="failure" style="display: none;"></ul>');
        $('#the-form').find('input[type=text],textarea').removeClass('highlight');
        $('highlight',d).each(function(){
          var selected = $('#'+$(this).text());
          if (selected.hasClass('textfield')) {
            selected.addClass('texthighlight');
          } else {
            selected.addClass('highlight');
          }
        });
        $('error',d).each(function(){
          $('#user-messages').append('<li>' + $(this).text() + '</li>');
          $.scrollTo('h2',{duration: 200});
          $('#user-messages').fadeIn();
        });
      } else {
          $('#user-messages').fadeOut().remove();
          $('#main p:first').after('<ul id="user-messages" class="success" style="display: none;"><li>Your message has been sent successfully.</li></ul>');
          $.scrollTo('h2',{duration: 200});
          $('#user-messages').fadeIn();
          $('#submit').attr('disabled','disabled');
      }
    },'xml'); 
    return false; 
  });
});