var busy = false;

Event.addBehavior({
	
	// eMarket Sign up
	'body form#emarket_signup input#marketing_addr_zip:blur' : function() {
		$('addr_zip_clone').innerHTML = this.value ? this.value : 'N/A';
	},
	
	'body form#emarket_signup input#marketing_email:blur': function() {

		$('email_check').removeClassName('error');
		$('email_check').removeClassName('highlight');
		$('email_check').removeClassName('success');
		
		if ($F('marketing_email').blank()) {
			
			$('email_check').update('Please enter an email address.');
				
		} else {
			new Ajax.Request('/market/emarket/ajax_email_lookup/'+this.value+'/', {onComplete : function(t) { 
				var target = $('email_check');
				var msg;
				var className;
				
				//Clean Class Names
				target.removeClassName('error');
				target.removeClassName('highlight');
				target.removeClassName('success');

				if (t.responseText == 'error') {
					msg = 'Please enter a valid email address.';
					className = 'error';
				} else {
					msg = 'Email address valid.';
					className = 'success';
					
					if (t.responseText == 'marketing') {
						new Effect.BlindDown('personal_info',{duration:0.3,delay:0,queue:'end'});
					} else {
						new Effect.BlindUp('personal_info',{duration:0.3,delay:0,queue:'end'});
					}
				}
				target.addClassName(className);
				target.update(msg);
			
			}});
		}
	},
	
	// eMarket Preferences
	'body form#emarket_preferences input.emarket_format:click' : function() {
		
		if (this.value == 'html' || this.value == '1') {
			if (!$('unsubscribe_section')) {
				if ($('first_background')) $('first_background').removeClassName('withbg');
				if ($('second_background')) $('second_background').addClassName('withbg');
			}
			$$('body form#emarket_preferences .wants_emarkets').invoke('show');
			$$('body form#emarket_preferences .no_emarkets').invoke('hide');			
		} else {
			if (!$('unsubscribe_section')) {
				if ($('second_background')) $('second_background').removeClassName('withbg');
				if ($('first_background')) $('first_background').addClassName('withbg');
			}
			$$('body form#emarket_preferences .wants_emarkets').invoke('hide');
			$$('body form#emarket_preferences .no_emarkets').invoke('show');
		}
		
	},
  
	
	'body form#emarket_preferences input.cat_override:click' : function() {

		if (this.value == 'custom') {
			new Effect.BlindDown('custom_categories',{duration:0.3,delay:0,queue:'end'});
		} else {
			new Effect.BlindUp('custom_categories',{duration:0.3,delay:0,queue:'end'});
		}
	},
	
	'body form#emarket_preferences input.national_override:click' : function() {

		if (this.value == 'custom') {
			new Effect.BlindDown('custom_national',{duration:0.3,delay:0,queue:'end'});
		} else {
			new Effect.BlindUp('custom_national',{duration:0.3,delay:0,queue:'end'});
		}
	},
	
	'body form#emarket_preferences a#toggleMSAs:click' : function() {

		var showMSA; 
		$$('body form#emarket_preferences .msa_sub').each(function (o)  {
		
			// hide and show MSAs
			if ( o.style.display == 'none' ) {
				o.style.display = '';
				showMSA = true;
			} else {
				o.style.display = 'none'
				showMSA = false;
			}
			
		});

		// fix row highlighting
		var MSACounter = 0;
		if ( showMSA ) {
			$$('body form#emarket_preferences div.e_locations .row').each(function(o) {
				o.removeClassName('row-1');
				o.removeClassName('row-2');
				o.addClassName( ( MSACounter % 2 ? 'row-1' : 'row-2' ));
				MSACounter++;
			});
		} else {
			$$('body form#emarket_preferences div.e_locations .msa').each(function(o) {
				o.removeClassName('row-1');
				o.removeClassName('row-2');
				o.addClassName( ( MSACounter % 2 ? 'row-1' : 'row-2' ));
				MSACounter++;
			});

		}


	},
	
	'body form#emarket_step1 input.link_listing:change' : function() {
		if (this.value == 'Yes') {
			$('list_number_wrapper').show();
		} else {
			$('list_number_wrapper').hide();
			$('emarket_list_number').value = '';
		}
	
	},
	
	'body form#emarket_custom_editor input.selection_source:click' : function() {

		if (this.value == 'other') {
			$('url_wrapper').hide();
			$('flyer_wrapper').show();
			$('flyer_files').show();
			//$('flyer_container').show();
		} else {
			//$('flyer_container').hide();
			$('flyer_wrapper').hide();
			$('flyer_files').hide();
			$('url_wrapper').show();
		}
	},
	
	'body div#content div#contentwithsidebar form#emarket_preferences a#select_toggle:click' : function(){

		if (this.innerHTML == 'Check All'){
			this.innerHTML = 'Uncheck All';
			$('emarket_checkboxes').childElements().each( function(a){
				a.childElements().each( function(b){
					if (b.type == 'checkbox') b.checked = true;
				});
			});
		} else {
			this.innerHTML = 'Check All';
			$('emarket_checkboxes').childElements().each( function(a){
				a.childElements().each( function(b){
					if (b.type == 'checkbox') b.checked = false;
				});
			});
		}
	}
	
});


