function newEventSpaceLine(){
	$foo = 'eventSpace'+document.getElementById('eventSpaceCount').value;
	if (parseInt(document.getElementById('eventSpaceCount').value) >= 5){
		alert("The maximum number of functions you can enter per event is 5!");
	}else{
		$newVar = parseInt(document.getElementById('eventSpaceCount').value)+1;
		$foo2 = 'eventSpace'+ $newVar;
		document.getElementById($foo).innerHTML='<table width="100%" border="0" cellpadding="10" cellspacing="0"><tr><td width="16%"><INPUT STYLE="font-size : 10px" TYPE="text" NAME="eventDate'+$newVar+'" id="eventDate'+$newVar+'" VALUE="" SIZE=10 onClick="cal5xx.select(document.getElementById(\'eventDate'+$newVar+'\'),\'anchor5xx'+$newVar+'\',\'dd/MM/yyyy\', document.getElementById(\'startDate\').value); return false;"><INPUT TYPE="image" src="/images/icon_cal.gif" alt="" onClick="cal5xx.select(document.getElementById(\'eventDate'+$newVar+'\'),\'anchor5xx'+$newVar+'\',\'dd/MM/yyyy\', document.getElementById(\'startDate\').value); return false;" TITLE="Select a date" NAME="anchor5xx'+$newVar+'" ID="anchor5xx'+$newVar+'"></td><td width="40%"><input type="text" name="funcName'+$newVar+'" id="funcName'+$newVar+'" STYLE="font-size : 10px" size="30" maxlength="30" value=""></td><td width="22%"><SELECT STYLE="font-size : 10px" name="startHour'+$newVar+'" id="startHour'+$newVar+'"><option value="0">00</option><option value="1">01</option><option value="2">02</option><option value="3">03</option><option value="4">04</option><option value="5">05</option><option value="6">06</option><option value="7">07</option><option value="8">08</option><option value="9">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option></SELECT><SELECT STYLE="font-size : 10px" name="startMin'+$newVar+'" id="startMin'+$newVar+'"><option value="00">00</option><option value="15">15</option><option value="30">30</option><option value="45">45</option></SELECT></td><td width="22%"><SELECT name="endHour'+$newVar+'" id="endHour'+$newVar+'" STYLE="font-size : 10px"><option value="0">00</option><option value="1">01</option><option value="2">02</option><option value="3">03</option><option value="4">04</option><option value="5">05</option><option value="6">06</option><option value="7">07</option><option value="8">08</option><option value="9">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option></SELECT><SELECT STYLE="font-size : 10px" name="endMin'+$newVar+'" id="endMin'+$newVar+'"><option value="00">00</option><option value="15">15</option><option value="30">30</option><option value="45">45</option></SELECT></td></tr></table><div id="eventSpace'+$newVar+'" name="eventSpace'+$newVar+'"></div>';
		document.getElementById('eventSpaceCount').value=$newVar;
		document.getElementById('removeEventSpaceBtn').disabled = false;
	}
}
function removeLine(countVar, btnVar, divVar){
	$newVar = parseInt(document.getElementById(countVar).value)-1;
	$foo = divVar+$newVar;
	if ($newVar==1){
		document.getElementById(btnVar).disabled = true;
	}
	document.getElementById($foo).innerHTML='';
	document.getElementById(countVar).value = $newVar;
}
//-----------
function isValidEmail(str) {
	return (str.lastIndexOf(".") > 2) && (str.indexOf("@") > 0) && (str.lastIndexOf(".") > (str.indexOf("@")+1)) && (str.indexOf("@") == str.lastIndexOf("@"));
} 

