var cnt = 1;
var inviteCount;

//turn on AJAX links
document.getElementById("add_date_link").style.display = "inline";
document.getElementById("add_date_button").style.display = "none";
document.getElementById("add_date_button").disabled = "true";

if(document.getElementById("add_person_link")){
	document.getElementById("add_person_link").style.display = "inline";
}

if(document.getElementById("add_person_button")){
	document.getElementById("add_person_button").style.display = "none";
}

if(document.getElementById("add_person_button")){
	document.getElementById("add_person_button").disabled = "true";
}

function setInviteCount(cnt){
	inviteCount = cnt;
}

function addinvite(namelbl, emaillbl){
	if(inviteCount < 50){
		new Insertion.Bottom('invites', '<li><span>' + namelbl +'</span><input type="text" name="name[]" class="name-input"> <span>' + emaillbl + '</span><input type="text" name="email[]" class="email-input"></li>');
		inviteCount++;
	}else{
		alert("Sorry, but you cannot invite more than 50 people to a meeting");
	}
}	

function adddate(locale){
	var placeholder = document.getElementById("dates");
	
	//load currently selected dates and send them in Ajax call
//	var day = placeholder.childNodes[placeholder.childNodes.length].childNodes[0].value;
//	console.log(placeholder.childNodes[3].childNodes[1].value);
	var lastLi;
	var idx = placeholder.childNodes.length - 1;
	var found = false;
	var day;
	var month;
	var year;
	var hour;
	var min;
	
	while(idx > 0 && !found){
		if(placeholder.childNodes[idx].nodeName == "LI"){
			found = true;
			day = placeholder.childNodes[idx].firstChild.value;
			month = placeholder.childNodes[idx].childNodes[1].value;
			year = placeholder.childNodes[idx].childNodes[2].value;
			hour = placeholder.childNodes[idx].childNodes[3].value;
			min = placeholder.childNodes[idx].childNodes[4].value;
		}
		idx--;
	}
	
	var newLi = document.createElement("li");
	newLi.setAttribute("id", "dt_" + cnt);
	placeholder.appendChild(newLi);
//	console.log(lastLi.innerHTML);
	pars = "day=" + day + "&month=" + month + "&year=" + year + "&hour=" + hour + "&min=" + min + "&locale=" + locale;
	
	var aj = new Ajax.Updater(
		"dt_" + cnt,
		'/meeting/generateDateBoxes',
		{method:"get", parameters: pars, onComplete: addBinIcon}			
		);
	return false;
}


function addBinIcon(){
	var newLi = document.getElementById("dt_" + cnt);
	
	//attach bin to end of new list item
	var binLink = document.createElement("a");
	binLink.href = "javascript:removeDate('" + newLi.id + "')";

	var binImg = document.createElement("img");
	binImg.src = "/templates/_img/bin.gif";
	binImg.alt = removelbl;
	binImg.title = removelbl;
	newLi.appendChild(binLink);
	binLink.appendChild(binImg);
	
	cnt++;
}

function removeDate(id){
	var placeholder = document.getElementById("dates");
	var elem = document.getElementById(id);
	
	placeholder.removeChild(elem);
}


/*
var name = document.getElementById("name");
document.getElementById("my_name").onfocus = function(){
	//name.style.display = "block";
}

name.onclick = function(){
	name.style.display = "none";
	document.getElementById("my_name").focus();
}


document.getElementById("my_name").onblur = function(){
	name.style.display = "none";
}
*/