﻿	
//positining functions	
function getRealLeft(el) {
    xPos = el.offsetLeft;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function getRealTop(el) {
    yPos = el.offsetTop;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
    alert(yPos);
}	
	
function CharacterCount_Truncate(txtIn,txtOut,max){
    alert
    var ll = String
    ll = txtIn.value.length
    
    if (ll > max) {
        txtIn.value = txtIn.value.substr(0, max);
    }
    
    if (txtOut != null) {
        document.getElementById(txtOut).innerHTML = ll
    }
    return ll
}		
	
function CharacterCount(txtIn,txtOut){
    var ll = String
    ll = txtIn.length
   
    if (txtOut != null) {
        document.getElementById(txtOut).innerHTML = ll
    }
    return ll
}	
	
function rollover(img, onoff, permanent) {
    str = img.src
    if (onoff) {
        if (str.indexOf('.jpg') >0) {
            str = str.substring(0,str.length-4) + 'b.jpg'
        } else {
            str = str.substring(0,str.length-4) + 'b.gif'
        }
    } else {
        if (str.indexOf('.jpg') >0) {
            str = str.substring(0,str.length-5) + '.jpg'
        } else {
            str = str.substring(0,str.length-5) + '.gif'
        }
    }
    img.src = str
    if (permanent) {
        img.onmouseover = function() {}
        img.onmouseout = function() {}
    }
    
    
}	
	
function addLoadEvent(func) {
  var oldonload = window.onload;
  
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
	
function removeRow(tbl) {
    tbody = document.getElementById(tbl);
    tbody = tbody.getElementsByTagName("tbody")[0];
            
    if (currrow > 0) {
        tbody.removeChild(tbody.getElementsByTagName("tr")[currrow])
    }                    
    currrow = -1   
}		
	

function loadRow(tmpStr, tbl, cls,  rnum, cnum, remove, no_remove) {
    tbody = document.getElementById(tbl);
    tbody = tbody.getElementsByTagName("tbody")[0];
            
    if ((currrow > 0) && (remove)) {
        tbody.removeChild(tbody.getElementsByTagName("tr")[currrow])
    }                    
    trow = tbody.getElementsByTagName("tr")[rnum + 1];                
    
    var row = document.createElement("TR")
    var td1 = document.createElement("TD")
    row.appendChild(td1);
    td1.innerHTML = tmpStr        
    td1.colSpan = cnum
    td1.className = cls

    if ((rnum + 1) == (tbody.getElementsByTagName("tr").length)) {
       tbody.appendChild(row);
    } else {
        tbody.insertBefore(row,trow);
    }
    
    if (no_remove) {
        currrow = -1   
    } else {
        currrow = rnum + 1 
    }
}	

function row_class_change(tbl, cls,  rnum, bgcolor) {
    tbody = document.getElementById(tbl);
    tbody = tbody.getElementsByTagName("tbody")[0];
              
    trow = tbody.getElementsByTagName("tr")[rnum + 1];                
    trow.className = trow.className + ' ' + cls
    
    if (bgcolor) {
        cval = parseInt(bgcolor.substr(1,30),16)
        tds = trow.getElementsByTagName("td")
        for (var x = 0; x <= tds.length - 1; x++) {
            tds[x].style.backgroundColor = bgcolor
            
            try {
            ary = new Array()
            ary = Contrast.match(bgcolor, ['#000000', '#333333','#666666', '#999999', '#CCCCCC', '#FFFFFF'])
            
            tdata = tds[x].getElementsByTagName("a")
            for (var y = 0; y <= tdata.length - 1; y++) {
                tdata[y].style.color = ary[1];
            }
            tds[x].style.color = ary[1]
            } catch (e) {
            
            }
        }
    }
}	


function showCaption(id, onoff) {
	if (navigator.userAgent.indexOf("Safari") > 0) {return}
	
    cap = document.getElementById(id)
    if (onoff) {
        cap.style.display = 'block';
        cap.style.margin = '-' + (cap.scrollHeight - 0) + 'px 0px 0px 0px';
        cap.className = 'media_object_caption';
    } else {
        cap.style.display = 'none';
    }
}

function obscure_email(fp,sp) {
    document.write ('<a href="mailto:' + fp + '@' + sp + '">' + fp + '@' + sp + '</a>')
}
	


function append_action (str) {
    document.forms[0].action = str
}

function popup_window(url,h,w) {
    var aw, aah
    aw = screen.availWidth;
    ah = screen.availHeight;

    var popW = w, popH = h;
    var leftPos = (aw-popW)/2, topPos = (ah-popH)/2;
    window.open(url,'popup','scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos);
}

function animate(onOrOff, emt, pos, neg) {
	button = document.getElementById(emt)

	if (parseInt(button.style.top) < 0) {
		ddtimer = window.setTimeout("moveButton(1,'" + emt + "'," + pos + "," + neg + ");", 0, "JavaScript");
	} else {
		ddtimer = window.setTimeout("moveButton(0,'" + emt + "'," + pos + "," + neg + ");", 0, "JavaScript");
	}
}

function moveButton(onOrOff, emt, pos, neg) {
	button = document.getElementById(emt)

    //if (!confirm('continue')) return false
	if (onOrOff == 1) {
		if (parseInt(button.style.top) < pos) {
			button.style.top = parseInt(button.style.top) + 10 + 'px';
		    ddtimer = window.setTimeout("moveButton(1,'" + emt + "'," + pos + "," + neg + ");", 0, "JavaScript");
		}
	} else {
		if (parseInt(button.style.top) > neg) {
			button.style.top = parseInt(button.style.top) - 10 + 'px';
			ddtimer = window.setTimeout("moveButton(0,'" + emt + "'," + pos + "," + neg + ");", 0, "JavaScript");
		}
	}
}


/*
  Color Contrast | Andrew Waer 
  Origins: http://juicystudio.com/services/colourcontrast.php

  Usage:
 
  // Contrast test for two colors
  // returns passing score OR false if test fails
  Contrast.test('#ffffff', '#000000');

  // find best match from one or two color sets
  // returns array containing two hex values OR false if no match
  Contrast.match('#ffffff', ['#000000', '#336699']);
  Contrast.match(['#ffffff', '#000000', '#336699']);
  Contrast.match(['#ffffff','#ffffcc'], ['#000000', '#336699']);
*/

var Contrast = function()
{
  // private functions and properties
  var _private =
  {
    min : { 
      'brightness': 50, 
      'difference': 300 
    },
    brightness : function(rgb1, rgb2){
      var b1 = ((rgb1.r * 299) + (rgb1.g * 587) + (rgb1.b * 114)) / 1000;
      var b2 = ((rgb2.r * 299) + (rgb2.g * 587) + (rgb2.b * 114)) / 1000;
      return Math.abs(Math.round(b1-b2));
    },
    difference : function(rgb1, rgb2){
      var diff = (Math.max(rgb1.r, rgb2.r) - Math.min(rgb1.r, rgb2.r)) + 
                 (Math.max(rgb1.g, rgb2.g) - Math.min(rgb1.g, rgb2.g)) + 
                 (Math.max(rgb1.b, rgb2.b) - Math.min(rgb1.b, rgb2.b));
      return Math.abs(Math.round(diff));
    },
    rgb : function(hex){
      hex = hex.replace('#','');
      var rgb = {
        r: parseInt(hex.charAt(0) + hex.charAt(1), 16),
        g: parseInt(hex.charAt(2) + hex.charAt(3), 16),
        b: parseInt(hex.charAt(4) + hex.charAt(5), 16)
      };
      return rgb;
    }
  };
  // public functions and properties
  var _public =
  {
    test : function(hex1, hex2){
      var rgb1 = _private.rgb(hex1);
      var rgb2 = _private.rgb(hex2);
      var brightness = _private.brightness(rgb1, rgb2);
      var difference = _private.difference(rgb1, rgb2);
      return (
        brightness >= _private.min.brightness && difference >= _private.min.difference
          ? ((brightness - _private.min.brightness) + (difference - _private. min.difference))
          : false
      );
    },
    match : function(hex1, hex2){
      var total_score, i, j;

      if (typeof hex1 == 'string') {hex1 = [hex1];}
      if (typeof hex2 == 'string') {hex2 = [hex2];}
      var best_match = { 
        score: 0,
        hex1:  null,
        hex2:  null
      };
      if (hex2 == null){
        for (i=0; i<hex1.length; i++){
          for (j=0; j<hex1.length; j++){
            total_score = _public.test(hex1[i], hex1[j]);
            if (total_score > best_match.score){
              best_match.score = total_score;
              best_match.hex1 = hex1[i];
              best_match.hex2 = hex1[j];
            }
          }
        }
      } 
      else {
        for (i=0; i<hex1.length; i++){
          for (j=0; j<hex2.length; j++){
            total_score = _public.test(hex1[i], hex2[j]);
            if (total_score > best_match.score){
              best_match.score = total_score;
              best_match.hex1 = hex1[i];
              best_match.hex2 = hex2[j];
            }
          }
        }
      }
      return (
        best_match.score > 0
        ? [ best_match.hex1, best_match.hex2 ]
        : false
      );
    }
  };
  return _public;
}();