﻿
var Fatima = {
  
  base: function(){
    var uri = String(window.location.href).split('#')[0];
    if(uri.lastIndexOf('/') === (uri.length - 1)){ uri += 'default.aspx'; }
    Fatima.base = function(){ return uri; }
    return uri;
  },

  request: function(options){
    new Ajax.Request(Fatima.base(), options);
  },
  
  invoke: function(params){
    if(typeof params === 'string'){ params = { ActionType:params } }
    Fatima.request({ parameters:params });
  },
  
  showWarning: function(container, message){
    container = $(container);
    container.update('<div class="warning">' + message + '</div>');
    var target = container.firstChild;
    Fatima.attachCloseButton(target);
    Fatima.blink(target, '#E86857');
  },
  
  showMessageScreen: function(recipientId, showLoginMessage){
    new Ajax.Request('/utils/public.aspx', { parameters:{ RecipientId:recipientId, ActionType:'ShowMessageScreen' }, onComplete:Fatima.displayNotification });
  },

  Checkout: function(form){
    $('SubmitButton').hide();
    $('ProcesingMessage').show();
    $('ProccessorErrors').hide();
    Fatima.SubmitForm(form);
  },

  SubmitForm: function(form){
    return Fatima.Request(Form.serialize(form));
  },
  
  removeNumber: function(number) {
    new Ajax.Request('/store/numbers.aspx', { parameters:{ Number:number, ActionType:'RemoveNumber' }, onComplete:Fatima.displayNotification });
   },

   removeSpeedDialNumber: function (code) {
   	new Ajax.Request('/store/speed-dial.aspx', { parameters: { Code: code, ActionType: 'RemoveSpeedDialNumber' }, onComplete: Fatima.displayNotification });
   }, 
  
  removeRecurring: function() {
   if (confirm('Remove auto-replenish order?')) {
    new Ajax.Request('/store/account.aspx', { parameters:{ ActionType:'RemoveRecurring' }, onComplete:Fatima.displayNotification });  
    }
  }, 
   
  displayNotification: function(xhr) {
    Fatima.displayContent(xhr.responseText);
  },
  
  displayContent: function(content){
    var offset = document.viewport.getScrollOffsets();
    var nc = $("notification-container");
    nc.setStyle({ top:(offset.top + 120)+'px' });
    $("notification").innerHTML = content;
    nc.show();
  },
  
  closeMessageScreen: function(){
    $("notification-container").hide();
    $("notification").innerHTML = '';
  },
  
  showMessage: function(container, message){
    container = $(container);
    container.update('<div class="message">' + message + '</div>');
    var target = container.firstChild;
    Fatima.attachCloseButton(target);
    Fatima.blink(target, '#87B036');
  },
  
  attachCloseButton: function(target){
    var a = new Element('a', { 'class': 'close-message' }).update('close');
    a.onclick = function(){ Fatima.remove(this.parentNode); }
    target.appendChild(a);
  },
  
  remove: function(element){
    element = $(element);
    element.setStyle({ overflow:'hidden' });
    var anim = new YAHOO.util.Anim(element, {opacity:{ to:0 }}, 0.5);
    anim.onComplete.subscribe(function(){ element.remove(); });
    anim.animate();
  },

  blink: function(target, color){
    target = $(target);
    var originalColor = target.getStyle('background-color');
    //target.setStyle({ 'background-color':color });
    var attr1 = { backgroundColor:{ from:originalColor, to:color } }
    var attr2 = { backgroundColor:{ from:color, to:originalColor } }
    var anim1 = new YAHOO.util.ColorAnim(target, attr1, 0.2);
    var anim2 = new YAHOO.util.ColorAnim(target, attr2, 0.5, YAHOO.util.Easing.bounceBoth);
    anim1.onComplete.subscribe(function(){ anim2.animate(); });
    anim1.animate();
    
//    var attr1 = { backgroundColor:{ from:target.getStyle('background-color'), to:color } }
//    var attr2 = { backgroundColor:{ from:attr1.backgroundColor.to, to:attr1.backgroundColor.from } }
//    var anim1 = new YAHOO.util.ColorAnim(target, attr1, 0.2);
//    var anim2 = new YAHOO.util.ColorAnim(target, attr2, 0.2);
//    var anim3 = new YAHOO.util.ColorAnim(target, attr1, 0.2);
//    var anim4 = new YAHOO.util.ColorAnim(target, attr2, 0.2);
//    anim1.onComplete.subscribe(function(){ anim2.animate(); });
//    anim2.onComplete.subscribe(function(){ anim3.animate(); });
//    anim3.onComplete.subscribe(function(){ anim4.animate(); });
//    anim1.animate();
  }

}


  function op(n) {
    if (n==1) { window.open("/info/tos.aspx","FatimaPhone","menubar=no,width=500,height=500"); }
    else if (n==2) { window.open('/info/disclaimer.aspx','FatimaPhone','menubar=no,width=500,height=500'); }
    else if (n==3) { window.open('/info/privacy.aspx','FatimaPhone','menubar=no,width=500,height=500'); }
    else if (n==4) { window.open('/info/paymentacceptance.aspx','FatimaPhone','menubar=no,width=500,height=500'); }
    else if (n==5) { window.open('/info/returnpolicy.aspx','FatimaPhone','menubar=no,width=500,height=500'); }
  }

document.observe("dom:loaded", function() {

  var eml = $('LoginEmail');
  var pswd = $('LoginPassword');
  if(!(eml && pswd)){ return; }
  
  if(eml.value == ''){ eml.addClassName('field-email'); }
  if(pswd.value == ''){ pswd.addClassName('field-password'); }
  
  Event.observe(eml, 'blur', function(){ ((eml.value == '') ? Element.addClassName : Element.removeClassName)(eml, 'field-email'); });
  Event.observe(pswd, 'blur', function(){ ((pswd.value == '') ? Element.addClassName : Element.removeClassName)(pswd, 'field-password'); });
  Event.observe(eml, 'focus', function(){ Element.removeClassName(eml, 'field-email'); });
  Event.observe(pswd, 'focus', function(){ Element.removeClassName(pswd, 'field-password'); });
  
});

