/*
 * Site-Wide Support Scripts    
 * =========================
 * Scripts that must be available on each and every C21 page.   
 *
 */
//Install common browser fixes
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;
   }
   g_pBrowser.fVersionMajor = parseInt(navigator.appVersion);
} catch(err) {}

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 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 GetNodeValue(obj, tag)
{
   if( obj ) {
      var pElement = obj.getElementsByTagName(tag);
      if( pElement && pElement[0] ) {
         if( pElement[0].firstChild ) {
            return pElement[0].firstChild.nodeValue;
         }
      }
   }
   return "";
}

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 CyberCoreFindWidth(obj)
{
   var cursize = 0;
   if (document.getElementById || document.all) {
      cursize = obj.offsetWidth;
   } else if (document.layers)
      cursize = obj.width;

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

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

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

   return curtop;
}

function CyberCoreAddEvent(pElement, strEventType, pFunction, bUseCapture) {
   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 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 SetInnerHtml(strObjId, strValue) {
   var pObj = document.getElementById(strObjId);
   if( pObj ) {
      pObj.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 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;
}

