var catTexts = [];
var onlineRate = null;
function openPhotoWindow(url) {
	open(url, "Classified Ad Photos", "toolbar=no, menubar=no, location=no, width=500, height=510, resizable=yes");
	return false;
}

function initDatePicker() {
	if ( typeof( window[ 'issueDay' ] ) == "undefined" ) {
		issueDay = 3;
	}
	
	if ( typeof( window[ 'earliestIssueDate' ] ) == "undefined" ) {
		earliestIssueDate = 0;
	} else {
		//make date object from earliestIssueDate string
		earliestIssueDate = new Date(earliestIssueDate);
	}
	
	jQuery("#ClassifiedAdBillStartDate").datepicker({
		dateFormat: jQuery.datepicker.ATOM, 
		minDate: earliestIssueDate,
		beforeShowDay: onlyIssueDay
	});
}

function onlyIssueDay(date) {
	return [date.getDay() == issueDay, '', 'Choose an Issue Date'];
}

function onlineOnlyForm() {
	$('ClassifiedAdBillOnlineOnly').observe('change', togglePrintElems);
	if($('ClassifiedAdBillOnlineOnly').checked) {
		hidePrintElementsQuick();
		hideBorderStylesQuick();
		swapCatPrices();
		if(view == 'edit') {
			showWeekEditButton();
		}
	} else {
		showBorderStylesQuick();
		if(view == 'edit') {
			hideWeekEditButton();
		}
	}
	
	
}

function enableProcessingDiv() {
	if($('processing')) {
		$('processing').hide();
		if('ClassifiedAdBillCheckoutForm') {
			$('ClassifiedAdBillCheckoutForm').observe('submit', showProcessingDiv);
		}
	}
}

function showProcessingDiv() {
	if($('processing')) {
		$('processing').show();
	}
	return;
}

function togglePrintElems(event) {
	var oOCb = event.element();
	if(oOCb.checked) {
		//gather up all the elements we don't need and remove them
		hidePrintElements();
		hideBorderStyles();
		swapCatPrices();
		if(view == 'edit') {
			showWeekEditButton();
		}
	} else {
		//show the print-only elements
		showPrintElements();
		showBorderStyles();
		restoreCatPrices();
		if(view == 'edit') {
			hideWeekEditButton();
		}
	}
}

function hidePrintElements() {
	if($('print_options')) {
		Effect.BlindUp($('print_options'), { duration: 0.5 });
	}
}

function showPrintElements() {
	if($('print_options')) {
		Effect.BlindDown($('print_options'), { duration: 0.5 });
	}
}

function hidePrintElementsQuick() {
	if($('print_options')) {
		$('print_options').hide();
	}
}

function showPrintElementsQuick() {
	if($('print_options')) {
		$('print_options').show();
	}
}

function hideBorderStyles() {
	if($('borderstyles')) {
		Effect.BlindUp($('borderstyles'), { duration: 0.5 });
		if($('borderstyles_alt')) {
			Effect.BlindDown($('borderstyles_alt'), { duration: 0.5, delay: 0.5 });
		}
	}
}

function showBorderStyles() {
	if($('borderstyles_alt')) {
		Effect.BlindUp($('borderstyles_alt'), { duration: 0.5 });
		if($('borderstyles')) {
			Effect.BlindDown($('borderstyles'), { duration: 0.5, delay: 0.5 });

		}
	}
}

function hideBorderStylesQuick() {
	if($('borderstyles')) {
		$('borderstyles').hide();
		if($('borderstyles_alt')) {
			$('borderstyles_alt').show();
		}
	}
}

function showBorderStylesQuick() {
	if($('borderstyles_alt')) {
		$('borderstyles_alt').hide();
		if($('borderstyles')) {
			$('borderstyles').show();

		}
	}
}

function showWeekEditButton() {
	if($('editweeks')) {
		$('editweeks').show();
	}
}

function hideWeekEditButton() {
	if($('editweeks')) {
		$('editweeks').hide();
	}
}

function swapCatPrices() {
	onlineRate = getOnlineRate();
	//console.log('onlineRate = ' + onlineRate);
	var rateRE = /(\$\d{1,2}\.\d{2})/;
	var catOptions = $('ClassifiedAdClassifiedChildCatId').options;
	for( var i = 0; i < catOptions.length; i++) {
		if ((catRate = rateRE.exec(catOptions[i].text)) != null) {
			if(catRate[1] != undefined) {
				catTexts[i] = catRate[1];
				var text = catOptions[i].text;
				text = text.replace(catRate[1], onlineRate);
				$('ClassifiedAdClassifiedChildCatId').options[i].text = text;
			}
			
		}
	}
}

