function ignore_server(url) { var srv = "componentart.com";return url.substring(url.indexOf(srv) + srv.length); }


// Log in- & dialog-related functions
function init_login_link(el) {
	var p = el.parentNode;

	if (window.logged_in) AccountMenu.showContextMenuAtElement(p);
	else {
		if (typeof AccountDialog != "undefined") {
			var fr = document.getElementById("fr-acct");

			if (window.logged_out != true) {
				if (ignore_server(fr.src) != "/user/account.aspx?returnurl=" + location.href) {
					fr.src = "/user/account.aspx?returnurl=" + location.href;
					AccountDialog.Value = "/user/account.aspx?returnurl=" + location.href; }
			} else {
				if (ignore_server(fr.src) != "/user/account.aspx?returnurl=/") {
					fr.src = "/user/account.aspx?returnurl=/";
					AccountDialog.Value = "/user/account.aspx?returnurl=/";
				}
			}
		}

		AccountDialog.Show();
	}
}


function login_redirect(urlstr,msgstr,acct) {
	var message = "Please log in or create an account to continue.";
	if (msgstr) message = msgstr;

	if (!logged_in) {
		if (msgstr != null) {
			document.getElementById("fr-acct").src = "/user/account.aspx?returnurl=" + urlstr + "&message=" + message;
			AccountDialog.Value = "/user/account.aspx?returnurl=" + urlstr + "&message=" + message;
		} else {
			document.getElementById("fr-acct").src = "/user/account.aspx?returnurl=" + urlstr;
			AccountDialog.Value = "/user/account.aspx?returnurl=" + urlstr;
		}

		AccountDialog.show();
	} else window.location.href = urlstr;
}




//	Log out
function log_out() { document.location = "/?logout"; }

function close_login_menu() { LoginMenu.hide(); }


//	Dialogue routines
function close_dialog(dlg) {
	var url = String(document.location.href);
	if (url.indexOf("sitelogin.aspx") > -1 || url.indexOf("community/login.aspx") > -1) document.location.href = "/";

	dlg.close();

	if (dlg.get_id().indexOf("AccountDialog") > -1) {
		document.getElementById("fr-acct").src = "/user/loading.aspx";
		hide_error_bar();


	}
}

window.active_dialog = null;

function show_dialog(sender,args) { window.active_dialog = sender; }

function close_dialog_on_escape(e) {
	if (window.active_dialog != null) {
		var k  = (window.event) ? event.keyCode : e.keyCode;
		var esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;

		if (k == esc) {
			try { close_dialog(window.active_dialog); }
			catch(err) { }
		}

		window.active_dialog = null;
	}
}

//	Fire the password-change dialogue
function change_password() { PasswordChangeDialog.show(); }



// Flash
function write_swf(swf_url,w,h) {
	//	Object tag for non-Mozilla browsers
	var swf = "\n<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\" width=\"" + w + "\" height=\"" + h + "\" align=\"middle\">";
		swf += "\n\t<param name=\"allowScriptAccess\" value=\"sameDomain\" />";
		swf += "\n\t<param name=\"allowFullScreen\" value=\"false\" />";
		swf += "\n\t<param name=\"movie\" value=\"" + swf_url + "\" />";
		swf += "\n\t<param name=\"menu\" value=\"false\" />";
		swf += "\n\t<param name=\"quality\" value=\"high\" />";
		swf += "\n\t<param name=\"align\" value=\"l\" />";
		swf += "\n\t<param name=\"salign\" value=\"tl\" />";
		swf += "\n\t<param name=\"wmode\" value=\"transparent\" />";

		//	Embed for Mozilla browsers
		swf += "\n\n\t<embed src=\"" + swf_url + "\" wmode=\"transparent\" align=\"l\" salign=\"tl\" menu=\"false\" quality=\"high\" width=\"" + w + "\" height=\"" + h + "\" align=\"middle\" allowScriptAccess=\"sameDomain\" allowFullScreen=\"false\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />";
		swf += "\n</object>";

	document.write(swf);
}

function ie_swf_fix() {
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		var o = document.getElementsByTagName("object");
		for (var i = 0; i < o.length; i++) { o[i].outerHTML = o[i].outerHTML; }
	}
}


function get_position(el) {
	var el_x = 0;
	var el_y = 0;

	do {
		el_x += el.offsetLeft;
		el_y += el.offsetTop;
	} while (el = el.offsetParent);

	return [el_x,el_y];
}

//	Immediate functions
// IE6 background flicker fix
try { if( document.execCommand ) document.execCommand("BackgroundImageCache", false, true); } catch(err) { }

document.onkeypress = close_dialog_on_escape;



// Log in / create new account scripts
function checkbox_init(el,chk) {
	var a = ((typeof el) == "object") ? el : document.getElementById(el);
	if (document.getElementById(chk).checked == false) a.className = "";
}

function checkbox_toggle(el,chk) {
	document.getElementById(chk).checked = !document.getElementById(chk).checked;
	el.className = (document.getElementById(chk).checked) ? "sel" : "";
}

function toggle_dialog_error(attn,classname,msg) {
	var d = document.getElementById("acct-err");

	// insert <strong> tags
	msg = msg.replace("/**","</strong>");
	msg = msg.replace("**","<strong>");


	if (!attn) {
		d.className = (d.className == "errorbar ok") ? "errorbar" : "errorbar ok";
	} else {
		d.className = "errorbar";
	}

	if (classname != "" && classname != null) set_error_class(classname);

	if (msg != "" && msg != null) {
		var d = document.getElementById("acct-err");
		var p = d.getElementsByTagName("p")[0];

		p.innerHTML = msg;
	}

	if (d.className == "errorbar" && attn) { //
		window.flash_bar_count = 6;	// divide by 2 = number of error bar flashes
		flash_bar(0);
	}
}


function clear_error_bar() {
	var d = document.getElementById("acct-err");
	if (d == null || (typeof d == "undefined")) d = document.getElementById("pwd-err");

	if (d) {
		d.className = "errorbar ok";
		d.style.backgroundColor = "#ffffe1";
	}
}

function flash_bar(s) {
	if (window.flash_bar_count > 0) {
		(s == 0) ? flash_bar_on() : flash_bar_off();
		window.flash_bar_count--;
	}
}

function flash_bar_on() {
	document.getElementById("err-msg").style.backgroundColor = "#d0d0be";
	setTimeout("flash_bar(1)",250);
}

function flash_bar_off() {
	document.getElementById("err-msg").style.backgroundColor = "#ffffe1";
	setTimeout("flash_bar(0)",250);
}

function set_error_class(classname) {
	if (classname == "err") classname = "error";
	if (classname == "nfo") classname = "info";
	if (classname == "wrn") classname = "warning";

	var d = document.getElementById("acct-err");
	var p = d.getElementsByTagName("p")[0];
	p.className = classname;
}

function hide_error_bar() { document.getElementById("acct-err").className = "errorbar ok"; }


function increase_account_iframe_height() {
	/*
	var p = document.getElementById("fr-acct");
	if (p != null && window.resize_dialog == true) {
		p.style.height = "120%";
	}
	*/
}

function decrease_account_iframe_height() {
	var p = document.getElementById("fr-acct");
	if (p != null) p.style.height = "100%";
}

window.resize_dialog = false;
