function processXML(url) {
  // native  object

  if (window.XMLHttpRequest) {
    // obtain new object
    obj = new XMLHttpRequest();
    // set the callback function
    obj.onreadystatechange = processChange;
    // we will do a GET with the url; "true" for asynch
    obj.open("GET", url, true);
    // null for GET with native object
    obj.send(null);
  // IE/Windows ActiveX object
  } else if (window.ActiveXObject) {
    obj = new ActiveXObject("Microsoft.XMLHTTP");
    if (obj) {
      obj.onreadystatechange = processChange;
      obj.open("GET", url, true);
      // don't send null for ActiveX
      obj.send();
    }
  } else {
    alert("Sorry your browser does not support AJAX");
  }
}

function processChange() {
    // 4 means the response has been returned and ready to be processed
    if (obj.readyState == 4) {
        // 200 means "OK"
        if (obj.status == 200) {
			document.getElementById('timetable').innerHTML=obj.responseText;
			updateNavLinks();
			app_button = document.getElementById('app_'+document.getElementById('first_pref_id').value);
			if (app_button != null)
				app_button.disabled=true;
			app_button = document.getElementById('app_'+document.getElementById('second_pref_id').value);
			if (app_button != null)
				app_button.disabled=true;
			app_button = document.getElementById('app_'+document.getElementById('third_pref_id').value);
			if (app_button != null)
				app_button.disabled=true;
        } else {
            alert("There was a problem in the returned data:\n");
        }
    }
}

function showPrevWeek() {
	week--;
	showWeek();
}

function showNextWeek() {
	week++;
	showWeek();
}

function updateNavLinks() {
	if (document.getElementById('prev_week') == null)
		return;
	
	if (week >= 1) {
		document.getElementById('prev_week').innerHTML="<INPUT type='button' onclick='showPrevWeek()' value='Previous Week'>";
	}
	else {
		document.getElementById('prev_week').innerHTML="<INPUT type='button' onclick='showPrevWeek()' value='Previous Week' disabled>";
	}

	if (week <= 8) {
		document.getElementById('next_week').innerHTML="<INPUT type='button' onclick='showNextWeek()' value='Next Week'>";
	}
	else {
		document.getElementById('next_week').innerHTML="<INPUT type='button' onclick='showNextWeek()' value='Next Week' disabled>";
	}
}

function showWeek() {
	updateNavLinks();
	document.getElementById('timetable').innerHTML="<br/><br/><br/><center>Please wait, loading calendar...</center>";
	processXML(booking_url+"&week="+week);
}

function addPreference(prettyInput,input) {
	if (document.getElementById('first_pref_id').value == '') {
		document.getElementById('first_pref').innerHTML=prettyInput + " <INPUT type='button' onclick='removePreference(1)' value='Remove'>";
		document.getElementById('first_pref_id').value=input;
		document.getElementById('first_pref_pretty').value=prettyInput;
		document.getElementById('app_'+input).disabled=true;	
	}
	else if  (document.getElementById('second_pref_id').value == '') {
		document.getElementById('second_pref').innerHTML=prettyInput + " <INPUT type='button' onclick='removePreference(2)' value='Remove'>";
		document.getElementById('second_pref_id').value=input;
		document.getElementById('second_pref_pretty').value=prettyInput;
		document.getElementById('app_'+input).disabled=true;	
	}
	else if  (document.getElementById('third_pref_id').value == '') {
		document.getElementById('third_pref').innerHTML=prettyInput + " <INPUT type='button' onclick='removePreference(3)' value='Remove'>";
		document.getElementById('third_pref_id').value=input;
		document.getElementById('third_pref_pretty').value=prettyInput;
		document.getElementById('app_'+input).disabled=true;	
	}
	else {
		alert("I'm sorry you are only allowed to enter three preferences");
	}
}



