// functions used by compendic.com
// AGC - 2009
function getElement (the_id) {
	if (typeof the_id != 'string') {
		return the_id;
	}
	if (typeof document.getElementById != 'undefined') {
		return document.getElementById(the_id);
	} else if (typeof document.all != 'undefined') {
		return document.all[the_id];
	} else if (typeof document.layers != 'undefined') {
		return document.layers[the_id];
	} else {
		return null;
	}
}

function SetContentShadowSize(Content, Shadow) {
	var _content = getElement(Content);
	var _shadow = getElement(Shadow);
	_shadow.style.height = _content.offsetHeight + 'px';
	_shadow.style.width = _content.offsetWidth + 'px';
}

function EmailTest (emf, em) {
  var p;
  p = em.indexOf('@');
  if (p > 0 && p < (em.length-1)) {
    p = em.indexOf('.',p+1);
	if (p > 0 && p < (em.length-1)) {
	  return true;
	}
  }
  alert ('Please enter a complete e-mail address (e.g. name@domain.com)');
  emf.focus();
  emf.select();
  return false;
}

function CheckOneEmail (emailfield) {
  var emf, em;
  emf = getElement(emailfield);
  em = emf.value;
  return (EmailTest (emf, em));
}
  
function CheckEmails (email1field, email2field) {
  var em1f, em2f, em1, em2;
  em1f = getElement(email1field);
  em2f = getElement(email2field);
  em1 = em1f.value;
  em2 = em2f.value;
  if (EmailTest (em1f, em1) && EmailTest (em2f, em2)) {
    if (em1 == em2) {
	  return true;
	}
	alert ('e-mail addresses do not match');
	em1f.focus();
	em1f.select();
  }
  return false;
}

function CheckPasswords (pw1field, pw2field, required) {
	var pw1f, pw2f, pw1, pw2;
	pw1f = getElement(pw1field);
	pw2f = getElement(pw2field);
	pw1 = pw1f.value;
	pw2 = pw2f.value;
	if ((required) && (pw1 == '')) {
		alert ('You must select a password (it cannot be blank)');
		pw1f.focus();
		pw1f.select();
		return false;
	}
	if (pw1 != pw2) {
		alert ('Passwords do not match');
		pw1f.focus();
		pw1f.select();
		return false;
	}
	return true;
}

function CheckUserAndPasswords (ufield, pw1field, pw2field) {
	var uf, u;
	uf = getElement(ufield);
	u = uf.value;
	if (u == '') {
		alert ('User name may not be blank');
		uf.focus();
		uf.select();
		return false;
	}
	if (u.search('[^A-Za-z0-9\-_&\.\, ]') >= 0) {
		alert ('The user name you chose contains an invalid character. Letters, digits, spaces, and few punctuation characters are accepted');
		uf.focus();
		uf.select();
		return false;
	}
	return CheckPasswords (pw1field, pw2field);
}

function ValueOf(field) {
  var f;
  f = getElement(field);
  if (f) return (f.value); else return ('');
}

function isNum(str) {
   var test = "0123456789"
   for (i=0; i <= str.length-1; i++) {
      if (test.indexOf(str.charAt(i)) == -1) return false;
   }
   return true;
}

function NumValueOf(field) {
	var v = ValueOf(field);
	if (isNum(v)) return v; else return 0;
}

function isAlpha(str) {
   var test = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
   for (i=0; i <= str.length-1; i++) {
      if (test.indexOf(str.charAt(i)) == -1) return false;
   }
   return true;
}

