function cookieSet(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) +
		((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}
function cookieGet(c_name) {
	if (document.cookie.length > 0) {
		var c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			var c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}
 function DateFromIso(dIso) {
	//get a js date from an ISO date (YYYYMMDD no delimiters)
	//ISO dates are acceptable to MSSQL and are used when passing dates around in the query string or passing to a stored proc
	var ds = dIso.substr(0, 4) + "/" + dIso.substr(4, 2) + "/" + dIso.substr(6, 2);
	return new Date(ds);
}
function DateToIso(d) {
	//get an ISO representation of a date
	return d.getFullYear() + padLeading(d.getMonth() + 1, 2, "0") + padLeading(d.getDate(), 2, "0");
}
function DateDiff(d1, d2) {
	//number of days between two dates - rounded up to complete days
	//always returns positive number (abs())
	//daylight savings won't affect the calc (rounded up)
	var len = Math.abs(Math.round((d2.getTime() - d1.getTime()) / 86400000));
	return len;
}
function DateAdd(dt, n)
//add n days to date dt
{
	var dy = dt.getFullYear();
	var dm = dt.getMonth();
	var dd = dt.getDate() + n;
	return new Date(dy, dm, dd)
}
function DateDaysToMonthEnd(dt)
// returns number of days till end of month from date dt
{
	var d1 = new Date(dt.getFullYear(), dt.getMonth(), 1)
	d1.setMonth(d1.getMonth() + 1);
	return DateDiff(dt, d1)
}
function DateToday() {
	//returns start of today
	var today = new Date();
	today.setHours(0);
	today.setMinutes(0);
	today.setSeconds(0);
	today.setMilliseconds(0);
	return today;
}

function debugTrace(msg, append){
	var db =document.getElementById("debug");
	if (db) if (append) { db.innerHTML = db.innerHTML + msg } else { db.innerHTML = msg }
}

function elementOpacitySet(element, opacity) {
	//all other browsers
	element.style.opacity = opacity
	// ie8
	element.style.filter = '"progid:DXImageTransform.Microsoft.Alpha(Opacity=' + opacity * 100 + ')";'
	// ie7
	element.style.filter = 'alpha(opacity = ' + opacity * 100 + ')';
}

function elementPos(o) {
	/* return the position of the element o, relative to it's absolute/relative positioned parent element */
	var oX = 0;
	var oY = 0;
	if (o.offsetParent) {
		while (1) {
			oX += o.offsetLeft;
			oY += o.offsetTop;
			if (!o.offsetParent) break;
			o = o.offsetParent;
			if (o.style.position == 'relative') break;
			if (o.style.position == 'absolute') break;
		}
	} else if (o.x) {
		oX += o.x;
		oY += o.y;
	}
	return new Point(oX, oY);
}

function fadeIn(eid, fadeDelay) {
	var element = document.getElementById(eid)
	element.fadeState = -2
	fade(eid, fadeDelay)
}
function fadeOut(eid, fadeDelay) {
	var element = document.getElementById(eid)
	element.fadeState = 2
	fade(eid, fadeDelay)
}
function fade(eid, fadeDelay) {
	// fades an element in or out (toggles)
	/*'FadeState', which is a property that this function will create on the element when fade is called on that element for the first time.
	FadeState  is one of 4 numbers - 
	2 means that the element is fully opaque, 
	1 means that the element is currently fading from transparent to opaque, 
	-1 means that the element is currently fading from opaque to transparent, and 
	-2 means that the element is fully transparent. 
	If the FadeState property doesn't exist, we try and determine it from the state of the opacity css element.*/
	var element = document.getElementById(eid)
	if (element == null) return;
	element.fadeDelay = fadeDelay;
	if (element.fadeState == null) {
		if (element.style.opacity == null || element.style.opacity == '' || element.style.opacity == '1') {
			element.fadeState = 2;
		}
		else {
			element.fadeState = -2;
		}
	}
	if (element.fadeState == 1 || element.fadeState == -1) {
		element.fadeState = element.fadeState == 1 ? -1 : 1;
		element.fadeTimeLeft = element.fadeDelay - element.fadeTimeLeft;
	}
	else {
		element.fadeState = element.fadeState == 2 ? -1 : 1;
		element.fadeTimeLeft = element.fadeDelay;
		element.style.display = 'block';
		setTimeout("fadeAnimate(" + new Date().getTime() + ",'" + eid + "')", 33);
	}
}
function fadeAnimate(lastTick, eid) {
	var element = document.getElementById(eid)
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;
	if (element == null) return;
	if (element.fadeTimeLeft <= elapsedTicks) {
		elementOpacitySet(element, element.fadeState == 1 ? '1' : '0');
		element.fadeState = element.fadeState == 1 ? 2 : -2;
		if (element.fadeState == -2) element.style.display = 'none';
		return;
	}
	element.fadeTimeLeft -= elapsedTicks;
	var newOpVal = element.fadeTimeLeft / element.fadeDelay;
	if (element.fadeState == 1)
		newOpVal = 1 - newOpVal;
	elementOpacitySet(element, newOpVal);
	setTimeout("fadeAnimate(" + curTick + ",'" + eid + "')", 33);
}

function isChildOf(parent, child) {
/*********************************************************************
* http://codingrecipes.com/onmouseout-fix-on-nested-elements-javascript
* No onMouseOut event if the mouse pointer hovers a child element 
* *** Please do not remove this header. ***
* This code is working on my IE7, IE6, FireFox, Opera and Safari
* 
* Usage: 
* <div onMouseOut="fixOnMouseOut(this, event, 'JavaScript Code');"> 
*		So many childs 
*	</div>
*
* @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com
* Commented and tweaked for naming by Peter Brand 2010/07/13		
**/
	if (child != null) {
		while (child.parentNode) {
			/* navigate the parent heirarchy looking for the parent as element */
			if ((child = child.parentNode) == parent) {
				return true;
			}
		}
	}
	return false;
}

function numbersOnly(myfield, e)
// allows numbers only in an input field
// <input onkeypress="return numbersOnly(this, event)" ....
{
	var key;
	var keychar;
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
		(key==9) || (key==13) || (key==27) )
	   return true;
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	else
	   return false;
}

function OnMouseOutParent(element, event, JavaScriptcode) {
	/* Checks that the mouse has moved out of a parent element .
		For example, if a TD contains an A child element - on onmouseout event will occur as the mouse moves from the TD into its A child.
		This function will ignore that event and only fire once the target element is oustide of the TD.
	*/ 
	
	var currentMouseTarget = null; /* element which the mouse is now over */
	if (event.toElement) { /* IE */
		currentMouseTarget = event.toElement;
	} else if (event.relatedTarget) { /* W3C */
		currentMouseTarget = event.relatedTarget;
	}
	if (!isChildOf(element, currentMouseTarget) && element != currentMouseTarget) {
		/* target of mouse is now an element which is not the element above or one of its children */
		eval(JavaScriptcode);
	}
}

function padLeading(number, length, pad) {
	var a = number.toString();
	while (a.length < length) {
		a = pad + a;
	}
	return a;
}

function parentDivShow(a) {
	a.parentNode.style.left = '0px';
}
function parentDivHide(a) {
	a.parentNode.style.left = '-20000px';
}

function Point(x, y) {
	this.x = x;
	this.y = y;
}

function selectAddOption(sel, text, value) {
/*  cross-browser add option to Select element) */
	var op=document.createElement('option');
	op.text= text;
	op.value = value
	try
	{
		sel.add(op,null); // standards compliant
	}
	catch(ex)
	{
		sel.add(op); // IE only
	}
}
function selectClear(sel) {
	while (sel.options.length > 0) sel.remove(0)
}

var translationSpans 
function translationOuter(spi, lang)
/********************************************************************** 
	Google Translation API
	requires
	<script type="text/javascript" src="http://www.google.com/jsapi"></script>
	followed by google.load("language", "1");
*/
{
/* wrapper for google's callback routine - keeps track of which span to put the translated text into*/
	function tran(result)
	// function in function - aka a JavaScript Closure - http://www.jibbering.com/faq/notes/closures/
	{
		if (result.error) {
			//spi.innerHTML = result.error.message + '|' + lang + '|' + spi.innerHTML //for debug purposes only
		} else {
			spi.innerHTML = result.translation;
			spi.setAttribute("lang", lang)
		}
	};
	return tran; //expose this inner function as the function for google to callback
}
function translateSpans(lang)
// scan for any <spans lang="en"> and translate their content
// these are used to mark data from the database, that have not been translated
{
	if (lang != 'en') {
		translationSpans = document.getElementsByTagName("span");
		for (i = 0; i < translationSpans.length; ++i) {
			if (translationSpans[i].getAttribute('lang') == 'en') {
				google.language.translate(translationSpans[i].innerHTML, "en", lang, translationOuter(translationSpans[i], lang));
			}
		}
	}
}

function TextSelect(field, start, end ) {
/*http://webcloud.se/log/Selecting-text-with-JavaScript/*/
/* Selects part of text in a text box/area
Usage 
	var target = document.getElementById("target-input-or-textfield");
	TextSelect(target, 0, 20);
*/
	if (field.createTextRange) {
		/* 
		IE calculates the end of selection range based 
		from the starting point.
		Other browsers will calculate end of selection from
		the beginning of given text node.
		*/
		var newend = end - start;
		var selRange = field.createTextRange();
		selRange.collapse(true);
		selRange.moveStart("character", start);
		selRange.moveEnd("character", newend);
		selRange.select();
	}
	/* For the other browsers */
	else if (field.setSelectionRange) {
		field.setSelectionRange(start, end);
	}
	field.focus();
}

function winPopUp(url, name, w, h) {
	var win = window.open(url, name, 'width=' + w + ', height=' + h + ', menubar=no, location=no, status=no, toolbar=no, scrollbars=yes, resizable=yes')
	win.focus();
	win.resizeTo(w, h);
}
