/* simple javascript routines for dance.net */

function pLink(url,name) {
	var p = window.open(url,name,'scrollbars=yes,resizable=yes,toolbar=no,width=500,height=500');
}
function xpOpenPopup( url, name, widgets, openerUrl )
{
	window.top.name = "opener";
	var popupWin = window.open( url, name, widgets );

	if ( popupWin && popupWin.opener ) {
		if ( openerUrl ) {
			popupWin.opener.location = openerUrl;
			popupWin.focus();
		}
	}
}

function xpToggleDisplay(id)
{
	var i = document.getElementById(id);
	if (i) {
		if (i.style.display=='block')
			i.style.display = 'none';
		else
			i.style.display = 'block';
	}
}

function xpSpoiler(id)
{
	var i = document.getElementById(id);
	var ii = document.getElementById(id+'toggle');
	if (i) {
		if (i.style.display=='block') {
			i.style.display = 'none';
			ii.innerHTML = "Show";
		} else {
			i.style.display = 'block';
			ii.innerHTML = "Hide";
		}
	}
}

/* NOTE: we should officially deprecate this strategy
** The standard says that disabled form elements do not send data
** For those which include multiple submit buttons, we probably do care about that data
** ... and that's hell to debug when only certain browsers obey the standard
*/
function submitonce(theform) {
	//if IE 4+ or NS 6+
	if (document.all||document.getElementById){
		//screen thru every element in the form, and hunt down "submit" and "reset"
		for (i=0;i<theform.length;i++){
			var tempobj=theform.elements[i];
			if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset"){
				//disable em
				tempobj.disabled=true;
				tempobj.value = 'Processing . . . Please wait . . .';
			}
		}
	}
}


// from the o'reilly book...
function getElementPosition(elemID) {
	var offsetTrail = document.getElementById(elemID);
	var offsetLeft = 0;
	var offsetTop = 0;
	while (offsetTrail) {
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf("Mac") != -1 && 
		typeof document.body.leftMargin != "undefined") {
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	return {left:offsetLeft, top:offsetTop};
}

var xpLayerPop = {



	popLayer : function(event, o, kind, id) {
		

	} // xpLayerPop::popLayer



} // xpLayerPop

// ajax

var xp_ajax_handle = null;

function xpSendAjax(url, responsefunc) {
	xp_ajax_handle = new XMLHttpRequest();
	xp_ajax_handle.onreadystatechange = xpReceiveAjax;
	xp_ajax_handle.open('get',url,true);
	xp_ajax_handle.send(null);
	xp_ajax_handle.responsefunc = responsefunc;
}

function xpReceiveAjax() {
	if (xp_ajax_handle.readyState == 4) {
		if (xp_ajax_handle.status == 200) {
			response = xp_ajax_handle.responseXML.documentElement;
/*			method = response.getElementsByTagName('method')[0].firstChild.data; */
/*			eval(method + '(response)'); */
			xp_ajax_handle.responsefunc(response);
		}
	}
}

function xpVoteUp(topic) {
	xpSendAjax("/xml/newsvote.xml?dir=up&topic_id=" + topic, xpVoteResult);
}
function xpVoteDown(topic) {
	xpSendAjax("/xml/newsvote.xml?dir=down&topic_id=" + topic, xpVoteResult);
}
function xpVoteResult(response)
{
	var topic = response.getElementsByTagName('topic')[0].firstChild.data;
	var result = response.getElementsByTagName('result')[0].firstChild.data;
	var score = response.getElementsByTagName('score')[0].firstChild.data;
	var up = document.getElementById("newsvote" + topic + "up");
	var down = document.getElementById("newsvote" + topic + "down");
	var number = document.getElementById("newsvote" + topic + "score");
	up.setAttribute("disabled", true);
	up.setAttribute("href", "javascript:return false;");
	up.style.opacity = 0.2;
	down.setAttribute("disabled", true);
	down.setAttribute("href", "javascript:return false;");
	down.style.opacity = 0.2;
	number.innerHTML = score;
}

function xpImageOrder(id)
{
        $.prompt('<input type="hidden" name="image_id" value="'+id+'">Choose sizes and amounts<p/><table><tr><td><input style="width: auto;" size=3 name="s8x10" id="8x10"></td><td> 8x10</td><td>$15 each</td></tr><tr><td><input style="width: auto;" size=3 name="s5x7" id="5x7"><td> 5x7</td><td>$5 each</td></tr><tr><td><input style="width: auto;" size=3 name="s4x6" id="4x6"><td> 4x6</td><td>$1 each</td></tr></table>', { callback: xpImageOrderCallback, buttons: { Add : true, Cancel : false}, prefix: "jqi" });
}
function xpImageOrderCallback(v,m,f)
{
        if (v) {
                sel = "image_id=" + f.image_id +
			"&8x10=" + f.s8x10 +
			"&5x7=" + f.s5x7 +
			"&4x6=" + f.s4x6;

                $.ajax({
                        type: "POST",
                        url: "/xml/printorder.xml",
                        data: sel
                });
        }
}
function xpImageOrderCheckout()
{
        window.location.href = "http://" + window.location.host + xp.urlpath + "printorder.html";
}