function restoreCatPrices() {
	if(catTexts != undefined) {
		if(onlineRate == null) onlineRate = getOnlineRate();
		var rateRE = /(\$\d{1,2}\.\d{2})/;
		var catOptions = $('ClassifiedAdClassifiedChildCatId').options;
		for( var i = 0; i < catOptions.length; i++) {
			if ((catRate = rateRE.exec(catOptions[i].text)) != null) {
				var text = catOptions[i].text;
				text = text.replace(catRate[1], catTexts[i]);
				$('ClassifiedAdClassifiedChildCatId').options[i].text = text;
			} 
			
		}
	} else {
		console.log('restoreCatPrices() thinks catTexts is undefined');
	}
}

function enableTabs() {
	tabs = new TabsController();
}

var TabsController = Class.create();
TabsController.prototype = {
	initialize: function() {
		$$('#tabbed_menu li').invoke('observe', 'click', this.showSelected);
		this.hideAll();
		$('step1-tab').addClassName('current_tab');
		$('step1').show();
	},
	
	hideAll: function() {
		$$('.tabbed').invoke('hide');
	},
	
	showSelected: function() {
		var divId = this.id.split('-');
		divId = divId[0];
		tabs.hideAll();
		
		this.siblings().invoke('removeClassName', 'current_tab');
		this.addClassName('current_tab');
		$(divId).show();
		return false;
	}
};

function enableRadioToggle(formId) {
	rt = new RadioToggler(formId);
}

var RadioToggler = Class.create();
RadioToggler.prototype = {
	initialize: function(formId) {
		this.isDisabled = false;
		if(typeof($(formId)) != undefined) {
			this.currentForm = $(formId);
		} else {
			console.log('form is undefined to RadioToggler');
			this.isDisabled = true;
		}
		if(!this.isDisabled) {
			//continue initialization
			this.toggleRadio.bind(this);
			this.radioArray = new Array();
			this.radios = $(formId).getInputs('radio');
			for(var i = 0; i < this.radios.length; i++) {
				this.radioArray[this.radios[i].id] = this.radios[i].checked;
				this.radios[i].observe('click', this.toggleRadio.bind(this));
			}
		}	
	},
	
	toggleRadio: function(event) {
		var thisRadio = event.element();
		var radioID = thisRadio.id;
		if(this.radioArray[radioID]) {
			thisRadio.checked = false;
			this.radioArray[radioID] = false;
		} else {
			thisRadio.checked = true;
			this.radioArray[radioID] = true;
		}
		for(var i = 0; i < this.radios.length; i++) {
			this.radioArray[this.radios[i].id] = this.radios[i].checked;
		}
	}
};

function getOnlineRate() {
	//console.log('wklyrates.length = ' + wklyrates.length);
	for (var i = 1; i < wklyrates.length; i++) {
		if(wklyrates[i].name == 'Online') {
			return '$'+parseFloat(wklyrates[i].rate).toFixed(2);
		}
	}
}

function observeEditWeeks() {
	hideTotalWeeks();
	if($('editweeks')) 
		$('editweeks').observe('click', toggleTotalWeeks);
	
	if($('update_rundates'))
		$('update_rundates').observe('click', submitForm);
}

function hideTotalWeeks() {
	if($('totalweeks')) {
		$('totalweeks').hide();
	}
}

function toggleTotalWeeks(event) {
	if($('totalweeks')) {
		$('totalweeks').toggle();
	}
	event.stop();
}

function initFormCheck(formId) {
	if(typeof($(formId)) != undefined) {
		var currentForm = $(formId);
		currentForm.observe('submit', submitForm);
	} else {
		console.log('form is undefined to initFormCheck');
	}
}

function checkFields() {
	//make sure all required form fields are set
	refreshPreview(false);
	
	if($F('ClassifiedAdClassifiedChildCatId')) {
		if($F('ClassifiedAdBillWordCount') > 0) {
			if($F('ClassifiedAdBillStartDate')) {
				return true;
			} else {
				alert('Please choose a starting date for your ad (Step 4).');
			}
		} else {
			alert('Please enter ad text for your ad. (Step 2)')
		}
	} else {
		alert('Please choose a category for your ad. (Step 1)');
	}
	return false;
}

function submitForm(event) {
	if(checkFields()) {
		// $('ClassifiedAdEditForm').submit();
		event.element().submit();
	} else {
		
		event.stop();
	}

}
