// JavaScript Document
var thePeeps = new Array();
var theIndex =-1; //the current person

function enableMe(ele){
	var theName = ele.id;
	document.getElementById(theName+"Skill").disabled=!document.getElementById(theName+"Skill").disabled;
	if(document.getElementById(theName+"Lesson")) document.getElementById(theName+"Lesson").disabled=!document.getElementById(theName+"Lesson").disabled;
	document.getElementById(theName+"Hire").disabled=!document.getElementById(theName+"Hire").disabled;
}

function writePeeps(){
	//we want to go through thePeeps and write the data to the text area "SkillsNeeds"
	if(typeof thePeeps != 'undefined'){
		//the Peeps is defined
		var theText="";
		
		for(var t=0; t<thePeeps.length;t++){
			var thisOne ="";
			var changed=0;
			//write the person first
			thisOne = "--- Person " + (1+t) + "--- \r\n";
			
			//check each sport for it's existance, then drop it into the thisOne string
			if(typeof(thePeeps[t].kitesurf) !='undefined'){
				changed=1;
				thisOne += thePeeps[t].kitesurf.name + " - ";
				thisOne += thePeeps[t].kitesurf.skill + " / ";
				thisOne += thePeeps[t].kitesurf.lesson + " / ";
				thisOne += thePeeps[t].kitesurf.hire + "\r\n";
			}
			if(typeof(thePeeps[t].windsurf) !='undefined'){
				changed=1;
				thisOne += thePeeps[t].windsurf.name + " - ";
				thisOne += thePeeps[t].windsurf.skill + " / ";
				thisOne += thePeeps[t].windsurf.lesson + " / ";
				thisOne += thePeeps[t].windsurf.hire + "\r\n";
			}
			if(typeof(thePeeps[t].surf) !='undefined'){
				changed=1;
				thisOne += thePeeps[t].surf.name + " - ";
				thisOne += thePeeps[t].surf.skill + " / ";
				thisOne += thePeeps[t].surf.lesson + " / ";
				thisOne += thePeeps[t].surf.hire + "\r\n";
			}
			if(typeof(thePeeps[t].snowkite) !='undefined'){
				changed=1;
				thisOne += thePeeps[t].snowkite.name + " - ";
				thisOne += thePeeps[t].snowkite.skill + " / ";
				thisOne += thePeeps[t].snowkite.lesson + " / ";
				thisOne += thePeeps[t].snowkite.hire + "\r\n";
			}
			if(changed==0){
				thisOne+="No sports were added for this person\r\n";
			}
			theText += thisOne;
		}
		
		document.getElementById("SkillsNeeds").value = theText;
	}
	
}


//ok start all over again

