window.addEvent('domready', function(){
	$('send-postcard').addEvent('submit', function(e){
		new Event(e).stop();
		var error = validateSend(this);
		if(!error){
			//alert('caught the form submit');
			$$('#sendButton .send')[0].setStyle('display', 'none');
			$$('#sendButton .ajax-loader')[0].setStyle('display', 'block');
			this.send({
				update: $('send-return-status'),
				onComplete: function(){
					$('send-postcard').setStyle('display', 'none');
					$('send-postcard-explaination').setStyle('display', 'none');
					$$('#sendButton .send')[0].setStyle('display', 'block');
					$$('#sendButton .ajax-loader')[0].setStyle('display', 'none');
				},
				onFailure: function(){
					$$('#sendButton .send')[0].setStyle('display', 'block');
					$ES('#sendButton .ajax-loader')[0].setStyle('display', 'none');
					$('error-sending').setStyle('display', 'block');
				}
			});
		}
	});
});

function validateSend(form){
	var error = false;
	var field = new Array("input_TO_LIST");
	
	for(i=0;i<1;i++){
		document.getElementById(field[i]).style.display='none';
	}
	
	var e = form.TO_LIST.value;	
	if(!validField(e)){
		error = true;
		document.getElementById('input_TO_LIST').style.display='block';
	}
	
	var c = e.indexOf(',');
	if (c<0){
		if(!validEmail(e)){
			error = true;
			document.getElementById('input_TO_LIST').style.display='block';
		}
	}else{
		var emailList=e.split(",")
		for(var i=0;i<emailList.length;i++){
			if(!validEmail(emailList[i])){
				error = true;
				document.getElementById('input_TO_LIST').style.display='block';
			}
		}
	}
	
	if(error){
		document.getElementById('TO_LIST').focus();
	}
	
	return error;
}

var maxValue = 255;
function txtCount(me){
	if (me.value.length>maxValue){
    	me.value=me.value.substring(0,maxValue);
   		return false;
	}
}

function showForm(){
	$('send-postcard-explaination').setStyle('display', 'block');
	$('send-postcard').setStyle('display', 'block');
	$('send-return-status').empty();
}