function changeSendType(obj) {
	
	obj.value == 'all' ? $('all').show() : $('all').hide();
	obj.value == 'state' ? $('state').show() : $('state').hide();
	obj.value == 'msa' ? $('msa').show() : $('msa').hide();
	
	calculatePrice();	
}

function updateOtherStates() {
	document.getElementById('other_states_container').style.display = 'none';
	document.getElementById('other_states_loading').style.display = '';
	
	form = $('emarket_step3');
	new Ajax.Updater('other_states_container', '/market/emarket/update_other_states/', {parameters:form.serialize(), onComplete:updateOtherStatesComplete, onFailure:updateOtherStatesFailed, asynchronous:true, evalScripts:true});	
}

function updateOtherStatesComplete(t) {
	document.getElementById('other_states_container').style.display = '';
	document.getElementById('other_states_loading').style.display = 'none';
}

function updateOtherStatesFailed(t) {
	alert('Unable to complete the request. Please try again!');
	document.getElementById('other_states_container').style.display = '';
	document.getElementById('other_states_loading').style.display = 'none';	
}

function updateOtherCBSAs() {
	document.getElementById('other_cbsas_container').style.display = 'none';
	document.getElementById('other_cbsas_loading').style.display = '';
	
	form = $('emarket_step3');
	new Ajax.Updater('other_cbsas_container', '/market/emarket/update_other_cbsas/', {parameters:form.serialize(), onComplete:updateOtherCBSAsComplete, onFailure:updateOtherCBSAsFailed, asynchronous:true, evalScripts:true});	
}

function updateOtherCBSAsComplete(t) {
	document.getElementById('other_cbsas_container').style.display = '';
	document.getElementById('other_cbsas_loading').style.display = 'none';
	calculatePrice();
}

function updateOtherCBSAsFailed(t) {
	alert('Unable to complete the request. Please try again!');
	document.getElementById('other_cbsas_container').style.display = '';
	document.getElementById('other_cbsas_loading').style.display = 'none';	
}

function calculatePrice() {
	document.getElementById('emarket_total_container').style.display = 'none';
//	document.getElementById('emarket_total_loading').style.display = '';
	
	form = document.getElementById('emarket_step3');
	new Ajax.Updater('emarket_total_container', '/market/emarket/calculate_price/', {parameters:Form.serialize(form), onComplete:calculatePriceComplete, onFailure:calculatePriceFailed, asynchronous:true, evalScripts:true});	
}

function calculatePriceComplete(t) {
	document.getElementById('emarket_total_container').style.display = '';
//	document.getElementById('emarket_total_loading').style.display = 'none';
}

function calculatePriceFailed(t) {
	document.getElementById('emarket_total_container').style.display = '';
	document.getElementById('emarket_total_loading').style.display = 'none';
}


function removeAttachment(file) {
	if (confirm('Are you sure you want to remove this file?')) {
		new Ajax.Request('/market/emarket/remove_custom_attachment/'+file, {});
	}
}

function editListing() {
	$('edit_listing').value = 'true';
	$('emarket_custom_editor').submit();
}
