// global flag
var isIE = false;
// global request and XML document objects
var req;
var cur_object;
var cur_query;
var cur_id;
var cur_display;
var cur_date;
var cur_bib;

function createRequestObject() 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        thereq = new XMLHttpRequest();
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        thereq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	return thereq;
}

function get_start_date(thedisplay, thevalue, thevar)
{
	req = createRequestObject();
	var url='/ajaxsearch.php?h5='+thevalue+'&partner_id='+thevar;
	cur_object='start_date';
	cur_display = thedisplay;
	if (req && url)
	{
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	}
}

/*function change_event_membership(thedisplay, theid)
{
	req = createRequestObject();
	var url='/ajaxsearch.php?h6='+theid;
	cur_object='event_membership';
	cur_display = thedisplay;
	if (req && url)
	{
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	}
}*/

function search_division(query, current_id, thedisplay)
{
	req = createRequestObject();
	thevalue = document.getElementById(query).value;
	thevalue = thevalue.replace("+","--");
	thevalue = thevalue.replace("&","||");
	var url='/ajaxsearch.php?h3='+thevalue;
	cur_object='ac_mainuser';
	cur_query = query;
	cur_id = current_id;
	cur_display = thedisplay;
	cur_bib="";
	if (req && url)
	{
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	}
}

function search_main_member(query, current_id, thedisplay, thedate)
{
	req = createRequestObject();
	var url='/ajaxsearch.php?h1='+document.getElementById(query).value+"&member=2";
	cur_object='ac_mainmember';
	cur_query = query;
	cur_id = current_id;
	cur_display = thedisplay;
	cur_date = thedate;
	cur_bib="";
	if (req && url)
	{
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	}
}

function search_main_director(query, current_id, thedisplay)
{
	req = createRequestObject();
	var url='/ajaxsearch.php?h1='+document.getElementById(query).value+"&member=3";
	cur_object='ac_mainuser';
	cur_query = query;
	cur_id = current_id;
	cur_display = thedisplay;
	cur_bib="";
	if (req && url)
	{
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	}
}

function search_main_user(query, current_id, thedisplay)
{
	req = createRequestObject();
	var url='/ajaxsearch.php?h1='+document.getElementById(query).value;
	cur_object='ac_mainuser';
	cur_query = query;
	cur_id = current_id;
	cur_display = thedisplay;
	cur_bib="";
	if (req && url)
	{
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	}
}
function search_main_user1(query, current_id, thedisplay)
{
	req = createRequestObject();
	var url='/ajaxsearch.php?h1='+document.getElementById(query).value+"&seat=1";
	cur_object='ac_mainuser';
	cur_query = query;
	cur_id = current_id;
	cur_display = thedisplay;
	cur_bib="";
	if (req && url)
	{
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	}
}

function search_attendee(query, current_id, thedisplay, thebib)
{
	req = createRequestObject();
	var url='/ajaxsearch.php?h1='+document.getElementById(query).value+"&bib=1";
	cur_object='ac_mainuser';
	cur_query = query;
	cur_id = current_id;
	cur_display = thedisplay;
	cur_bib=thebib;
	if (req && url)
	{
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	}
}

function search_main_participant(query, current_id, thedisplay)
{
	req = createRequestObject();
	var url='/ajaxsearch.php?h2='+document.getElementById(query).value;
	cur_object="ac_mainparticipant";
	cur_query = query;
	cur_id = current_id;
	cur_display = thedisplay;
	cur_bib = "";
	if (req && url)
	{
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	}
}

function search_participant(query, current_id, thedisplay, thebib)
{
	req = createRequestObject();
	var url='/ajaxsearch.php?h2='+document.getElementById(query).value+"&bib=1";
	cur_object='ac_mainparticipant';
	cur_query = query;
	cur_id = current_id;
	cur_display = thedisplay;
	cur_bib=thebib;
	if (req && url)
	{
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	}
}

function search_user(query, current_id, thedisplay, thebib)
{
	req = createRequestObject();
	var url='/ajaxsearch.php?h4='+document.getElementById(query).value;
	cur_object='ac_user';
	cur_query = query;
	cur_id = current_id;
	cur_display = thedisplay;
	if (req && url)
	{
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	}
}

// handle onreadystatechange event of req object
function processReqChange() 
{
    if (req.readyState == 4) 
	{
        if (req.status == 200) 
		{
			if (cur_object=="ac_mainmember")
				process_user_member (req.responseText, cur_query, cur_id, cur_display, cur_date);
			if (cur_object=="ac_user")
				process_user1 (req.responseText, cur_query, cur_id, cur_display);
			if (cur_object=="ac_mainuser" || cur_object=="ac_mainparticipant")
				process_user (req.responseText, cur_query, cur_id, cur_display, cur_bib);
			if (cur_object=="start_date")
				document.getElementById(cur_display).value=req.responseText;
			if (cur_object=="event_membership")
				document.getElementById(cur_display).innerHTML=req.responseText;
        } else {
		 	alert ('Error');
    	}
    } else
	{

	}
}

function process_user2(data, the_query, the_id, the_display, the_bib, the_date)
{
	thedata = data.split("|");
	if (thedata.length>0 && thedata != "")
	{
		document.getElementById(the_id).value='';
		show_data = "";
		for (var i=0; i<thedata.length; i++)
		{
			temp = thedata[i].split("--");
			display=temp[0];
			temp[0] = temp[0].replace("'","\\\'");
			show_data += "<div id='cell"+(i+1)+"' onmouseover='assignCSSClass(\"cell"+(i+1)+"\",\"ajax_highlight\");' onmouseout='assignCSSClass(\"cell"+(i+1)+"\",\"ajax_normal\");' onclick=\"process_user2('"+temp[1]+"','"+temp[0]+"','"+temp[2]+"','"+the_query+"','"+the_id+"','"+the_display+"','"+the_bib+"')\" class=\"ajax_normal\">"+display+"</div>\r\n";
		}
		if (show_data != "")
		{
			document.getElementById(the_display).style.display='';
			document.getElementById(the_display).innerHTML=show_data;
		} else
		{
			document.getElementById(the_display).style.display='none';
			document.getElementById(the_display).innerHTML='';
		}
	} else
	{
		document.getElementById(the_display).style.display='none';
		document.getElementById(the_display).innerHTML='';
	}
}

