/**
* Contact form javascript
* $Id$
*/

var PBContact = {
initialize: function(FORM_TYPE) {
  this.fnameMessage = 'Please enter a first name';
  this.lnameMessage = 'Please enter a last name';
  this.emailMessage = 'Please enter your email address';
  this.phoneMessage = 'Please enter your phone number';
  switch(FORM_TYPE) {
    case 'general':
    case 'event':
    case 'vendor':
    case 'press':
    case 'help':
      this.formType = FORM_TYPE;
    break;
    default:
      this.formType = 'general';
    break;
  }
  this.hideError();
  this.checkFields();
  if (this.error == true) {
  } else {
    var url_string = this.processFields();
      $pbj('#form-container').html('<h2>Thank You!</h2><p>Your request has been sent. We will contact you shortly.</p>');
      Metrics.trackEvent('form','submit','contact general');
  }
  return false;
},
checkFields: function() {
  this.error = false;
  // check first name
  if (this.validateEmpty('fname') == false) { this.error = true; this.presentError('fname',this.fnameMessage);}
  if ($pbj('#fname').val() == this.fnameMessage) { this.error = true; this.presentError('fname',this.fnameMessage);}
  // check last  name
  if (this.validateEmpty('lname') == false) { this.error = true; this.presentError('lname',this.lnameMessage);}
  if ($pbj('#lname').val() == this.lnameMessage) { this.error = true; this.presentError('lname',this.lnameMessage);}
  // check email
  if (this.validateEmail('contact-email') == false) { this.error = true; this.presentError('contact-email',this.emailMessage);}
  if ($pbj('#contact-email').val() == this.emailMessage) { this.error = true; this.presentError('contact-email',this.emailMessage);}
  //check phone
  if (this.validateEmpty('phone') == false) { this.error = true; this.presentError('phone',this.phoneMessage);}
  if ($pbj('#phone').val() == this.phoneMessage) { this.error = true; this.presentError('phone',this.phoneMessage);}
  // check country
  if (this.validatePulldown('country') == false) { this.error = true; this.presentError('country','');}
},
displayErrorMessage: function(MESSAGE,ID) {
  $pbj('#'+ID).addClass('error-message');
  $pbj('#'+ID).val(MESSAGE);
},
validateEmpty: function(ID) {
  var testobj = $pbj.trim($pbj('#'+ID).val());
  if (testobj.length < 1) {
    return false;
  }
  else {
    return true;
  }
},
validateEmail: function(ID) {
  if (this.validateEmpty(ID) == false) {return false;}
  else {
    var value = $pbj('#'+ID).val();
    return /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i.test(value);
  }
},
validatePulldown: function(ID) {
  var obj = $pbj('#'+ID);
  var value = $pbj('#'+ID+' option:selected').val();
  if (value.length < 1) {
    return false;
  } else {
    return true;
  }
},
toggleCountry: function(ID) {
  var value = $pbj('#'+ID+' option:selected').val();
  if (value == 'United States') {
    this.showStates(true);
  } else {
    this.showStates(false);
  }
},
showStates: function(SHOW) {
  if (SHOW == true) {
    $pbj('#states-row').show();
  } else {
    $pbj('#states-row').hide();
  }
},
toggleReferral: function(ID) {
  var value = $pbj('#'+ID+' option:selected').val();
  if (value == 'Other') {
    $pbj('#contact-other').show();
  } else {
    $pbj('#contact-other').hide();
  }
},
presentError: function(ID,MESSAGE) {
  $pbj('#'+ID + '-row > .check-arrow').css('visibility','visible');
  this.displayErrorMessage(MESSAGE,ID);
  $pbj('#'+ID).bind('focus',function() {
    if (this.value == MESSAGE) {this.value = ''};
  });
},
hideError: function() {
  $pbj('.check-arrow').css('visibility','hidden');
  $pbj('#contact-form .error-message').removeClass('error-message');
},
getValue: function(ID) {
  var stop_checking = false;
    // see if it's option
    testval = $pbj('#'+ID+' option:selected').val();
    if (typeof(testval) != 'undefined') {
      stop_checking = true;
    }

  // see if it's a checkbox
  if (stop_checking == false) {
    testval = $pbj('#'+ID+':checked').val();
    if (typeof(testval) != 'undefined') {
      stop_checking = true;
    }
  }
  // finally get value if all else fails
  if (stop_checking == false) {
    testval = $pbj('#'+ID).val();
  }
  if (typeof(testval) == 'undefined') {
    return '';
  } else {
    return testval;
  }
},
processFields: function() {
  // get source
  var source_value = $pbj('#referral option:selected').val();
  if (typeof(source_value) == 'undefined' || source_value == '' || source_value == 'Other') {
    source_value = $pbj('#referral-other').val();
  }

  // get date
  var month_value = $pbj('#event_month option:selected').val();
  var day_value = $pbj('#event_day option:selected').val();
  var year_value = $pbj('#event_year option:selected').val();

  if (typeof(month_value) == 'undefined') {
    month_value = '';
  }
  if (typeof(day_value) == 'undefined') {
    day_value = '';
  }
  if (typeof(year_value) == 'undefined' || year_value == '') {
    var now = new Date();
    year_value = now.getYear() + 1900;
  }

  // put the dates together
  if (month_value !='' && day_value !='') {
    final_date = month_value+'/'+day_value+'/'+year_value;
  }
  else if (month_value != '') {
    final_date = month_value+'/'+year_value;
  } else {
    final_date = year_value;
  }
  
  var formtype = 'form_type=' + this.formType;
  var formname = '&formname=' + encodeURI(this.getValue('formname'));
  var fname = '&First+Name=' + encodeURI(this.getValue('fname'));
  var lname = '&Last+Name=' + encodeURI(this.getValue('lname'));
  var email = '&Email=' + encodeURI(this.getValue('contact-email'));
  var phone = '&Phone=' + encodeURI(this.getValue('phone'));
  var source = '&Lead+Source=' + encodeURI(source_value);
  var country = '&Country+of+Residence=' + encodeURI(this.getValue('country'));
  var state = '&state=' + encodeURI(this.getValue('state'));
  var location = '&Location+of+Event=' + encodeURI(this.getValue('event_location'));
  var company = '&Organization%2FCompany=' + encodeURI(this.getValue('company'));
  var date = '&Date+of+Event=' + encodeURI(final_date);
  var comments = '&Comments+%2F+Feedback=' + encodeURI(this.getValue('comments'));
  var people = '&Number+of+Invitees=' + encodeURI(this.getValue('event_guests'));
  var final_string = formtype + formname + fname + lname + email + phone + source + country + state + location + company + date + comments + people;
// send final_string to server
$pbj.post('/wp-content/themes/preston-3.0/contact-broker.php',final_string);
//alert (final_string);
},
putValueiFrame: function(field,value) {
  $pbj('#post-iframe').contents().find('#'+field).val(value);
},
zzz: function() {
}
};

