/*******************************************************************
**
**      Filename		: brcheck.js
**      Description	: script for check fields in report form.
**		Author			: harrie
**		Author			: Lorna (modified)
**		Last Updated	: 09-12-2000
**
*******************************************************************/

 var refNumber		= "-1234567890";

// Check Form Validation. Make sure all fields are filled 

function checkForm(tt) {

	// check category

	if(!isSelect(tt.category)) {
  		alert("Sila pilih kategori pengguna.")
		tt.category.focus();
		return false; 
	}
	
	// check type of access

	if(!isSelect(tt.access)) {
  		alert("Sila pilih jenis akses.");
		tt.access.focus();
		return false; 
	}
	
	// check area

	if(isFieldBlank (tt.district)) {
 		alert("Sila masukkan nama kawasan.");
		tt.district.focus();
		return false;	  
    }

	// check problem type
	
	if(!isSelect(tt.problem)) {
		alert("Sila pilih jenis masalah.");
		tt.problem.focus();
		return false; 
	}
	
	// check time of fault

	if(!isSelect(tt.masa)) {
		alert("Sila pilih masa masalah berlaku.");
		tt.masa.focus();
		return false; 
	}
	
	// check valid date

	if(!isSelect(tt.day)) {
  		alert("Sila masukkan tarikh yang sah.");
		tt.day.focus();
		return false; 
	}

	if(!isSelect(tt.month)) {
  		alert("Sila masukkan tarikh yang sah.");
		tt.month.focus();
		return false; 
	}

	if(isFieldBlank(tt.year)) {
  		alert("Sila masukkan tarikh yang sah.");
		tt.year.focus();
		return false;
	}
	    
	var daToday = new Date();
	var nThisYear = daToday.getFullYear();
	var strYear = tt.year;
	var strMonth = tt.month;
	var strDay = tt.day;
	
	if((strYear.value < 1950) || (strYear.value >= (nThisYear + 1))) {
  		alert("Sila masukkan tarikh yang sah.");
		tt.year.focus();
		return false;
	}

	// february
	if(strMonth.value == "Feb") {
		if(strYear.value % 4 > 0 && strDay.value > 28) {
	  		alert("Sila masukkan tarikh yang sah.");
			tt.day.focus();
			return false;
		} else if(strDay.value > 29) {
  			alert("Sila masukkan tarikh yang sah.");
			tt.day.focus();
			return false;
		}
	}

	// april, jun, sep, nov
	if((strMonth.value == "Apr") || (strMonth.value == "Jun") || (strMonth.value == "Sep") || (strMonth.value == "Nov")) {
		if(strDay.value > 30) {
	  		alert("Sila masukkan tarikh yang sah.");
			tt.day.focus();
			return false;
		}
	}
	
	// check problem description

	if(isFieldBlank(tt.descript) == true) {
		alert("Sila jelaskan masalah yang dihadapi.");
		tt.descript.focus();
		return false;	  
	}
	
	// check full name
	
	if(isFieldBlank(tt.name)) {
  		alert("Sila isikan nama penuh.");
		tt.name.focus();
		return false;	  
	}

	// check if email field is valid

	if (!isValidEmail1(tt.emailsn)) {
		alert("Sila isikan alamat e-mel Sabah.Net yang sah.");
		tt.emailsn.focus();
		return false;	  
	}

	if (!isValidEmail2(tt.emailalt)) {
		alert("Sila isikan alamat e-mel alternatif yang sah.");
		tt.emailalt.focus();
		return false;	  
	}

	if (isPhoneBlank (tt.phone) || isFieldBlank (tt.phone) || !isOnlyNumber (tt.phone.value)) {
		alert("Sila isikan nombor telefon.");	
		tt.phone.focus();
		return false;	  
	}

	// submit the form if the form is filled up correctly

	alert("Sila tunggu. Permohonan ini akan diproses secepat mungkin.");
	return true;
}

// OUTSIDE FUNCTION 


// Check For a Blank Field
function isFieldBlank(frmField) {
	if(frmField.value == "")
		return true;
	else
		return false;
}

// Check For a Blank Phone Field
function isPhoneBlank (formObj) {
	if(formObj.value == "088-" )
		return true;
	else
		return false;
}

// Check if Phone is Valid Number 
function isOnlyNumber (frmField) {
	if (frmField.length == 0 )
	return false; 
	for ( check=0; check < frmField.length; check++) {
		tempNumber = frmField.substring (check, check+1);
		if (refNumber.indexOf(tempNumber, 0) == -1) 	
			return false;
	}
	return true;
}

// Check Valid Email Address
function isValidEmail1(formObj) {
	if((isFieldBlank(formObj) == true) || ((formObj.value.indexOf("@sabah.net.my") == -1) && (formObj.value.indexOf('@sabah.com.my') == -1) && (formObj.value.indexOf('@sabah.gov.my') == -1) && (formObj.value.indexOf('@sabah.edu.my') == -1) && (formObj.value.indexOf('@sabah.org.my') == -1)))
		return false;
	else
		return true;
}

function isValidEmail2(formObj) {
	if((!isFieldBlank(formObj)) && ((formObj.value.indexOf('@') == -1 ) || (formObj.value.indexOf(".") == -1)))
		return false;
  	else
		return true;
}

// Check Only Numeric Required
function isValidNumber(frmField) {
	if (isNaN(frmField.value))
		return false;
	else
		return true;
}

// Check selection option
function isSelect(frmField) {
	if (frmField.options[frmField.selectedIndex].value != "0")
		return true;
	else
		return false;
}