/* Performs field validation by looping through all available fields on the form.Field classes to be used:		required --> field cannot be empty		numeric --> field must contain numeric values*/// This is the main function that needs to be called from the form.function validateFields ( formName ){	var msg = "";	var emptyFields = "";	var numericFields = "";	var noErrorList = "";	var index = 0;	possibleErrorList = new Object();		for (var i = 0; i < formName.length; i++)	{		var e = formName.elements[i];						// validate required field	if (e.className.indexOf("required") != -1)		{			// validate radio and checkbox field			if (e.type == "radio" || e.type == "checkbox")			{				if (e.checked == true)				{					noErrorList += e.title;				}				else				{					possibleErrorList[index++] = e.title;				}			}						// validate other types of field			emptyFields += checkRequiredField ( e );		}				// validate numeric field		if (e.className.indexOf("numeric") != -1)		{			numericFields += checkNumericField ( e );		}	}	// Find out which radio button and checkboxes are actually empty.	// If the field's title isn't in noErrorList, add it to emptyFields variable.	// Also add the field's title to noErrorList so the same title isn't added to emptyFields again and again.	for (count in possibleErrorList)	{		if (noErrorList.indexOf (possibleErrorList[count]) == -1)		{			emptyFields += "\n" + possibleErrorList[count];			noErrorList += possibleErrorList[count];		}	}		// display msg	if (!emptyFields && !numericFields)	{		return true	}	else	{		var strFieldTitle, strEmptyFieldTitle, strNumericFieldTitle;		var intPos;				strEmptyFieldTitle = "";		strNumericFieldTitle = "";				if (emptyFields)		{			intPos = emptyFields.indexOf("\n");			strEmptyFieldTitle = intPos > -1 ? emptyFields.substr(0, intPos) : emptyFields.substr(0);			msg += "The following information below cannot be empty: \n\n";			msg += emptyFields + "\n\n";		}		if (numericFields)		{			intPos = numericFields.indexOf("\n");			strNumericFieldTitle = intPos > -1 ? numericFields.substr(0, intPos) : numericFields.substr(0);						msg += "The following information below can only accept number value:\n\n";			msg += numericFields + "\n\n";		}		alert (msg);			//Set the first field name that needs to be validated		strFieldTitle = strEmptyFieldTitle != "" ? strEmptyFieldTitle : strNumericFieldTitle;				for (var i = 0; i < formName.length; i++)		{			var e = formName.elements[i];			if (e.title == strFieldTitle)			{				if (document.getElementById("tab"))		//----------------added by cynthia - function to focus on which tab 				{					if (e.className.charAt(8))					{						n=e.className.charAt(8) 	//m=String(parseInt(e.className.charAt(8))+1)						//alert (n)						if (n==1)						{ShowHideTab('tab'+n,'tab'+String(parseInt(n)+1),'1')}						else						{ShowHideTab('tab'+n,'tab'+String(parseInt(n)-1),'2')}					}				}	//---------------------end of function to focus on which tab			if (e.type != "hidden") { // added by zakiah - function not to focus on radio and checkbox type field					e.focus()				}				break;			}		}		return false;	}}function checkRequiredField ( e ){	var emptyFields = "";		if ( (e.type == "text" || e.type == "textarea") && (e.value == null || e.value == "") )	{		emptyFields += e.title + "\n";	}			if (e.type == "select-multiple" && e.selectedIndex == -1)	{		emptyFields += e.title + "\n";	}		//if (e.type == "select-one" && e.selectedIndex == 0)				previous got no checking on selected empty value	if (e.type == "select-one" && e.selectedIndex == 0 && e.options[e.selectedIndex].text=="")	// modified by cynthia	{		emptyFields += e.title + "\n";	}	return emptyFields;}function checkNumericField ( e ){	var numericFields = "";		if (e.type == "text" || e.type == "textarea")	{		if ( !isNumber (e.value) )		{			numericFields += e.title + "\n";		}	}	return numericFields;}function isNumber ( num ){	decimalPoint = false;		for (var i = 0; i < num.length; i++)	{		var oneChar = num.charAt ( i );		if (oneChar < "0" || oneChar > "9")		{			if (i == 0 && oneChar == "-")			{				continue;			}			if (oneChar == "." && !decimalPoint)			{				decimalPoint = true;				continue;			}			return false;		}	}	return true;}function check_field_alert(field, str_Alert){	var Flag_Select = "No"	if (check_field(field))	{	Flag_Select = "Yes"	}		if (Flag_Select == "No")	{	field_alert(field, str_Alert)	return false	}	else	{	return true	}}function check_field(field){ 	if (field.type == "text" || field.type == "textarea")	{		if (field.value != "")		{			return true		}	}	else if (field.type == "select-one")	{ 		if (field.options.selectedIndex != 0)		{		return true		}	}	else if (field.type == "select-multiple")	{ 		if (field.options.selectedIndex != -1)		{		return true		}	}	else	{ 		for(i = 0; i<field.length; i++)		{		if (field[i].checked)		{		return true		break		}		}	}}function field_alert(field, str_Alert){		if (str_Alert != "")		{		alert(str_Alert)		if (field.type == "text" || field.type == "textarea" || field.type == "select-one")		{			field.focus()		}		}		else if (field.type == "text" || field.type == "textarea" || field.type == "select-one")		{		alert(field.title + " cannot be empty.")		field.focus()		}		else		{		alert("The following information cannot be empty.")		}}function check_date(input_date,greater_than_today_flag){	  if (input_date != "")   {		var date_len = input_date.length;		if (date_len > 10 || date_len < 10)		{			alert("Please insert again the date according to this format: \ndd/mm/yyyy  (E.g. 07/02/2003)");			return false;		}				if (Instr(input_date,"/") == -1) 		{			alert("Please insert again the date according to this format: \n contohnya 07/02/2003");			return false;			}			day = parseInt(input_date.substring(0, 2),10);			if (day > 31 || day <1)		{					alert("Please check the day for the date.")			//eval ("document.forms[0]." + fieldname + ".focus()")				return false;		}			mth = parseInt(input_date.substring(3, 5),10)		if (mth < 1 || mth > 12)		{				alert("Please check the month for the date.")			return false;		}				if (greater_than_today_flag == "true") 		{			yr = parseInt(input_date.substring(6,10),10)					//Set the two dates			today=new Date()			var date1=new Date() 			date1.setMonth(mth - 1) //Month is 0-11 in JavaScript			date1.setFullYear(yr)			date1.setDate(day)					//Set 1 day in milliseconds			//	var one_day=1000*60*60*24			if (today.getTime() < date1.getTime()) 			{				alert("Please check again the date. \nDate cannot be previous date.")				return false;			}		}		}	 } 