function process_user(data, the_query, the_id, the_display, the_bib)
{
	thedata = data.split("|");
	if (thedata.length>0 && thedata != "")
	{
		document.getElementById(the_id).value='';
		show_data = "";
		for (var i=0; i<thedata.length; i++)
		{
			temp = thedata[i].split("--");
			display=temp[0];
			temp[0] = temp[0].replace("'","\\\'");
			show_data += "<div id='cell"+(i+1)+"' onmouseover='assignCSSClass(\"cell"+(i+1)+"\",\"ajax_highlight\");' onmouseout='assignCSSClass(\"cell"+(i+1)+"\",\"ajax_normal\");' onclick=\"process_user2('"+temp[1]+"','"+temp[0]+"','"+temp[2]+"','"+the_query+"','"+the_id+"','"+the_display+"','"+the_bib+"')\" class=\"ajax_normal\">"+display+"</div>\r\n";
		}
		if (show_data != "")
		{
			document.getElementById(the_display).style.display='';
			document.getElementById(the_display).innerHTML=show_data;
		} else
		{
			document.getElementById(the_display).style.display='none';
			document.getElementById(the_display).innerHTML='';
		}
	} else
	{
		document.getElementById(the_display).style.display='none';
		document.getElementById(the_display).innerHTML='';
	}
}

function process_user_member(data, the_query, the_id, the_display, the_date)
{
	thedata = data.split("|");
	if (thedata.length>0 && thedata != "")
	{
		document.getElementById(the_id).value='';
		show_data = "";
		for (var i=0; i<thedata.length; i++)
		{
			temp = thedata[i].split("--");
			display=temp[0];
			temp[0] = temp[0].replace("'","\\\'");
			show_data += "<div id='cell"+(i+1)+"' onmouseover='assignCSSClass(\"cell"+(i+1)+"\",\"ajax_highlight\");' onmouseout='assignCSSClass(\"cell"+(i+1)+"\",\"ajax_normal\");' onclick=\"process_user2('"+temp[1]+"','"+temp[0]+"','','"+the_query+"','"+the_id+"','"+the_display+"','','"+temp[2]+"','"+the_date+"')\" class=\"ajax_normal\">"+display+"</div>\r\n";
		}
		if (show_data != "")
		{
			document.getElementById(the_display).style.display='';
			document.getElementById(the_display).innerHTML=show_data;
		} else
		{
			document.getElementById(the_display).style.display='none';
			document.getElementById(the_display).innerHTML='';
		}
	} else
	{
		document.getElementById(the_display).style.display='none';
		document.getElementById(the_display).innerHTML='';
	}
}

function process_user1(data, the_query, the_id, the_display)
{
	thedata = data.split("|");
	if (thedata.length>0 && thedata != "")
	{
		document.getElementById(the_id).value='';
		show_data = "";
		for (var i=0; i<thedata.length; i++)
		{
			temp = thedata[i].split("--");
			display=temp[0];
			temp[0] = temp[0].replace("'","\\\'");
			show_data += "<div id='cell"+(i+1)+"' onmouseover='assignCSSClass(\"cell"+(i+1)+"\",\"ajax_highlight\");' onmouseout='assignCSSClass(\"cell"+(i+1)+"\",\"ajax_normal\");' onclick=\"process_user2('"+temp[1]+"','"+temp[0]+"','"+temp[2]+"','"+the_query+"','"+the_id+"','"+the_display+"')\" class=\"ajax_normal\">"+display+"</div>\r\n";
		}
		if (show_data != "")
		{
			document.getElementById(the_display).style.display='';
			document.getElementById(the_display).innerHTML=show_data;
		} else
		{
			document.getElementById(the_display).style.display='none';
			document.getElementById(the_display).innerHTML='';
		}
	} else
	{
		document.getElementById(the_display).style.display='none';
		document.getElementById(the_display).innerHTML='';
	}
}

function process_user2(selectedValue, selectedText, selectedBIB, the_query, the_id, the_display, the_bib, selectedDate, the_date)
{
	document.getElementById(the_id).value=selectedValue;
	document.getElementById(the_query).value=selectedText;
	document.getElementById(the_display).style.display='none';
	document.getElementById(the_display).innerHTML='';
	if (the_bib)
	{
		document.getElementById(the_bib).value=selectedBIB;
	}
	if (the_date)
	{
		document.getElementById(the_date).value=selectedDate;
	}
}

function hide_stuff()
{
	var theobject=new Array();
	theobject[0] = 'main_user_search';
	theobject[1] = 'main_participant_search';
	theobject[2] = 'user_id_search';
	theobject[3] = 'main_user_id_search';
	theobject[4] = 'participant_id_search';
	theobject[5] = 'main_participant_id_search';
	theobject[6] = 'leader_par_id_search';
	theobject[7] = 'follower_par_id_search';

	for (i=0; i<theobject.length; i++)
	{
		if (document.getElementById(theobject[i]))
		{
			document.getElementById(theobject[i]).style.display='none';
			document.getElementById(theobject[i]).innerHTML='';
		}
	}
}

function assignCSSClass(elementid,classname)
{
	document.getElementById(elementid).className=classname;
}
