var host = "http://gmvvideo.com/";
var alt_host = "http://www.gmvvideo.com/";

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

function reloadPage() {
	top.frames.main.location.reload();
}

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

function screenReturn( icon, msg ) {
	showMessage( icon, msg );
	self.setTimeout( history.back(), 2000)  
}

function assignUrl( win, url ) {
	win.location = url;
//	win.location.assign(url);
}

//---------------------------------------------------------------------
//  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 != 'msg' && 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 );
	processMsg( response );

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

function updateForm( response ) {
	var frame = response.getElementsByTagName('frame')[0].firstChild.data;
	var doc = eval( "top.frames."+frame+".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 != "frame" && tag != "function" && tag != "form" && 
			tag != 'msg' && tag != 'msgIcon' && tag != 'command' && 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 {
					if( confirm( "bad tag - "+tag ) ) {
						debugXml( response );
						return;
					}
				}
			} else { 
				if( confirm( "bad tag - "+tag ) ) {
					debugXml( response );
					return;
				}
			}
		}
	}
	processCommands( response );
	processMsg( response );
}

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 processMsg( 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 );
	}
}

function genXmlCallback( response ) {

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

	processMsg( response );

	var evalTag = response.getElementsByTagName('innerHtml');
	if( evalTag != null ) {
		for( var i=0; i<evalTag.length; i++ ) {
			if( evalTag[i] != null ) 
				if( !replaceInnerHtml( evalTag[i] ) ) debugXml( response );
		}
	}

	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();
		}
	}

	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 );
		}
	}
	var evalTag = response.getElementsByTagName('function2');	// can be multiple <function2></function2> tags
	if( evalTag != null ) {
		for( var i=0; i<evalTag.length; i++ ) {
			if( evalTag[i] != null ) 
				if( !evalResponseFunction2( evalTag[i] ) ) debugXml( 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 replaceInnerHtml( 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;
		}
	}
	var tag = response.getElementsByTagName( 'tag' );
	if( tag != null && tag[0] != null ) {
		var div = document.getElementById( tag[0].firstChild.data );
		if( div == null ) alert( 'unknown innerhtml tag = ' + tag[0].firstChild.data );
		else div.innerHTML = x;
		return true;
	} else {
		alert( "malformed <innerHTML> tags "+ data );
		return false;
	}
}

//
// parse xml response <function2><command>command</command>
//					<param><data>seg1a</data><data>seg1b</data></param>
//					<param><data>seg2a</data><data>seg2b</data></param></function2>
//
// then eval command( param1, param2, ... )
//

function evalResponseFunction2( response ) {
	var parameters = "";
	var param = response.getElementsByTagName( 'param' );
	var p = Array();
	if( param != null ) {
		for( var i=0; i<param.length; i++ ) {
			p[i] = "";
			var data = param[i].getElementsByTagName( 'data' );
			for( var j=0; j<data.length; j++ ) {
				if( data[j] != null ) p[i] = p[i] + data[j].firstChild.data;
			}
			if( parameters != "" ) parameters = parameters + ",";
			parameters = parameters + "p["+i+"]";
		}
	}
	var command = response.getElementsByTagName( 'cmd' );
	if( command != null && command[0] != null ) {
		eval( command[0].firstChild.data + "("+ parameters + ")" );
		return true;
	} else {
		alert( "malformed <function2> tags " );
		return false;
	}
}


//
//	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
//var d = new Date()
//showMessage( 0, d.getTime() );
	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.png", "noticeIcon.png", "checkIcon.png", "processing.gif" );
var iconClass = Array( "status_normal", "status_alert", "status_alert", "status_normal", "status_normal" );
var iconColor = Array( "#000000", "#aa0000", "#aa0000", "#00aa00", "#00aa00" );

function showMessage( icon, text ) {
	if( icon == null ) icon = 0;
	var iconNum = icon;
	var doc = top.frames.main == null ? document : top.frames.main.document;
	var x = doc.getElementById( statusMsgIcon );
	if( x != null ) {
		if( icon == null || icon == 0 ) {
			icon = "/images/"+iconImg[0]; 
			x.src = host + icon;
			x.style.visibility = "hidden";			// may need functionality for NS4/IE differences
		} else {
			if( !isNaN(icon) ) icon = "/myIcons/"+iconImg[icon]; 
			x.src = host + icon;
			x.style.visibility = "visible";
		}
	}
	x = doc.getElementById( statusMsgText );
	if( x != null ) {
		if( !isNaN(iconNum) ) x.style.color = iconColor[iconNum];
//		if( !isNaN(iconNum) ) x.className = iconClass[iconNum];
		if( text == null ) text = "&nbsp;";
		x.innerHTML=text;
	}
	statusMsgTime = new Date();
}

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
//---------------------------------------------------------------------

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 ) {
	var req = allocXmlHttpRequest();
	if( req == null ) {
		alert( "Unable to send XML request.  Do you have ActiveX turned on?\nMac OS X users please use Safari, Firefox, Opera or Camino." );
		return;
	}
	req.onreadystatechange = function () {
		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"
				if( req.responseXML != null ) {
					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" );
					}
				} else alert( req );
			}
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
			var w = open();
			var d = w ? w.document : document;
			d.write( "<html><body>" + req.responseText + "</body></html>" );
    	}
	};
	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);
	}
	delete req;
}

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



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

String.prototype.trim = strtrim;

function debugXml( response ) {
	var w = window.open();
	var d = w ? w.document : 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>" );
			}
		}
	}
}