﻿var xmlhttp;
var SERVER_URL = "http://www.vintageblue.com.au/cgi-bin/cgiemail/subscribe.txt";

function doSubmit(objForm){    

	var errorMessage = '';
	
	if( isEmptyText(objForm.email) || ! isValidEmail(objForm.email.value) )
		errorMessage += 'Please enter a valid email address';
		
	if( errorMessage != '')
		alert(errorMessage);
	else
		return doSubscribe(objForm,objForm.email.value);
	return false;	
}


function doSubscribe(objForm,semail){
	xmlhttp = getHTTPObject();
	var bJSPost =  (xmlhttp != null);
//	alert(bJSPost);
	var gourl=SERVER_URL;
	if (bJSPost)
	{
		var parameters= "email="+semail+"&success=/postsuccesfull.html";
		xmlhttp.onreadystatechange=onSubscribeResult;
		try
		{
		xmlhttp.open('POST', gourl, true);
	    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    xmlhttp.setRequestHeader("Content-length", parameters.length);
	    xmlhttp.setRequestHeader("Connection", "close");
	    xmlhttp.send(parameters);
		}
		catch (e)
		{
//			alert('catch error');
			bJSPost =  false;
			objForm.action= gourl;
		}
	}
	else
	{
		objForm.action= gourl;
//		alert (objForm.action);
	}
	return !bJSPost;
}

function onSubscribeResult()
{
	var oMsg = document.getElementById('message');
	//oMsg.style.display='block';
	
	if(xmlhttp.readyState!=4){
		oMsg.innerHTML="<div id='loading'>Loading...</div>";
	}
	if(xmlhttp.readyState==4){
		if (trim(xmlhttp.responseText) =='postsuccesfull')
		{
			oMsg.innerHTML= '<p>Thank you for your subscription</p>';
		}
		else
		{
			oMsg.innerHTML= '<p>Sorry, subscriptions are not available at this time</p>';
		}
	}
}
