var host = "http://musclememory.net/";
var alt_host = "http://www.musclememory.net/";

function setClass( id, className ) {
	id.className = className;
}

function GoToPage( page ) {
	top.frames.main.location.replace( host + page );
}

//---------------------------------------------------------------------
//  generic XML callbacks
//---------------------------------------------------------------------

function updateText( response ) {
	var reload = response.getElementsByTagName('reload')[0].firstChild.data;
	var frame = response.getElementsByTagName('frame')[0].firstChild.data;
	var doc = eval( "top.frames."+frame+".document" );
	if( doc == null ) doc = document;
	var x = response.childNodes;
	for( var i=0; i<x.length; i++ ) {
		var y = x.item(i);
		var tag = y.nodeName;
		var val;
		if( tag != "method" && tag != "frame" && tag != "function" &&
			tag != "reload" && tag.indexOf( "#" ) < 0 ) {
			if( y.childNodes.length > 0 ) {
				val = y.childNodes.item(0).nodeValue;
			} else {
				val = "";
			}
			if( tag == "option" ) {
				var o_array = val.split("|");
				var z=document.getElementById(o_array[0]);
				if( z != null ) {
					for( j=0; j<z.length; j++ ) {
						if( z.options[j].value == o_array[1] )
							z.options[j].text = o_array[2];
					}
				}
			} else if( tag == "field" ) {
				var o_array = val.split("|");
				var b = doc.getElementById(o_array[0]);
				if( b != null ) b.value = o_array[1];
			} else {
				var z = doc.getElementById( tag );
				if( z != null ) z.innerHTML = val;
			}
		}
	}
	processCommands( response );

	if( reload != null ) {
		top.frames.main.location.replace( unescape(reload) );
	} else {
//		alert( "no reload" );
	}
}

function updateForm( response ) {
	var frame = response.getElementsByTagName('frame');
	if( frame == null || frame[0] == null ) {
		doc = document;
	} else {
		var doc = eval( "top.frames."+frame[0].firstChild.data+".document" );
	}
	var formTag = response.getElementsByTagName('form')[0].firstChild.data;
	var form = eval(  "doc."+formTag );
	var x = response.childNodes;
	for( var i=0; i<x.length; i++ ) {
		var y = x.item(i);
		var tag = y.nodeName;
		if( tag != "method" && tag != "form" && tag.indexOf( "#" ) < 0 ) {
			var val;
			if( y.childNodes.length > 0 ) {
				val = y.childNodes.item(0).nodeValue;
			} else {
				val = "";
			}
//			alert( tag + ", " + val );
			if( tag == "id" ) {
				var o_array = val.split("|");
				var z = doc.getElementById( o_array[0] );
				if( z != null ) z.innerHTML = o_array[1];
			} else if( tag == "radio" ) {
				var o_array = val.split("|");
				var item = form.elements[o_array[0]];
				if( item != null ) {
					for( var j=0; j<item.length; j++ ) {
						if( item[j].value == o_array[1] ) item[j].checked = true;
					}
				}
			} else if( tag.trim().length > 0 ) {			// text, pass, hidden, checkbox
				var item = form.elements[tag];
				if( item != null ) {
					if( item.type == "checkbox" ) {
						item.checked = (val == 1);
					} else {
						item.value = val;
					}
				}
			} else { alert( "bad tag '"+tag+"'" ); }
		}
	}
	processCommands( response );

	var msgTag = response.getElementsByTagName('msg');
	if( msgTag != null && msgTag[0] != null ) 
		showMessage( msgTag[0].firstChild.data );
}

function genXmlCallback( response ) {

	var msgTag = response.getElementsByTagName('debug');
	if( msgTag != null && msgTag[0] != null ) debugXml( response );

	var msgTag = response.getElementsByTagName('msg');
	if( msgTag != null && msgTag[0] != null ) {
		var iconTag = response.getElementsByTagName('msgIcon');
		if( iconTag != null && iconTag[0] != null && iconTag[0].firstChild != null ) {
			icon = iconTag[0].firstChild.data;
		} else {
			icon = null;
		}
		showMessage( icon, msgTag[0].firstChild.data );
	}

	var fieldTag = response.getElementsByTagName( 'field' );
	if( fieldTag != null && fieldTag[0] != null ) {
		var text = eval( fieldTag[0].firstChild.data );
		if( text != null ) {
			if( text.type == 'text' || text.type == 'password' ) {
				text.focus();
				text.value = '';
			}
		}
	}

	var focusTag = response.getElementsByTagName('focus');
	if( focusTag != null && focusTag[0] != null ) {
		text = eval( focusTag[0].firstChild.data );
		if( text != null ) {
			text.select();
			text.focus();
		}
	}

	processFunctions( response );
	processCommands( response );
	
	var redirect = response.getElementsByTagName('redirect');
	if( redirect != null && redirect[0] != null &&
		redirect[0].firstChild != null &&
		redirect[0].firstChild.data != null ) {
		window.location = redirect[0].firstChild.data;
	}

	redirect = response.getElementsByTagName('parent');
	if( redirect != null && redirect[0] != null &&
		redirect[0].firstChild != null &&
		redirect[0].firstChild.data != null ) {
		parent.location = redirect[0].firstChild.data;
	}
	
	window.status="";
}


