/* Auction results global javascript */

function submitPropertyDataSearchDetails() {

	/* 	# %PUBLIC%
		# NAME:*		submitPropertyDataSearch
		# DESCR:*		Grabs the property data suburb value from the "propertydatasub" text input field
		# :*			and property data state value from the "propertydatastate" select control, checks
		# :*			the suburb name is not null or blank and redirects browser to the propertydata
		# :*			CGI:Application module run mode "propertydata" to search for property data
		# :*			with the given criteria.		
		# USAGE:*		submitPropertyDataSearch();
		# RETURNS:*		false always.
		# %ENDPUBLIC% */

	var suburbname = $('propertydatasub').value;
	var state = $('propertydatastate').value;
	var from = $('fromauction').value;
	// Check that our propertydatasub field is not blank
	if (suburbname == '' || suburbname == null) {
		alertBox('Property Data Search Error:', 'You must enter a suburb name to search for.');
		return false;
	}

	window.location = '/portal/propertydata?rm=propertydata&search=1' + '&suburbname=' + suburbname + "&state=" + state + "&fromauction=" + from;

	return false;
}


function NoReiPopup(state) {
	$('alertbox').style.display = 'block';
	$('StateHeader').innerHTML = state + ' AUCTION RESULTS';
	$('StateName').innerHTML = state;
	$('rei_state').value = state;
	$('NoRei').style.visibility = 'visible';
	$('WrongEmail').style.visibility = 'hidden';
	$('NoReiResult').style.visibility = 'hidden';
	$('alertboxdialog').style.visibility = 'hidden';
	
}

function submitnoreiform() {
	var email = $('rei_email').value;
	var state = $('rei_state').value;
	if (email) {
		if (testEmailAddress(email)) {
				SendEmail(email,state)
		} else {
			$('NoRei').style.visibility = 'hidden';
			$('WrongEmail').style.visibility = 'visible';
			return false;
		}
		
	}
}

function CloseReiPopup() {
		$('alertbox').style.display = 'none';
		$('NoReiResult').innerHTML = '';
		$('NoRei').style.visibility = 'hidden';
		$('WrongEmail').style.visibility = 'hidden';
		$('NoReiResult').style.visibility = 'hidden';
		$('rei_email').value = '';
		$('rei_state').value = '';
		
}

function ActivateReiPopup() {
	$('WrongEmail').style.visibility = 'hidden';
	$('NoRei').style.visibility = 'visible';
}

function SendEmail(email,state) {

	// Assocative array to hold ajax objects
	this.ajaxObjects = new Object();

	// Register global responders that will occur on all AJAX requests
	var obj = this;

	Ajax.Responders.register({

		onCreate: function(request) {

			request['timeoutId'] = window.setTimeout(
					
				function() {
						
					// If we have hit the timeout and the AJAX request is active, abort it and let the user know
						
					if (callInProgress(request.transport)) {
						request.transport.abort();
						alertBox('ViewAlert Registration', 'The server did not respond. Unable to register your ViewAlert.');

						// Run the onFailure method if we set one up when creating the AJAX object
						if (request.options['onFailure']) {							
							request.options['onFailure'](request.transport, request.json);
						}
					}

				}, 30000);
		},

		onComplete: function(request) {
			// Clear the timeout, the request completed
				window.clearTimeout(request['timeoutId']);
			}
	});

	// Collect our ViewAlert params and send off the request.

	var today = new Date();
	var time = today.getTime();
	
	var url = "/portal/auctionresults?rm=sendemail&";
		
	var pars = 'email=' + email + '&state=' + state + '&time=' + time;

	// Abort all objects if they exist
	for (objects in this.ajaxObjects) {
		if (callInProgress(this.ajaxObjects[objects].transport)) {
			this.ajaxObjects[objects].transport.abort();
			window.clearTimeout(app.ajaxObjects[objects]['timeoutId']);
		}
		this.ajaxObjects[objects] = null;
	}

	this.ajaxObjects['NoRei'] = new Ajax.Request( 
		url, 
		{
			method: 'get', 
			parameters: pars,
			onLoading:function() {
					
				},
			onComplete: function(obj) {
					$('loader').style.display = 'none';
					var content = obj.responseText;
					try {
						if (content != false) {
							$('NoRei').style.visibility = 'hidden';
							$('NoReiResult').innerHTML = content;
							$('NoReiResult').style.visibility = 'visible';
																				
						}
					
					} catch(e) {
						// An exception occurred. Print the error.
						printDebug("SendEmail()", e);	
					}
				}		
		}); 

}

