/***************************************************************************************************

  FILE:
    - _interface.js
    
  SERVICES:
    - Java Scripts of interfaces

  DESCRIPTION:
    - none

  NOTE:
    - none


  Copyright (c) 2006 - 2010 by NETAIR.ru  All Rights Reserved.

  This software is the confidential and proprietary information of NETAIR.ru
  You shall not disclose such Confidential Information and shall use it only in
  accordance with the terms of the license agreement you entered into with NETAIR.ru

***************************************************************************************************/

/***************************************************************************************************
 REVISION HISTORY
 
 date       who            describe
 --------   ---------      ------------------------------------------------------------------------- 
 10/05/08   agolubev       Initial Version
 10/30/09   agolubev       Added interface_SetPage() & interface_LoadPage()
 02/25/10   agolubev       Added delayed actions executing functionality
 02/25/10   agolubev       Fixed timer functionality
 06/23/10   agolubev       Added functionality for change opacity
****************************************************************************************************/ 

  //Change it according to interface location
  var interface_Location = '/libs/interface/';

  //Include scripts
  interface_Include('http://'+document.location.hostname+interface_Location+'js_libs/_external/calendar/calendar.js');
  interface_Include('http://'+document.location.hostname+interface_Location+'js_libs/_external/fixpng/fixpng.js');
  interface_Include('http://'+document.location.hostname+interface_Location+'js_libs/_external/nft/nft.js');
  interface_Include('http://'+document.location.hostname+interface_Location+'js_libs/ipopup.interface.js');
  interface_Include('http://'+document.location.hostname+interface_Location+'js_libs/igeneral.interface.js');

  var LinkToPage;
  var sInterface_Format = new String();
  var sInterface_Elem   = new String();
  var nInterface_Time = 0;
  var nInterface_TimerId = 0;
  var sInterface_Action = new String();
  var bInterface_ActionShowTime = true;

/*================================================
FUNCTION: interface_Include

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:

SIDE EFFECTS:
 none
================================================*/

function interface_Include(ScriptPath) 
{
  document.write('<' + 'script');
  document.write(' type="text/javascript"');
  document.write(' src="' + ScriptPath + '">');
  document.write('</' + 'script' + '>');
}  

/*================================================
FUNCTION: interface_Return

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:

SIDE EFFECTS:
 none
================================================*/
function interface_Return(MethodString)
{
  var Method = eval(MethodString);

  return Method;
}

/*================================================
FUNCTION: interface_SetPage

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:

SIDE EFFECTS:
 none
================================================*/
function interface_SetPage(sLink)
{
  LinkToPage = sLink;
}

/*================================================
FUNCTION: interface_LoadPage

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:

SIDE EFFECTS:
 none
================================================*/
function interface_LoadPage()
{
  return "location.href="+LinkToPage;
}

/*================================================
FUNCTION: interface_SetTimer

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:

SIDE EFFECTS:
 none
================================================*/
function interface_SetTimer(nSeconds, sElement)
{
  nInterface_Time   = nSeconds;
  sInterface_Elem   = sElement;
  sInterface_Format = 'H:i:s';
  interface_showTimer();
}

/*================================================
FUNCTION: interface_showTimer

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:

SIDE EFFECTS:
 none
================================================*/
function interface_showTimer()
{
  nInterface_TimerId = setTimeout('interface_showTimer()', 1000);
  interface_Timer();
}     

/*================================================
FUNCTION: interface_showTimer

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:

SIDE EFFECTS:
 none
================================================*/
function interface_TimerWithDelayedAction(sAction, nSeconds, sElement, bShowTimer)
{
  if ( sInterface_Action.length == 0 )
  {
    sInterface_Action   = sAction;
    nInterface_Time     = nSeconds;
    sInterface_Elem     = sElement;
    bInterface_ActionShowTime = bShowTimer;
    sInterface_Format   = 'H:i:s';
  }

  nInterface_TimerId = setTimeout('interface_TimerWithDelayedAction()', 1000);
  interface_Timer();
}

