function toggle(section_id) {
	if(document.getElementById(section_id).style.display) {
		document.getElementById(section_id + "-img").src = "/images/details-open.gif";
		document.getElementById(section_id).style.display = '';
	}
	else {
		document.getElementById(section_id + "-img").src = "/images/details-closed.gif";
		document.getElementById(section_id).style.display = 'none';
	}
}

function formAsString(form) {
	var form_string = "";

	for(var i = 0; i < form.elements.length; i++) {
		switch(form.elements[i].type) {
			case "text" :
			case "hidden" :
			case "hidden" :
			case "textarea" :
			case "password" :
			case "button" :
			case "submit" :
				form_string += form.elements[i].name + "=" + escape(form.elements[i].value) + "&";
				
				break;

			case "select-one" :
				form_string += form.elements[i].name + "=" + form.elements[i].options[form.elements[i].selectedIndex].value + "&";
				
				break;
			
			case "select-multiple" :
				form_string += form.elements[i].name + "=";

				for(var x = 0; x < form.elements[i].length; x++)
					if(form.elements[i].options[x].selected)
						form_string += form.elements[i].options[x].value + ",";
				
				if(form_string.substr(form_string.length - 1, form_string.length) == ",")
					form_string = form_string.substr(0, form_string.length - 1);
				
				form_string += "&";
				break;
			
			case "checkbox" :
			case "radio" :
				form_string += form.elements[i].name + "=";

				if(form.elements[i].checked)
					form_string += form.elements[i].value + ",";
				
				if(form_string.substr(form_string.length - 1, form_string.length) == ",")
					form_string = form_string.substr(0, form_string.length - 1);
				
				form_string += "&";
				break;
		}
	}

	form_string = form_string.substr(0, form_string.length - 1);
	return form_string;
}

function getMousePos(oEvent) {
	var oPos = {x:0, y:0};

	if(typeof oEvent == "undefined") oEvent = window.event;
	if(typeof oEvent.pageX == "undefined" || typeof oEvent.pageY == "undefined") {
		oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
		oEvent.pageY = oEvent.clientY + document.body.scrollTop;
	}

	oPos.x = oEvent.pageX;
	oPos.y = oEvent.pageY;

	return oPos;
}

function trim(string) {
	return string.replace(/^\s+(.*?)\s+$/, "$1");
}

function protect_email2(username, domain) {
	email = username + '&#64;' + domain;
	email2 = username + '@' + domain;
	document.write('<a href="mailto:' + email2 + '">' + email + '</a>');

	return false;
}

function loginRequest(site, endFunc, form) {
	if(form) {
		waitingMessage.setMessage("logging_in", "Processing login...");
		requestPage("/r/functions/login.cfm", "pop=1&site_id=" + site + "&" + formAsString(form), function(requestText) {
			waitingMessage.remove();
			
			if(requestText.match(/^timeout$/i)) {
				waitingMessage.remove();
				timeoutAlert(true);
			}
			else if(!requestText.match(/^true$/i))
				errorAlert(requestText)
			else {
				loginRequest();
				eval(endFunc);
			}
		});
	}
	else if(!document.getElementById("jt_login_popup")) {
		waitingMessage.remove();
		var size = windowSize();
		newAlert("jt_login_popup_box", '<form method="post" action="#" id="jt_login_popup" onsubmit="return loginRequest(\'' + site + "','" + endFunc.replace(/'/g, "\\'") + '\', this);"><div class="jt_title">Please Log In</div><div class="jt_subtitle">As a safety measure, you were automatically logged out due to inactivity. Please log in again below; none of your data will be lost.</div><table border="0" cellspacing="0" cellpadding="0"><tr><th>Email:</th><td><input type="text" class="jt_text" name="j_username"></td></tr><tr><th>Password:</th><td><input type="password" class="jt_text" name="j_password"></td></tr></table><div class="jt_links"><input type="submit" class="jt_button" name="login" value="Log In"></div></form>', (size.height / 2) - 75, (size.width / 2) - 125, 250, null, null, true);
		document.getElementById("jt_login_popup").j_username.focus();
	}
	else
		newAlert("jt_login_popup_box");

	return false;
}


function siteGet() {
	var site_id = "";
	if(location.search) {
		var qstring = window.location.search.substr(1).split("&");
		for (i = 0; i < qstring.length; i++) {
			if(qstring[i].split("=")[0] == "site_id") {
				site_id = qstring[i].split("=")[1];
				break;
			}
		}
	}

	return site_id;
}

function dollarformat(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function currencyformat(cost, currency, code) {
	var ret = cost;

	switch(currency) {
		case "usd" :
			ret = dollarformat(cost) + ((code) ? ' USD' : '');
			break;
		case "cad" :
			ret = dollarformat(cost) + ((code) ? ' CAD' : '');
			break;
		case "aud" :
			ret = dollarformat(cost) + ((code) ? ' AUD' : '');
			break;
		case "eur" :
			ret = dollarformat(cost).toString().replace(/\$/g, '&euro;') + ((code) ? ' EUR' : '');
			break;
		case "gbp" :
			ret = dollarformat(cost).toString().replace(/\$/g, '&pound;') + ((code) ? ' GBP' : '');
			break;
	}

	return ret;
}
