// Validation

function getKeyCode(e) {
	try {
		return event.keyCode;
	}
	catch(err) {
		return e.which;
	}
}

function testRegEx(e, re) {
	var kc = getKeyCode(e);
	var key = String.fromCharCode(kc);
	return (re.test(key) || kc == 0 || kc == 8 || kc == 9);
}

function digitsOnly(e) {
	return testRegEx(e, /[\d]/);
}

function phoneChar(e) {
	return testRegEx(e, /[\d #*()+\-]/);
}

function validEmail(addr) {
	var i, re, eArr, uArr, dArr, uArrLen, dArrLen;
	eArr = addr.split("@");
	if (eArr.length != 2) {
		return false;
	}
	uArr = eArr[0].split(".");
	uArrLen = uArr.length;
	re = /^[a-zA-Z0-9+\-=\^_{}~]+$/;
	for (i=0;i<uArrLen;i++) {
		if (!re.test(uArr[i])) {
			return false;
		}
	}
	dArr = eArr[1].split(".");
	dArrLen = dArr.length;
	if (dArrLen < 2) {
		return false;
	}
	re = /^[a-zA-z]{2,6}$/;
	if (!re.test(dArr[dArrLen-1])) {
		return false;
	}
	re = /^([a-zA-Z0-9][a-zA-Z0-9\-]*)?[a-zA-Z0-9]$/;
	for (i=0;i<(dArrLen-1);i++) {
		if (!re.test(dArr[i])) {
			return false;
		}
	}
	return true;
}

function validDate(d, m, y) {
	d = parseInt(d, 10);
	m = parseInt(m, 10);
	y = parseInt(y, 10);
	if (isNaN(d) || isNaN(m) || isNaN(y)) return false;
	if (y < 2008 || y > 2100) {
		return false;
	}
	else if (d < 1 || d > 31) {
		return false;
	}
	else if (m < 1 || m > 12) {
		return false;
	}
	else if ((m == 4 || m== 6 || m == 9 || m == 11) && d > 30) {
		return false;
	}
	else if (m == 2 && (d > 29 || (((y % 4) != 0 || ((y % 100) == 0 && (y % 400) != 0)) && d > 28))) {
		return false;
	}
	return true;
}

function clearDate() {
	if (this.value == "dd" || this.value == "mm" || this.value == "yyyy") this.value = "";
	this.select();
}

function dateOK(val, min, max) {
	var val = parseInt(val, 10);
	if (isNaN(val) || val < min || val > max) {
		return false;
	}
	return true;
}

function setDay() {
	if (!dateOK(this.value, 1, 31)) this.value = "dd";
}

function setMonth() {
	if (!dateOK(this.value, 1, 12)) this.value = "mm";
}

function setYear() {
	if (!dateOK(this.value, 2008, 2100)) this.value = "yyyy";
}

function setNext(o, dis) {
	o.disabled = dis;
	o = o.style
	if (dis) {
		o.color = "#fe9";
		o.backgroundColor = "#960";
		o.borderColor = "#c90 #630 #630 #c90";
	}
	else {
		o.color = "#bbe";
		o.backgroundColor = "#557";
		o.borderColor = "#779 #223 #223 #779";
	}
	return dis;
}

function chkP1() {
	var arr = gt("controls-1", "input");
	return setNext(g("next-1"), arr[0].value.length < 2 || arr[1].value.length < 2 || arr[2].value.length < 2 || !validEmail(arr[3].value));
}

function chkP2() {
	return setNext(g("next-2"), !(parseInt(g("adults").value, 10) > 0));
}

function chkP3() {
	var arr = gt("controls-3", "input");
	return setNext(g("next-3"), !validDate(arr[0].value, arr[1].value, arr[2].value) || !validDate(arr[3].value, arr[4].value, arr[5].value));
}

function chkP4() {
	return setNext(g("next-4"), g("acc-type").value == "0" && g("special-acc").value.length < 5);
}

function chkP5() {
	var i, arr, arrLen;
	var dis = (g("special-tour").value.length < 5);
	if (dis) {
		arr = gt("controls-5", "input");
		arrLen = arr.length;
		for (i=0;i<arrLen;i++) {
			if (!arr[i].disabled && arr[i].checked) {
				dis = false;
				break;
			}
		}
	}
	return setNext(g("next-5"), dis);
}

function chkP6() {
	if (!blocked) {
		setNext(g("send"), (track == 0 ? (g("message").value.length < 5) : false));
	}
}

function chkP5Intro() {
	// To prevent that a click on a checkbox will be cancelled if chkP5 returns false
	chkP5();
}

// Button Mouse Events

function butHover() {
	if (!this.disabled) {
		this.style.color = "#557";
		this.style.backgroundColor = "#bbe";
		this.style.borderColor = "#eef #88b #88b #eef";
	}
}

function butOut() {
	if (!this.disabled) {
		this.style.color = "#bbe";
		this.style.backgroundColor = "#557";
		this.style.borderColor = "#779 #223 #223 #779";
	}
}

function butDown() {
	if (!this.disabled) this.style.borderColor = "#88b #eef #eef #88b";
}

function butUp() {
	if (!this.disabled) this.style.borderColor = "#eef #88b #88b #eef";
}

function resetPage(id) {
	var i, arr, arrLen;
	switch(id) {
		case 0: // Introduction
			g("acc-quote").checked = false;
			g("tour-quote").checked = false;
			break;
		case 1: // Personal Information
			arr = gt("controls-1", "input");
			arrLen = arr.length;
			for(i=0;i<arrLen;i++) {
				arr[i].value = "";
			}
			break;
		case 2: // Number of Persons
			arr = gt("controls-2", "input");
			arrLen = arr.length;
			for(i=0;i<arrLen;i++) {
				arr[i].value = "";
			}
			break;
		case 3: // Period of Stay
			arr = gt("controls-3", "input");
			for(i=0;i<6;i++) {
				switch(i) {
					case 0: case 3:
						arr[i].value = "dd";
						break;
					case 1: case 4:
						arr[i].value = "mm";
						break;
					case 2: case 5:
						arr[i].value = "yyyy";
						break;
				}
			}
			break;
		case 4: // Accommodation Requirements
			g("acc-type").value = "0";
			g("special-acc").value = "";
			break;
		case 5: // Tour Requests
      arr = gt("controls-5", "input");
			arrLen = arr.length;
			for(i=0;i<arrLen;i++) {
				arr[i].checked = false;
			}
			g("special-tour").value = "";
			break;
		case 6: // Message
			g("message").value = "";
	}
	if (id > 0 && id < 6) setNext(g("next-" + id), true);
}

function resetAll() {
	var i;
	for(i=0;i<7;i++) {
		resetPage(i);
	}
	blocked = false;
  setNext(g("send"), false);
	clearTimeout(dotID);
	g("processing").style.display = "none";
	g("error").style.display = "none";
	g("bad-request").style.display = "none";
	g("success").style.display = "none";
	g("page-" + tracks[track][page]).style.display = "none";
	g("page-0").style.display = "block";
	page = 0;
}

function resetWarning() {
	if (confirm("This will reset the entire Wizard and delete all your information!\n\nClick \"OK\" to reset!\nClick \"Cancel\" to regret!")) resetAll();
}

function previous() {
	g("page-" + tracks[track][page]).style.display = "none";
	g("page-" + tracks[track][page - 1]).style.display = "block";
	page--;
}

function next() {
	var i1, i2, o1, o2, arr, aDate, dDate, today, errMsg = "";
	var actPage = tracks[track][page];
	var newPage = tracks[track][page + 1];
	switch(actPage) {
		case 0: // Introduction
			track = (g("acc-quote").checked ? 1 : 0) + (g("tour-quote").checked ? 2 : 0);
			newPage = tracks[track][page + 1];
			break;
		case 1: // Personal Information
			if (chkP1()) return;
			break;
		case 2: // Number of Persons
			if (chkP2()) return;
			break;
		case 3: // Period of Stay
			if (chkP3()) return;
			arr = gt("controls-3", "input");
			aDate = new Date(parseInt(arr[2].value, 10), parseInt(arr[1].value, 10) - 1, parseInt(arr[0].value, 10));
			dDate = new Date(parseInt(arr[5].value, 10), parseInt(arr[4].value, 10) - 1, parseInt(arr[3].value, 10));
			aDate = aDate.getTime();
			dDate = dDate.getTime();
			today = g("today").value.split("-");
			today = new Date(parseInt(today[0], 10), parseInt(today[1], 10) - 1, parseInt(today[2], 10));
			today = today.getTime();
			if (aDate <= today) errMsg = "Date of Arrival must be later than today!";
			else if (aDate > dDate) errMsg = "Date of Arrival is later than Date of Departure!";
			else if (aDate == dDate) errMsg = "Date of Arrival and Date of Departure are the same date!";
			if (errMsg != "") {
				alert(errMsg);
				return;
			}
			break;
		case 4: // Accommodation Requirements
			if (chkP4()) return;
			break;
		case 5: // Tour Requests
			if (chkP5()) return;
	}
	if (newPage == 5) {
		arr = gt("controls-3", "input");
		i1 = parseInt(arr[1].value, 10);
		i2 = parseInt(arr[4].value, 10);
		o1 = g("turtle-tour-1");
		o2 = g("turtle-tour-label").style;
		if ((i1 > 10 || i1 < 3) || (i2 > 10 || i2 < 3)) {
			o1.disabled = false;
			o2.color = "#bbc";
		}
		else {
      o1.disabled = true;
			o2.color = "#779";
		}
    o1 = g("whale-tour-10");
		o2 = g("whale-tour-label").style;
    if ((i1 > 5 && i1 < 12) || (i2 > 5 && i2 < 12)) {
			o1.disabled = false;
			o2.color = "#bbc";
		}
		else {
      o1.disabled = true;
			o2.color = "#779";
		}
		chkP5();
	}
	g("page-" + actPage).style.display = "none";
	g("page-" + newPage).style.display = "block";
	page++;
	switch(newPage) {
		case 1:
			g("name-1").focus();
			break;
		case 2:
			g("adults").focus();
			break;
		case 3:
			g("da").focus();
			break;
		case 4:
			g("acc-type").focus();
			break;
    case 5:
			g("feast-tour-0").focus();
			break;
    case 6:
    	chkP6();
			g("message").focus();
	}
}

function getVal(id) {
	return enc(g(id).value);
}

var subReq, dotID, dot = 0, blocked = false;
var dots = ["", ".", ". .", ". . ."];

function setDots() {
	g("dots").innerHTML = dots[dot];
	if (++dot > 3) dot = 0;
	dotID = setTimeout(setDots, 300);
}

function hideProcess() {
	clearTimeout(dotID);
	g("processing").style.display = "none";
}

function subResponse(rsp) {
	if (!rsp) {
		hideProcess();
		g("error").style.display = "block";
		blocked = false;
		setNext(g("send"), false);
	}
	else if (rsp == "BR") {
    hideProcess();
		g("bad-request").style.display = "block";
	}
	else {
		hideProcess();
		g("success").style.display = "block";
	}
}

function submitForm() {
	var i, j, p, q, arr, arrLen;
	var str = "";
	var tLen = tracks[track].length;
	setNext(g("send"), true);
	blocked = true;
	g("error").style.display = "none";
	g("processing").style.display = "block";
	setDots();
	for (i=0;i<tLen;i++) {
		p = tracks[track][i];
		switch(p) {
			case 0: // Introduction
				q = "track=" + track;
				break;
      case 1: // Personal Information
				q += "&name=" + getVal("name-1") + " " + getVal("name-2") + "&country=" + getVal("country") + "&email=" + getVal("email");
				q += "&telephone=" + getVal("telephone") + "&fax=" + getVal("fax") + "&cell=" + getVal("cell");
				break;
      case 2: // Number of Persons
      	q += "&adults=" + getVal("adults") + "&children=" + getVal("children") + "&toddlers=" + getVal("toddlers");
				break;
      case 3: // Period of Stay
      	arr = gt("controls-3", "input");
      	q += "&arrival=" + enc(arr[2].value + "-" + arr[1].value + "-" + arr[0].value);
      	q += "&departure=" + enc(arr[5].value + "-" + arr[4].value + "-" + arr[3].value);
				break;
      case 4: // Accommodation Requirements
      	q += "&acctype=" + getVal("acc-type") + "&spcacc=" + getVal("special-acc");
				break;
      case 5: // Tour Requests
      	arr = gt("controls-5", "input");
      	arrLen = arr.length;
      	for (j=0;j<arrLen;j++) {
      		if (!arr[j].disabled && arr[j].checked) {
      			str += "-" + arr[j].id.split("-")[2];
					}
				}
				q += "&tours=" + str.substr(1) + "&spctour=" + getVal("special-tour");
				break;
      case 6: // Message
      	q += "&message=" + getVal("message");
		}
	}
	if (!httpRequest("/mail.php", q, subResponse)) {
		hideProcess();
		blocked = false;
		setNext(g("send"), false);
		return;
	}
}

var track = 0, page = 0;
var tracks = [
	[0, 1, 6],
	[0, 1, 2, 3, 4, 6],
	[0, 1, 2, 3, 5, 6],
	[0, 1, 2, 3, 4, 5, 6]
]

function initLocal() {
	var i, o, arr, arrLen, id, r, rAll, b, n, s;
	init();
	// Initialise Buttons
	arr = gt("wizard", "input");
	arrLen = arr.length;
	for (i=0;i<arrLen;i++) {
		id = arr[i].id;
		r = (id.indexOf("reset-") == 0);
		rAll = (id.indexOf("resetall-") == 0);
		b = (id.indexOf("back-") == 0);
		n = (id.indexOf("next-") == 0);
		s = (id == "send");
		if (r || rAll || b || n || s) {
			arr[i].onmouseover = butHover;
			arr[i].onmouseout = butOut;
			arr[i].onmousedown = butDown;
			arr[i].onmouseup = butUp;
			if (r) {
				arr[i].onclick = new Function("resetPage(" + id.split("-")[1] + ")");
			}
			else if (rAll) {
				arr[i].onclick = resetWarning;
			}
			else if (b) {
				arr[i].onclick = previous;
			}
			else if (n) {
				arr[i].onclick = next;
			}
			else if (s) {
				arr[i].onclick = submitForm;
			}
		}
	}

	// Initialise Wizard Page 1 (Personal Information)
	arr = gt("controls-1", "input");
	for (i=0;i<4;i++) {
		arr[i].onkeyup = chkP1;
	}
  for (i=4;i<7;i++) {
		arr[i].onkeypress = phoneChar;
	}

	// Initialise Wizard Page 2 (Number of Persons)
	arr = gt("controls-2", "input");
	for (i=0;i<3;i++) {
		arr[i].onkeypress = digitsOnly;
	}
	arr[0].onkeyup = chkP2;

	// Initialise Wizard Page 3 (Period of Stay)
	arr = gt("controls-3", "input");
  for (i=0;i<6;i++) {
  	arr[i].onfocus = clearDate;
		arr[i].onkeypress = digitsOnly;
		arr[i].onkeyup = chkP3;
		switch(i) {
			case 0: case 3:
				arr[i].onblur = setDay;
				break;
			case 1: case 4:
				arr[i].onblur = setMonth;
				break;
			case 2: case 5:
				arr[i].onblur = setYear;
		}
	}

	// Initialise Wizard Page 4 (Accommodation Requirements)
	o = g("acc-type");
	o.onchange = chkP4;
	if (!ie && !opera) {
		o.onkeyup = chkP4;
	}
	g("special-acc").onkeyup = chkP4;

	// Initialise Wizard Page 5 (Tour Requests)
	arr = gt("controls-5", "input");
	arrLen = arr.length;
	for (i=0;i<arrLen;i++) {
		arr[i].onclick = chkP5Intro;
	}
	g("special-tour").onkeyup = chkP5;

	// Initialise Wizard Page 6 (Message)
	g("message").onkeyup = chkP6;

	resetAll();
}

window.onload = initLocal;