var ie4 = (document.all) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById && !document.all) ? true : false;

/**
 * Do nothing :)
 */
function noop() {
	return false;
}

var mouse_X;
var mouse_Y;
var tip_active = 0;
var ie = document.all ? true : false;

if (!ie) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMouseXY;

function update_tip_pos() {
	document.getElementById('baloon').style.left = mouse_X + 20;
	document.getElementById('baloon').style.top  = mouse_Y;
}

function getMouseXY(e) {
	if (ie) { // grab the x-y pos.s if browser is IE
		mouse_X = event.clientX + document.body.scrollLeft;
		mouse_Y = event.clientY + document.body.scrollTop;
	} else { // grab the x-y pos.s if browser is NS
		mouse_X = e.pageX;
		mouse_Y = e.pageY;
	}
	if (mouse_X < 0) { mouse_X = 0; }
	if (mouse_Y < 0) { mouse_Y = 0; }
	if(tip_active) { update_tip_pos(); }
}

function baloon(which, id) {
	if(which) {
		update_tip_pos();
		tip_active = 1;
		document.getElementById('baloon').style.visibility = "visible";
		document.getElementById('baloon').innerHTML = "tooltip for "+id;
	} else {
		tip_active = 0;
		document.getElementById('baloon').style.visibility = "hidden";
	}
}

/**
 * Changes form's action url and submits the form
 * @param form - form to submit
 * @param url - a new url to submit the form to
 */
function redir(form, url) {
	form.action = url;
	form.submit();
}

function askDelete(what) {
	return confirm('Are you sure you want to delete this ' + what);
}

/**
 * Opens url replacing current url
 */
function urlOpen(url) {
	var frame = (arguments.length>1) ? arguments[1] : window;
    //if(document.images)
    //    frame.location.replace(url);
    //else
        frame.location.href = url;
}

function submitForm(formName) {
	var theform = eval('document.' + formName);
	if(theform != 'undefined') theform.submit();
	return false;
}

/**
 * Gets rid of a comma in formatted double value
 */
function cleanDouble(value) {
	var re = /\,/g;
	return value.replace(re,'');
}

// ===================================================================
// Layer show/hide functions
// ===================================================================

/**
 * Hides a layer
 * @param layer - name of the layer to be hidden
 * @param imgHide - image to hide
 * @param imgShow - image to show
 */
function hide(layer, imgHide, imgShow) {
	if(ie4) {
		document.all[layer].style.visibility = "hidden";
		document.all[layer].style.display= "none";
		if(typeof(imgHide) != 'undefined' && imgHide != '') {
			document.all[imgHide].style.visibility = "hidden";
			document.all[imgHide].style.display = "none";
		}
		if(typeof(imgShow) != 'undefined' && imgShow != '') {
			document.all[imgShow].style.visibility = "visible";
			document.all[imgShow].style.display = "block";
		}
	}
	if(ns4) {
		document.layers[layer].visibility = "hide";
	}
	if(ns6) {
		document.getElementById([layer]).style.visibility = "hidden";
		document.getElementById([layer]).style.display = "none";
		if(typeof(imgHide) != 'undefined' && imgHide != '') {
			document.getElementById([imgHide]).style.visibility = "hidden";
			document.getElementById([imgHide]).style.display = "none";
		}
		if(typeof(imgShow) != 'undefined' && imgShow != '') {
			document.getElementById([imgShow]).style.visibility = "visible";
			document.getElementById([imgShow]).style.display = "block";
		}
	}
}

/**
 * Shows a layer
 * @param layer - name of the layer to be shown
 * @param imgHide - image to hide
 * @param imgShow - image to show
 */
function show(layer, imgHide, imgShow) {
	if(ie4) {
		document.all[layer].style.visibility = "visible";
		document.all[layer].style.display= "block";
		if(typeof(imgHide) != 'undefined' && imgHide != '') {
			document.all[imgHide].style.visibility = "hidden";
			document.all[imgHide].style.display = "none";
		}
		if(typeof(imgShow) != 'undefined' && imgShow != '') {
			document.all[imgShow].style.visibility = "visible";
			document.all[imgShow].style.display = "block";
		}
	}
	if(ns4) {
		document.layers[layer].visibility = "show";
	}
	if(ns6) {
		document.getElementById([layer]).style.visibility = "visible";
		document.getElementById([layer]).style.display = "block";
		if(typeof(imgHide) != 'undefined' && imgHide != '') {
			document.getElementById([imgHide]).style.visibility = "hidden";
			document.getElementById([imgHide]).style.display = "none";
		}
		if(typeof(imgShow) != 'undefined' && imgShow != '') {
			document.getElementById([imgShow]).style.visibility = "visible";
			document.getElementById([imgShow]).style.display = "block";
		}
	}
}

var __c = 0;
function toggle(n) {
	var obj = getObj('s' + n);
	if (obj != null) {
		if (obj.style.visibility == 'visible') {
			// hide
			hide('s' + n);
		} else {
			if (__c != 0) hide('s' + __c);
			// show
			show('s' + n);
			__c = n;
		}
	}
}

