// js/vpc.js

var cal_popup = null;
function vpc_document_ready()
{
	$("#txtStartDate")
		.bind('blur', function(){
			var the_id = '#' + this.id;
			vali_date(the_id, '');
		})
		.bind('dblclick', function(){
			$(this).select();
		});
	$(".day_11>input:text,.day_22>input:text")
		.bind(
			'blur',
			function()
			{
				var num = Math.bound(this.value,0,24);
				if ( isNaN(num) ) num = 0;
				var color = ( num > 0 ) ? '#FF8888' : '';
				$(this).css('background-color', color);
				$(this).val(num);
				calc_parenting_time();
			}
		)
		.bind(
			'keyup',
			function(event)
			{
				var val = parseInt(this.value);
				switch ( event.keyCode )
				{
					case 38:
						val++;
						break;
					case 40:
						val--;
						break;
					default:
						break;
				}
				val = Math.bound(val,0,24);
				var color = ( val > 0 ) ? '#FF8888' : '';
				$(this).css('background-color', color);
				$(this).val(val);
				calc_parenting_time();
			}
		)
		.each(function(){$(this).blur();});
	$("#txtHoursInDay")
		.bind(
			'blur',
			function()
			{
				var num = Math.bound(this.value,1,24);
				if ( isNaN(num) ) num = 24;
				$(this).val(num);
				calc_parenting_time();
			}
		)
		.bind(
			'keyup',
			function(event)
			{
				var val = parseInt(this.value);
				switch ( event.keyCode )
				{
					case 38:
						val++;
						break;
					case 40:
						val--;
						break;
					default:
						break;
				}
				val = Math.bound(val,1,24);
				$(this).val(val);
				calc_parenting_time();
			}
		);
	$(".toggle_day")
		.bind('click', function(){
			cal_popup.invoke(this.id);
		});
	cal_popup = new popup();
	cal_popup.new_val = null;
	cal_popup.init(
		'vpc_cal_popup',
		'generic_bg',
		function(){
			$("#generic_bg")
				.bind('click', function(){
					cal_popup.new_val = -1;
					cal_popup.devoke();
				});
			$('#divPopupDate').removeClass('day_0').removeClass('day_1').removeClass('day_1am').removeClass('day_1pm').removeClass('day_2').removeClass('day_2am').removeClass('day_2pm');
			$('#vpc_cal_popup img').attr('src','img/blank.png');
			if ( $(this._caller_id).hasClass('day_1') )
			{
				$('#divPopupDate').addClass('day_1');
				$('#divPopupDay1 img').attr('src','img/tick.png');
			}
			if ( $(this._caller_id).hasClass('day_1am') )
			{
				$('#divPopupDate').addClass('day_1am');
				$('#divPopupDay1am img').attr('src','img/tick.png');
			}
			if ( $(this._caller_id).hasClass('day_1pm') )
			{
				$('#divPopupDate').addClass('day_1pm');
				$('#divPopupDay1pm img').attr('src','img/tick.png');
			}
			if ( $(this._caller_id).hasClass('day_2') )
			{
				$('#divPopupDate').addClass('day_2');
				$('#divPopupDay2 img').attr('src','img/tick.png');
			}
			if ( $(this._caller_id).hasClass('day_2am') )
			{
				$('#divPopupDate').addClass('day_2am');
				$('#divPopupDay2am img').attr('src','img/tick.png');
			}
			if ( $(this._caller_id).hasClass('day_2pm') )
			{
				$('#divPopupDate').addClass('day_2pm');
				$('#divPopupDay2pm img').attr('src','img/tick.png');
			}
			if ( $(this._caller_id).hasClass('day_0') )
			{
				$('#divPopupDate').addClass('day_0');
				$('#divPopupDay0 img').attr('src','img/tick.png');
			}
			if ( $(this._caller_id).hasClass('first') )   $('#divPopupDate').addClass('first');
			if ( $(this._caller_id).hasClass('holiday') ) $('#divPopupDate').addClass('holiday');
			var _html = $(this._caller_id).html();
			$('#divPopupDate').html(_html);
		},
		function(){
			$("#generic_bg").unbind('click');
			$('#divPopupDate').html('').removeClass('first').removeClass('holiday');
			if ( this.new_val >= 0 )
			{
				var work = this._caller_id.split('_');
				var _d = parseInt(work[1]);
				var _w = parseInt(work[2]);
				var pos = _d + 7 * (_w - 1);
				var cur_val = parseInt(cal_config.charAt(pos));
				$(this._caller_id).removeClass('day_0').removeClass('day_1').removeClass('day_1am').removeClass('day_1pm').removeClass('day_2').removeClass('day_2am').removeClass('day_2pm');
				switch ( parseInt(this.new_val) )
				{
					case 1:
						$(this._caller_id).addClass('day_1');
						break;
					case 2:
						$(this._caller_id).addClass('day_1am');
						break;
					case 3:
						$(this._caller_id).addClass('day_1pm');
						break;
					case 4:
						$(this._caller_id).addClass('day_2');
						break;
					case 5:
						$(this._caller_id).addClass('day_2am');
						break;
					case 6:
						$(this._caller_id).addClass('day_2pm');
						break;
					default:
						$(this._caller_id).addClass('day_0');
						break;
				}
				undo.push(cal_config);
				cal_config = cal_config.replaceCharAt(pos, this.new_val);
				calc_parenting_time();
			}
		},
		-12,
		-12
	);
	$("#chxIncludeAllWeeks")
		.bind(
			'click',
			function()
			{
				$("#calendar>tbody :checkbox").attr('checked', $("#chxIncludeAllWeeks").attr('checked'));
			}
		);
	$("#txtHoursInDay")
		.bind('keyup', calc_total_hours)
		.bind('change', calc_total_hours);
	
	calc_total_hours();
}

