/*
	BitBallot JavaScript Functions
	Created by Rohit Sodhia
*/

var SITEROOT = '';

function openFile(fileName, label, width, height, scrollbars) {
	if (scrollbars == 1) { scrollbars = 'yes'; }
	else if (scrollbars == 0) { scrollbars = 'no'; }
	window.open(fileName, label, 'width=' + width + ', height=' + height + ', menubar=no, resizeable=no, scrollbars=' + scrollbars + ', status=no');
}

function numDayInMonth(monthExp, year) {
	if (monthExp.test('01-03-05-07-08-10-12')) { return 31; }
	else if (monthExp.test('02')) {
		if ((year / 4) == Math.round(year / 4)) { return 29; }
		else { return 28; }
	} else { return 30; }
}

function setMonthDays(idRoot) {
	var monthExp = new RegExp($('#' + idRoot + '_month').val());
	var numDays = numDayInMonth(monthExp, $('#' + idRoot + '_year').val());
	
	$('#' + idRoot + '_date').html('');
	for (var count = 1; count <= numDays; count++) { $('<option>').val(count).html(count).appendTo($('#' + idRoot + '_date')); }
}

function setNumDays() {
	var idRoot = this.id.split("_")[0];
	
	var openDate = new Date();
	openDate.setMonth($('#openDate_month').val() - 1);
	openDate.setDate($('#openDate_date').val());
	openDate.setFullYear($('#openDate_year').val());
	var closeDate = new Date();
	closeDate.setMonth($('#closeDate_month').val() - 1);
	closeDate.setDate($('#closeDate_date').val());
	closeDate.setFullYear($('#closeDate_year').val());
	
	if (idRoot == 'openDate') {
		var currentDate = new Date();
		if (openDate.getTime() < currentDate.getTime()) {
			$('#openDate_month option:eq(' + currentDate.getMonth() +')').attr('selected', 'selected');
			setMonthDays(idRoot);
			$('#openDate_date option:eq(' + (currentDate.getDate() - 1) + ')').attr('selected', 'selected');
			$('#openDate_year').val() = currentDate.getFullYear();
		}
		if (openDate.getTime() > closeDate.getTime()) {
			$('#closeDate_month option:eq(' + openDate.getMonth() + ')').attr('selected', 'selected');
			setMonthDays(idRoot);
			$('#closeDate_date option:eq(' + (openDate.getDate() - 1) + ')').attr('selected', 'selected');
			$('#closeDate_year').val() = openDate.getFullYear();
		}
	} else {
		if (closeDate.getTime() < openDate.getTime()) {
			$('#closeDate_month option:eq(' + openDate.getMonth() + ')').attr('selected', 'selected');
			setMonthDays(idRoot);
			$('#closeDate_date option:eq(' + (openDate.getDate() - 1) + ')').attr('selected', 'selected');
			$('#closeDate_year').val() = openDate.getFullYear();
		}
	}
}

function setupHelpLinks() {
	$('.help').each(function () {
		$(this).click(function () {
			openFile($(this).attr('href'), 'help', 400, 300, 1);
			return false;
		});
	});
}

function setupDating () {
	var monthExp = new RegExp($('#openDate_month').val());
	var numDays = numDayInMonth(monthExp, $('#openDate_year').val());
	
	$('#openDate_date option:gt(' + (numDays - 1) + ')').remove();
	$('#closeDate_date option:gt(' + (numDays - 1) + ')').remove();
	
	$('#openDate_month').change(setNumDays);
//	$('#openDate_date').change(setNumDays);
	$('#openDate_year').blur(setNumDays);
	$('#closeDate_month').change(setNumDays);
//	$('#closeDate_date').change(setNumDays);
	$('#closeDate_year').blur(setNumDays);
}

$(function () {
//	setupHelpLinks();
	if ($('#page_adminNewBallot').size() || $('#page_adminBallot').size()) { setupDating(); }
	else if ($('#page_adminQuestions').size()) {
//		$('.detailsLink').each(function() {$(this).click(function () {
//			openFile(SITEROOT + '/admin/optionDetails/' + $('#questionType').val() + '/' + $(this).parent().attr('id'), 'candidateDetails', 500, 500, 1);
//			return false;
//		});});
	}
});




/*		$(function () {
			$('.lResume').each(function () {
				$(this).click(function () {
					openFile($(this).attr('href'), 'resume', 500, 300, 1);
					return false;
				});
			});
		});
		window.onload = function () {
			var allA = document.getElementsByTagName("a");
			for (var count = 0; count < allA.length; count++) {
				if (allA[count].id.split("_").length == 2 && allA[count].id.split("_")[0] == "lResume") {
					allA[count].onclick = function () {
						openFile("resume.php?optionID="+this.id.split("_")[1],"intent",500,300);
						return false;
					}
				}
			}
		}*/
