var g_pBrowser = new Object();
g_pBrowser.bIsMsIe = false;
g_pBrowser.bIsFirefox = false;
g_pBrowser.bIsSafari = false;
g_pBrowser.bIsOpera = false;
g_pBrowser.fVersionMajor = 1;

//IE specific background cacheing
try {
   if( navigator.appName == "Microsoft Internet Explorer" ) {
      if (navigator.userAgent.indexOf('Opera') == -1) {
         g_pBrowser.bIsMsIe = true;
         document.execCommand("BackgroundImageCache", false, true);
      }
   }
   if (navigator.userAgent.indexOf('Opera') != -1) {
      g_pBrowser.bIsOpera = true;
   }
   if (navigator.userAgent.indexOf('Firefox') != -1) {
      g_pBrowser.bIsFirefox = true;
   }
   if (navigator.userAgent.indexOf('Safari') != -1) {
      g_pBrowser.bIsSafari = true;
   }

   if(g_pBrowser.bIsMsIe ) {
      g_pBrowser.fVersionMajor = parseFloat(navigator.appVersion.split("MSIE")[1]);
   } else {
      g_pBrowser.fVersionMajor = parseInt(navigator.appVersion);
   }
} catch(err) {}

Number.prototype.toCurrency = function() {
   return (this + '').toCurrency();
}

Number.prototype.toInterestRate = function() {
   return (this + '').toInterestRate();
}

Number.prototype.toDecimal = function() {
   if(arguments[0]!=null) {
      return (this + '').toDecimal(arguments[0]);
   } else {
      return (this + '').toDecimal();
   }
}

String.prototype.toCurrency = function(cfg, fieldname) {
   ret = this.replace(new RegExp("[a-zA-Z\\_\\-]","g"),"");
   ret = ret.replace(new RegExp("[^0-9\\.\\,\\$]","g"),"");
   if(cfg && fieldname) {
      if(cfg.fields && cfg.fields[fieldname]) {
         if(this=='' && (typeof(cfg.fields[fieldname]['default']) != 'undefined')) {
            return cfg.fields[fieldname]['default'].toCurrency();
         }
      }
   }
   ret = ((ret.indexOf("$")>-1) ? '':'$') + AddCommas(AddDecimals(ret,2));
   return ret;
}

String.prototype.stripCurrency = function() {
   return this.replace(/\$|,|\%/g,'');
}

String.prototype.toInterestRate = function(cfg, fieldname) {
   ret = this.replace(new RegExp("[a-zA-Z\\_\\-]","g"),"");
   ret = ret.replace(new RegExp("[^0-9\\.\\,\\$]","g"),"");
   if(cfg && fieldname) {
      if(cfg.fields && cfg.fields[fieldname]) {
         if(ret=='' && (typeof(cfg.fields[fieldname]['default']) != 'undefined')) {
            return cfg.fields[fieldname]['default'].toInterestRate();
         }
      }
   }
   return AddDecimals(ret,3) + '%';
}

String.prototype.toDecimal = function() {
   places = (arguments[0]) ? arguments[0]:3;
   return AddDecimals(this,places);
}

String.prototype.repeat = function(l) {
	return new Array(l+1).join(this);
}

function HasClass(pPop, strClass) {
   if (typeof pPop == 'string') {
      pPop = document.getElementById(pPop);
   }
   if( pPop && pPop.className ) {
      return pPop.className.match(new RegExp('(\\s|^)'+ strClass +'(\\s|$)'));
   }
   return false;
}

function AddClass(pPop, strClass) {
   if (typeof pPop == 'string') {
      pPop = document.getElementById(pPop);
   }
   if( pPop ) {
      if (!this.HasClass(pPop, strClass)) {
         pPop.className += " "+ strClass;
         return true;
      }
   }
   return false;
}

function RemoveClass(pPop, strClass) {
   if (typeof pPop == 'string') {
      pPop = document.getElementById(pPop);
   }
   if( pPop ) {
      if (HasClass(pPop, strClass)) {
         var reg = new RegExp('(\\s|^)'+ strClass +'(\\s|$)');
         pPop.className=pPop.className.replace(reg,' ');
         return true;
      }
   }
   return false;
}

function ToggleClass(pPop, strClass) {
   if ( HasClass(pPop, strClass) ) {
      RemoveClass(pPop, strClass);
   }
   else {
      AddClass(pPop, strClass);
   }
}

