function xn_js_getElement(element) {
	if (typeof(element) == 'string') {
		return document.getElementById(element);
	}
	return element;
}

function xn_js_toggle(element, display) {
	element = xn_js_getElement(element);
	if (element.style.visibility == "hidden"){
		xn_js_show(element, display);
	}
	else {
		xn_js_hide(element);
	}
}

function xn_js_show(element, display) {
	element = xn_js_getElement(element);
	element.style.visibility="visible";
	// Change back to 'block' unless specified otherwise.
	if (typeof(display) == "undefined" || display.length == 0) {
		element.style.display="block";
	} else {
		element.style.display=display;
	}
}

function xn_js_hide(element) {
	element = xn_js_getElement(element);
	element.style.visibility="hidden";
	element.style.display="none";
}

function xn_js_focusAndSelect(element) {
	element = xn_js_getElement(element);
	if (element != null) {
		element.focus();
		element.select();
	}
}

function xn_js_changeClass(element, className) {
	element = xn_js_getElement(element);
	element.className = className;
}

function xn_js_toggleClass(element, classNameA, classNameB) {
	element = xn_js_getElement(element);
	element.className = element.className == classNameA ? classNameB : classNameA;
}

function xn_js_kill(e) {
	if (!e && window['event']) {
		e = window.event
	}
	e.cancelBubble = true;
	if (e.stopPropagation) {
		e.stopPropagation();
	}
	return false;
}

function xn_js_focusTextfield(e, defaultText, removeClass) {
	var input = e.srcElement || e.target;
	if (input.value == defaultText) {
		input.value = '';
		var newClasses = [];
		var oldClasses = input.className.split(' ');
		for (var i = 0; i < oldClasses.length; ++i) {
			var c = oldClasses[i];
			if (c && c != removeClass) {
				newClasses.push(c);
			}
		}
		input.className = newClasses.join(' ');
	}
}

function xn_js_blurTextfield(e, defaultText, addClass) {
	var input = e.srcElement || e.target;
	if (input.value == '') {
		input.className = input.className + " " + addClass;
		input.value = defaultText;
	}
}

function xn_js_submitIfEnterPressed(event, passwordFieldId, formId) {
	// In Firefox, when you start typing in the username, you will be presented with an autocompletion list.
	// If you hit Enter, we get here and the form gets submitted unintentionally. To work around this,
	// check that the password field has been filled in. [Jon Aquino 2006-01-18]
	if (event.keyCode==13 && document.getElementById(passwordFieldId).value.length > 0) {
		var formNode = document.getElementById(formId);
		if (xn_js_loginHack(formNode)) {
			formNode.submit();
			return false;
		}
	}
	return true;
}

function xn_js_requireLogin(targetUrl, loggedIn) {
	if (('' + loggedIn) != 'true') {
		scroll(0,0);
		xn_js_show('xn_entry');
		xn_js_show('xn_signin');
		xn_js_focusAndSelect('f-username');
		xn_js_getElement('xn_signin_target').value = targetUrl;
		// need to maintain dynamic targets on login errors, somehow -bmc
//		var error_target = xn_js_getElement('xn_signin_error_target');
//		if (error_target != null) {
//			alert("??" + targetUrl + "???");
//
//			error_target.value = targetUrl + "&xn_error=LOGIN";
//		}
		return false;
	}
	return true;
}

function xn_js_show_def(prefix, display) {
	xn_js_show(prefix + "-dt");
	xn_js_show(prefix + "-dd");
}

function xn_js_hide_def(prefix) {
	xn_js_hide(prefix + "-dt");
	xn_js_hide(prefix + "-dd");
}

function xn_js_loginHack(formNode) {
	if (!window.event || navigator.userAgent.indexOf("Opera") >= 0 || !document.all || navigator.appVersion.indexOf("MSIE 7.0") >= 0) {
		// not IE 6 or lower
		return true;
	}
	if (!formNode) {
		formNode = window.event.srcElement; // we know we're in IE, so this is okay :)
	}
	var query = /\?/.test(formNode.action) ? "&" : "?";
	for (var i = 0; i < formNode.elements.length; i++) {
		var elm = formNode.elements[i];
		if (!elm || elm.tagName.toLowerCase() == "fieldset") { continue; }
		var type = elm.type.toLowerCase();
		if (elm.disabled || !elm.name || elm == "file" || elm == "submit" || elm == "image" || elm == "reset" || elm == "button") { continue; }
		var name = encodeURIComponent(elm.name);
		if (type == "select-multiple") {
			for (var j = 0; j < elm.options.length; j++) {
				if (elm.options[j].selected) {
					query += encodeURIComponent(name) + "=" + encodeURIComponent(elm.options[j].value) + "&";
				}
			}
		}
		else if (type == "radio" || type == "checkbox") {
			if (elm.checked) {
				query += encodeURIComponent(name) + "=" + encodeURIComponent(elm.value) + "&";
			}
		}
		else {
			query += encodeURIComponent(name) + "=" + encodeURIComponent(elm.value) + "&";
		}
	}
	window.location = formNode.action + query.substring(0, query.length-1);
	return false;
}

var originalPageTitle = document.title;
function markAsUnsaved(id) {
	var savedStatus = document.getElementById(id);
	savedStatus.innerHTML = "*";
	document.title = originalPageTitle + "*";
}

function confirmSubmit(msg, form)
{
	var conf = confirm(msg);
	if (conf) {
		form.submit();
	}
	return false
}

function confirmSubmitWithProgressBar(msg, form, mainUrl)
{
	var conf = confirm(msg);
	if (conf) {
		form.submit();
		xn_js_showProgressBar(mainUrl);
	}
	return false
}

