﻿var isIE = false;
var isOpera = false;
var isMozilla = false;
var isSafari = false;
var isKonq = false; 

function CheckBrowserVersion()
{
  sUserAgent = navigator.userAgent;
  isOpera = sUserAgent.indexOf("Opera") > -1;
  isIE = sUserAgent.indexOf("compatible") > -1 &&
         sUserAgent.indexOf("MSIE") > -1 &&
         !isOpera;
  isKHTML = sUserAgent.indexOf("KHTML") > -1 &&
            sUserAgent.indexOf("Konqueror") > -1 &&
            sUserAgent.indexOf("AppleWebKit") > -1;
  if (isKHTML)
  {
    isSafari = sUserAgent.indexOf("AppleWebKit") > -1;
    isKonq = sUserAgent.indexOf("Konqueror") > -1;
  }
  isMozilla = sUserAgent.indexOf("Gecko") > -1 &&
              !isKHTML;
}
CheckBrowserVersion();

function attachEventToElement(eventElement, eventName, eventFunction)
{
  if (isIE)
  {        
    eventElement.attachEvent("on" + eventName, eventFunction);
  }
  else
  {
    eventElement.addEventListener(eventName, eventFunction, true);
  }
}

function getDocumentBodyElement()
{
  if (isIE || isMozilla)
  {
    return document.documentElement;
  }
  else if (isOpera)
  {
    return document.body;
  }
  else
  {
    if (document.documentElement)
    {
      return document.documentElement;
    }
    else if (document.body)
    {
      return document.body;
    }
  }
  return null;
}

function getCurrStyle(defObj)
{
  if (defObj)
  {
    if (defObj.currentStyle) 
    {
      return defObj.currentStyle;
    } 
    else if (window.getComputedStyle) 
    {
      return document.defaultView.getComputedStyle(defObj, null);
    }
  }
  return null;
}
function getCurrStyleValue(defObj, IEstyleProp, MozStyleProp) 
{
  if (defObj.currentStyle) 
  {
    return defObj.currentStyle[IEstyleProp];
  } 
  else if (window.getComputedStyle) 
  {
    return document.defaultView.getComputedStyle(defObj, null).getPropertyValue(MozStyleProp);
  }
  return null;
}
function getCurrStyleFloat(defObj)
{
  if (defObj)
  {
    if (defObj.currentStyle) 
    {
      return defObj.currentStyle.styleFloat;
    } 
    else if (window.getComputedStyle) 
    {
      return document.defaultView.getComputedStyle(defObj, null).cssFloat;
    }
  }
  return null;
}
function getStyleFloat(defObj)
{
  if (defObj)
  {
    if (defObj.style.styleFloat || (defObj.style.styleFloat == "")) 
    {
      return defObj.style.styleFloat;
    } 
    else if (defObj.style.cssFloat || (defObj.style.cssFloat == "")) 
    {
      return defObj.style.cssFloat;
    }
  }
  return null;
}
function setStyleFloat(defObj, value)
{
  if (defObj)
  {
    if (defObj.style.styleFloat || (defObj.style.styleFloat == "")) 
    {
      defObj.style.styleFloat = value;
    } 
    else if (defObj.style.cssFloat || (defObj.style.cssFloat == "")) 
    {
      defObj.style.cssFloat = value;
    }
  }
}

function YesNoCancelMsgBox(question)
{
  if (isIE || window.VBYesNoCancelMsgBox)
  {
    return VBYesNoCancelMsgBox(question);
  }
  else
  {
    return confirm(question) ? 6 : 7;
  }
}

function YesNoMsgBox(question)
{
  if (isIE || window.VBYesNoMsgBox)
  {
    return VBYesNoMsgBox(question);
  }
  else
  {
    return confirm(question) ? 6 : 7;
  }
}

function YesNoWarrningMsgBox(question)
{
  if (isIE || window.VBYesNoWarrningMsgBox)
  {
    return VBYesNoWarrningMsgBox(question);
  }
  else
  {
    return confirm(question) ? 6 : 7;
  }
}

function OKInformationMsgBox(infoText)
{
  if (isIE || window.VBOKInformationMsgBox)
  {
    VBOKInformationMsgBox(infoText);
    
  }
  else
  {
    alert(infoText);
  }
}

function OKExclamationMsgBox(infoText)
{
  if (isIE || window.VBOKExclamationMsgBox)
  {
    VBOKExclamationMsgBox(infoText);
    
  }
  else
  {
    alert(infoText);
  }
}