function editPeep(){
	//first, check if we've made any people yet, the first time the form is editted, we'll add them
	if(theIndex<0){
		var thisPeep = new Object();
		var theName="kitesurf";
		for(var t=0; t<4;t++){
			//we'll loop through the different sports
			var theSport = new Object();
			if(document.getElementById(theName)){
				if(document.getElementById(theName).checked){
					
					theSport.name=theName;
					theSport.skill=document.getElementById(theName+"Skill").value;
					if(document.getElementById(theName+"Lesson")) theSport.lesson=document.getElementById(theName+"Lesson").value;
					theSport.hire=document.getElementById(theName+"Hire").value;
					
					
				}
			}
			//here we add the new set of data to the object
			if(t==0) {
				thisPeep.kitesurf = theSport;
				theName="windsurf";
			}
			if(t==1){
				thisPeep.windsurf = theSport;
				theName="surf";
			}
			if(t==2) {
				thisPeep.surf = theSport;
				theName="snowkite";
			}
			if(t==3) {
				thisPeep.snowkite = theSport;
			}
		}
		
		thePeeps.push(thisPeep);
		theIndex=thePeeps.length-1;
		
		if(document.getElementById("editPeople")){
			//we only want these to run when the form has the right parts for a multi pass
			appendOptionLast(thePeeps.length,document.getElementById("editPeople"));
			document.getElementById("editPeople").selectedIndex=thePeeps.length-1;
			document.getElementById("editPeople").disabled=false;
		
			document.getElementById("addButton").style.display = "block";
		
			document.getElementById("personTitle").innerHTML = "Sports, skills and needs -  Person " + (thePeeps.length) + " of " + thePeeps.length + " ";
		}
	
	}
	//we should then do the edit routine as normal, a bit wasteful, but nevermind
	var theName="kitesurf";
	var but=theIndex;
	for(var t=0; t<4;t++){
		//we'll loop through the different sports
		var theSport = new Object();
		if(document.getElementById(theName)){
			if(document.getElementById(theName).checked){
				
				theSport.name=theName;
				theSport.skill=document.getElementById(theName+"Skill").value;
				if(document.getElementById(theName+"Lesson")) theSport.lesson=document.getElementById(theName+"Lesson").value;
				theSport.hire=document.getElementById(theName+"Hire").value;
				
			}
		}
		//here we add the new set of data to the object
		if(t==0) {
			thePeeps[theIndex].kitesurf = theSport;
			theName="windsurf";
		}
		if(t==1){
			thePeeps[theIndex].windsurf = theSport;
			theName="surf";
		}
		if(t==2) {
			thePeeps[theIndex].surf = theSport;
			theName="snowkite";
		}
		if(t==3) {
			thePeeps[theIndex].snowkite = theSport;
		}
	}
	
	
	
}

function removeThis(){
	var saveI = theIndex
	removeOptionLast(document.getElementById("editPeople"));
	document.getElementById("editPeople").selectedIndex=theIndex-1;
	switchPeep(document.getElementById("editPeople"));
	thePeeps.splice(saveI-1,1);
	document.getElementById("personTitle").innerHTML = "Sports, skills and needs - edit Person " + (theIndex+1) + " of " + thePeeps.length + " ";
	document.getElementById("addButton").style.display = "block";
	document.getElementById("peopleCount").style.width=(thePeeps.length *17)+"px";
	return false;
}

function addPeep(){
	editPeep();
	//now the add peep function must clear the form and add a new record to edit
	var thisPeep = new Object();
	var theName="kitesurf";
	for(var t=0; t<4;t++){
		//we'll loop through the different sports

		if(document.getElementById(theName)){
			document.getElementById(theName+"Skill").selectedIndex=0;
			document.getElementById(theName+"Hire").selectedIndex=0;
			
			
			document.getElementById(theName+"Skill").disabled=true;
			document.getElementById(theName+"Hire").disabled=true;
			
			document.getElementById(theName).checked=false;
			
			if(document.getElementById(theName+"Lesson")){
				document.getElementById(theName+"Lesson").selectedIndex=0;
				document.getElementById(theName+"Lesson").disabled=true;
			}
		}
		//here we add the new set of data to the object
		if(t==0) {
			theName="windsurf";
		}
		if(t==1){
			theName="surf";
		}
		if(t==2) {
			theName="snowkite";
		}
	}

	
	thePeeps.push(thisPeep);

	appendOptionLast(thePeeps.length,document.getElementById("editPeople"));
	document.getElementById("editPeople").selectedIndex=thePeeps.length-1;
	document.getElementById("personTitle").innerHTML = "Sports, skills and needs - add Person " + (thePeeps.length) + " of " + thePeeps.length + " ";
	document.getElementById("rem").innerHTML = "<a href='#' onclick='return removeThis();'>(Remove)</a>";
	document.getElementById("peopleCount").style.width=(thePeeps.length *17)+"px";
	theIndex=thePeeps.length-1;
	if(theIndex>8){
		document.getElementById("addButton").style.display = "none";
	}
	return false;
}



