/*
Usage::

var palert = new com_okeefeauctioneers_alert( 'form_id',
                                              { 
                                                pType: 'fieldname',
                                                pMaxPrice: 'fieldname',
                                                pMinPrice: 'fieldname',
                                                pMinBeds: 'fieldname'
                                              }
                                            );
The map hash just maps form fields onto the alert fields in the database

*/
function com_okeefeauctioneers_alert(form_id, map, args) {  
  this.form = $('#'+form_id)
  this.map = map
  this.args = args
}

com_okeefeauctioneers_alert.prototype = {
  resource: '/alerts.php',
  
  add: function(details_form){
    errors = false;
    if(details_form.alert_name.value == ''){
      errors=true;
      $(details_form.alert_name).css({border:'solid 2px #c00'})
    }
    else{
      $(details_form.alert_name).css({border:''})
    }
    if(!details_form.alert_email.value.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/)){
      errors=true;
       $(details_form.alert_email).css({border:'solid 2px #c00'})
    }
    else{
      $(details_form.alert_email).css({border:''})
    }
    
    if(!errors){
      putdata = {}
      putdata['name'] = details_form.alert_name.value
      putdata['email'] = details_form.alert_email.value
      for(key in this.map){
        putdata[key] = eval('this.form[0].'+this.map[key]+'.value')
      }
      
      var thisObj = this
      $.ajax({
        type: 'PUT',
        url: this.resource,
        data: $.toJSON(putdata),
        complete: function(data,status){thisObj.on_add(data,status)}
      });
    }
    
  },
  on_add: function(data,status){
    if(status == 'success'){
      notice = '<div><h2>Thank You</h2><p>Your property alert is now active.</p></div>'
    }
    else{
      notice = '<h2>Sorry.</h2><p>There was a problem creating your property alert. We will try to fix this as soon as possible. Please try again later.</p>'
    }
    this.update_dialog(notice)
  },
  
  get_details: function(){
    content = $('<div></div>')
    details_form = $('<form class="com_okeeffeauctioneers_alert add"></form>')
    alertObject = this
    details_form.submit(function(){alertObject.add(this); return false})
    form_html  = '<h2>New Property Alert</h2>'
    form_html += '<p>Property Alerts are a new service from O\'Keeffe Auctioneers.</p>'
    form_html += '<p>Just enter your name and email address below and we\'ll check every new property that we add to our system for you and email with the details if it matches your search.</p>'
	form_html += '<p>Your property alerts are based on our search system. If you choose the default settings, you will be alerted to each new property we add to OKeeffeAuctioneers.com. You will be able to manage and unsubscribe to alerts from the bottom of each email alert received.</p>'
    form_html += '<p><label>Name</label><input type="text" name="alert_name" value=""/><span class="info">eg. John Power</span></p>'
    form_html += '<p><label>Email</label><input type="text" name="alert_email" value=""/><span class="info">eg. you@gmail.com</span></p>'
    form_html += '<p><input type="submit" value="Subscribe to Property Alerts" /></p>'
    content.append(details_form.append(form_html))
    this.dialog = $.facebox(content)
  },
  
  list: function(){},
  
  update: function(){},
  
  cancel: function(id){
    if(confirm('Are you sure?')){
      var thisObj = this
      $.ajax({
        type: 'DELETE',
        url: this.resource + '?id='+id,
        complete: function(data,status){thisObj.on_cancel(id, data,status)}
      });
    }
  },
  
  on_cancel: function(id, data, status){
    if(status == 'success'){
      $('li#alert_'+id).hide(600, function(){$(this).remove(); $('#alert_count').text($('ol.alerts').children().length)})
    }
    else{
      alert('Sorry, there was a problem deleting this.\n Please try again later.')
    }
  },
  
  update_dialog: function(content){
    content = $(content)
    content.css({display:'none'})
    current = $('div#facebox div.content').children()
    current.slideUp(350,function(){
      $(this).replaceWith(content)
      content.slideDown(250)
    })
  }
}