/*
	author: ryan neal
*/

var r_XMLHttp_heap = new Array();
var r_undefined;
var r_boat_id;



//##############################################################################
function r_XMLHttp_request(method, url, params, eID, tasks)
/*
	method = (GET | POST)
	url = duh?
	params = params for url
	eID = the id of the element who's innerHTML will be replaced
	tasks = an Array() of things to do after successful completion

	innerHTML is not in the official standards,. but is useful, and may as well be, as just about everything under the sun plays nice with it.
*/
{
	if (tasks == r_undefined)
	{
		tasks = new Array();
	}
	var success = true;
	var isIE = false;
	//show_element('status_wheel');
	
	var request = new Array();
	if (window.XMLHttpRequest)
	{
		request.push(new XMLHttpRequest());
		if (request[0] && request[0].overrideMimeType)
		{
			request[0].overrideMimeType('text/xml');
		}
	}
	else
	{
		isIE = true;
		if (window.ActiveXObject)
		{
			try
			{
				request.push(new ActiveXObject("Msxml2.XMLHTTP"));
			}
			catch (e)
			{
				try
				{
					request.push(new ActiveXObject("Microsoft.XMLHTTP"));
				}
				catch (e)
				{alert(e)}
			}
		}
	}
	if (request[0])
	{
		request.push(eID);
		var timestamp = new Date();
		request.push(Math.floor(timestamp.getTime()/1000));
		request.push(tasks);
		request[0].onreadystatechange = r_alertContents;
		request[0].open(method, url, true);
		if (method == 'POST')
		{
			request[0].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			request[0].setRequestHeader("Content-length", params.length);
			request[0].setRequestHeader("Connection", "close");
			request[0].send(params);
		}
		else
		{
			if (isIE)
			{
				request[0].send(null);			
			}
			else
			{
				request[0].send();
			}	
		}
		r_XMLHttp_heap.push(request);
	}
	else
	{
		alert('Cannot create XMLHTTP instance');
		success = false;
	}
	return success;
}

//##########################################################################
function r_alertContents()
/*
	shows the nice status_wheel
	pupulates eID.innerHTML with the response, and runs any queued tasks
*/
{
	//show_element('status_wheel');
	var timestamp = new Date();
	for (var i = 0; i < r_XMLHttp_heap.length; i++)
	{
		if (r_XMLHttp_heap[i][0].readyState == 4)
		{
			if(r_XMLHttp_heap[i][0].status == 200)
			{
				document.getElementById(r_XMLHttp_heap[i][1]).innerHTML = r_XMLHttp_heap[i][0].responseText;
				while (r_XMLHttp_heap[i][3].length)
				{
					eval(r_XMLHttp_heap[i][3].pop());
				}
			}
		}
	}

//cleanup
	for (var i = r_XMLHttp_heap.length -1; i >= 0; i--)
	{
		if (r_XMLHttp_heap[i][0].readyState == 4 || (Math.floor(timestamp.getTime()/1000) - r_XMLHttp_heap[i][2]) > 30)
		{
			if ((Math.floor(timestamp.getTime()/1000) - r_XMLHttp_heap[i][2]) > 30)
			{
				r_XMLHttp_heap[i][0].abort();
			}
			r_XMLHttp_heap.splice(i, 1);
		}
	}
//	if (r_XMLHttp_heap.length == 0)
//	{
//		setTimeout("hide_element('status_wheel');", 500);
//	}
}

//##############################################################################
function r_refresh_reviews() 
{
    if ( document.getElementById( 'itbr_msg' ).innerHTML == '' ) {
         params = 'action=get_reviews&boat_id=' + r_boat_id;
         r_XMLHttp_request('POST', '/cgi-bin/new_boats/reviews.cgi', params, 'itbr_user_reviews');
    }
}

//##############################################################################
function r_report( boat_id, review_id )
{
    if ( review_id != r_undefined ) {
        if ( confirm( "Are You Sure You Want To Report This Review?") ) {
            params = 'action=report_review&review_id=' + review_id + '&boat_id=' + boat_id;
            r_XMLHttp_request('POST', '/cgi-bin/new_boats/reviews.cgi', params, 'itbr_user_reviews');
        }
    }
}

//##############################################################################
function submit_review( name, title, body, boat_id)
{
    errors = '';
    err_cnt = 0;
    r_boat_id = boat_id
    if ( name.length > 32 ) {
        err_cnt++;
        errors += '<span style="color:red;">' + err_cnt + ') "Name" is limited to 32 characters</span><br/>';
    }
    if ( title.length > 64 ) {
        err_cnt++;
        errors += '<span style="color:red;">' + err_cnt + ') "Name" is limited to 32 characters</span><br/>';
    }

    if ( title == r_undefined || title == '' ) {
        err_cnt++;
        errors += '<span style="color:red;">' + err_cnt + ") Please Enter a Title for your Review</span><br/>";
    }
    if ( body == r_undefined || body == '' ) {
        err_cnt++;
        errors += '<span style="color:red;">' + err_cnt + ") Can't Submit a blank Review</span><br/>";
    }
    
    if ( errors == '' ) {
        params = 'action=submit_review&name=' + encodeURIComponent(name) + '&title=' + encodeURIComponent(title) + '&body=' + encodeURIComponent(body) + '&boat_id=' + r_boat_id;
        r_XMLHttp_request('POST', '/cgi-bin/new_boats/reviews.cgi', params, 'itbr_submit_box', Array( "r_refresh_reviews();" ) );
    }
    else {
        errors = "The following Errors were found with your review:<br/>" + errors;
        document.getElementById('itbr_msg' ).innerHTML = errors; 
    }
}
