<!--
/*------ start: check start date ------*/

// used by calendar, 设置 TIP
function checkStartDate() {
	// start date
	var regempty    = /^\s*$/;

	var desired_starting_date = document.getElementById('fdt1'); // Rehearsal Date

	if ( regempty.test(desired_starting_date.value) ) {
		desired_starting_date.value = today;
		return false;
	}

	if ( regempty.test(document.getElementById('location').value) ) {
		document.getElementById('location_msg').innerHTML = 'Please select location & room.';
		document.getElementById('location').focus();
		return false;
	} else {
		document.getElementById('location_msg').innerHTML = '';
	}

	calculate_TIP();
	return true;
}

/*------ end: check start date ------*/

// change monthly rent
function calculate_TIP() {

	// Clear out date message
	document.getElementById('date_msg').innerHTML = '';

	/**
	Total Initial Payment 这样计算：

	如果 Date (desired starting date） 在 这个月的

	1-5 TIP （Total Initial Payment）= MR (Monthly Rent) x 2
	6-12 TIP = MR x 1.75
	13-19 TIP = MR x 1.5
	20-28,29,30,31 = MR x 1.25

	如果date 在下个月，TIP = MR x 2
	如果是 lockout room, TIP=2 X MONTHLY RENT
	*/

	/**
	* TIP 右边加上字：
	* tip01: (for SHARED): "Represents your 1-month deposit plus Rent for [Current Month] (your deposit functions as your final month's rent)"
	* tip02: (for SHARED): (if DSD is next month: "Represents your 1-month deposit plus Rent for [Next Month (your deposit functions as your final month's rents)]")
	* tip03: (for LOCKOUT): "Represents your 2-Month Deposit" (your deposit functions as your final 2 month's rent)
	* */

	var desired_starting_date = document.getElementById('fdt1'); // Rehearsal Date
	var d = desired_starting_date.value.split("-");
	d[0] = parseInt(d[0], 10);
	d[1] = parseInt(d[1], 10);
	d[2] = parseInt(d[2], 10);

	var select_month = d[1];

	var select_date = new Date();
	select_date.setYear(d[0]);
	select_date.setMonth(d[1]-1);
	select_date.setDate(d[2]);

	var today = new Date();
	var current_month = today.getMonth()+1;
	var current_day = today.getDate();

	// assume lockout room factor
	var tip_factor = 2;

	if (
	document.getElementById('location').value == 'Floor 1 Room 1 (Shared Room)' ||
	document.getElementById('location').value == 'Floor 2 Room 1 (Shared Room)' ||
	document.getElementById('location').value == 'Floor 2 Room 2 (Shared Room)'
	) {
		// shared room factor
		if (current_month == select_month) {
			// the same month
			if (current_day >= 1 && current_day <= 5) {
				tip_factor = 2;
			} else if (current_day >= 6 && current_day <= 12) {
				tip_factor = 1.75;
			} else if (current_day >= 13 && current_day <= 19) {
				tip_factor = 1.5;
			} else {
				tip_factor = 1.25;
			}
		} else {
			/*
			If desired starting date is next month, it must be the FIRST of month. If user selects any other day of next month, then calendar should simply auto-select the 1st (eg, June 1).
			*/
			if ( select_month == current_month + 1 ) {
				document.getElementById('fdt1').value = d[0] + '-' + select_month + '-01';
			}

			/**
			* If Desired Starting Date is more than 32 days later than Current Date, then Error Message = "Starting Date must be sooner; please email us if you require clarification"
			* 如果 Date 是32天以后，显示 error message
			*/
			var desired_starting_date = document.getElementById('fdt1'); // Rehearsal Date
			var d;
			d = desired_starting_date.value.split("-");
			d[0] = parseInt(d[0], 10);
			d[1] = parseInt(d[1], 10);
			d[2] = parseInt(d[2], 10);

			var select_month = d[1];

			var select_date = new Date();
			select_date.setYear(d[0]);
			select_date.setMonth(d[1]-1);
			select_date.setDate(d[2]);

			var days = 0;
			var difference = 0;

			difference = select_date - today;

			days = difference/86400000;
			if ( days > 62 ) {
				// alert('Starting Date must be sooner; please email us if you require clarification.');
				document.getElementById('date_msg').innerHTML = 'Starting Date is more than 62 days from today.  It must be sooner; please email us if you require clarification.';
				document.getElementById('fdt1').focus();
			}


		}

		var todayDate = new Date();
		var today_m = todayDate.getMonth()+2;
		if(today_m == 13) {
			today_m = 1;
		}
		var desired_starting_date = document.getElementById('fdt1'); // Rehearsal Date
		var arr = desired_starting_date.value.split("-");
		if (parseInt(arr[1], 10) == today_m) {
			// next month
			document.getElementById('tip01').style.display = 'none';
			document.getElementById('tip02').style.display = 'block';
			document.getElementById('tip03').style.display = 'none';
		} else {
			document.getElementById('tip01').style.display = 'block';
			document.getElementById('tip02').style.display = 'none';
			document.getElementById('tip03').style.display = 'none';
		}

	} else {

		// Lockout Room

		/*
		* 如果　ｒｏｏｍ　选　ｌｏｃｋｏｕｔ，　ｄａｔｅ　只能选　这个月，下个月，下下个月的第一天
		* 如果没选第一天　error = "Please Select 1st of the Month; contact us with questions"
		* 文本框日期，本月，下月，下下月的一号 ： d_00，d_01，d_02，d_03
		*/

		// Floor 1 Room 2
		// Floor 2 Room 3

		if ( d[2] != 1 ) {
			// alert('Please Select 1st of the Current Month, Next Month or Next Next Month; contact us with questions.');
			document.getElementById('date_msg').innerHTML = 'Please Select 1st of the Current Month, Next Month or After Next Month; contact us with questions.';
			document.getElementById('fdt1').focus();
		}

		document.getElementById('tip01').style.display = 'none';
		document.getElementById('tip02').style.display = 'none';
		document.getElementById('tip03').style.display = 'block';
	}

	var temp_pattern = /^[1-9]{1}[0-9]{0,12}(\.)?[0-9]{0,10}$/;

	if (temp_pattern.test(document.getElementById('monthly_rent').value)) {
		document.getElementById('amount').value = tip_factor * document.getElementById('monthly_rent').value;
	}
}

