// ------------------------------------------------------------------
// Make form field check
// ------------------------------------------------------------------

// ------------------------------------------------------------------
// validate(form)
//function validate(form) {
	//if (!checkBlank(form.email)) return false;
	//if (!checkEmail(form.email)) return false;
	//if (!checkBlank(form.uname)) return false;
	//if (!checkBlank(form.birthy)) return false;
	//if (!checkBlank(form.birthm)) return false;
	//if (!checkBlank(form.birthd)) return false;
	//if (!checkBirth(form.birthy, form.birthy.value, form.birthm.value, form.birthd.value)) return false;
	//return true;
//}

// ------------------------------------------------------------------
// error(code, msg)
function error(code, msg) {
	alert("Fatal Error (" + code + "): " + msg + ".");
}

// ------------------------------------------------------------------
// checkEmail(form, insist)
function checkEmail(form) {
	var email = new String(form.value);
	if (!(email.indexOf("@") > 0) || !(email.indexOf("@") < email.length-1)) {
		error(1001, "Invalid email address!!");
		form.focus();
		return false;
	}
	return true;
}

// ------------------------------------------------------------------
// checkBlank(form)
function checkBlank(form) {
	var field = new String(form.value);
	if (field.length == 0) {
		error(1002, "You must fill in the information!!");
		form.focus();
		return false;
	}
	return true;
}

// ------------------------------------------------------------------
// checkNumber(form)
function checkNumber(form) {
	var field = new String(form.value);
	if (isNaN(field)) {
		error(1002, "You must fill in number only!!");
		form.focus();
		return false;
	}
	return true;
}

// ------------------------------------------------------------------
// checkDate(MonthField, month, DayField, day)
function checkDate(MonthField, month, DayField, day) {
	m = parseInt(month);
	d = parseInt(day);
	if (m > 12 || m < 0) {
		error(1003, "Invalid month");
		MonthField.focus();
		return false;
	}
	if (d > 31 || d < 0) {
		error(1004, "Invalid day of month");
		DayField.focus();
		return false;
	}
	switch (m) {
		case 4: case 6: case 9: case 11:
			if (d > 30) {
				error(1004, "Invalid day of month");
				DayField.focus();
				return false;
			} break;
		case 2:
			if (d > 29) {
				error(1004, "Invalid day of month");
				DayField.focus();
				return false;
			} break;
	}
	return true;
}

// ------------------------------------------------------------------
// checkBirth(form)
function checkBirth(form, year, month, day) {
	var today = new Date();
	var birth = new Date(month + "/" + day + "/" + year);
	if (birth >= today) {
		error(1003, "Invalid birthdate");
		form.focus();
		return false;
	}
	return true;
}

// --------------------------------------------------------------------------
// checkDateFormat(form)
//  Malaysia Date format is DD-MM-YYYY ; check the exact format!!

function checkDateFormat(form)
{
	var int_date = new String(form.value);
	i = parseInt(int_date.length)
	
	if(i != 10)
	{
		error(1006, "Date Format Shall Have 10 Chars!!");
		form.focus();
		return false;
	}
	if(int_date.substring(2,3) != "-" || int_date.substring(5,6) != "-")
	{
		error(1007, "Date Format Error!\n Please put the '-' in right position!");
		form.focus();
		return false;
	}
	else
	{
		day = int_date.substring(0,2);
		month = int_date.substring(3,5);
		year = int_date.substring(6,10);
		if(isNaN(day) || isNaN(month) || isNaN(year))
		{
			error(1008, "Please input number only in DD or MM or YYYY!");
			form.focus();
			return false;
		}
		if(day <=0 || day >= 32)
		{
			error(1008, "The DD is out of date range!");
			form.focus();
			return false;
		}
		if(month <=0 || month >=13)
		{
			error(1008, "The MM is out of date range!");
			form.focus();
			return false;
		}
	}
	return true;
}