function AddClassToChildTags( pContainer, strTagType, strClass ) {
   if (typeof pContainer == 'string') {
      pContainer = document.getElementById(pContainer);
   }
   var aChildren = pContainer.getElementsByTagName(strTagType);
   if( aChildren && aChildren.length > 0 ) {
      for( var i=0; i < aChildren.length; i++ ) {
         AddClass( aChildren[i], strClass );
      }
   }
}

function RemoveClassFromChildTags( pContainer, strTagType, strClass ) {
   if (typeof pContainer == 'string') {
      pContainer = document.getElementById(pContainer);
   }
   var aChildren = pContainer.getElementsByTagName(strTagType);
   if( aChildren && aChildren.length > 0 ) {
      for( var i=0; i < aChildren.length; i++ ) {
         RemoveClass( aChildren[i], strClass );
      }
   }
}

function trim(stringToTrim) {
   return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) {
   return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim) {
   return stringToTrim.replace(/\s+$/,"");
}
function AddCommas(nStr) {
   nStr += '';
   x = nStr.split('.');
   x1 = x[0];
   x2 = x.length > 1 ? '.' + x[1] : '';
   var rgx = /(\d+)(\d{3})/;
   while (rgx.test(x1)) {
      x1 = x1.replace(rgx, '$1' + ',' + '$2');
   }
   return x1 + x2;
}

function AddDecimals(sStr,dec) {
   sStr += '';
   if(sStr=='') return '0.' + '0'.repeat(dec);
   var x = sStr.split('.');
   if(x[1] && x[1].length==dec) return sStr;
   if(x.length==1 || (x.length>1 && parseInt(x[1])==0)) {
      return x[0] + '.' + '0'.repeat(dec);
   } else if(x.length>1 && x[1].length<dec) {
      return x[0] + '.' + x[1] + '0'.repeat(dec - x[1].length);
   } else {
      ret = Math.round(parseFloat(sStr)*Math.pow(10,dec))/Math.pow(10,dec);
      return AddDecimals(ret,dec);
   }
}

function CyberCoreFindWidth(pObj) {
   if (typeof pObj == 'string') {
      pObj = document.getElementById(pObj);
   }
   var cursize = 0;
   if (document.getElementById || document.all) {
      cursize = pObj.offsetWidth;
   } else if (document.layers)
      cursize = pObj.width;

   return cursize;
}
function CyberCoreFindHeight(pObj) {
   if (typeof pObj == 'string') {
      pObj = document.getElementById(pObj);
   }
   var cursize = 0;
   if (document.getElementById || document.all) {
      cursize = pObj.offsetHeight;
   } else if (document.layers)
      cursize = pObj.height;

   return cursize;
}
function CyberCoreFindPosX(pObj) {
   if (typeof pObj == 'string') {
      pObj = document.getElementById(pObj);
   }
   var curleft = 0;
   if (document.getElementById || document.all) {
      while (pObj.offsetParent) {
         curleft += pObj.offsetLeft;
         pObj = pObj.offsetParent;
      }
   } else if (document.layers)
      curleft += pObj.x;

   return curleft;
}
function CyberCoreFindPosY(pObj) {
   if (typeof pObj == 'string') {
      pObj = document.getElementById(pObj);
   }
   var curtop = 0;
   if (document.getElementById || document.all) {
      if( !pObj.offsetParent ){
         curtop += pObj.offsetTop;
      }
      while (pObj.offsetParent) {
         curtop += pObj.offsetTop;
         pObj = pObj.offsetParent;
      }
   } else if (document.layers)
      curtop += pObj.y;

   return curtop;
}

function CyberCoreAddEvent(pElement, strEventType, pFunction, bUseCapture) {
   if( !pElement ) {
      pElement = document;
   }
   if (pElement.addEventListener) {
      pElement.addEventListener(strEventType, pFunction, bUseCapture);
      return true;
   } else if (pElement.attachEvent) {
      var r = pElement.attachEvent('on' + strEventType, pFunction);
      return r;
   } else {
      return false;
   }
}

function CyberCoreRemoveEvent( pObjectToAttachTo, strEvent, FunctionCall ) {
   if( !pObjectToAttachTo ) {
      pObjectToAttachTo = document;
   }
   if (pObjectToAttachTo.removeEventListener) {
      var strTemp = strEvent.replace(/^on/i, '');
      pObjectToAttachTo.removeEventListener(strTemp, FunctionCall, true );
      return true;
   } else if (pObjectToAttachTo.detachEvent) {
      pObjectToAttachTo.detachEvent('on' + strEvent, FunctionCall );
      return true;
   } else {
      return false;
   }
}

