

window.addEvent('domready', function(){

	// check to see if id #list exists
	if ($('list')) {
	
		// add a listener to the image
		$('list').getElement('img.add').addEvent('click', function(c){
			addToCM(c);			
		});
		
		// add listener for form submission
		$('frm-list').addEvent('submit', function(s) {
			addToCM(s);
		});

	}

});

// function for mailchimp form
function addToCM(ev) {
			
	if (ev) ev.stop();
				
	// send request to cm
	// returns a json string
	new Request.JSON({
	  url: 'cms/modules/cm.php',
	  data: {
	    	method: 'get',
	        email: $('frm-email').get('value')
	  }
	})
	.addEvent('success', function(j){
		if (j){
		
			$('list').setStyle('display','none');
			$('submit').setStyle('display','none');
		
			// if j == 1 we've not received an error
			if (j == '1'){
				$('success').setStyle('display','block');
			}
			// otherwise it must = 0 and we have a problem
			// give them warning and then give them option to try again.
			else {
				$('failure').setStyle('display','block');
			}
				
			$$('a.tryagain')[0].addEvent('click', function(t){
				$('list').setStyle('display','block');
				$('failure').setStyle('display','none');
			});					
			
		}
	})
	.send();

}


