﻿function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
// Opens new window in center of screen
function NewWindow(mypage,myname,w,h,scroll,resizable){
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  if(!w) w = 600;
  if(!h) h = 500;
  if(!scroll) scroll = 2;
  var settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable='+resizable+',status=1'
  if (scroll == 2) {
   scroll = 0
   settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable='+resizable+',status=1'
  }
  win = window.open(mypage,myname,settings)
  //return false;
}

function Browser(){
    this.bIsie = (document.getElementById && (document.all && !window.opera));
}
Browser.prototype.EventButton = BrowserEventButton;
Browser.prototype.EventWheel = BrowserEventWheel;

// returns an int. 1 = leftbutton, 2 = rightbutton, 3 = mousewheel click
function BrowserEventButton(iEventButton){
    if(this.bIsie){
        switch(iEventButton){
            case 4:
                return 3;
                break;
            default:
                return iEventButton;
        }
    }else{
         switch(iEventButton){
            case 0:
                return 1;
                break;
            case 1:
                return 3;
                break;
            default:
                return iEventButton;
        }    
    }
}

function BrowserEventWheel(e){
    if(this.bIsie){
        return e.wheelDelta;
    }else{
        return (e.detail > 0) ? -1 : 1;
    }
}

Browser.prototype.GetDocumentHeight = function BrowserGetDocumentHeight(oWindow){
    if(!oWindow) oWindow = window;
    return (this.bIsie) ? oWindow.document.documentElement.offsetHeight : oWindow.innerHeight;
}

Browser.prototype.GetDocumentWidth = function BrowserGetDocumentWidth(oWindow){
    if(!oWindow) oWindow = window;
    return (this.bIsie) ? oWindow.document.documentElement.offsetWidth : oWindow.innerWidth;
}

Browser.prototype.GetScrollHeight = function BrowserGetScrollHeight(oWindow){
    if(!oWindow)oWindow = window;
    return (this.bIsie) ? oWindow.document.documentElement.scrollHeight : oWindow.document.body.scrollHeight;
}

Browser.prototype.GetScrollWidth = function BrowserGetScrollWidth(oWindow){
    if(!oWindow)oWindow = window;
    return (this.bIsie) ? oWindow.document.documentElement.scrollWidth : oWindow.document.body.scrollWidth;
}

Browser.prototype.GetScrollTop = function BrowserGetScrollTop(oWindow){
    if(!oWindow)oWindow = window;
    return oWindow.document.documentElement.scrollTop;
}

Browser.prototype.CenterElement = function BrowserCenterElement(oElement){    
    oElement.style.left = ((this.GetDocumentWidth() - parseInt(oElement.style.width)) / 2) + 'px';
    oElement.style.top = ((this.GetDocumentHeight() - parseInt(oElement.style.height)) / 2) + 'px';
}

var oBrowser = new Browser();


function AddEvent(element, type, handler){
    if(document.addEventListener) {
        if(type == 'mousewheel')type = 'DOMMouseScroll';
        element.addEventListener(type, handler, false);
    }else if(document.attachEvent){
        element.attachEvent(('on' + type), handler);    
    }
}

function RemoveEvent(element, type, handler){
    if(document.removeEventListener) {
        if(type == 'mousewheel')type = 'DOMMouseScroll';
        element.removeEventListener(type, handler, false);
    }else if(document.detachEvent){
        element.detachEvent(('on' + type), handler);    
    }
}

function GetOffsetTop(oElement){
    var CalculatedTotalOffsetTop = 0;
    while (oElement.offsetParent) { 
        CalculatedTotalOffsetTop += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return CalculatedTotalOffsetTop;
}

function GetOffsetLeft(oElement){
    var CalculatedTotalOffsetLeft = 0;
    while (oElement.offsetParent) { 
        CalculatedTotalOffsetLeft += oElement.offsetLeft ; 
        oElement = oElement.offsetParent ; 
    }
    return CalculatedTotalOffsetLeft;
}

function SetOpacity(oElement, iOpacity){
    if(typeof(oElement) == 'string')oElement = document.getElementById(oElement);
    if(oBrowser.bIsie){
        oElement.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+ iOpacity +');';
    }else{
        oElement.style.opacity = (iOpacity/100);
    }
}

function doClass(obj, wN){
    if(typeof(obj) == 'string')obj = document.getElementById(obj);
    if(typeof(wN) != 'number'){
        var iNumber = parseInt(obj.className.substring((obj.className.length-1), obj.className.length));
        wN = (iNumber == 0) ? 1 : 0;
    }
    obj.className = obj.className.replace(/\d/, wN);
}

function doBackgroundImage(obj, wN){
    if(typeof(obj) == 'string')obj = document.getElementById(obj);
    if(typeof(wN) != 'number'){
        var iNumber = parseInt(obj.style.backgroundImage.substring((obj.style.backgroundImage.length - 6), obj.style.backgroundImage.length));
        wN = (iNumber == 0) ? 1 : 0;
    }
    obj.style.backgroundImage = obj.style.backgroundImage.replace(/\d/, wN);
}

function roundAccuracy(num, accuracy, posetive){
	if(posetive){
		return (accuracy-(num % accuracy)+num);
	}else{
		return (num-(num % accuracy));
	}
}

function GetIdFromElementId(sElementId){
    //var pattern = /\d/;
    var pattern = /\d{1,9}/;
    var ar = pattern.exec(sElementId);
    if(ar && ar.length){
        return ar[0];
    }else{
        return 0;
    }
}

function GetParentElementByRegExp(oElement, sRegExp){
    var oRegExp = new RegExp(sRegExp);
    
    while(oElement = oElement.parentElement){
        if(oRegExp.test(oElement.id)) return oElement;
    }
    return false;
}

function GetJavascriptFromString(sValue){
       var oRegExp = new RegExp('<script.*?>(.*?)<\/script>' ,'ig');
       var aRegExpResult;
       var sJavascript ='';       
       oRegExp.lastIndex = 0;
       
       while((aRegExpResult = oRegExp.exec(sValue)) != null){
           for(var i = 0; i < aRegExpResult.length; i++){
               if(aRegExpResult[i].indexOf('<script'))sJavascript = aRegExpResult[i] + sJavascript;
           }
       }
       return sJavascript;
}

function GetHtmlFromString(sValue){
    return sValue.replace(/<script.*?>(.*?)<\/script>/gi, '');
}
   
function SwitchFieldsInArray(bDisabledStatus, aFields){
    for(var i=0; i < aFields.length; i++){
        if(document.getElementById(aFields[i])){
            document.getElementById(aFields[i]).disabled = bDisabledStatus;
        }
    }
}