function executeSubmit(){
	if (document.getElementById('rights').checked){
		//The user has agreed to the terms and conditions
		//Assume that there is nothing wrong with the data the user has supplied...
		 var allClear = true;
		var errMsgs = "<strong>Errors:</strong><br/>";
		//begin error checking
		//Event Tagline
		if (document.getElementById('eventTagline').value.length < 1){
			allClear = false;
			document.getElementById('eventTagline').style.backgroundColor="#f69292";
			errMsgs = errMsgs + "&raquo; A tagline must be provided for the event!<br/>"
		}else{
			document.getElementById('eventTagline').style.backgroundColor="#FFFFFF";
		}

		//Host name
		if (document.getElementById('ownerName').value.length < 4){
			allClear = false;
			document.getElementById('ownerName').style.backgroundColor="#f69292";
			errMsgs = errMsgs + "&raquo; Your full name needs to be provided!<br/>"
		}else{
			document.getElementById('ownerName').style.backgroundColor="#FFFFFF";
		}

		//Host email
		if (isValidEmail(document.getElementById('ownerEmail').value)){
			document.getElementById('ownerEmail').style.backgroundColor="#FFFFFF";
		}else{
			allClear = false;
			document.getElementById('ownerEmail').style.backgroundColor="#f69292";
			errMsgs = errMsgs + "&raquo; Please provide a valid email address!<br/>"
		}

		//Host number
		if (document.getElementById('ownerTel').value.length < 7){
			allClear = false;
			document.getElementById('ownerTel').style.backgroundColor="#f69292";
			errMsgs = errMsgs + "&raquo; A valid telephone number must be provided!<br/>"
		}else{
			document.getElementById('ownerTel').style.backgroundColor="#FFFFFF";
		}
		
		//Event Location
		if (document.getElementById('eventLocation').value.length < 4){
			allClear = false;
			document.getElementById('eventLocation').style.backgroundColor="#f69292";
			errMsgs = errMsgs + "&raquo; A valid location must be provided!<br/>"
		}else{
			document.getElementById('eventLocation').style.backgroundColor="#FFFFFF";
		}

		//Feedback Column
		if (document.getElementById('feedbackCol').value.length < 4){
			allClear = false;
			document.getElementById('feedbackCol').style.backgroundColor="#f69292";
			errMsgs = errMsgs + "&raquo; Please specify a longer column title for your guest feedback!<br/>"
		}else{
			document.getElementById('feedbackCol').style.backgroundColor="#FFFFFF";
		}

		//Check the event space rows
		var evLoopSize = parseInt(document.getElementById('eventSpaceCount').value);
		for (var currEv = evLoopSize; currEv > 0; currEv -=1){
			//$evDate = document.getElementById('eventDate'+$currEv).value;
			//This whole form is based on the premise that there is at least one function they are inviting people to
			//The first row MUST contain some data, all other rows are only useful if there is
			//date OR description information in there
			if ((currEv==1)||(document.getElementById('eventDate'+currEv).value!="")||(document.getElementById('funcName'+currEv).value!="")) {
				//Check date field
				if ((ValidateForm('eventDate'+currEv))&&(getDateObject('eventDate'+currEv,"/")>=getDateObject('startDate',"/"))&&(getDateObject('eventDate'+currEv,"/")<=getDateObject('endDate',"/"))){
					document.getElementById('eventDate'+currEv).style.backgroundColor="#FFFFFF";
				}else{
					allClear = false;
					document.getElementById('eventDate'+currEv).style.backgroundColor="#f69292";
					errMsgs = errMsgs + "&raquo; The date specified for the function on line "+currEv+" should be a date between the start and end date of the event<br/>"
				}
				//Check description field
				if ((isAlphabet('funcName'+currEv))&&(document.getElementById('funcName'+currEv).value.length>0)){
					document.getElementById('funcName'+currEv).style.backgroundColor="#FFFFFF";
				}else{
					allClear = false;
					document.getElementById('funcName'+currEv).style.backgroundColor="#f69292";
					errMsgs = errMsgs + "&raquo; The function name on line "+currEv+" should not contain any special characters and only use characters between A-Z and 0-9<br/>";
				}
				//Check times
				if (timecheck('startHour'+currEv, 'startMin'+currEv, 'endHour'+currEv, 'endMin'+currEv)){
					document.getElementById('startHour'+currEv).style.backgroundColor="#FFFFFF";
					document.getElementById('startMin'+currEv).style.backgroundColor="#FFFFFF";
					document.getElementById('endHour'+currEv).style.backgroundColor="#FFFFFF";
					document.getElementById('endMin'+currEv).style.backgroundColor="#FFFFFF";
				}else{
					document.getElementById('startHour'+currEv).style.backgroundColor="#f69292";
					document.getElementById('startMin'+currEv).style.backgroundColor="#f69292";
					document.getElementById('endHour'+currEv).style.backgroundColor="#f69292";
					document.getElementById('endMin'+currEv).style.backgroundColor="#f69292";
					allClear = false;
					errMsgs = errMsgs + "&raquo; The start time of the event on line "+currEv+" should be before the end time<br/>"
				}
			}else{
				//Nothing in here, remove it
				//..but as we're removing from the bottom upwards, see if there is something in the bottom row that should be copied here...
				var newLastRow = parseInt(document.getElementById('eventSpaceCount').value);
				if (newLastRow != currEv){
					//this means that the actual last row contains data... copy it up
					document.getElementById('eventDate'+currEv).value = document.getElementById('eventDate'+newLastRow).value;
					document.getElementById('funcName'+currEv).value = document.getElementById('funcName'+newLastRow).value;
					document.getElementById('startHour'+currEv).value = document.getElementById('startHour'+newLastRow).value;
					document.getElementById('startMin'+currEv).value = document.getElementById('startMin'+newLastRow).value;
					document.getElementById('endHour'+currEv).value = document.getElementById('endHour'+newLastRow).value;
					document.getElementById('endMin'+currEv).value = document.getElementById('endMin'+newLastRow).value;
					document.getElementById('roomSize'+currEv).value = document.getElementById('roomSize'+newLastRow).value;
					document.getElementById('attendees'+currEv).value = document.getElementById('attendees'+newLastRow).value;
					document.getElementById('config'+currEv).value = document.getElementById('config'+newLastRow).value;
					document.getElementById('eventAV'+currEv).value = document.getElementById('eventAV'+newLastRow).value;
				}
				removeLine('eventSpaceCount','removeEventSpaceBtn', 'eventSpace');
			}
		}
		//submit form
		if (allClear){
			document.createGuestlistForm.submit();
		}else{
			document.getElementById('errorPanel').innerHTML = errMsgs;
			alert("There are problems with the form - please review and correct the highlighted errors.");
		}
	}else{
		alert("You need to accept the Legal and Rights notice before your guestlist can be made!");
	}
}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=2009;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format in the fields should be : dd/mm/yyyy\n\nPlease also ensure that the Start Date is selected first.")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please make sure valid months are entered")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please make sure valid days are entered")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter valid dates")
		return false
	}
return true
}

function isAlphabet(varName)
{
	var alphaExp = /^[a-zA-Z0-9 ]+$/;
	elem = document.getElementById(varName);
	if(elem.value.match(alphaExp)&&(elem.value.length>0))
	{
		return true;
	}
	else
	{
		alert("Please make sure that the event name contains only alphabets and numerals");
		elem.focus();
		return false;
	}
}

function ValidateForm(frmElement){
	var dt=document.getElementById(frmElement);
	if (isDate(dt.value)==false){
		dt.focus();
		return false;
	}
    return true;
 }
