﻿/// <reference path="interface.js" />
/// <reference name "MicrosoftAjax.js" assembly="System.Web.Extensions" />

Type.registerNamespace('eCommerce.Web');

eCommerce.Web.fillInput = function(inputID, newValue) { 
  inputElement = $get(inputID);
  if (inputElement) {
    inputElement.value = newValue; 
  }
}

Type.registerNamespace('eCommerce.Web.DashBoard');

eCommerce.Web.DashBoard.HideLink = function(hidePanelID, hideLinkID, hideImageID, imagePath, readCookie){
    
    this.hidePanelID = hidePanelID;
    
    this.hidePanel = $get(hidePanelID);
    
    this.hideLinkID = hideLinkID;
    
    this.hideLink = $get(hideLinkID);
    
    this.hideImageID = hideImageID;
    
    this.hideImage = $get(hideImageID);
    
    this.imagePath = imagePath;
    
    this.showHide(true);
}

eCommerce.Web.DashBoard.HideLink.visible = 'block';

eCommerce.Web.DashBoard.HideLink.invisible = 'none';

eCommerce.Web.DashBoard.HideLink.showText = 'Show these hints';

eCommerce.Web.DashBoard.HideLink.hideText = 'Hide these hints';

eCommerce.Web.DashBoard.HideLink.showIconName = 'show.gif';

eCommerce.Web.DashBoard.HideLink.hideIconName = 'hide.gif';

eCommerce.Web.DashBoard.HideLink.prototype = {

  showHide : function(readCookie) {
    if(readCookie) {
      cookieValue = getCookie(this.hidePanelID);
      if(cookieValue != null) {
        if(cookieValue == eCommerce.Web.DashBoard.HideLink.invisible) {
          this.setShowButton();
        }
        else {
          this.setHideButton();
        }
      }
      else {
        this.setHideButton();
      }
    }
    else {
      if(this.hidePanel != null) {
        if(this.hidePanel.style.display == eCommerce.Web.DashBoard.HideLink.invisible) {
          this.setHideButton();
        }
        else {          
          this.setShowButton();
        }
      }
    }
  }, 
   
  setShowButton : function() {
    this.setBlockDetails(eCommerce.Web.DashBoard.HideLink.invisible, 
                          eCommerce.Web.DashBoard.HideLink.showText, 
                          this.imagePath + eCommerce.Web.DashBoard.HideLink.showIconName);
  },  

  setHideButton : function() {
    this.setBlockDetails(eCommerce.Web.DashBoard.HideLink.visible, 
                          eCommerce.Web.DashBoard.HideLink.hideText, 
                          this.imagePath + eCommerce.Web.DashBoard.HideLink.hideIconName);
  },

  setBlockDetails : function(hidePanelDisplay, hideText, imageSrc) {
       this.hidePanel.style.display = hidePanelDisplay;
       this.hideLink.innerHTML = hideText;
       this.hideImage.src = imageSrc;
       this.hideImage.alt = hideText;
       time = new Date();
       time.setTime(new Date().getTime() + 30*24*60*60*1000);
       setCookie(this.hidePanelID, hidePanelDisplay , time, '/', null, null);
  } 
}

eCommerce.Web.DashBoard.HideLink.registerClass('eCommerce.Web.DashBoard.HideLink');

eCommerce.Web.setFieldValue = function(fieldID, newValue) {
  fieldElement = $get(fieldID);
  if (fieldElement) {
    fieldElement.value = newValue;
  }
}

var savedValues = {};

eCommerce.Web.clearField = function() {
  savedValues[this.id] = this.value;
  this.value = '';
}

eCommerce.Web.restoreField = function() {
  if (this.value == '' && savedValues[this.id]) {
    this.value = savedValues[this.id];
  }
}

eCommerce.Web.addClearOnFocus = function(fieldID) {
  fieldElement = $get(fieldID);
  if (fieldElement) {
    $addHandler(fieldElement, 'focus', Function.createDelegate(fieldElement, eCommerce.Web.clearField));
    $addHandler(fieldElement, 'blur', Function.createDelegate(fieldElement, eCommerce.Web.restoreField));
  }
}

eCommerce.Web.showBasketPopup = function(basketPopupID) {
  new Effect.ScrollTo(basketPopupID, {duration: 1.0});
  if (Effect) {
    new Effect.Opacity(basketPopupID, { from: 0, to: 1, duration: 1.0 }); 
  }   
}

eCommerce.Web.hideBasketPopup = function(basketPopupID) {
  if (Effect) {
    new Effect.Opacity(basketPopupID, { from: 1, to: 0, duration: 2.0  }); 
  }
  basketPopup = $get(basketPopupID);
  if (basketPopup) {
    window.setTimeout( function() {basketPopup.style.display = 'none';}, 2000);
  }  
}

eCommerce.Web.SwitchTextMode = function(textAreaID, wysiwygID, isPlainText)
{
  if (isPlainText)
  {
    new Effect.Appear(textAreaID);
    new Effect.Fade(wysiwygID);
  }
  else
  {
    new Effect.Fade(textAreaID);
    new Effect.Appear(wysiwygID);
  }
}

eCommerce.Web.upldateAddressVisibility = function(element, visible) {
  element = $(element);
  if (element) {
    if (visible) {
      new Effect.SlideDown(element);
      //addressDiv.show();
    }
    else {        
      new Effect.SlideUp(element);
      //addressDiv.hide();
    }
  }
}

//TODO: write a function to clear all input fields inside an element
eCommerce.Web.clearInputFields = function(inputIds) {
  
  for(i = 0; i < inputIds.length; i++) {
    input = document.getElementById(inputIds[i]);
    
    if(!input) {
      continue;
    }
    
    if(typeof input.checked != "undefined") {
      input.checked = false;
    }
    
    if(typeof input.value != "undefined") {
      input.value = '';
    }
  }
}

eCommerce.Web.PopupHider = function(overlayId, frameId, elementId) {
    this.overlayId = overlayId;
    this.frameId = frameId;
    this.elementId = elementId;
    this.endRequestDelegate = Function.createDelegate(this, this.endRequestHandler);
    
    this.init();
};

eCommerce.Web.PopupHider.prototype = {
    init: function() {
        if (Sys && Sys.WebForms) {
            Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(this.endRequestDelegate);
        }
    },

    endRequestHandler: function(sender, args) {
        $(this.elementId).remove();
        $(this.overlayId).hide();
        $(this.frameId).hide();
        Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(this.endRequestDelegate);
    }
};

function UnCheckAllCheckbox(control, sectionId) {
    if (jQuery(control).is(':checked')) {
        jQuery('#' + sectionId + ' .simple input:checkbox').attr('checked', false);
    }
    else {
        if (CheckboxCheckedCount(sectionId) == 0)
            jQuery(control).attr('checked', true)
    }
}

function UnCheckFirstCheckbox(sectionId) {
    var isChecked = true;
    if (CheckboxCheckedCount(sectionId) > 0)
        isChecked = false;

    jQuery('#' + sectionId + ' input:checkbox').first().attr('checked', isChecked);
}

function CheckboxCheckedCount(sectionId) {
    return jQuery('#' + sectionId).find(".simple :checked").length;
}

function MarkNumeric() {
    jQuery(document).ready(function() {
        jQuery('.numeric').keyup(function() {
            this.value = this.value.replace(/[^0-9\.]/g, '');
        });
    })
}

function RegCarousel(carouselID) {
    jQuery(document).ready(function() {
        jQuery('#carousel' + carouselID).jcarousel();
    });
}