/*================================================
FUNCTION: interface_Timer

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:

SIDE EFFECTS:
 none
================================================*/
function interface_Timer()
{
  var nHour = 0;
  var nMin  = 0;
  var nSec  = 0;
  
  var sHour = new String();
  var sMin  = new String();
  var sSec  = new String();
  
  //alert(nInterface_Time);
  if ( nInterface_Time < 0 )
  {
    clearTimeout(nInterface_TimerId);
    nInterface_TimerId = 0;

    if ( sInterface_Action.length != 0 )
    {
      html_doc = document.getElementsByTagName('body').item(0);
      js = document.createElement('script');
      js.innerHTML = sInterface_Action;
      html_doc.appendChild(js);      
      sInterface_Action = '';
    }
    else
    {
      document.getElementById(sInterface_Elem).innerHTML = 'still in progress...';
    }

    return;
  }
    
  nHour = Math.floor( nInterface_Time / 3600 );
  nMin  = Math.floor( ( nInterface_Time - nHour * 3600 ) / 60 );
  nSec  = ( nInterface_Time - nHour * 3600 ) - nMin * 60;

  if ( nHour / 10 < 1 ) 
    sHour = '0' + nHour;
  else 
    sHour = nHour;

  if ( nMin / 10 < 1 ) 
    sMin = '0' + nMin;
  else 
    sMin = nMin;

  if ( nSec / 10 < 1 ) 
    sSec = '0' + nSec;
  else 
    sSec = nSec;

  if ( bInterface_ActionShowTime == true && nInterface_Time >= 0 )
  {
    document.getElementById(sInterface_Elem).innerHTML = sHour + ":" + sMin + ":" + sSec;
  }

  nInterface_Time--;
}

/*================================================
FUNCTION: interface_setCookie

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:
 name - имя cookie
 value - значение cookie
 [expires] - дата окончания действия cookie (по умолчанию - до конца сессии)
 [path] - путь, для которого cookie действительно (по умолчанию - документ, в котором значение было установлено)
 [domain] - домен, для которого cookie действительно (по умолчанию - домен, в котором значение было установлено)
 [secure] - логическое значение, показывающее требуется ли защищенная передача значения cookie

SIDE EFFECTS:
 none
================================================*/
function interface_setCookie(name, value, expires, path, domain, secure) 
{
  var curCookie = name + "=" + escape(value) +
                 ((expires) ? "; expires=" + expires.toGMTString() : "") +
                 ((path) ? "; path=" + path : "") +
                 ((domain) ? "; domain=" + domain : "") +
                 ((secure) ? "; secure" : "");
                
//  if (!caution || (name + "=" + escape(value)).length <= 4000)
  {
    document.cookie = curCookie;
  }
}