function getObj(name) {
	var obj = null;
	if (document.getElementById) {
		obj = document.getElementById(name);
  	} else if (document.all) {
		obj = document.all[name];
	} else if (document.layers) {
		obj = document.layers[name];
	}
	return obj;
}

// ===================================================================
// String helper functions
// ===================================================================

/**
 * Full string trimmer - removes leading and trailing spaces
 */
function trim(str) {
	if(str == null || str == 'undefined') {
		return null;
	}
	// Match spaces at beginning and end of text and replace with null strings
	return str.replace(/^\s+/,'').replace(/\s+$/,'');
}

/**
 * Returns true if 'what' is part of 'where' using optional delimiter (default is '; ')
 */
function isPartOfString(what, where) {
	var delimiter = (arguments.length > 2) ? arguments[2] : "; ";
	return (what == where) ||
			(where.indexOf(what+delimiter) == 0) ||
			(where.indexOf(delimiter+what) == where.length-delimiter.length-what.length) ||
			(where.indexOf(delimiter+what+delimiter) != -1);
}

function formatAmount(val) {
	var v = '' + (Math.abs(val) + 0.001);
	return v.substr(0, v.indexOf('.')+3);
}

function padLeft(s, l, p) {
	while(s.length < l) s = p + s;
	return s;
}

// ===================================================================
// Date/Time helper functions
// ===================================================================

/**
 * Calculates number of days in a month 'm' of year 'y'
 */
function daysInMonth(y, m) {
    with (new Date(y, m, 1, 12)) {
        setDate(0);
        return getDate();
    }
}

/**
 * Calculates a difference between two dates. Returns an array of years, months and days
 */
function diffDate(a1, a2) {
    var dm, dd;
    dm = (12 * a1[0] + a1[1]) - (12 * a2[0] + a2[1]);
    dd = a1[2] - a2[2];
    if(dd < 0) {
        dm--;
        dd += daysInMonth(a2[0], a2[1]);
    }
    return [(dm / 12) | 0, dm % 12, dd];
}

/**
 * Returns tru if a date is valid
 */
function validDate(y, m, d) {
	with (new Date(y, m, d)) {
		return (getMonth()==m && getDate()==d);
	}
}

/**
 * Parses a date. Format: MM-DD-YYYY or MM/DD/YYYY
 */
function parseDate(q) {
	var t;
	if( (t = /^(\d\d)([-\/])(\d\d)(\2)(\d+)$/.exec(q)) == null ) {
		return -2; // bad format
	}
	for(var j=1; j<=5; j+=2) {
		t[j] = +t[j]; // convert to numbers
	}
	if(!validDate(t[5], t[1]-1, t[3])) {
		return -1; // bad value
	}
	return [ t[5], t[1], t[3] ]
}


function getExpiry(days) {
	var timeSpan = days * 24 * 60 * 60 * 1000;
	var expDate  = new Date();
	expDate.setTime( expDate.getTime() + timeSpan );
	return expDate;
}

function logout(msg) {
	if(msg == null || confirm(msg)) {
		document.cookie	= "username=; expires=" + getExpiry(0).toGMTString() + "; path=/;";
		document.cookie	= "password=; expires=" + getExpiry(0).toGMTString() + "; path=/;";
		return true;
	}
	return false;
}

function closeTip() {
	hide('sticky');
	document.cookie	= "tip=0; " +
		"expires=" + getExpiry(1).toGMTString() + "; " +
		"path=/;";
	return false;
}

//	-------------------- Cookie functions

function getCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if((!start && name != document.cookie.substring(0, name.length)) || (start == -1)) return null;
	var end = document.cookie.indexOf(";", len);
	if(end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

function setCookie(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime(today.getTime());
	if(expires) expires = expires * 1000 * 60 * 60 * 24;
	var expires_date = new Date(today.getTime() + expires);

	document.cookie = name + "=" + escape(value) +
		(expires ? ";expires=" + expires_date.toGMTString() : "") +
		(path ? ";path=" + path : "") + 
		(domain ? ";domain=" + domain : "") +
		(secure ? ";secure" : "");
}

function deleteCookie(name, path, domain) {
	if(getCookie(name)) document.cookie = name + "=" +
			(path ? ";path=" + path : "") +
			(domain ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

//	-------------------- Check if cookie is enabled
var cookieSupportEnabled = false;
setCookie('test', 'none', '', '/', '', '');
if(getCookie('test')) {
	cookieSupportEnabled = true;
	deleteCookie('test', '/', '');
}

function printTextarea(cols, rows, name) {
	document.write('<textarea' +
		(cols > 0 ? ' cols="' + cols + '" ' : '') +
		(rows > 0 ? ' rows="' + rows + '" ' : '') +
		' name="' + name + '"' +
		'></textarea>'
	);
}