function GetScrollTop() {
   return this.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
}
function DisableFormElements( pContainer, bDisabled ) {
   if (typeof pContainer == 'string') {
      pContainer = document.getElementById(pContainer);
   }
   var aChildren = pContainer.getElementsByTagName('input');
   if( aChildren && aChildren.length > 0 ) {
      for( var i=0; i < aChildren.length; i++ ) {
         aChildren[i].disabled = bDisabled;
      }
   }
   aChildren = pContainer.getElementsByTagName('select');
   if( aChildren && aChildren.length > 0 ) {
      for( var i=0; i < aChildren.length; i++ ) {
         aChildren[i].disabled = bDisabled;
      }
   }
   aChildren = pContainer.getElementsByTagName('checkbox');
   if( aChildren && aChildren.length > 0 ) {
      for( var i=0; i < aChildren.length; i++ ) {
         aChildren[i].disabled = bDisabled;
      }
   }

}

function GetElementsByClassName(cl) {
   var retnode = [];
   var myclass = new RegExp('\\b'+cl+'\\b');
   var elem = this.getElementsByTagName('*');
   for (var i = 0; i < elem.length; i++) {
      var classes = elem[i].className;
      if (myclass.test(classes)) retnode.push(elem[i]);
   }
   return retnode;
}
function GetEventInformation(e) {
   var pEventInfo = new Object();
        var posx = 0;
        var posy = 0;
        if (!e) var e = window.event;
        if (e.pageX || e.pageY)         {
                posx = e.pageX;
                posy = e.pageY;
        }
        else if (e.clientX || e.clientY)        {
                posx = e.clientX + document.body.scrollLeft
                        + document.documentElement.scrollLeft;
                posy = e.clientY + document.body.scrollTop
                        + document.documentElement.scrollTop;
        }

   pEventInfo.iPosX = posx;
   pEventInfo.iPosY = posy;
   pEventInfo.pTarget = e.relatedTarget || e.fromElement;

        return pEventInfo;
}

function FormDataEnteredInContainer(pContainer) {
   if (typeof pContainer == 'string') {
      pContainer = document.getElementById(pContainer);
   }
   if( !pContainer ) {
      return false;
   }
   var bDataEntered = false;
   var aChildren = pContainer.getElementsByTagName('input');
   for( var i=0; i < aChildren.length; i++ ) {
      if( (aChildren[i].type == 'text'  || aChildren[i].type == 'hidden') && aChildren[i].name != 'svf' ) {
         if( aChildren[i].strDefaultValue ) {
            if( !((aChildren[i].value == aChildren[i].strDefaultValue) || (aChildren[i].value == '')) ) bDataEntered = true;
         } else {
            if( aChildren[i].value != '' ) bDataEntered = true;
         }
      }
      return bDataEntered;
   }
   return false;
}

function clearDefaultValue(e) {
   var pFormField = window.event ? window.event.srcElement : e ? e.target : null;
   if (!pFormField) return;
   if (pFormField.value == pFormField.strDefaultValue) {
      pFormField.value = '';
   }
}
function setDefaultValue(e) {
   var pFormField = window.event ? window.event.srcElement : e ? e.target : null;
   if (!pFormField) return;

   if (pFormField.value == '' && pFormField.strDefaultValue ) {
      pFormField.value = pFormField.strDefaultValue;
   }
}

function BindDefaultValue( strFormId, strFieldName, strDefaultValue ) {
   var pForm = document.getElementById( strFormId );
   if( pForm ) {
      var aChildren = pForm.getElementsByTagName('input');
      if( aChildren && aChildren.length > 0 ) {
         for( var i=0; i < aChildren.length; i++ ) {
            if( aChildren[i].name == strFieldName ) {
               aChildren[i].strDefaultValue = strDefaultValue;
               CyberCoreAddEvent(aChildren[i], 'focus', clearDefaultValue, false);
               CyberCoreAddEvent(aChildren[i], 'blur',  setDefaultValue, false);
            }
         }
      }
   }
}

function RemoveAllDefaultData( pForm ) {
   if( pForm ) {
      for( var i=0; i < pForm.elements.length; i++ ) {
         if( pForm.elements[i].type == 'text'  || pForm.elements[i].type == 'hidden' ) {
            if( pForm.elements[i].strDefaultValue ) {
               if( pForm.elements[i].value == pForm.elements[i].strDefaultValue ) {
                  pForm.elements[i].value = '';
               }
            }
         }
      }
   }
}

