﻿Date.prototype.getMonthName = function(date) {
	return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][this.getMonth()];
}

Date.prototype.getDayName = function() {
	return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][this.getDay()];
}

Date.prototype.getJJDate = function() {
	return this.getDayName() + ", " + this.getMonthName() + " " + this.getDate() + ", " + this.getFullYear();
}

JJ.validation = {
	"getKeyCode" : function(e) {
		try {
			return event.keyCode;
		}
		catch(err) {
			return e.which;
		}
	},

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

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

	"error" : "",

	"addError" : function(str) {
		this.error += (this.error == "" ? "" : "\n") + str;
	},

	"text" : function (val, mChr, fldName) {
		if (val == "" && mChr > 0) {
			this.addError(fldName + " must be provided!");
			return false;
		}
		else if (val.length < mChr) {
			this.addError(fldName + " is not valid!");
			return false;
		}
		return true;
	},

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

	"isDate" : function(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;
	}
	
};

window.onload = function() {

	init();

	JJ.addEvent([g("tel"), g("fax"), g("cell")], "keypress", function(e) {
		if (!JJ.validation.isValidKey(e, /[\d #*()+\-]/)) {
			try {
				e.preventDefault();
			}
			catch(err) {
				return false;
			}
		}
	});

	JJ.addEvent([g("adult"), g("child"), g("aday"), g("amonth"), g("ayear"), g("dday"), g("dmonth"), g("dyear")], "keypress", function(e) {
		if (!JJ.validation.isValidKey(e, /[\d]/)) {
			try {
				e.preventDefault();
			}
			catch(err) {
				return false;
			}
		}
	});

	JJ.addEvent([g("aday"), g("amonth"), g("ayear"), g("dday"), g("dmonth"), g("dyear")], "focus", function() {
		if (this.value == "dd" || this.value == "mm" || this.value == "yyyy") this.value = "";
		this.select();
	});

	JJ.addEvent([g("aday"), g("dday")], "blur", function() { if (!JJ.validation.isInInterval(this.value, 1, 31)) this.value = "dd"; });
	JJ.addEvent([g("amonth"), g("dmonth")], "blur", function() { if (!JJ.validation.isInInterval(this.value, 1, 12)) this.value = "mm"; });
	JJ.addEvent([g("ayear"), g("dyear")], "blur", function() { if (!JJ.validation.isInInterval(this.value, 2009, 2100)) this.value = "yyyy"; });

	JJ.addEvent("sbut", "click", function() {
		var val, aDate, dDate, str;
		var doub = parseInt(g("double").value, 10);
		var lux = parseInt(g("luxury").value, 10);
		if (isNaN(doub)) doub = 0;
		if (isNaN(lux)) lux = 0;
		with (JJ.validation) {
			error = "";
			text(g("name").value, 2, "Name");
			text(g("country").value, 2, "Country");
			val = g("email-address").value;
			if (text(val, 1, "Email Address")) {
				if (!isEmail(val)) addError("Email Address is not valid!");
			}
			val = parseInt(g("adult").value, 10);
			if (isNaN(val) || val == 0) {
				addError("Number of Adults must be provided!");
			}
			if (isDate(g("aday").value, g("amonth").value, g("ayear").value)) {
				aDate = new Date(parseInt(g("ayear").value, 10), parseInt(g("amonth").value, 10) - 1, parseInt(g("aday").value, 10));
			}
			else {
				addError("Date of Arrival is not valid!");
			}
			if (isDate(g("dday").value, g("dmonth").value, g("dyear").value)) {
				dDate = new Date(parseInt(g("dyear").value, 10), parseInt(g("dmonth").value, 10) - 1, parseInt(g("dday").value, 10));
			}
			else {
				addError("Date of Departure is not valid!");
			}
			if (aDate && dDate && aDate >= dDate) {
				addError("Date of Arrival must be earlier than Date of Departure!");
			}
			if (doub == 0 && lux == 0 && !g("honeymoon").checked && !g("family").checked) {
				addError("At least one room must be chosen in Accommodation Requirements!");
			}
			if (error != "") {
				alert(error);
				return;
			}
		}

		str = "Adults: " + parseInt(g("adult").value, 10);
		val = parseInt(g("child").value, 10);
		if (!isNaN(val) && val > 0) {
			str += ", Children Below 12 Years: " + val;
		}
		g("visit").value = str;

		g("arrival").value = aDate.getJJDate();
		g("departure").value = dDate.getJJDate();

		str = (doub == 0 ? "" : doub + " double room" + (doub > 1 ? "s" : ""));
		str += (lux == 0 ? "" : (str == "" ? "" : ", ") + lux + " luxury room" + (lux > 1 ? "s" : ""));
		str += (g("honeymoon").checked ? (str == "" ? "" : ", ") + "Honeymoon Room" : "");
		str += (g("family").checked ? (str == "" ? "" : ", ") + "Family Room" : "");
		g("accom").value = str;

		g("mail-form").submit();

	});

}
