// JavaScript Document


function updateCalendar() {
	var box = $(this).parents(".calendar_box");
	var target = box.attr("rel");
	var ajax_url = "/include/ajax_date.php"+($(this).attr("rel"))+"&target="+target;
	
	$.get(ajax_url, function(data) {
		box.html(data);
		setCalendarButtons();
	}, "html");	
	return false;
}

function setCalendarButtons() {
	$(".move_month").click(function(e) {
		e.preventDefault();								
		var box = $(this).parents(".calendar_box");
		var target = box.attr("rel");
		var ajax_url = "/include/ajax_date.php"+($(this).attr("rel"));//+"&target="+target;
	
		$.get(ajax_url, function(data) {
			box.html(data);
			setCalendarButtons();
		}, "html");	
		return false;
	});
}

function getPriceType() {	
	switch ($("#price_type").val()) {
		case "advance":
			$(".price_div").hide();
			$(".adv_price_div").show();
			$(".tm_div").show();
			break;
		case "adv-door":
			$(".price_div").show();
			$(".adv_price_div").show();
			$(".tm_div").show();
			break;
		case "door":
			$(".price_div").show();
			$(".adv_price_div").hide();
			$(".tm_div").hide();
			break;
		case "sold-out":
		case "no-cover":
			$(".price_div").hide();
			$(".adv_price_div").hide();
			$(".tm_div").hide();
			break;
	}
}


function validateEmail() {
	var reg = new RegExp('^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$');
	if (reg.test($('#email').val())) {
		$.post("addemail.php", $('#emailform').serialize(), function(data) {
			$('#emailform').hide();
			$('#reply').text(data);
		},
		"text");
		return false;
	} else {
		$("#email").css({'background-color' : '#FAA'});
		return false;
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function isUrl() {

	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	var valid = true;
	
	$('input.link').each(function() {
		if (this.value.trim() != '') {
			if (!regexp.test(this.value)) {
				valid = false;
				$(this).css({'background-color' : '#FAA'});
			} else {
				$(this).css({'background-color' : '#FFF'});
			}
		} else {
			$(this).css({'background-color' : '#FFF'});
		}
	});
	
	if (!valid) {
		$('html').animate({scrollTop: 0}, 'slow'); 
		$('#warning').text("There were errors in your form. Please correct the errors and resubmit").show('slow');	
	}
	
	return valid;
}


$(document).ready(function() {
	//$(".calendar_box").hide();
	$(".calendar_listener").html('<img src="/images/cal_icon.png" alt="Calendar" height="20"/>').click(function(event){		
		
		var cal_box = $("#"+$(this).attr("rel"));
/*
	$(".calendar_box").each(function() {
			if ($(this).get(0) != cal_box.get(0)) {
				//alert(this.id);
				//$(this).hide();
			}
		});
*/		
		cal_box.toggle();
		
		$(".calendar_listener").each(function() {
			if ($("#"+$(this).attr("rel")).css("display") == "block") {
				$(this).html('<img src="/images/close.png" alt="Close" height="20" />');
			} else {
				$(this).html('<img src="/images/cal_icon.png" alt="Calendar" height="20"/>');
			}
		});
	});
	setCalendarButtons();
	
	getPriceType();
	$("#price_type").change(getPriceType);
	
	$(".round").corners("10px");
	$(".box").corners("10px bottom");
	
	
});