function isLeftButtonClick(ev)
{
  if (isIE)
  {
    return ev.button == 1;
  }
  else
  {
    return ev.button == 0;
  }
}

function isRightButtonClick(ev)
{
  return ev.button == 2;
}

function GetRelativePath(path, applicationPath)
{
  pathname = location.pathname+location.search 
  prefix = location.href.substr(0, location.href.length - pathname.length) + applicationPath;  
  if (path.indexOf(prefix) >= 0)
  {
    return "~" + path.substr(prefix.length, path.length - prefix.length);
  }
  else
  {
    return path;
  }
}
function RefreshLabel4Disable(controlID, isFormDisabled)
{
  // its necessary to refresh label because on show property page
  // (disable current page) label duplicate text
  control = document.getElementById(controlID);
  if (control)
  {
    if (isFormDisabled)
    {
      control.style.visibility = 'visible';    
    }
    else
    {
      control.style.visibility = '';    
    }
  }
}

function SetSelectControlsVisible(parentControl, value)
{
    // value - 'hidden', 'visible'
	for( var i = 0; i < parentControl.childNodes.length; i++) 
	{
	   if (parentControl.childNodes[i].tagName && 
	       (parentControl.childNodes[i].tagName.toLowerCase() == 'select'))
	   {
         parentControl.childNodes[i].style.visibility = value;
	   }
	   SetSelectControlsVisible(parentControl.childNodes[i], value);
	}
}
function HideSelectControls(parentControlID)
{
    parentControl = document.getElementById(parentControlID);
    if (parentControl)
    {
      SetSelectControlsVisible(parentControl, 'hidden');
    }
}
function ShowSelectControls(parentControlID)
{
    parentControl = document.getElementById(parentControlID);
    if (parentControl)
    {
      SetSelectControlsVisible(parentControl, 'visible');
    }
}

function ConvertStringToBool(strValue)
{
  if (strValue && (strValue == 't'))
  {
    return true;
  }
  else
  {
    return false;
  }
}

function setDiabledAllElements(parentElement, isDisabled)
{
  if ((parentElement.disabled != null) &&
      (parentElement.disabled != undefined))
  {
    parentElement.disabled = isDisabled;
  }
  if (parentElement.childNodes)
  {
    for(var i = 0; i < parentElement.childNodes.length; i++) 
    {    
      setDiabledAllElements(parentElement.childNodes[i], isDisabled)
    }
  }
}

function keywordTextBoxBlur(textBox)
{
    if (textBox == null)
        return false;
    setKeywordRequest('/Pages/EniroKeyword.aspx', textBox);
    return false;
}

function getXMLHttpRequest()
{
    var xhr;
    if (window.XMLHttpRequest)
    {
        if (!(xhr = new XMLHttpRequest())) return false;
    } else
    {
        try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e)
        {
            try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { return false; }
        }
    }
    return xhr;
}

function setKeywordRequest(url, textBox)
{
    if (textBox == null)
        return false;
    var objectType = textBox.getAttribute('objectType');
    var objectID = textBox.getAttribute('objectID');
    var objectOldValue = textBox.getAttribute('objectOldValue');
    var objectNewValue = textBox.value;
    if (objectOldValue == objectNewValue)
        return false;
    textBox.setAttribute('disabled', 'true');
    
    var xhr = getXMLHttpRequest();
    if (xhr != null)
    {
        var url = url + '?t=' + objectType + '&id=' + objectID + '&v=' + objectNewValue;
        xhr.open('GET', url, true);
        xhr.setRequestHeader('If-Modified-Since', 'Thu, 1 Jan 1970 00:00:00 GMT');
        xhr.setRequestHeader('Cache-Control', 'no-cache');
        xhr.onreadystatechange = function()
        {
            if (xhr.readyState === 4)
            {
                xhr.onreadystatechange = function() { };
                setKeywordResponse(xhr, textBox, objectNewValue);
            }
        };
        xhr.send(null);
    }
    return false;
}

function setKeywordResponse(xhr, textBox, objectNewValue)
{
    var success = false;

    if (xhr && (xhr.status == 200) && xhr.responseText && (xhr.responseText != ''))
    {
        var values = xhr.responseText.split(';');
        if (values.length && values.length >= 1)
        {
            var result = values[0].toLowerCase();
            if (result == 'ok')
            {
                success = true;
            }
            else if (result == 'fail')
            {
                success = false;
            }
        }
    }
    if (success)
    {
        textBox.setAttribute('objectOldValue', objectNewValue);
    }
    textBox.removeAttribute('disabled');
}