/*================================================
FUNCTION: interface_getCookie

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:
 name - имя считываемого cookie

SIDE EFFECTS:
 none
================================================*/
function interface_getCookie(name) 
{
  var prefix = name + "=";
  var cookieStartIndex = document.cookie.indexOf(prefix);
        
  if (cookieStartIndex == -1)
  {
    return null;
  }
  
  var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
  
  if (cookieEndIndex == -1)
  {
    cookieEndIndex = document.cookie.length;
  }

  return (document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

/*================================================
FUNCTION: interface_deleteCookie

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:
 name - имя cookie
 [path] - путь, для которого cookie действительно
 [domain] - домен, для которого cookie действительно

SIDE EFFECTS:
 none
================================================*/
function interface_deleteCookie(name, path, domain) 
{
  if ( interface_getCookie(name) ) 
  {
    document.cookie = name + "=" + 
                     ((path) ? "; path=" + path : "") +
                     ((domain) ? "; domain=" + domain : "") +
                     "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

/*================================================
FUNCTION: interface_addtoCookie

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:
 name - имя cookie
 value - значение cookie

SIDE EFFECTS:
 none
================================================*/
function interface_addtoCookie(name, value, expires, path) 
{
  var Cookie = interface_getCookie(name);
  
  if ( Cookie == null )
  {
    interface_setCookie(name, value, expires, path);
  }
  else
  {
    Cookie = unescape(Cookie) + ',' + value;
    interface_setCookie(name, Cookie, expires, path);
  }
}

/*================================================
FUNCTION: interface_CookieValueExist

DESCRIPTION:

RETURN VALUE:
  TRUE if needed value is present/ FALSE overwise

ARGUMENTS:
 name - имя cookie
 value - значение cookie

SIDE EFFECTS:
 none
================================================*/
function interface_CookieValueExist(name, value) 
{
  var Cookie = interface_getCookie(name);
  var bRetVal = false;

  if ( Cookie != null )
  {
    Cookie = unescape(Cookie);

    if ( Cookie.lastIndexOf(value) != -1 )
    {
      bRetVal = true;
    }
  }
  
  return bRetVal;
}

/*================================================
FUNCTION: interface_ShowBlock

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:


SIDE EFFECTS:
 none
================================================*/
function interface_ShowBlock(Name)
{
  document.getElementById(Name).style.display = '';  
}

/*================================================
FUNCTION: interface_HideBlock

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:


SIDE EFFECTS:
 none
================================================*/
function interface_HideBlock(Name)
{
  document.getElementById(Name).style.display = 'none';  
}

/*================================================
FUNCTION: interface_setElementOpacity

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:


SIDE EFFECTS:
 none
================================================*/
function interface_setElementOpacity(sElemId, nOpacity)
{
  var opacityProp = interface_getOpacityProperty();
  var pElement = document.getElementById(sElemId);

  if ( !pElement || !opacityProp) 
  {
    return; // Если не существует элемент с указанным id или браузер не поддерживает ни один из известных функции способов управления прозрачностью
  }

  if ( opacityProp == "filter" )  // Internet Exploder 5.5+
  {
    // Если уже установлена прозрачность, то меняем её через коллекцию filters, иначе добавляем прозрачность через style.filter
    var oAlpha = pElement.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
    
    if ( oAlpha ) 
    {
      oAlpha.opacity = nOpacity;
    }
    else 
    {
      pElement.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")"; // Для того чтобы не затереть другие фильтры используем "+="
    }
  }
  else // Другие браузеры
  { 
    nOpacity = nOpacity / 100;
    pElement.style[opacityProp] = nOpacity;
  }
}

/*================================================
FUNCTION: interface_getOpacityProperty

DESCRIPTION:

RETURN VALUE:

ARGUMENTS:


SIDE EFFECTS:
 none
================================================*/
function interface_getOpacityProperty()
{
  if (typeof document.body.style.opacity == 'string') // CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9)
  {
    return 'opacity';
  }
  else if (typeof document.body.style.MozOpacity == 'string') // Mozilla 1.6 и младше, Firefox 0.8 
  {
    return 'MozOpacity';
  }
  else if (typeof document.body.style.KhtmlOpacity == 'string') // Konqueror 3.1, Safari 1.1
  {
    return 'KhtmlOpacity';
  }
  else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) // Internet Exploder 5.5+
  {
    return 'filter';
  }

  return false; //нет прозрачности
}

function interface_LoadImage(ImgId, location)
{
  document.getElementById(ImgId).src = location;
}

function interface_incInner(Id)
{
  var nValue = document.getElementById(Id).innerHTML;

  nValue = (nValue * 1 + 1);

  return ( document.getElementById(Id).innerHTML = nValue );
}

function interface_decInner(Id)
{
  var nValue = document.getElementById(Id).innerHTML;

  nValue = (nValue * 1 - 1);

  if ( nValue < 0 )
  {
    nValue = 0;
  }

  return ( document.getElementById(Id).innerHTML = nValue );
}

function interface_incValue(Id)
{
  var nValue = document.getElementById(Id).value;

  nValue = (nValue * 1 + 1);

  return ( document.getElementById(Id).value = nValue );
}

function interface_decValue(Id)
{
  var nValue = document.getElementById(Id).value;

  nValue = (nValue * 1 - 1);

  if ( nValue < 0 )
  {
    nValue = 0;
  }

  return ( document.getElementById(Id).value = nValue );
}

function interface_setInner(Id, nNewValue, bReplace, bFormat)
{
  var nValue = document.getElementById(Id).innerHTML;
  //var nNewValueStr = new String();

  if ( nNewValue == null )
  {
    nNewValue = 0;
  }

  nValue    = nValue.replace(" ", "");

  //nNewValueStr = nNewValue.toString();

  nNewValue = Number((nNewValue.toString()).replace(" ", ""));

  if ( !bReplace )
  {
    ( bFormat ) ? document.getElementById(Id).innerHTML = interface_Formatted( (nValue*1) + (nNewValue*1) ) :
                  document.getElementById(Id).innerHTML = (nValue*1) + (nNewValue*1);
  }
  else
  {
    ( bFormat ) ? document.getElementById(Id).innerHTML = interface_Formatted(nNewValue) :
                  document.getElementById(Id).innerHTML = nNewValue;
  }
  
  return document.getElementById(Id).innerHTML;
}

function interface_Formatted(Value)
{
  var num = new NumberFormat();
  
  num.setInputDecimal('.');
  num.setNumber(Value);
  num.setPlaces('2', false);
  num.setCurrencyPosition(num.LEFT_OUTSIDE);
  num.setNegativeFormat(num.LEFT_DASH);
  num.setNegativeRed(false);
  num.setSeparators(true, ' ', ' ');

  return num.toFormatted();   
}

function interface_SetActiveImg(ImgId, sStrCmp, sImgOn)
{
  Cookie = getCookie('eshop_uri');

  if ( Cookie != null && Cookie.length )
  {
    //alert(pNewImg.complete);
    if ( sStrCmp == Cookie )
    {
      document.getElementById(ImgId).src = sImgOn;
    }
  }
}