function toggle_day(the_id, right_click)
{
	var cur_val = parseInt(cal_config.charAt(pos));
	var new_val = 0;
	$(the_id).removeClass('day_0').removeClass('day_1').removeClass('day_1am').removeClass('day_1pm').removeClass('day_2').removeClass('day_2am').removeClass('day_2pm');
	if ( right_click )
	{
		switch ( cur_val )
		{
			case 0:
				$(the_id).addClass('day_0');
				new_val = 0;
				break;
			case 1:
				$(the_id).addClass('day_1am');
				new_val = 2;
				break;
			case 2:
				$(the_id).addClass('day_1pm');
				new_val = 3;
				break;
			case 3:
				$(the_id).addClass('day_1');
				new_val = 1;
				break;
			case 4:
				$(the_id).addClass('day_2am');
				new_val = 5;
				break;
			case 5:
				$(the_id).addClass('day_2pm');
				new_val = 6;
				break;
			case 6:
				$(the_id).addClass('day_2');
				new_val = 4;
				break;
		}
	}
	else
	{
		switch ( cur_val )
		{
			case 0:
				$(the_id).addClass('day_1');
				new_val = 1;
				break;
			case 1:
			case 2:
			case 3:
				$(the_id).addClass('day_2');
				new_val = 4;
				break;
			case 4:
			case 5:
			case 6:
				$(the_id).addClass('day_0');
				new_val = 0;
				break;
		}
	}
	undo.push(cal_config);
	cal_config = cal_config.replaceCharAt(pos, new_val);
	calc_parenting_time();
}

function calc_total_hours()
{
	totalHours = $("#calendar>tbody :checkbox").length * 7 * parseInt($('#txtHoursInDay').val());
	$('#totalHours').html(commafy(totalHours));
	var val = $('#txtHoursInDay').val();
	$('#numHours').html(val);
	calc_parenting_time();
}

