function checkEmail(inputvalue)
{
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;

	if(pattern.test(inputvalue))
	{
		jQuery('#submit').removeAttr('disabled');
		return true;
    }
	else
	{
		return false;
    }
}


function checkEmailDetail(input)
{
	var pass = true; //set the default to true
	input = input.toLowerCase(); //grab the input from user and convert to lowercase

	//validate the email address to be in the form: abc@abc.com
	var emailCheck = true;
	
	emailCheck = checkEmail(input);
	if (!emailCheck)
	{
		jQuery('#submit').attr('disabled', 'disabled');		
		jQuery('#message').html("<p class='error'>Please enter a valid email address</p>");
		return false;
	}
	else
	{
		jQuery('#message').html("<p class='success'></p>");
	}

	//also check that it's not a webmail address 
	var exclusionList = new Array("gmail",
								  "googlemail", 
								  "yahoo",
								  "excite",
								  "hotmail",
								  "email"
								  );

	//get domain of input
	var atsign = input.substring(0,input.lastIndexOf('@')+1);
	var domain = input.substring(atsign.length,input.length+1);
	var domainName = domain.substring(0,domain.indexOf('.'));

	//check that domain of email address is not in exclusion list
	for (var i=0; i < exclusionList.length; i++)
	{
		if (exclusionList[i] == domainName)
		{
			pass = false;
		}
	}

	//if the email address is valid, continue and insert into db
	// otherwise, spit an error message
	if (!pass)
	{
		jQuery('#submit').attr('disabled', 'disabled');
		jQuery('#message').html("<p class='error'>Please enter a valid company address</p>");
		return false;
	}
	else
	{
		jQuery('#submit').removeAttr('disabled');

		jQuery('#message').html("<p class='success'></p>");
		return true;
	}

}

function setEmailValue(value)
{
	jQuery('#email').attr('value',value);
}


function redirect()
{
	jQuery("#countdown_sign_up").show();
	jQuery("#countdown_sign_up").html("<p>You will be redirected in 6 seconds</p>");
	jQuery("#countdown_sign_up").fadeIn().animate({ opacity: 1.0 },6000).fadeOut(0, function() {
		//check which option was selected
		var sector = jQuery("input[@name='sector']:checked").val();
	
		if (sector == 1)	//Business
		{
			window.location.replace("http://g2.newzstand.com/grp/bzn/an");
		}
		else if (sector == 2) //Marketing
		{
			window.location.replace("http://g2.newzstand.com/grp/mkt/an");
		}
		else if (sector == 3) //technology
		{
			window.location.replace("http://g2.newzstand.com/grp/tech/an");
		}
		else if (sector == 4) //finance
                {
                        window.location.replace("http://g2.newzstand.com/grp/fin/an");
                }


	}
	);

}



function save_email()
{
	jQuery('#message').html("Please wait...");
	
	jQuery.ajax({
		type: "GET",
		url: "http://www.newzstand.com/wp-includes/default/save_email.php",
		data: ({ 
			email : jQuery('#email').val(),
			special : jQuery('#special').val()
			}),
		success: function(html)
		{
			if (html == "success")
			{
				jQuery('#message').html("<p class='success'>Thank you for requesting a Beta invite. We will be contacting you shortly.</p>");
			}
			else if (html == "emailexists")
			{
				jQuery('#message').html("<p class='error'>That email address already exists.</p>");
			}
		}
	});
}

function save_email_sign_up()
{
	//get values in form
	var sector = jQuery("input[@name='sector']:checked").val();
	var email = jQuery('#email').val();
	
	jQuery('#message_sign_up').show();
	jQuery('#message_sign_up').html("Please wait...");
	
	jQuery.ajax({
		type: "GET",
		url: "http://www.newzstand.com/wp-includes/default/save_email_sign_up.php",
		data: ({ 
			email : email,
			sector : sector
			}),
		success: function(html)
		{
			if (html == "success")
			{
				jQuery('#message_sign_up').html("<p class='success'>Thank you for signing up. A confirmation email has been sent to your address.</p>");
				redirect();
			}
			else if (html == "emailexists")
			{
				jQuery('#message_sign_up').html("<p class='error'>That email address already exists.</p>");
			}
		}
	});
}