function processCommands( response ) {
	var evalTag = response.getElementsByTagName('command');		// can be multiple <command></command> tags
	if( evalTag != null ) {
		for( var i=0; i<evalTag.length; i++ ) {
			if( evalTag[i] != null ) eval( evalTag[i].firstChild.data );
		}
	}
}

function processFunctions( response ) {
	var evalTag = response.getElementsByTagName('function');	// can be multiple <function></function> tags
	if( evalTag != null ) {
		for( var i=0; i<evalTag.length; i++ ) {
			if( evalTag[i] != null ) 
				if( !evalResponseFunction( evalTag[i] ) ) debugXml( response );
		}
	}
}
//
//	parse xml response <function><command>command</command><data>big data</data></function>
//	then
// 	eval command( big_data )
//

function evalResponseFunction( response ) {
	var data = response.getElementsByTagName( 'data' );
	var x = "";
	if( data != null ) {
		for( var i=0; i<data.length; i++ ) {
			if( data[i] != null ) x = x + data[i].firstChild.data;
		}
///		if( x.length > 4000 ) alert( "big data " + x.length );
		var param = "(x)";
	} else { 
		param = "()";
	}
	var command = response.getElementsByTagName( 'cmd' );
	if( command != null && command[0] != null ) {
		eval( command[0].firstChild.data + param );
		return true;
	} else {
		alert( "malformed <function> tags "+ data );
		return false;
	}
}


function doXml( script, params ) {
// status msg stuff removed
	loadXMLDoc( script, "POST", params );
}

function doXmlFromForm( form ) {
	var params = extractParamsFromForm( form, null );
	doXml( form.action, params );
}

function doXmlFromFormWithSelect( form, selectArray ) {
	var params = extractParamsFromForm( form, selectArray );
	doXml( form.action, params, null, null );
}

function extractParamsFromForm( form, selectArray ) {
	var script = null;
	for( var i=0; i<form.elements.length; i++ ) {
		var item = form.elements[i];
		switch( item.type ) {
			case "button":
			case "submit":
				break;
			case "select-one":
			case "select-multiple":
				if( selectArray ) {
					var str = item.name + "[99]=" + item.value;		// i know it's a hack
					if( script == null ) script = str;
					else script = script + "&" + str;
					for( var j=0; j<item.options.length; j++ ) {
						var opt = item.options[j];
						if( opt.value.length == 0 ) {
							str = item.name+"["+j+"]" + "=" + opt.text;
						} else {
							str = item.name+"["+j+"]" + "=" + opt.value;
						}
						script = script + "&" + str;
					}
				} else {
					var str = item.name + "=" + item.value;
					if( script == null ) script = str;
					else script = script + "&" + str;
				}
				break;
			case "checkbox":
			case "radio":
				if( ! item.checked ) break;
			default:
				var str = item.name + "=" + escape(item.value);
				if( script == null ) script = str;
				else script = script + "&" + str;
				break;
		}
	}
	return script;
}

var statusMsgTime = null;
var statusMsgText = 'status_msg';
var statusMsgIcon = 'status_icon';

function clearMessage() {
	var time = new Date();
	if( time - statusMsgTime > 500 ) {
		showMessage( null, null );
		statusMsgTime = null;
	}
}

var iconImg = Array( "blankIcon.gif", "alertIcon.gif", "noticeIcon.gif", "checkIcon.gif", "processing.gif" );

