/* Llama e-mail form handling JavaScript

*/

carrierReady = false;
deviceReady = false;
emailReady = false;
otherDeviceReady = false;

window.addEvent('domready', function(){

  var carrier = $('carrierElement');
  var device = $('deviceElement');
  var email = $('emailElement');
  carrier.value = "select";
  device.value = "none"

  /*if ($('submitButton')){
    $('submitButton').addEvent('click', function(e){
      new Event(e).stop();
      var jsonObj;
      if($('OtherDeviceInput'))
      {
        jsonObj = JSON.encode({email: email.value, otherdevice: $('OtherDeviceInput').value, device: device.value, carrier: carrier.value, notify: ($('deviceInfo').value = 'TRUE'?true:false)});
      }
      else
      {
        jsonObj = JSON.encode({email: email.value, otherdevice: null, device: device.value, carrier: carrier.value, notify: ($('deviceInfo').value = 'TRUE'?true:false)});
      }
      rqStr = 'rqststr=' + jsonObj;
      var req = new Request.JSON({
        method: 'get',
        url: "requestFormEmailHandler.php",
        onSuccess: function(responseJSON, responseText){
          jsonObj = JSON.decode(responseText);
          $('submitButton').value = "Request another e-mail";
          if($('statusMessagePane')){
            $('statusMessagePane').empty();
            if( jsonObj.status == "SUCCESS" ){
              $('statusMessagePane').setStyle('color', '#0D0');
              $('statusMessagePane').appendText( jsonObj.message );
            }
            else
            {
              $('statusMessagePane').setStyle('color', '#F00');
              $('statusMessagePane').appendText( jsonObj.message );
            }
          }
        }
      }).send(rqStr);


    });
  }*/

  submitable(device);

  if(email.value != "")
  {
    emailReady = true;
  }

  var known = new Element('input', {'type':'checkbox', 'name':'updates', 'value':'true', 'checked':'checked'});
  var other = new Element('input', {'type': 'text','id':'OtherDeviceInput', 'name':'otherDevice', 'events':{'keyup': function(event){
    if( $('OtherDeviceInput').value != "" )
    {
      otherDeviceReady = true;

    }
    else
    {
      otherDeviceReady = false;
    }
    submitable(device);
  }}});

  // <input id="emailElement" class="textbox" type="text" name="address" />
  //var emailElem = new Element('input', {'id':'emailElement', 'class':'testbox', 'type':'text', 'name':'address'});

  $('deviceInfo').empty();
  $('deviceInfo').adopt(known);
  $('deviceInfo').appendText('Notify me of updates.');

  carrier.addEvent('change', function(event){
    if( carrier.value != "select" )
    {
      carrierReady = true;
      deviceReady = true;
    }
    else
    {
      carrierReady = false;
      deviceReady = false;
    }

    switch(carrier.value)
    {
      case 'ATT':
        // iPhone, Curve 8900
        device.empty();
        device.adopt(new Element('option', {'value': 'iphone', 'html': 'iPhone 3G or 3GS'}),
                     new Element('option', {'value': 'curve8900', 'html': 'BlackBerry Curve 8900'}),
                     new Element('option', {'value': 'bold', 'html': 'BlackBerry Bold 9700'}),
		     new Element('option', {'value': 'other', 'html': 'Other device'}));
      break;

      case 'Verizon':
        // Storm, Tour, Pearl Flip
        device.empty();
        device.adopt(new Element('option', {'value': 'storm', 'html': 'BlackBerry Storm 9530'}),
                     new Element('option', {'value': 'tour', 'html': 'BlackBerry Tour 9630'}),
                     new Element('option', {'value': 'pearlflip', 'html': 'BlackBerry Pearl Flip 8230'}),
                     new Element('option', {'value': 'other', 'html': 'Other device'}));
      break;
      
      case 'Sprint':
        // Tour, pearl Flip
        device.empty();
        device.adopt(new Element('option', {'value': 'tour', 'html': 'BlackBerry Tour 9630'}),
                     new Element('option', {'value': 'other', 'html': 'Other device'}));

      break;

      case 'TMobile':
        // Curve 8900
        device.empty();
        device.adopt(new Element('option', {'value': 'curve8900', 'html': 'BlackBerry Curve 8900'}),
                     new Element('option', {'value': 'other', 'html': 'Other device'}));
      break;
      
      case 'select':
        device.empty();
        device.adopt(new Element('option', {'value': 'none', 'html': 'First select a carrier'}));
      break;

      default:
      break;
    }
    submitable(device);
  });
  
  device.addEvent('change', function(event){
    if( device.value != "none" )
    {
      deviceReady = true;
    }
    else
    {
      deviceReady = false;
    }

    if(device.value == "other"){
      $('deviceInfo').empty();
      $('deviceInfo').appendText('Enter your device:');
      $('deviceInfo').adopt(other);

      $('emailTitle').empty();
      $('emailTitle').appendText('Enter your e-mail address to be notified when Llama is available for your device:');
      
      $('formContainer').set('styles', {'height':'275px'});
    }
    else{
      $('deviceInfo').empty();
      $('deviceInfo').adopt(known);
      $('deviceInfo').appendText('Notify me of updates.');
      
      $('emailTitle').empty();
      $('emailTitle').appendText('E-mail:');
      
      $('formContainer').set('styles', {'height':'200px'});
      otherDeviceReady = false;
    }
    submitable(device);
  });

  //$('tanger').addEvent('click', function(event){
  //    location.href="http://www.tangeroutlet.com";
  //});

  email.addEvent('keyup', function(event){
    if( email.value != "" )
    {
      emailReady = true;
    }
    else
    {
      emailReady = false;
    }
    submitable(device);
  });

  //$('appStore').addEvent( 'click', function(event){
  //  location.href = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=315682105&mt=8";
  //});
});

var emailValidate = function(){
  var re = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
  m = re.exec($('emailElement').value);
  if( m == null)
  {
    return false;
  }
  else
  {
    return true;
  }
}

var submitable = function(device){
  if( carrierReady && deviceReady && emailReady )
  {
    if( device.value == "other" )
    {
      if( otherDeviceReady )
      {
        if( emailValidate() )
        {
          $('submitButton').erase('disabled');
        }
        else
        {
          $('submitButton').set('disabled', 'true');
        }
      }
      else
      {
        $('submitButton').set('disabled', 'true');
      }
    }
    else
    {
      if( emailValidate() )
      {
        $('submitButton').erase('disabled');
      }
      else
      {
        $('submitButton').set('disabled', 'true');
      }
    }
  }
  else
  {
    $('submitButton').set('disabled', 'true');
  }
}
