if (!pfrt) var pfrt = {};

(function($){

pfrt.init = function() {
  var selector = (typeof pf != 'undefined') ? '.pf_designer .pf_script' : '.pf_form_container .pf_script';
  $(selector).each(function(id, scriptElement){
    eval($(scriptElement).text());
  });
};

pfrt.requireCss = function(url, ieOnly) {
  if (ieOnly && !$.browser.msie) return;
  var loaded = false;
  $('link').each(function(){
    if ($(this).attr('href') == url) {
      loaded = true;
      return false;
    }
  });
  if (!loaded) {
    $('<link href="' + url + '" rel="styleSheet" type="text/css"/>').appendTo($('head'));
  }
};

pfrt.utils = {
  formId: function(viewModelId) {
    return viewModelId + '-pf_form';
  },
  noBlank: function(s) {
    return s.replace(/ /g, '__');
  }
};

if (typeof pf == 'undefined') {
  $(function(){
    pfrt.init();
  });
}

})(jQuery);

/* workaround for "waiter not defined" issue with integralenergy jsvalidate*/
if (typeof waiter == 'undefined') {
  waiter = function() {};
}if (typeof pf == 'undefined') {

(function($){
  var userAgent = navigator.userAgent.toLowerCase(); 
  $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 

  // Is this a version of Chrome?
  if($.browser.chrome){
    userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
    userAgent = userAgent.substring(0,userAgent.indexOf('.'));
    $.browser.version = userAgent;
    // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
    $.browser.safari = false;
  }

  // Is this a version of Safari?
  if($.browser.safari){
    userAgent = userAgent.substring(userAgent.indexOf('safari/') +7);
    userAgent = userAgent.substring(0,userAgent.indexOf('.'));
    $.browser.version = userAgent;
  }
  $(function(){
    if ($.browser.safari && $('div.pf_uploader').size() > 0 && false) {
      $('form.pf_form').html('<div style="position: relative; width: 570px; border: 1px solid #EE6352; background: #FAE1D0; padding: 8px !important;"> <div style="position:relative">You are using a Safari browser which is not supported for file uploading at the moment. Please click anyone of the following links to get a compatible browser</div><ul style="position:relative;float:left;clear:both;padding-left:1em !important;"><li><a href="http://www.google.com/chrome/">Google Chrome</a></li><li><a href="http://firefox.com/">Mozilla Firefox</a></li><li><a href="http://ie.microsoft.com">Microsoft Intenet Explorer</a></li></div>');
      return;
    } else if ($.browser.msie && ($.browser.version == '6.0')) {
      if ($('div.pf_uploader').size() > 0) {
      //if (false) {
        $('form.pf_form').html('<div style="position: relative; width: 570px; border: 1px solid #EE6352; background: #FAE1D0; padding: 8px !important;"> <div style="position:relative">You are using an outdated browser which is not supported. Please click anyone of the following to get a compatible browser</div><ul style="position:relative;float:left;clear:both;padding-left:1em !important;"><li style="width: 15em;"><a href="http://www.google.com/chrome/"  style="width: 15em;">Google Chrome</a></li><li style="width: 15em;"><a href="http://firefox.com/"  style="width: 15em;">Mozilla Firefox</a></li><li style="width: 15em;"><a href="http://ie.microsoft.com"  style="width: 15em;">Microsoft Intenet Explorer</a></li></div>');
        return;
      }
      // disable datepicker on IE6
      $('div.pf_field input.date').removeClass('date').addClass('hasDatepicker');
    }
    $('form.pf_form')
      .attr('action', '/pf_server')
      .submit(function(){
        if (!$(this).valid()) return false;
        var $form = $(this).addClass('pf_busy');
        var $names = $('#__pf_names__'), names = '';
        if ($names.size() == 0) $names = $('<input type="hidden" id="__pf_names__" name="__pf_names__"></input>').appendTo($form);
        
        $form.find('input:not(:button, :reset, :submit), textarea, select').each(function(id, el){
          names += "," + $(this).attr('name');
        });
        $names.val(names);
        $form.parent().find('div.pf_busyIndicator').show();
        $form.ajaxComplete(function(){
          $form.removeClass('pf_busy').parent().find('div.pf_busyIndicator').hide();
        });
        var fileNames = [];
        $form.find('li.qq-upload-success > span.qq-upload-file').each(function(){
          fileNames.push($(this).text());
        });
        $form.find('input[type=hidden].pf_hid_uploadFileName').val(fileNames.join(","));
        $.post($form.attr('action'), $form.serialize(), function(data){
          var tmplSuccess = "<p>Thank you, your request has been received.</p><p>Your reference number is: ${ok}",
            tmplError = "<p>Oops, error found during processing your request: ${err}</p>";
          $form.removeClass('pf_busy').parent().find('div.pf_busyIndicator').hide();
          if (data.ok) {
            $form.replaceWith($.tmpl(tmplSuccess, data));
          } else {
            $form.replaceWith($.tmpl(tmplError, data));
          }
        });
        return false;
      })
      .find('div.pf_uploader').each(function() {
        new qq.FileUploader({
          element: this,
          action: '/pf_server/upload'
        });
      });
    var steps = $('form.pf_form .pf_step').size();
    if (steps > 0) {
      $($('form.pf_form .pf_step')[steps - 1]).find('.pf_actions').css('display', 'none');
    }
 });
})(jQuery);

}