function Trim(s) {
	return s.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function ShowRow(rowid) {
	var r = getElement(rowid);
	if (r) r.style.display = '';
}

function HideRow(rowid) {
	var r = getElement(rowid);
	if (r) r.style.display = 'none';
}

// Thanks to Simon Willison (http://simonwillison.net/) for the following
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// Thanks to Matt Kruse (http://www.mattkruse.com/) for the following function
/* 
AnchorPosition.js
Author: Matt Kruse
Last modified: 10/11/02

DESCRIPTION: These functions find the position of an <A> tag in a document,
so other elements can be positioned relative to it.

COMPATABILITY: Netscape 4.x,6.x,Mozilla, IE 5.x,6.x on Windows. Some small
positioning errors - usually with Window positioning - occur on the 
Macintosh platform.

FUNCTIONS:
getAnchorPosition(anchorname)
  Returns an Object() having .x and .y properties of the pixel coordinates
  of the upper-left corner of the anchor. Position is relative to the PAGE.

getAnchorWindowPosition(anchorname)
  Returns an Object() having .x and .y properties of the pixel coordinates
  of the upper-left corner of the anchor, relative to the WHOLE SCREEN.

NOTES:

1) For popping up separate browser windows, use getAnchorWindowPosition. 
   Otherwise, use getAnchorPosition

2) Your anchor tag MUST contain both NAME and ID attributes which are the 
   same. For example:
   <A NAME="test" ID="test"> </A>

3) There must be at least a space between <A> </A> for IE5.5 to see the 
   anchor tag correctly. Do not do <A></A> with no space.
*/ 

// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}


// Adjust size of window to fit our content
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function resizeWindow(RefDiv, iWidth, iHeight){   
    var resizeRef = document.getElementById(RefDiv);   
    var iOuterWidth = iWidth + 10;   
    var iOuterHeight = iHeight + 29;   
    
    if (resizeRef) {   
        var iPreWidth = resizeRef.offsetWidth;   
        var iPreHeight = resizeRef.offsetHeight;   
        window.resizeTo(iPreWidth,iPreHeight);   
        var iPostWidth = resizeRef.offsetWidth;   
        var iPostHeight = resizeRef.offsetHeight;   
        iOuterWidth = iWidth + (iPreWidth-iPostWidth);   
        iOuterHeight = iHeight + (iPreHeight-iPostHeight);   
    }   
    window.resizeTo(iOuterWidth, iOuterHeight);   
}
function SizeWindowToFit(ContentElement, RefDiv) {
	var box = getElement(ContentElement);
	var pos = findPos(box);
	
	resizeWindow (RefDiv, box.offsetWidth+(2*pos[0]), box.offsetHeight+(2*pos[1]));
}; 

// Tooltip class, to allow popup of cool, rounded, fading in/out tooltips when rolling over certain screen elements.
// Thanks to Michael Leigeber at sixrevisions.com
var tooltip=function(){
 var id = 'tt';
 var top = 3;
 var left = 3;
 var maxw = 300;
 var speed = 10;
 var timer = 20;
 var endalpha = 95;
 var alpha = 0;
 var tt,t,c,b,h;
 var ie = document.all ? true : false;
 return{
  show:function(v,w){
   if(tt == null){
    tt = document.createElement('div');
    tt.setAttribute('id',id);
    t = document.createElement('div');
    t.setAttribute('id',id + 'top');
    c = document.createElement('div');
    c.setAttribute('id',id + 'cont');
    b = document.createElement('div');
    b.setAttribute('id',id + 'bot');
    tt.appendChild(t);
    tt.appendChild(c);
    tt.appendChild(b);
    document.body.appendChild(tt);
    tt.style.opacity = 0;
    tt.style.filter = 'alpha(opacity=0)';
    document.onmousemove = this.pos;
   }
   tt.style.display = 'block';
   c.innerHTML = v;
   tt.style.width = w ? w + 'px' : 'auto';
   if(!w && ie){
    t.style.display = 'none';
    b.style.display = 'none';
    tt.style.width = tt.offsetWidth;
    t.style.display = 'block';
    b.style.display = 'block';
   }
  if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
  h = parseInt(tt.offsetHeight) + top;
  clearInterval(tt.timer);
  tt.timer = setInterval(function(){tooltip.fade(1)},timer);
  },
  pos:function(e){
   var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
   var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
   tt.style.top = (u - h) + 'px';
   tt.style.left = (l + left) + 'px';
  },
  fade:function(d){
   var a = alpha;
   if((a != endalpha && d == 1) || (a != 0 && d == -1)){
    var i = speed;
   if(endalpha - a < speed && d == 1){
    i = endalpha - a;
   }else if(alpha < speed && d == -1){
     i = a;
   }
   alpha = a + (i * d);
   tt.style.opacity = alpha * .01;
   tt.style.filter = 'alpha(opacity=' + alpha + ')';
  }else{
    clearInterval(tt.timer);
     if(d == -1){tt.style.display = 'none'}
  }
 },
 hide:function(){
  clearInterval(tt.timer);
   tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
  }
 };
}();

