// js/contact.js

// dependencies: common.js, profile-funcs.js

function reveal_form()
{
	$('#check_faq').hide();
	$('#contact_form').show();
}

function send_message()
{
	if ( false == send_message_GATEKEEPER() ) return;
	send_message_SETUP_AJAX();
	ajax.onCompletion = send_message_AJAX_COMPLETE;
	ajax.runAJAX();
}

function send_message_GATEKEEPER()
{
	var test = $.trim($('#txtName').val());
	if ( test == '' )
	{
		safe_alert('Please provide your name.');
		$('#txtName').focus();
		return false;
	}
	
	if ( !check_email('#txtEmail') )
	{
		safe_alert('Please provide a valid return email address.');
		$('#txtEmail').focus();
		return false;
	}
	
	test = $.trim($('#txtSubject').val());
	if ( test == '' )
	{
		safe_alert('Please provide a subject.');
		$('#txtSubject').focus();
		return false;
	}
	
	test = $.trim($('#txtMsg').val());
	if ( test == '' )
	{
		safe_alert('Type your message first!');
		$('#txtMsg').focus();
		return false;
	}
	
	$('#send_button').disabled = true;
	return true;
}

function send_message_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar("name", $.trim($('#txtName').value));
	ajax.setVar("email", $.trim($('#txtEmail').value));
	ajax.setVar("subject", $.trim($('#txtSubject').value));
	ajax.setVar("msg", $.trim($('#txtMsg').value));
	ajax.setVar("submission", "contact");
	ajax.requestFile = "backdoor.php";
}

function send_message_AJAX_COMPLETE()
{
	safe_alert(ajax.response);
	
	// passing any argument at all to this function will prevent the page from reloading
	// this is a slight compromise to help with testing
	if ( arguments.length == 0 )
	{
		// just reload the page we're already on
		redirect(document.URL);
	}
	
	return true;
}

function check_email(_id)
{
	var trimmed = $.trim($(_id).val());
	$(_id).val(trimmed);
	var s = $(_id).val();
	return echeck(s);
}

function echeck(str)
{
	// courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	var valid = true;
	if (str.indexOf(at)==-1) valid = false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) valid = false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) valid = false;
	if (str.indexOf(at,(lat+1))!=-1) valid = false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) valid = false;
	if (str.indexOf(dot,(lat+2))==-1) valid = false;
	if (str.indexOf(" ")!=-1) valid = false;
	
	return valid;
}

// end of file
