var request = null;
var queryString = null;
var homeGameID = null;
var newurl = null;
var timeoutURL = null;
var timeoutData = null;
var timeoutQueryString = null;
var timeoutHomeGameID = null;

function setQueryStringGeneric(  ){
    queryString="";
    var frm = document.forms[0];
    var numberElements =  frm.elements.length;
    for(var i = 0; i < numberElements; i++) {
        if(i < numberElements-1) {
            queryString += frm.elements[i].name+"="+
                           encodeURIComponent(frm.elements[i].value)+"&";
        } else {
            queryString += frm.elements[i].name+"="+
                           encodeURIComponent(frm.elements[i].value);
        }

    }
opener.updatepagestring( 1,queryString);
}

function setQueryString(  ){
    queryString="";
/*            alert("Entering setQueryString="+queryString);*/
    var frm = document.forms[0];

	/* After parsing out the supplied url in the hidden form variables.*/
/*    newurl = encodeURIComponent(frm.newurl.value);*/
    newurl = frm.newurl.value;
/*            alert("newurl="+newurl);*/

	/* Save the homegameid for placement into the proper div on main page. */
    homeGameID = frm.homegameid.value;

	/* Get the pieces that we care about. */
    queryString += "action="+ encodeURIComponent(frm.action.value);
    queryString += "&homegameid="+ encodeURIComponent(frm.homegameid.value);
    queryString += "&groupid="+ encodeURIComponent(frm.groupid.value);

if (typeof( frm.allids.length) == "undefined") {
/*            alert("typeof allids true....frm.allids.length="+frm.allids.length);*/
/* frm.allids = [ frm.allids ];...maybe frm.allids.value = []...*/
/*            alert("now...typeof allids true....frm.allids.length="+frm.allids.length);*/
		if (frm.numbers.value > 0)
		{
			queryString += "&memberid="+ encodeURIComponent(frm.allids.value);
			queryString += "&numbers="+ encodeURIComponent(frm.numbers.value);
		}
}
else /* Normal, multiple entry case. */
{
/*
            alert("maybe just one....frm.allids.length="+frm.allids.length);
            alert("maybe just one....frm.allids["+frm.allids.length+"].value="+frm.allids[frm.allids.length-1].value);
            alert("maybe just one....frm.numbers="+frm.numbers);
            alert("maybe just one....frm.numbers.length="+frm.numbers.length);
 */
	/* Put only member ids for those numbers that have non-zero values. */
	for ( var i=0; i<frm.numbers.length; i++ )
	{
		if (frm.numbers[i].value > 0)
		{
			queryString += "&memberid="+ encodeURIComponent(frm.allids[i].value);
			queryString += "&numbers="+ encodeURIComponent(frm.numbers[i].value);
		}
  	}
}
/*            alert("Leaving setQueryString="+queryString);*/

// opener.updatepagestring( 1,queryString);
}

//
// newSetQueryString - Build the string to be attached to the url so that the 
//	assignments requested in small dialog in alltickets.jsp is done correctly.
//
function newSetQueryString( myform ){
/*    var frm = document.embeddedForm;*/
    var frm = myform;
    queryString="";
/*            alert("Entering setQueryString="+queryString);*/

	/* After parsing out the supplied url in the hidden form variables.*/
/*    newurl = encodeURIComponent(frm.newurl.value);*/
    newurl = frm.newurl.value;
/*            alert("newurl="+newurl);*/

	/* Save the homegameid for placement into the proper div on main page. */
    homeGameID = frm.homegameid.value;

	/* Get the pieces that we care about. */
    queryString += "action="+ encodeURIComponent(frm.action.value);
    queryString += "&homegameid="+ encodeURIComponent(frm.homegameid.value);
    queryString += "&groupid="+ encodeURIComponent(frm.groupid.value);

/*            alert("Later setQueryString="+queryString);*/
if (typeof( frm.allids.length) == "undefined") {
/*            alert("typeof allids true....frm.allids.length="+frm.allids.length);*/
/* frm.allids = [ frm.allids ];...maybe frm.allids.value = []...*/
/*            alert("now...typeof allids true....frm.allids.length="+frm.allids.length);*/
		if (frm.numbers.value > 0)
		{
			queryString += "&memberid="+ encodeURIComponent(frm.allids.value);
			queryString += "&numbers="+ encodeURIComponent(frm.numbers.value);
		}
}
else /* Normal, multiple entry case. */
{
/*
            alert("maybe just one....frm.allids.length="+frm.allids.length);
            alert("maybe just one....frm.allids["+frm.allids.length+"].value="+frm.allids[frm.allids.length-1].value);
            alert("maybe just one....frm.numbers="+frm.numbers);
            alert("maybe just one....frm.numbers.length="+frm.numbers.length);
 */
	/* Put only member ids for those numbers that have non-zero values. */
	for ( var i=0; i<frm.numbers.length; i++ )
	{
		if (frm.numbers[i].value > 0)
		{
			queryString += "&memberid="+ encodeURIComponent(frm.allids[i].value);
			queryString += "&numbers="+ encodeURIComponent(frm.numbers[i].value);
		}
  	}
}
/*            alert("Leaving setQueryString="+queryString);*/

// opener.updatepagestring( 1,queryString);
}

