// get the key pressed
function KeyCheck(evt) {
	var evt = (evt) ? evt : ((window.event) ? window.event : null);
	var evver = (evt.target) ? evt.target : ((evt.srcElement) ?evt.srcElement : null );
	return evt.keyCode;
}

// change letters typed to uppercase
function cUpper(cObj) {cObj.value=cObj.value.toUpperCase();}

// get the mouse x co-ordinate
function mouseX(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	if (evt.pageX) return evt.pageX;
	if (evt.clientX)
	return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	else return null;
}

// get the mouse y co-ordinate
function mouseY(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	if (evt.pageY) return evt.pageY;
	else if (evt.clientY)
	return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	else return null;
}

// get the element the event targets
function getTarget(e) {
	var targ=0;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
	return targ;
}

// get absolute X position of element
function getAbsX( oElement ) {
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

// get absolute Y position of element
function getAbsY( oElement ) {
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

// setup timer
function setCountdown(tdate, tdate_ff, wid, page) {
	
	active = true;
	stepper = -1;
	leadingZero = true;
	cformat = "%%H%%:%%M%%.%%S%%";
			
	stepper = Math.ceil(stepper);
	if (stepper == 0) active = false;
	timeperiod = (Math.abs(stepper)-1)*1000 + 990;
	
	var dnow = new Date();
	
	var dthen = new Date(tdate);
	
	if (dthen == "Invalid Date") {  // fix for firefox
		dthen = new Date(tdate_ff);	
	}
	
	if (stepper>0) {ddiff = new Date(dnow-dthen);} else {ddiff = new Date(dthen-dnow);}
	
	gsecs = Math.floor(ddiff.valueOf()/1000);
	
	CountTime(gsecs, tdate, active, stepper, leadingZero, cformat, timeperiod, wid, page);
}

// get the propeties of a object

function inspect(obj, maxLevels, level) {
  var str = '', type, msg;

    // Start Input Validations
    // Don't touch, we start iterating at level zero
    if(level == null)  level = 0;

    // At least you want to show the first level
    if(maxLevels == null) maxLevels = 1;
    if(maxLevels < 1)     
        return '<font color="red">Error: Levels number must be > 0</font>';

    // We start with a non null object
    if(obj == null)
    return '<font color="red">Error: Object <b>NULL</b></font>';
    // End Input Validations

    // Each Iteration must be indented
    str += '<ul>';

    // Start iterations for all objects in obj
    for(property in obj)
    {
      try
      {
          // Show "property" and "type property"
          type =  typeof(obj[property]);
          str += '<li>(' + type + ') ' + property + 
                 ( (obj[property]==null)?(': <b>null</b>'):('')) + '</li>';

          // We keep iterating if this property is an Object, non null
          // and we are inside the required number of levels
          if((type == 'object') && (obj[property] != null) && (level+1 < maxLevels))
          str += inspect(obj[property], maxLevels, level+1);
      }
      catch(err)
      {
        // Is there some properties in obj we can't access? Print it red.
        if(typeof(err) == 'string') msg = err;
        else if(err.message)        msg = err.message;
        else if(err.description)    msg = err.description;
        else                        msg = 'Unknown';

        str += '<li><font color="red">(Error) ' + property + ': ' + msg +'</font></li>';
      }
    }

      // Close indent
      str += '</ul>';

    return str;
}

function getClientWidth() {
	
	cwidth = window.innerWidth ? window.innerWidth : (document.documentElement ? document.documentElement.clientWidth : 0);
	if (document.body && cwidth == 0) {document.body.clientWidth;}
	return cwidth;
}
function getClientHeight() {
	
	cheight = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : 0);
	if (document.body && cheight == 0) {document.body ? document.body.clientHeight : 0}
	
	return cheight;
}

// create new div element (id = new div id, parent = the parent element (id) of the div (opt), html = html code to put in the div (opt))
function createDiv (id, parent, html) {

	// check if div already exists, if doesn't then create it
	
	if (!document.getElementById(id)) {
		var newdiv = document.createElement('div');

		newdiv.setAttribute('id', id);

		if (html) {newdiv.innerHTML = html;} else {newdiv.innerHTML = "    ";}
		// check if div is to belong to parent element
		if (parent) {
    
			if (document.getElementById(parent)) {
				// store the div in the parent element
				document.getElementById(parent).appendChild(newdiv);
			} else {
				// store div in body
				document.body.appendChild(newdiv);
			}
		
		} else {
			// store div in body
			document.body.appendChild(newdiv);
		}
	}
}

// remove div element (div is the id of the div)
function removeDiv(div){
	// if div exists the remove it
	
	if (document.getElementById(div)) {
		// find the parent element of the div and remove the child div
		document.getElementById(div).parentNode.removeChild(document.getElementById(div));
	}
}

function fadeElement(elem, op) {
	var opacityAsInt = op;
	var opacityAsDecimal = opacityAsInt;
	if (opacityAsInt > 100) opacityAsInt = opacityAsDecimal = 100;  else if (opacityAsInt < 0) opacityAsInt = opacityAsDecimal = 0; 
	opacityAsDecimal /= 100;
	if (opacityAsInt < 1) opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0
	elem.style.opacity = (opacityAsDecimal);
	elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
}
