/****************************************************************************
* | 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.firstName.value == '' || f.firstName.value == 'Name' )
	{
		alert("Please enter your First Name");
		f.firstName.focus();
		return false;
	}
	
	if ( f.surname.value == '' || f.surname.value == 'Surname' )
	{
		alert("Please enter your Surname");
		f.surname.focus();
		return false;
	}
	
	if ( f.country.value == '' )
	{
		alert("Please enter your Country of residence");
		f.country.focus();
		return false;
	}
	
	if ( f.emailAddr.value == '' || f.emailAddr.value == 'Email' )
	{
		alert("Please enter your email address");
		f.emailAddr.focus();
		return false;
	} else {
		if ( !valid_email( f.emailAddr.value ) )
		{
			alert("Please enter a valid email address");
			f.emailAddr.focus();
			return false;
		}
	}
	
	var status = do_subscription( f.firstName.value, f.surname.value, f.country.value, f.emailAddr.value );
	
	alert(status);
	
	reset_subs_form( f ); // reset form
	
	return false;
}

/**
* Sets form fields to nothing
* 
* @param f 	: object
* @return void
*/
function reset_subs_form(f)
{
	f.firstName.value = 'Name';
	f.surname.value = 'Surname';
	f.country.selectedIndex = 0;
	f.emailAddr.value = 'Email';
	window.close();
}

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

/**
* Attempts ajax call to subscribe user,
* returns results or error message
* @param n,e 	: string
* @return string
*/
function do_subscription(fn,ln,c,e)
{

	//alert(PATH + '/application/gateway/newsletter.php?do=new_subscription&attribs=' + fn + '|' + ln + '|' + c + '|' + e);
	
	var ajax_results = do_ajax( PATH + '/application/gateway/newsletter.php?do=new_subscription&attribs=' + fn + '|' + ln + '|' + c + '|' + 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[1];
}