function calc_parenting_time()
{
	var total = 0;
	var _d, day_id, hrs_id, pos, set, tmp;
	for ( var _w = 53; _w > 0; _w-- )
	{
		for ( _d = 0; _d < 7; _d++ )
		{
			day_id = '#day_'+_d+'_'+_w;
			hrs_id = '#txtHours_'+_d+'_';
			if ( $(day_id) )
			{
				pos = ((_w-1)*7) + _d;
				set = parseInt(cal_config.charAt(pos));
				switch ( set )
				{
					case 0:
						break;
					case 1:
					case 2:
					case 3:
						hrs_id += '1';
						total += parseInt($(hrs_id).val());
						break;
					case 4:
					case 5:
					case 6:
						hrs_id += '2';
						total += parseInt($(hrs_id).val());
						break;
				}
			}
		}
	}
	
	hoursNCP = total;
	hoursCP  = totalHours - total;
	pctNCP   = 100 * hoursNCP / totalHours;
	pctCP    = 100 * hoursCP  / totalHours;
	
	tmp = commafy(hoursNCP);
	$("#hoursNCP").html(tmp);
	tmp = commafy(hoursCP);
	$("#hoursCP").html(tmp);
	tmp = precision_round(pctNCP, 1);
	$("#pctNCP").html(tmp.toFixed(1));
	tmp = precision_round(pctCP, 1);
	$("#pctCP").html(tmp.toFixed(1));
}

function batch_toggle(type)
{
	// type 0 - clear all
	// type 1 - odd
	// type 2 - even
	// type 3 - all
	// type 4 - selected
	// type 5 - from data string
	
	var _d, day_id, radio_0, radio_1, radio_2, keep_going, chx, pos, set, tmp_config;
	var classes = ['day_0','day_1','day_1am','day_1pm','day_2','day_2am','day_2pm'];
	tmp_config = cal_config;
	if ( !type )
	{
		for ( _d = 0; _d < 7; _d++ )
		{
			radio_0 = '#radBatch_' + _d + '_0';
			$(radio_0).attr('checked', true);
		}
	}
	for ( var _w = 53; _w > 0; _w-- )
	{
		keep_going = false;
		if ( (type == 0) || (type == 3) || (type == 5) ) keep_going = true;
		if ( (type == 1) && ((_w%2) == 1) ) keep_going = true;
		if ( (type == 2) && ((_w%2) == 0) ) keep_going = true;
		if ( type == 4 )
		{
			chx = '#chxIncludeWeek' + _w;
			keep_going = $(chx).attr('checked');
		}
		
		if ( keep_going )
		{
			for ( _d = 0; _d < 7; _d++ )
			{
				day_id = '#day_'+_d+'_'+_w;
				if ( $(day_id) )
				{
					pos = ((_w-1)*7) + _d;
					if ( type == 5 )
					{
						set = parseInt(tmp_config.charAt(pos));
						$(day_id).removeClass('day_0').removeClass('day_1').removeClass('day_1am').removeClass('day_1pm').removeClass('day_2').removeClass('day_2am').removeClass('day_2pm');
						$(day_id).addClass(classes[set]);
					}
					else
					{
						radio_0 = '#radBatch_' + _d + '_0';
						radio_1 = '#radBatch_' + _d + '_1';
						radio_2 = '#radBatch_' + _d + '_2';
						if ( $(radio_0).attr('checked') )
						{
							$(day_id).removeClass('day_0').removeClass('day_1').removeClass('day_1am').removeClass('day_1pm').removeClass('day_2').removeClass('day_2am').removeClass('day_2pm');
							$(day_id).addClass('day_0');
							tmp_config = tmp_config.replaceCharAt(pos, '0');
						}
						if ( $(radio_1).attr('checked') )
						{
							$(day_id).removeClass('day_0').removeClass('day_1').removeClass('day_1am').removeClass('day_1pm').removeClass('day_2').removeClass('day_2am').removeClass('day_2pm');
							$(day_id).addClass('day_1');
							tmp_config = tmp_config.replaceCharAt(pos, '1');
						}
						if ( $(radio_2).attr('checked') )
						{
							$(day_id).removeClass('day_0').removeClass('day_1').removeClass('day_1am').removeClass('day_1pm').removeClass('day_2').removeClass('day_2am').removeClass('day_2pm');
							$(day_id).addClass('day_2');
							tmp_config = tmp_config.replaceCharAt(pos, '4');
						}
					}
				}
			}
		}
	}
	if ( !type )
	{
		for ( _d = 0; _d < 7; _d++ )
		{
			radio_0 = '#radBatch_' + _d + '_3';
			$(radio_0).attr('checked', true);
		}
	}
	if ( tmp_config != cal_config )
	{
		undo.push(cal_config);
		cal_config = tmp_config;
		calc_parenting_time();
	}
}

