/****************************************************************************
* | Important Note:
* | This source code belongs to Active Ice CC and forms part of 
* | the Retained Copyright governed by the terms and conditions 
* | of service located at http://activeice.co.za/terms/
* | Reuse of this entire file and/or it's scripts without 
* | permission from Active Ice CC is prohibited
* |
* | /seed/newsletter.js		ajax newsletter
* | dependants:				/ajax/ajax.js
* | created: 				20 Mar 2007
* | updated: 				25 Mar 2007
*/


/**
* Validates user input, calls subscription function
* @param f 	: form as object
* @return string
*/
function subscribe(f)
{
	
	if ( !( f.nl_full_name && f.nl_email ) )
	{
		alert("Incorrect naming of fields");
		return false;
	}
	
	if ( f.nl_full_name.value == '' || f.nl_full_name.value == 'Name' )
	{
		alert("Please enter your Full Name");
		f.nl_full_name.focus();
		return false;
	}
	
	if ( f.nl_email.value == '' || f.nl_email.value == 'Email' )
	{
		alert("Please enter your email address");
		f.nl_email.focus();
		return false;
	}
	
	if ( !valid_email( f.nl_email.value ) )
	{
		alert("Please enter a valid email address");
		f.nl_email.focus();
		return false;
	}
	
	var status = do_subscription( f.nl_full_name.value, f.nl_email.value );
	
	show_hide('subscribe-form');
	add_html('subscribe-form-status', status[1] + '<br /><a href="javascript:reset_nl_form();">Back</a>');
	show_hide('subscribe-form-status');
	
	return false;
}

/**
* Sets form fields to nothing
* 
* @param f 	: object
* @return void
*/
function reset_nl_form()
{
	show_hide('subscribe-form-status');
	show_hide('subscribe-form');
}

/**
* Show progress to user
* 
* @param msg	: string
* @return void
*/
function show_progress(msg)
{
	f.nl_full_name.value = '';
	f.nl_email.value = '';
}

/**
* Attempts ajax call to subscribe user,
* returns results or error message
* @param n,e 	: string
* @return string
*/
function do_subscription(n,e)
{
	
	var ajax_results = do_ajax( PATH + '/application/gateway/seed/newsletter.php?do=new_subscription&attribs=' + n + '|' + e );
	
	var results = ajax_results.split('|');
	
	// if we dont have a pipe, could be a PHP error..
	if ( !( results[0] && results[1] ) )
	{
		alert('Error: ' + ajax_results );
		return false;
	}
	
	return results;
}