//event handler for XMLHttpRequest
function handleResponse(  ){
/*
            alert("Made it into handleResponse readyState="+request.readyState);
    if(request.readyState == 1){
updatepagestring( 1,"loading...");
updatepagestring( homeGameID,"loading...");
    }
    else if(request.readyState == 2){
updatepagestring( 1,"state 2...");
    }
    else if(request.readyState == 3){
updatepagestring( 1,"state 3...");
    }
 */
    if(request.readyState == 4)
    {
/*	    alert(" handleResponse: request.status="+request.status);*/
        if(request.status == 200){
/*	    alert(" response="+request.responseText+"!");
updatepagestring( 1,request.responseText);*/
		updatepagestring( homeGameID, request.responseText);
            /*alert(request.responseText);*/
        } else {
            alert("A problem occurred in handleResponse with communicating between "+
                  "the XMLHttpRequest object and the server program.");
        }
    }//end outer if
}

//
// sendDataWrapper - Sends the ticket assignment url to server, on
//	orders from sendDataWrapper. Once the server gets this request,
//	it sends back a response that is handled by, er, "handleResponse".
//
function sendData(_url,_data,localQueryString, gameid)
{
/*        alert("Where setQueryString used to be, localQueryString=" + localQueryString + "!");
        alert("Before setQueryString used to be, queryString=" + queryString + "!");
 */
	queryString = localQueryString;
	homeGameID = gameid;
/*        alert("After setQueryString..., queryString=" + queryString);*/
/*        alert("After setQueryString..., homegameid=" + homeGameID);*/
    var url="http://127.0.0.1:8080/sharetix/findtickets";
/*    var url="http://localhost:8080/sharetix/findtickets";*/
/*
	newurl += "?";
	newurl += queryString;
    httpRequest("GET",newurl,false,handleResponse);
 */
/*    httpRequest("POST",newurl,true,handleResponse);*/
    httpRequest("POST",_url,true,handleResponse);
/*    httpRequest("POST",newurl,true,handleResponse);*/
/*    httpRequest("POST",newurl,true,handleResponse,_data);*/
}

//
// sendDataWrapper - Sends the ticket assignment url to server, on
//	orders from sendDataWithTO.
//
function sendDataWrapper()
{
/*
alert("In sendDataWrapper, timeoutURL="+timeoutURL+" data="+timeoutData);
alert("In sendDataWrapper, queryString="+timeoutQueryString);
alert("In sendDataWrapper, timeoutHomeGameID="+timeoutHomeGameID);
*/
	sendData( timeoutURL, timeoutData, timeoutQueryString, timeoutHomeGameID);
}

//
// sendDataWithTO - Sends the ticket assignment url to server.
//
function sendDataWithTO(newURL, args, inputString, homegameid_inp) {
	timeoutURL = newURL;
	timeoutData = args;
	timeoutQueryString = inputString;
	timeoutHomeGameID = homegameid_inp;
/*            alert("sendDataWithTO newURL="+newURL+"timeoutData="+args);*/
            /*alert("sendDataWithTO queryString="+timeoutQueryString);*/
	window.setTimeout('sendDataWrapper()', 0);
}

/* Wrapper function for constructing a request object.
 Parameters:
  reqType: The HTTP request type, such as GET or POST.
  url: The URL of the server program.
  asynch: Whether to send the request asynchronously or not.
  respHandle: The name of the function that will handle the response.
  Any fifth parameters, represented as arguments[4], are the data a
  POST request is designed to send. */
function httpRequest(reqType,url,asynch,respHandle){
    //Mozilla-based browsers
    if(window.XMLHttpRequest){
        request = new XMLHttpRequest(  );
    } else if (window.ActiveXObject){
        request=new ActiveXObject("Msxml2.XMLHTTP");
        if (! request){
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    //very unlikely, but we test for a null request
    //if neither ActiveXObject was initialized
    if(request) {
        //if the reqType parameter is POST, then the
        //5th argument to the function is the POSTed data
/*
        alert("Trying to initReq with reqType=" + reqType + " and " +
		"url=" + url + " and queryString=" + queryString + "!");
 */
        initReq(reqType,url,asynch,respHandle);
/*
        if(reqType.toLowerCase(  ) != "post") {
            initReq(reqType,url,asynch,respHandle);
        }  else {
            //the POSTed data
            var args = arguments[4];
            if(args != null && args.length > 0){
                initReq(reqType,url,asynch,respHandle,args);
            }
        }
 */

    } else {
        alert("Your browser does not permit the use of all "+
              "of this application's features!");
    }
}

/* Initialize a request object that is already constructed */
function initReq(reqType,url,bool){
    /* Specify the function that will handle the HTTP response */
    request.onreadystatechange=handleJson;
    request.open(reqType,url,bool);
    request.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded; charset=UTF-8");
    request.send(queryString);
}

function getURLParam( name )
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;

  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

/* Initialize a request object that is already constructed */
function initReq(reqType,url,bool,respHandle){
    try{
        /* Specify the function that will handle the HTTP response */
/*        request.onreadystatechange=respHandle;*/
/*    alert("In initReq with url=" + url + " queryString=" + queryString);*/
        request.open(reqType,url,bool);
/*
THE FOLLOWING WORKS WITH assignTickets....see if we can pass in handleResponse to respHandle
        request.onreadystatechange=handleResponse;
 */
        request.onreadystatechange=respHandle;
        //if the reqType parameter is POST, then the
        //5th argument to the function is the POSTed data
        if(reqType.toLowerCase(  ) == "post") {
            request.setRequestHeader("Content-Type",
                    "application/x-www-form-urlencoded; charset=UTF-8");
/*            request.send(arguments[4]);*/
    	    request.send(queryString);
        }  else {
            request.send(null);
/*	    request.send(queryString);*/
        }

    } catch (errv) {
        alert(
        "The application cannot contact "+
        "the server at the moment. "+
        "Please try again in a few seconds.\\n"+
        "Error detail: "+errv.message);
    }
}