function showInputLimit(input, output, limit)
{
	input = xn_js_getElement(input);
	output = xn_js_getElement(output);
	var charsLeft = limit - input.value.length;
	output.style.color = (charsLeft < 0 ? "#f00" : "");
	output.innerHTML = "(" + charsLeft + " letters remaining)";
	return charsLeft > 0;
}

/***********************************************
* WinXP Progress Bar- By Brian Gosselin- http://www.scriptasylum.com/
* Script featured on Dynamic Drive- http://www.dynamicdrive.com
* Please keep this notice intact
***********************************************/
// xp_progressbar
// Copyright 2004 Brian Gosselin of ScriptAsylum.com
//
// v1.0 - Initial release
// v1.1 - Added ability to pause the scrolling action (requires you to assign
//        the bar to a unique arbitrary variable).
//      - Added ability to specify an action to perform after a x amount of
//      - bar scrolls. This requires two added arguments.
// v1.2 - Added ability to hide/show each bar (requires you to assign the bar
//        to a unique arbitrary variable).

// var xyz = xn_js_createBar(
// total_width,
// total_height,
// background_color,
// border_width,
// border_color,
// block_color,
// scroll_speed,
// block_count,
// scroll_count,
// action_to_perform_after_scrolled_n_times
// )

/* helper function added for Ning usage
 * (we've also prefixed all the functions with xn_js_)
 */
function xn_js_showProgressBar(mainUrl) {
	document.getElementById('xn_progress').style.visibility = 'visible';
	document.getElementById('xn_progress').style.display = 'block';
	bar.xn_js_togglePause();
	// Trick Safari into continuing the progress-bar animation after the click by loading an animated gif [Jon Aquino 2006-01-26]
	document.getElementById('dummyAnimatedGif').src = mainUrl + '/xnstatic/wait.gif';
	return true;
}

var w3c=(document.getElementById)?true:false;
var ie=(document.all)?true:false;
var N=-1;

function xn_js_createBar(w,h,bgc,brdW,brdC,blkC,speed,blocks,count,action){
	if(ie||w3c){
		var t='<div id="_xpbar'+(++N)+'" style="visibility:visible; position:relative; overflow:hidden; width:'+w+'px; height:'+h+'px; background-color:'+bgc+'; border-color:'+brdC+'; border-width:'+brdW+'px; border-style:solid; font-size:1px;">';
		t+='<span id="blocks'+N+'" style="left:-'+(h*2+1)+'px; position:absolute; font-size:1px">';
		for(var i=0;i<blocks;i++){
			t+='<span style="background-color:'+blkC+'; left:-'+((h*i)+i)+'px; font-size:1px; position:absolute; width:'+h+'px; height:'+h+'px; '
			t+=(ie)?'filter:alpha(opacity='+(100-i*(100/blocks))+')':'-Moz-opacity:'+((100-i*(100/blocks))/100);
			t+='"></span>';
		}
		t+='</span></div>';
		document.write(t);
		var bA=(ie)?document.all['blocks'+N]:document.getElementById('blocks'+N);
		bA.bar=(ie)?document.all['_xpbar'+N]:document.getElementById('_xpbar'+N);
		bA.blocks=blocks;
		bA.N=N;
		bA.w=w;
		bA.h=h;
		bA.speed=speed;
		bA.ctr=0;
		bA.count=count;
		bA.action=action;
		bA.xn_js_togglePause=xn_js_togglePause;
		bA.showBar=function(){
			this.bar.style.visibility="visible";
		}
		bA.hideBar=function(){
			this.bar.style.visibility="hidden";
		}
		// bA.tid=setInterval('xn_js_startBar('+N+')',speed);
		bA.tid = 0
		return bA;
	}
}
function xn_js_startBar(bn){
	var t=(ie)?document.all['blocks'+bn]:document.getElementById('blocks'+bn);
	if(parseInt(t.style.left)+t.h+1-(t.blocks*t.h+t.blocks)>t.w){
		t.style.left=-(t.h*2+1)+'px';
		t.ctr++;
		if(t.ctr>=t.count){
			eval(t.action);
			t.ctr=0;
		}
	}else t.style.left=(parseInt(t.style.left)+t.h+1)+'px';
}
function xn_js_togglePause(){
	if(this.tid==0){
		this.tid=setInterval('xn_js_startBar('+this.N+')',this.speed);
	}else{
		clearInterval(this.tid);
		this.tid=0;
	}
}

function xn_js_getScreenWidth(){
    return screen.width;
 }

function xn_js_getScreenHeight(){
    return screen.height;
}

function xn_js_getWindowX(){
    return window.screenLeft || window.screenX || "" ;
}

function xn_js_getWindowY(){
    if (window.screenLeft) return window.screenTop;
    else if (window.screenX) return window.screenY;
    else return "";
}

function xn_js_getViewportWidth()
{
    if (window.innerWidth) return window.innerWidth;
    else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
    else if (document.body.clientWidth) return document.body.clientWidth;
    return "";
}

function xn_js_getViewportHeight()
{
    if (window.innerWidth) return window.innerHeight;
    if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientHeight;
    else if (document.body.clientWidth) return document.body.clientHeight;
    return "";
}

function xn_js_getScrollHorizontal()
{
    if (window.innerWidth) return window.pageXOffset;
    else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.scrollLeft;
    else if (document.body.clientWidth) return document.body.scrollLeft;
    return "";
}

function xn_js_getScrollVertical()
{
    if (window.innerWidth) return window.pageYOffset;
    else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.scrollTop;
    else if (document.body.clientWidth) return document.body.scrollTop;
    return "";
}

function xn_js_urlWithToken(url)
{
    window.location = url;
    return false;
}