function InitializeDefaults( pForm ) {
   if (typeof pForm == 'string') {
      pForm = document.getElementById(pForm);
   }
   if( pForm ) {
      for( var i=0; i < pForm.elements.length; i++ ) {
         if( pForm.elements[i].type == 'text'  || pForm.elements[i].type == 'hidden' ) {
            if( pForm.elements[i].strDefaultValue ) {
               if (pForm.elements[i].value == '' && pForm.elements[i].strDefaultValue ) {
                  pForm.elements[i].value = pForm.elements[i].strDefaultValue;
               }
            }
         }
      }
   }
}

function SetInnerHtml(pContainer, strValue) {
   if (typeof pContainer == 'string') {
      pContainer = document.getElementById(pContainer);
   }
   if( pContainer ) {
      pContainer.innerHTML = strValue;
   }
}
function KeystokeWasEnter(e) {
   var iCharCode = (e.which == undefined) ? e.keyCode : e.which;
   if (iCharCode == 13) {
      return true;
   }
   return false;
}

function SetOnEmpty(strObjId, strValue) {
   var pObj = document.getElementById(strObjId);
   if( pObj ) {
      if( pObj.value == '' ) {
         pObj.value = strValue;
      }
   }
}

function GetHiddenFieldIntValue( strField, iDefault ) {
   if( iDefault == null ) {
       iDefault = 0;
   }
   var pField = document.getElementById(strField);
   if( pField ) {
      if( pField.value != '' ) {
         return parseInt( pField.value );
      }
   }
   return iDefault;
}

function GetUserFieldIntValue( strField, iDefault ) {
   if( iDefault == null ) {
       iDefault = 0;
   }
   var pField = document.getElementById(strField);
   if( pField ) {
      if( pField.value != '' ) {
         return parseInt( pField.value.replace(/[^0-9\.]+/g,'') );
      }
   }
   return iDefault;
}

function GetHiddenFieldFloatValue( strField ) {
   var pField = document.getElementById(strField);
   if( pField ) {
      if( pField.value != '' ) {
         return parseFloat( pField.value );
      }
   }
   return 0;
}
//---------------------------------------------------------------------||
// FUNCTION:    getCookieVal                                           ||
// PARAMETERS:  offset                                                 ||
// RETURNS:     URL unescaped Cookie Value                             ||
// PURPOSE:     Get a specific value from a cookie                     ||
//---------------------------------------------------------------------||
function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);

   if ( endstr == -1 )
      endstr = document.cookie.length;
   return(unescape(document.cookie.substring(offset, endstr)));
}

//---------------------------------------------------------------------||
// FUNCTION:    GetCookie                                              ||
// PARAMETERS:  Name                                                   ||
// RETURNS:     Value in Cookie                                        ||
// PURPOSE:     Retrieves cookie from users browser                    ||
//---------------------------------------------------------------------||
function GetCookie (name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;

   while ( i < clen ) {
      var j = i + alen;
      if ( document.cookie.substring(i, j) == arg ) return(getCookieVal (j));
      i = document.cookie.indexOf(" ", i) + 1;
      if ( i == 0 ) break;
   }

   return(null);
}

//---------------------------------------------------------------------||
// FUNCTION:    SetCookie                                              ||
// PARAMETERS:  name, value, expiration date, path, domain, security   ||
// RETURNS:     Null                                                   ||
// PURPOSE:     Stores a cookie in the users browser                   ||
//---------------------------------------------------------------------||
function SetCookie (name,value,expires,path,domain,secure) {
   document.cookie = name + "=" + escape (value) +
                     ((expires) ? "; expires=" + expires.toGMTString() : "") +
                     ((path) ? "; path=" + path : "") +
                     ((domain) ? "; domain=" + domain : "") +
                     ((secure) ? "; secure" : "");
}


function SubmitFormOnEnter(e, pField) {
   if( pField ) {
      if( KeystokeWasEnter(e)) {
         if( pField.form ) {
            if( pField.form.onsubmit ) {
               pField.form.onsubmit();
            }
            pField.form.submit();
         }
      }
   }
}

function callFunctionOnEnter(e, func) {
    var keynum;
    var keychar;
    if(window.event){ //IE
        keynum = e.keyCode;
    } else if(e.which){ // Netscape/Firefox/Opera
      keynum = e.which;
    }
    if( keynum == 13 ) {
        func();
        return false;
    }
    return true;
}