function switchPeep(ele){
	//before we switch to the record, we need to save any changes made
	var theName="kitesurf";
	
	
	if(theIndex>thePeeps.length - 1){
		theIndex=thePeeps.length-1;
	}
	if(theIndex<0) theIndex=0;
	for(var t=0; t<4;t++){
		//we'll loop through the different sports
		var theSport = new Object();
		if(document.getElementById(theName)){
			if(document.getElementById(theName).checked){
				
				theSport.name=theName;
				theSport.skill=document.getElementById(theName+"Skill").value;
				theSport.hire=document.getElementById(theName+"Hire").value;
				
				document.getElementById(theName+"Skill").disabled=true;	
				document.getElementById(theName+"Hire").disabled=true;
				
				
				document.getElementById(theName+"Skill").selectedIndex=0;
				document.getElementById(theName+"Hire").selectedIndex=0;
				
				if(document.getElementById(theName+"Lesson")){
					theSport.lesson=document.getElementById(theName+"Lesson").value;
					document.getElementById(theName+"Lesson").disabled=true;
					document.getElementById(theName+"Lesson").selectedIndex=0;
				}
				
			}
			document.getElementById(theName).checked=false;
		}
		//here we add the new set of data to the object
		if(t==0) {
			thePeeps[theIndex].kitesurf = theSport;
			theName="windsurf";
		}
		if(t==1){
			thePeeps[theIndex].windsurf = theSport;
			theName="surf";
		}
		if(t==2) {
			thePeeps[theIndex].surf = theSport;
			theName="snowkite";
		}
		if(t==3) {
			thePeeps[theIndex].snowkite = theSport;
		}
	}
	
	
	//we need to get the form to update with the contents of the people array
	theIndex=parseInt(ele.value-1);
	if(theIndex<0) theIndex=0;
	//go through our Peep's sports and fill up the form boxes again
	var eh = thePeeps;
	var theSport=thePeeps[theIndex].kitesurf;
	var theName = "kitesurf";
	for(var t=0; t<4;t++){
		//we'll loop through the different sports
		if(document.getElementById(theName)){

			document.getElementById(theName+"Skill").disabled=true;
			if(document.getElementById(theName+"Lesson")) document.getElementById(theName+"Lesson").disabled=true;
			document.getElementById(theName+"Hire").disabled=true;
			document.getElementById(theName).checked=false;
			
			if(typeof theSport.name != 'undefined'){
				document.getElementById(theName+"Skill").disabled=false;
				if(document.getElementById(theName+"Lesson")) document.getElementById(theName+"Lesson").disabled=false;
				document.getElementById(theName+"Hire").disabled=false;
				document.getElementById(theName).checked = true;
				
				document.getElementById(theName+"Skill").value = theSport.skill;
				if(document.getElementById(theName+"Lesson")) document.getElementById(theName+"Lesson").value = theSport.lesson;
				document.getElementById(theName+"Hire").value = theSport.hire;
			}
		}
		//here we add the new set of data to the object
		if(t==0) {
			theSport = thePeeps[theIndex].windsurf;
			theName="windsurf";
		}
		if(t==1){
			theSport = thePeeps[theIndex].surf;
			theName="surf";
		}
		if(t==2) {
			theSport = thePeeps[theIndex].snowkite;
			theName="snowkite";
		}
	}
	document.getElementById("personTitle").innerHTML = "Sports, skills and needs - edit Person " + (theIndex+1) + " of " + thePeeps.length + " ";
	if(theIndex>0){
		document.getElementById("rem").innerHTML = "<a href='#' onclick='return removeThis();'>(Remove)</a>";
	}else{
		document.getElementById("rem").innerHTML = "";
	}
}

function removeOptionLast(elSel) {
	if (elSel.length > 0)
	{
		elSel.remove(elSel.length - 1);
	}
}
function appendOptionLast(num, ele)
{
	var elOptNew = document.createElement('option');
	elOptNew.text = num;
	elOptNew.value = num;

	try {
		ele.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		ele.add(elOptNew); // IE only
	}
}