function removePreference(input) {
	if (input == 1) {
		app_button = document.getElementById('app_'+document.getElementById('first_pref_id').value);
		if (app_button != null)
			app_button.disabled=false;

		document.getElementById('first_pref_id').value=document.getElementById('second_pref_id').value;
		document.getElementById('first_pref_pretty').value=document.getElementById('second_pref_pretty').value;
		document.getElementById('second_pref_id').value=document.getElementById('third_pref_id').value;
		document.getElementById('second_pref_pretty').value=document.getElementById('third_pref_pretty').value;
		document.getElementById('third_pref').innerHTML="";
		document.getElementById('third_pref_id').value="";
		document.getElementById('third_pref_pretty').value="";
	}
	else if (input == 2) {
		app_button = document.getElementById('app_'+document.getElementById('second_pref_id').value);
		if (app_button != null)
			app_button.disabled=false;
		document.getElementById('second_pref_id').value=document.getElementById('third_pref_id').value;
		document.getElementById('second_pref_pretty').value=document.getElementById('third_pref_pretty').value;
		document.getElementById('third_pref').innerHTML="";
		document.getElementById('third_pref_id').value="";
		document.getElementById('third_pref_pretty').value="";
	}
	else {
		app_button = document.getElementById('app_'+document.getElementById('third_pref_id').value);
		if (app_button != null)
			app_button.disabled=false;
		document.getElementById('third_pref').innerHTML="";
		document.getElementById('third_pref_id').value="";
		document.getElementById('third_pref_pretty').value="";
	}

	if (document.getElementById('first_pref_pretty').value != "")
		document.getElementById('first_pref').innerHTML=document.getElementById('first_pref_pretty').value + "<INPUT type='button' onclick='removePreference(1)' value='Remove'>";
	else
		document.getElementById('first_pref').innerHTML="";
	
	if (document.getElementById('second_pref_pretty').value != "")
		document.getElementById('second_pref').innerHTML=document.getElementById('second_pref_pretty').value + "<INPUT type='button' onclick='removePreference(2)' value='Remove'>";
	else
		document.getElementById('second_pref').innerHTML="";
}


function validateName() {
	nameVar = trim(document.getElementById('name').value);
	
	if (nameVar.length < 4) {
		return "Please enter your full name.";
	}
	else if (nameVar.indexOf(' ') == -1) {
		return "Please enter your full name.";
	}
	else {
		return "";
	}
}


function validateEmail() {
    var trimmed = trim(document.getElementById('email').value);	// value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (trimmed == "") {
        return "Please enter your email address.";
    } else if (!emailFilter.test(trimmed)) {              //test email for illegal characters
        return "The email address entered does not appear to be valid.";
    } else if (trimmed.match(illegalChars)) {
	return "The email address entered does not appear to be valid.";
    } else {
	return "";
    }
}

function validatePhone() {
    var stripped = document.getElementById('phone').value.replace(/[\(\)\.\-\ ]/g, '');    

   if (stripped == "") {
        return "Please enter your phone number.\n";
    } else if (isNaN(parseInt(stripped))) {
        return "The phone number entered does not appear to be valid.";
    } else if ((stripped.length != 10) && (stripped.length != 8)) {
        return "The phone number entered does not appear to be valid.";
    } else {
	return "";
    }
}

function validatePreference() {
	if (document.getElementById('first_pref_pretty').value == "") {
		return "Please select at least one appointment time.";
	}
	else {
		return "";
	}
}

function validateForm() {
	nameError = validateName(document.getElementById('name'));
	if (nameError != "") {
		alert(nameError);
		return false;
	}

	emailError = validateEmail(document.getElementById('email'));
	if (emailError != "") {
		alert(emailError);
		return false;
	}

	phoneError = validatePhone(document.getElementById('phone'));
	if (phoneError != "") {
		alert(phoneError);
		return false;
	}

	preferenceError = validatePreference();
	if (preferenceError != "") {
		alert(preferenceError);
		return false;
	}

	return true;
}

function trim(str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}