//Get a DOM object
function obj(strObject) {
   if (typeof strObject == 'string') {
      return document.getElementById(strObject);
   }
   return strObject;
}

function ResetAllFormData( pForm ) {
   var bPreviousState = g_bIgnoreQsSearchInput;
   g_bIgnoreQsSearchInput = true;
   if( pForm ) {
      for( var i=0; i < pForm.elements.length; i++ ) {
         if( (pForm.elements[i].type == 'text'  || pForm.elements[i].type == 'hidden') && pForm.elements[i].name != 'svf' ) {
            if( pForm.elements[i].strDefaultValue ) {
               pForm.elements[i].value = pForm.elements[i].strDefaultValue;
            } else {
               if( pForm.elements[i].name != 'svf' ) {
                  pForm.elements[i].value = '';
               }
            }
         } else if( pForm.elements[i].type == 'checkbox') {
            pForm.elements[i].checked = false;
         } else if( pForm.elements[i].type == 'select-one') {
            pForm.elements[i].selectedIndex = 0;
         }
      }
   }
   g_bIgnoreQsSearchInput = bPreviousState;
}

function OpenPopupWindow( url, name, widgets, openerUrl ) {
   var opened = false;
   var host = location.hostname;
   var popupWin = window.open( url, name, widgets );
   if( popupWin ){
      opened = true;
   }
   if ( popupWin && popupWin.opener ) {
      if ( openerUrl ) {
         popupWin.opener.location = openerUrl;
         popupWin.focus();
      }
      popupWin.opener.top.name = "opener";
   }

   return !opened;
}

//Returns FALSE on success, popup opened
function OpenInPagePopup( strUrl, strTitle, iWidth, iHeight ) {
   try {
      var pMask = obj('popupmask');
      var pContainer = obj('popupcontainer');
      SetInnerHtml('popuptitle', strTitle);
      var strContents = '<iframe id="popupiframe" src="'+strUrl+'" width="'+iWidth+'" height="'+iHeight+'" scrolling="auto" frameborder="0"></iframe>';
      SetInnerHtml('popupcontents', strContents);
      pMask.style.display = "block";
      pContainer.style.display = "block";
      pContainer.style.height = iHeight + 28 + 'px';

      pContainer.style.left = '50%';
      pContainer.style.marginLeft = '-' + parseInt(iWidth/2) + 'px';
      pContainer.style.width = iWidth + 'px';

      if( !g_pBrowser.bIsMsIe || g_pBrowser.fVersionMajor > 6  ) {
         try {
            pMask.style.position = "fixed";
            //Let user's scroll who are on small screens...
            //pContainer.style.position = "fixed";
         } catch( e ) {
         }
      }


      return false;
   } catch( e ) {
   }
   return true;
}

function CloseInPagePopup() {
   try {
      obj('popupcontainer').style.display = "none";
      obj('popupmask').style.display = "none";
      SetInnerHtml('popupcontents', '');
      return false;
   } catch( e ) {
   }
   return true;
}

function GotoAnchorOnDropDown( dropDownBox ) {
   var anchorName = dropDownBox.options[ dropDownBox.selectedIndex ].value;
   window.location.hash = anchorName;
}

function ThereCanBeOnlyOne( pContainer, pIgnore ) {
   if (typeof pContainer == 'string') {
      pContainer = document.getElementById(pContainer);
   }
   aChildren = pContainer.getElementsByTagName('input');
   if( aChildren && aChildren.length > 0 ) {
      for( var i=0; i < aChildren.length; i++ ) {
         if ( aChildren[i] != pIgnore ) {
            aChildren[i].checked = false;
         }
      }
   }
}

function getIndicator(color,strContent) {
   strAppend = (arguments[2]) ? arguments[2]:'';
   strContent = (strContent=='spaceimage') ? '<img src="../ImagesResource/layout/spacer.gif" width="16" height="16">':strContent;
   return '<DIV STYLE="margin-left:10px;background-color:'+color+';border:solid black 1px;display:inline;vertical-align:middle;margin-right:5px;padding:2px;">'+strContent+'</DIV>'+strAppend;
}

function toggleDiv() {
   for(i=0;i<arguments.length;i++) {
      if(document.getElementById(arguments[i])) {
         document.getElementById(arguments[i]).style.display = (document.getElementById(arguments[i]).style.display=='block') ? 'none':'block';
      }
   }
}