function undo_last()
{
	if ( undo.length > 0 )
	{
		cal_config = undo.pop();
		batch_toggle(5);
		calc_parenting_time();
	}
}

function mass_set_batch_toggle_controls(which)
{
	var the_id;
	which = Math.bound(which, 0, 3);
	for ( var _d = 0; _d < 7; _d++ )
	{
		the_id = '#radBatch_' + _d + '_' + which;
		$(the_id).attr('checked', true);
	}
}

function mass_set_hours_per_day_controls(which)
{
	var the_id;
	which = Math.bound(which, 0, 2);
	for ( var _d = 0; _d < 7; _d++ )
	{
		the_id = '#txtHours_' + _d + '_' + which;
		$(the_id).val(0);
		$(the_id).css('background-color', '');
	}
	calc_parenting_time();
}

function save_file()
{
	if ( false == save_file_GATEKEEPER() ) return;
	save_file_SETUP_AJAX();
	ajax.onCompletion = save_file_AJAX_COMPLETE;
	ajax.runAJAX();
}

function save_file_GATEKEEPER()
{
	$('#butSaveFile').attr('disabled', true);
	return true;
}

function save_file_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar("data", make_data_string());
	ajax.setVar("action", '1');
	ajax.setVar("submission", 'vpc');
	ajax.requestFile = "backdoor.php";
}

function save_file_AJAX_COMPLETE()
{
	if ( typeof ajax.response != 'undefined' )
	{
		var work = ajax.response.split('|');
		safe_alert(work[1]);
		if ( parseInt(work[0]) != 0 )
		{
			do_download('download.php?fn='+work[2]+'&type=vpc');
		}
	}
	$('#butSaveFile').attr('disabled', false);
}

function make_data_string()
{
	var data = cal_config;
	var _d, the_id;
	for ( _d = 0; _d < 7; _d++ )
	{
		the_id = '#txtHours_'+_d+'_1';
		data += zero_pad($(the_id).val(), 2);
	}
	for ( _d = 0; _d < 7; _d++ )
	{
		the_id = '#txtHours_'+_d+'_2';
		data += zero_pad($(the_id).val(), 2);
	}
	data += zero_pad($('#txtHoursInDay').val(), 2);
	data += $('#txtStartDate').val().replace('/','').replace('/','');
	data += parseInt($('#selCalendarLength').val());
	data += ( $('#radFirstDotw1').attr('checked') ) ? '1' : '0';
	return data;
}

function load_file()
{
	if ( $('#fileForUpload').val() == '' )
	{
		safe_alert('Please specify a file to upload.');
		return false;
	}
	$('#formFileUploader').submit();
}

function load_file_COMPLETE(status)
{
	switch ( parseInt(status) )
	{
		case 1:
			safe_alert('The file you uploaded does not appear to be a valid VPC file.  If you believe this is an error, please contact PeachForms.com immediately for further assistance.');
			return false;
		case 2:
			safe_alert('There was a problem uploading your file.  Please contact PeachForms.com immediately for further assistance.');
			return false;
		case 3:
			safe_alert('The ability to load data into a PeachForms.com application is only available to current subscribers.  Your subscription has expired.');
			return false;
		case 4:
			safe_alert('Please log in first.');
			return false;
		default:
			redirect('index.php?page=vpc');
			return true;
	}
}