function showMessage( icon, text ) {
	var x = document.getElementById( statusMsgIcon );
	if( x == null ) {
		if( text == null ) text = icon;
	} else {
		if( icon == null ) {
			x.style.visibility = "hidden";			// may need functionality for NS4/IE differences
		} else {
			if( !isNaN(icon) ) icon = "/images/"+iconImg[icon]; 
//			alert( "showMessage "+icon+", "+text );
			x.src = icon;
			x.style.visibility = "visible";
		}
	}
	x = document.getElementById( statusMsgText );
	if( text == null ) text = "&nbsp;";
	if( x != null ) x.innerHTML=text;
	statusMsgTime = new Date();
	return false;
}

function showMsg( span, name, msg ) {		// obsolete
	var doc;
	if( top.frames.main == null ) {
		doc = document;
	} else {
		doc = top.frames.main.document;
	}
	var x = doc.getElementById( span );
	if( x != null ) {
		x.innerHTML = msg;
		x.className = name;
	}
}

//---------------------------------------------------------------------
//  XMLHttpRequest stuff
//---------------------------------------------------------------------

var req = null;

function allocXmlHttpRequest() {
	if (window.XMLHttpRequest) {	    // native XMLHttpRequest object
	    	return new XMLHttpRequest();
    } else if (window.ActiveXObject) {	// IE/Windows ActiveX version
	    var msxmls = new Array(
				'Msxml2.XMLHTTP.5.0',
				'Msxml2.XMLHTTP.4.0',
				'Msxml2.XMLHTTP.3.0',
				'Msxml2.XMLHTTP',
				'Microsoft.XMLHTTP');
		for (var i = 0; i < msxmls.length; i++) {
			try {
				return new ActiveXObject(msxmls[i]);
			} catch (e) {
			}
		}
		return null;
    }
}

function loadXMLDoc( script, method, params ) {
//	if( req == null ) req = allocXmlHttpRequest();
//	if( script.substr( 0, 4 ) != "http" ) script = host + script;
	req = allocXmlHttpRequest();
	if( req == null ) {
		alert( "Unable to send XML request.  Do you have ActiveX turned on?\nMac OS X users please use Safari or Mozilla." );
		return;
	}
	req.onreadystatechange = processReqChange;
	if( method == "POST" && postSupported() ) {
		try {
			req.open("POST", script, true);
		} catch( e ) {
			try {
				req.open("POST", host+script, true);
			} catch( e ) {
				try {
					req.open("POST", alt_host+script, true);
				} catch( e ) {
					alert( "failed to open "+host+script );
				}
			}
		}
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//		req.setRequestHeader("Accept", "text/xml");
//		req.setRequestHeader("Cache-Control", "no-cache");
		if( params == null ) params = "dummy=0";
		req.send(params);
	} else {
		if( params != null ) script = script + "?" + params;
		try {
			req.open("GET", script, true);
		} catch( e ) {
			try {
				req.open("GET", host+script, true);
			} catch( e ) {
				try {
					req.open("POST", alt_host+script, true);
				} catch( e ) {
					alert( "failed to open "+host+script );
				}
			}
		}
		req.send(null);
	}
}

function postSupported() { return true; }	// need to figure out browser is Opera

function processReqChange() {
	if( req == null ) {
		alert( "error in processReqChange, null request handle" );
	} else if (req.readyState == 4) {    // only if req shows "complete"
        if (req.status == 200) {  // only if "OK"
			var response = req.responseXML.documentElement;
			var method = response.getElementsByTagName('method');
			if( method != null && method[0] != null  ) {
				eval(method[0].firstChild.data + '( response )');
				return;
			} else {
				alert( "processReqChange: didn't find method tag" );
			}
		}
		alert("There was a problem retrieving the XML data:\n" + req.statusText);
		var w = window.open();
		w.document.write( "<html><body>" + req.responseText + "</body></html>" );
    }
}

function strtrim() {
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

String.prototype.trim = strtrim;

function debugXml( response ) {
	var w = window.open(null,"debug");
	var d = w.document;
	d.write( "<html><body><dl>" );
	writeNode( d, response );
	d.write( "<p>END" );
	d.write( "</dl></body></html>" );
}

function writeNode( d, node ) {
	if( node != null ) {
		d.write( "<dd><p>Name " + node.nodeName );
		d.write( "<br>Type " + node.nodeType );
		d.write( "<br>String " + node.nodeTypeString );
		d.write( "<br>Text " + node.data );
		if( node.childNodes != null ) {
			d.write( "<br> num children " + node.childNodes.length );
			if( node.childNodes.length > 0 ) {
				d.write( "<dl>" );
				for( var i=0; i<node.childNodes.length; i++ ) {
					writeNode( d, node.childNodes[i] );
				}
				d.write( "</dl>" );
			}
		}
	}
}