function checkPost(obj) {
	with(obj) {

		var regempty    = /^\s*$/;


		/*
		if (agree.checked == false) {
		alert ('Did you read and agree to the indemnification agreement?');
		agree.focus();
		return false;
		}
		*/

		if ( regempty.test(firstname.value) ) {
			alert ('First Name'+' field is required!');
			firstname.focus();
			return false;
		}
		if ( regempty.test(lastname.value) ) {
			alert ('Last Name'+' field is required!');
			lastname.focus();
			return false;
		}

		// check cell phone
		var temp_pattern = /^[0-9]{10,16}$/;
		if (regempty.test(phone.value)) {
			alert ('Cell Phone'+' field is required!');
			phone.focus();
			return false;
		}
		if (!temp_pattern.test(phone.value)) {
			alert ('Cell Phone field must be numeric (length: 10 - 16).');
			phone.focus();
			return false;
		}
		if ( !regempty.test(phone2.value) && !temp_pattern.test(phone2.value) ) {
			alert ('Alt Phone field must be numeric (length: 10 - 16).');
			phone2.focus();
			return false;
		}

		/*-- start: check email address --*/
		if (emailWrong(email)) {
			return false;
		}

		if (!regempty.test(email2.value) && emailWrong(email2, 'Secondary')) {
			return false;
		}
		if (!regempty.test(email3.value) && emailWrong(email3, 'Third')) {
			return false;
		}
		if (!regempty.test(email4.value) && emailWrong(email4, 'Fourth')) {
			return false;
		}
		if (!regempty.test(email5.value) && emailWrong(email5, 'Fifth')) {
			return false;
		}
		if (!regempty.test(email6.value) && emailWrong(email6, 'Sixth')) {
			return false;
		}
		/*-- end: check email address --*/

		if ( regempty.test(address.value) ) {
			alert ('Bandleader(rent-payer) Address'+' field is required!');
			address.focus();
			return false;
		}


		if (location.value == '') {
			alert ('Location'+' field is required!');
			location.focus();
			return false;
		}

		var temp_pattern = /^[1-9]{1}[0-9]{0,12}(\.)?[0-9]{0,10}$/;
		if ( regempty.test(monthly_rent.value) ) {
			alert ('Monthly Rent'+' field is required!');
			monthly_rent.focus();
			return false;
		}
		if (!temp_pattern.test(monthly_rent.value)) {
			alert ('Monthly Rent must be numeric (e.g. 10.55)');
			monthly_rent.focus();
			return false;
		}
		if ( regempty.test(amount.value) ) {
			alert ('Total Initial Payment'+' field is required!');
			amount.focus();
			return false;
		}
		if (!temp_pattern.test(amount.value)) {
			alert ('Total Initial Payment must be numeric (e.g. 10.55)');
			amount.focus();
			return false;
		}

		var shared_room = /Shared Room/;
		if ( location.value.search(shared_room) ) {
			// check slot_days, start time and end time
			var p, slot, slot_num = 'Slot ' + p + ' : ';
			for (p = 1; p < 11; p++) {
				if (document.getElementById('valid_item'+p).value == 1) {
					slot = document.getElementById('slot_days'+p);
					if ( regempty.test(slot.value) ) {
						alert (slot_num + 'Days' + ' field is required!');
						slot.focus();
						return false;
					}
					if (!checkTime(p)) {
						return false;
					}
				}
			}

			if (drumkit[0].checked==false && drumkit[1].checked==false) {
				alert ('Drumkit Storage'+' field is required!');
				drumkit[0].focus();
				return false;
			}
		}
		if ( agree.checked==false ) {
			alert ('You must check the I agree box!');
			agree.focus();
			return false;
		}

		document.getElementById('submitppp').style.display = 'none';
		document.getElementById('progess').style.display = 'block';

		return true;
	}
}

function location_change() {
	// Williamsburg can only starts 2009-08-01
	var reg_location    = /Floor/;

	var location = document.getElementById('location'); // Rehearsal Date

	if ( ! reg_location.test(location.value) ) {
		document.getElementById('fdt1').value='2009-08-01';
	}

	var reg_location    = /Shared Room/;
	if ( reg_location.test(location.value) ) {
		// shared room

		// check which shared room selected
		for (i=1; i<11; i++){
			document.getElementById('room'+i).options.length=0;
			document.getElementById('room'+i).options[0] = new Option("Choose One", "",true,true);
			document.getElementById('room'+i).options[1] = new Option("Gowanus Floor 1 Room 1", "Gowanus Floor 1 Room 1",false,false);
			document.getElementById('room'+i).options[2] = new Option("Gowanus Floor 2 Room 1", "Gowanus Floor 2 Room 1",false,false);
			document.getElementById('room'+i).options[3] = new Option("Gowanus Floor 2 Room 2", "Gowanus Floor 2 Room 2",false,false);
		}

		document.getElementById('choose_item').style.display = 'block';
	} else {
		// lock room
		document.getElementById('choose_item').style.display = 'none';
	}

	calculate_TIP();

}

// change monthly rent
function monthly_rent_change() {
	calculate_TIP();
}

-->