function reset_calendar_display()
{
	if ( false == reset_calendar_display_GATEKEEPER() ) return;
	reset_calendar_display_SETUP_AJAX();
	ajax.onCompletion = reset_calendar_display_AJAX_COMPLETE;
	ajax.runAJAX();
}

function reset_calendar_display_GATEKEEPER()
{
	var start = $('#txtStartDate').val();
	var len   = $('#selCalendarLength').val();
	var dotw  = $('#radFirstDotw1').attr('checked') ? 1 : 0;
	if ( (start == default_start) && (parseInt(len) == parseInt(default_len)) && (parseInt(dotw) == parseInt(default_dotw)) )
	{
		batch_toggle(0);
		return false;
	}
	return true;
}

function reset_calendar_display_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar("data", make_offset_data_string());
	ajax.setVar("action", '2');
	ajax.setVar("submission", 'vpc');
	ajax.requestFile = "backdoor.php";
}

function reset_calendar_display_AJAX_COMPLETE()
{
	if ( typeof ajax.response != 'undefined' )
	{
		redirect('index.php?page=vpc');
	}
}

function make_offset_data_string()
{
	var start = $('#txtStartDate').val();
	var offset = determine_week_offset(default_start, start) * 7;
	var offset_abs = Math.abs(offset);
	if ( offset )
	{
		if ( offset_abs >= 371 )
		{
			cal_config = '0'.repeat(371);
		}
		else
		{
			var zeroes = '0'.repeat(offset_abs);
			var keep_length = cal_config.length - offset_abs;
			var keep = ( offset > 0 ) ? cal_config.substring(offset_abs) : cal_config.substring(0, keep_length);
			cal_config = ( offset > 0 ) ? keep + zeroes : zeroes + keep;
		}
	}
	
	var dotw  = $('#radFirstDotw1').attr('checked') ? 1 : 0;
	default_dotw = parseInt(default_dotw);
	if ( dotw != default_dotw )
	{
		var work = string_to_date_object(start);
		var weekday = work.getDay();
		if ( weekday == 0 )
		{
			cal_config = dotw ? cal_config = '000000' + cal_config.substring(0,365) : cal_config.substring(6) + '000000';
		}
		else
		{
			cal_config = dotw ? cal_config.substring(1) + '0'.repeat(1) : cal_config = '0' + cal_config.substring(0,(370));
		}
	}
	
	return make_data_string();
}

function determine_week_offset(past, future)
{
	var work, M, D, Y, past_weekday, past_unix, future_weekday, future_unix, one_day, num_weeks;
	var dotw  = $('#radFirstDotw1').attr('checked') ? 1 : 0;
	
	work = string_to_date_object(past);
	past_weekday = work.getDay();
	if ( (past_weekday == 0) && (dotw == 1) ) past_weekday = 7;
	past_unix = work.getTime();
	
	work = string_to_date_object(future);
	future_weekday = work.getDay();
	if ( (future_weekday == 0) && (dotw == 1) ) future_weekday = 7;
	future_unix = work.getTime();
	
	one_week = 1000*60*60*24*7;
	if ( future_unix > past_unix )
	{
		num_weeks = Math.floor((future_unix-past_unix)/one_week);
		if ( future_weekday < past_weekday ) num_weeks++;
	}
	else
	{
		num_weeks = Math.ceil((future_unix-past_unix)/one_week);
		if ( future_weekday > past_weekday ) num_weeks--;
	}
	return num_weeks;
}

function toggle_vpc_header()
{
	if ( $('#chxVPCHeader').attr('checked') )
	{
		$('#txtVPCHeader').attr('disabled','');
		$('#vpc_header').html($('#txtVPCHeader').val());
	}
	else
	{
		$('#txtVPCHeader').attr('disabled','disabled');
		$('#vpc_header').html('');
	}
}

function update_vpc_header()
{
	$('#vpc_header').html($('#txtVPCHeader').val());
}

// end of file
