// ************ Begin Validation FUNCTIONS *************

// Format the error message.
// var oPopup = window.createPopup();
var f;
var numChecks ;
var checkFields;
var errorLine1		= "Arahan ini tidak akan dilaksanakan sehingga anda memperbetulkan setiap \nkesalahan berikut.";
var errorLine2		= "_________________________________________________________           ";
var errorLine3		= "Kesalahan yang dikesan:";
var errorBullet		= "   ·   ";

// Define the valid characters. 
var validChars		= "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-";	//Used by checkValidChars().
var NotvalidKPT		= "~`!@#$%^&*()_+=\|]}[{;:/?.>,<";	//Used by checkValidKPT().
var validKPTBaru	= "0123456789-";	//Used by checkValidKPTBaru().
var validNames		= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@& '/*";	//Used by checkValidName().

function runValidation() {
//////////////////////////////////////////////////////////
	// Add the error messages, if any
	var obj_Error	= new ErrorMsg();
	obj_Error.AddCategory();
	for ( var i = 1; i < ( numChecks + 1 ); i++ )  {
		if ( checkFields[i] != "" ) obj_Error.AddMessage( checkFields[i] );
	}

	// Display errors
	if( obj_Error.errors == false ) {
		return true;
	} else {
		obj_Error.DisplayMessage();
		return false;
	}
}

function validateMinMax ( fieldName, fieldDescription, minLength, maxLength ) {
//////////////////////////////////////////////////////////
	errorMsg	= "";
	fieldValue	= f[fieldName].value;
	fieldLength	= f[fieldName].value.length;
	if ( fieldLength < minLength ) {
		errorMsg = "\"" + fieldDescription +  "\"  - Sila Masukkan Sekurang-kurangnya " + minLength + " huruf.";
	} else if (( fieldLength > maxLength ) && ( maxLength > 0 )) {
		errorMsg = "\"" + fieldDescription +  "\"  - Sila Masukkan Sekurang-kurangnya " + maxLength + " huruf.";
	}
	return errorMsg ;
}

function validateBtnsBox( fieldName, fieldDescription, errorLabel ) {
//////////////////////////////////////////////////////////
	errorMsg	= "";
	selection = null;
	thisButton		= f[fieldName];
	for( var i=0; i<thisButton.length; i++ ) {
		if( thisButton[i].checked ) {
		   selection = thisButton[i].value;
		}
	}
	if( selection == null ) {
		errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
	}
	return errorMsg;
}

function check_btns( fieldName, fieldValue ){
	thisButton		= f[fieldName];
	for( var i=0; i<thisButton.length; i++ ) {
		if( thisButton[i].value == fieldValue ) {
		   thisButton[i].checked = true;
		}
	}
}

function checkKPFields(fieldName1, fieldName2, fieldDescription, errorLabel){
////////////////////////////////////////////////////////
	//===== Check Fields IC to be fill in =====
	errorMsg	= "";
	fieldValue	= ""+ f[fieldName1].value + f[fieldName2].value +"";
	if((fieldValue == "") || (fieldValue == " ")){
		errorMsg = "\"" + fieldDescription + "\"  - "+ errorLabel + "";
	}
	return errorMsg ;
}

function validateNumBox( fieldName, fieldDescription, errorLabel ) {
//////////////////////////////////////////////////////////
	// ===== Check if entry contains only numbers =====
	errorMsg	= "";
	fieldValue	= f[fieldName].value;
	fieldLength	= f[fieldName].value.length;

	for( var i = 0; i < fieldLength; i++ ) {
		var ch = fieldValue.substring( i, i + 1 );
		if (( ch < "0" ) || ( ch > "9" )) {
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

function validateEmailBox ( fieldName, fieldDescription, errorLabel ) {
	// ===== Validate an email address =====//////////////////////////////////////////////////////////

	errorMsg	= "";
	fieldValue	= f[fieldName].value;
	fieldLength	= f[fieldName].value.length;
	
	errorLabel1	= "This is not a valid email address.";			// Not valid
	errorLabel2	= "Missing [ @ ] sign.";						// Missing "@"
	errorLabel3	= "Missing [ . ].";								// Missing "."
	errorLabel4	= "Cannot start with a space.";					// Starts with " "
	errorLabel5	= "Cannot start with [ @ ] sign.";				// Starts with "@"
	errorLabel6	= "Cannot start with [ . ].";					// Starts with "."

	if ( fieldLength > 0 ) {

		if (( errorLabel == "" ) || ( errorLabel == null )) {

			if ( fieldValue.indexOf( "@" ) == -1 ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel2 + "";
			} else if ( fieldValue.indexOf( "." ) == -1 ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel3 + "";
			} else if ( fieldValue.charAt( 0 ) == " " ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel4 + "";
			} else if ( fieldValue.charAt( 0 ) == "@" ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel5 + "";
			} else if ( fieldValue.charAt( 0 ) == "." ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel6 + "";
			}

		} else {

			if (( fieldValue.indexOf( "@" ) == -1 ) || 					// Missing "@"
				( fieldValue.indexOf( "." ) == -1 ) || 					// Missing "."
				( fieldValue.charAt( 0 ) == "@" ) || 					// Starts with "@"
				( fieldValue.charAt( 0 ) == "." ) 						// Starts with "."
				) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel1 + "";
			}
		}
	}
	return errorMsg ;
}

function checkValidChars( fieldName, fieldDescription, errorLabel ) {
//////////////////////////////////////////////////////////
	// ===== Check if netry contains only valid characters =====
	errorMsg	= "";
	fieldValue	= f[fieldName].value;
	fieldLength	= f[fieldName].value.length;
	for( var i=0; i<fieldLength; i++ ) {
		if ( validChars.indexOf( fieldValue.charAt( i )) == -1 ) {
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

function checkIdentical( fieldName, fieldDescription, errorLabel ) {
//////////////////////////////////////////////////////////
	// ===== Check if entry contains entirely identical characters =====
	errorMsg	= "";
	fieldValue	= f[fieldName].value;
	fieldLength	= f[fieldName].value.length;

	if (( fieldLength > 1 ) && ( isComposedOfChars( fieldValue.charAt( 0 ), fieldValue ))) {
		errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
	}
	return errorMsg ;
}

function isComposedOfChars( curChar, inString ) {
//////////////////////////////////////////////////////////
	return ( indexOfFirstNotIn( curChar, inString ) == -1 );
}

function indexOfFirstNotIn( okayChars, inString ) {
//////////////////////////////////////////////////////////
	for ( var i=0; i<inString.length; i++ ) {
		if ( okayChars.indexOf( inString.charAt( i )) == -1 ) {
			return i;
		}
	}
	return -1;
}

function checkSequential( fieldName, fieldDescription, errorLabel ) {
//////////////////////////////////////////////////////////
	// ===== Check if entry contains entirely sequential characters =====
	errorMsg	= "";
	fieldValue	= f[fieldName].value;
	fieldLength	= f[fieldName].value.length;

	if ( fieldLength > 1 ) {
		for ( var i = 1; i < ( numSeqStr + 1 ); i++ )  {
			if ( strSeq[i].indexOf( fieldValue ) != -1 ) {
				errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
			}
		}
	}
	return errorMsg ;
}

function checkValidKPTLama( fieldName, fieldDescription, errorLabel ) {
//////////////////////////////////////////////////////////
	errorMsg	= "";
	fieldValue	= f[fieldName].value;
	fieldLength	= f[fieldName].value.length;
	for( var i=0; i<fieldLength; i++ ) {
		if ( validChars.indexOf( fieldValue.charAt( i )) == -1 ) {
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

function checkValidName( fieldName, fieldDescription, errorLabel ) {
//////////////////////////////////////////////////////////
	errorMsg	= "";
	fieldValue	= f[fieldName].value;
	fieldLength	= f[fieldName].value.length;
	for( var i=0; i<fieldLength; i++ ) {
		if ( validNames.indexOf( fieldValue.charAt( i )) == -1 ) {
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

function validateSelectBox( fieldName, fieldDescription, errorLabel, checkWhat ) {
//////////////////////////////////////////////////////////
	errorMsg	= "";
	optSelected	= f[fieldName].selectedIndex;
	fieldValue	= f[fieldName].options[optSelected].value;
	if( fieldValue == checkWhat ) {
		errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
	}
	return errorMsg ;
}

// ===== Check if field is required =====
function isRequired( fieldName, fieldDescription, errorLabel ) {
//////////////////////////////////////////////////////////
	errorMsg	= "";
	//alert(fieldName);
	fieldValue	= f[fieldName].value;
	if(( fieldValue == "" ) || ( fieldValue == " " )) {
		errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel + "";
	}
	return errorMsg ;
}


function checkICFields(fieldName1, fieldName2, fieldName3,fieldName4, fieldDescription, errorLabel){
////////////////////////////////////////////////////////
	//===== Check Fields IC to be fill in =====
	errorMsg	= "";
	fieldValue	= ""+ fieldName1 + fieldName2 + fieldName3 + fieldName4 +"";
	if((fieldValue == "") || (fieldValue == " ")){
		errorMsg = "\"" + fieldDescription + "\"  - "+ errorLabel + "";
	}
	return errorMsg ;
}


function checkValidKPTBaru( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= f[fieldName].value;
	fieldLength	= f[fieldName].value.length;

	for( var i=0; i<fieldLength; i++ ) {
		if ( validKPTBaru.indexOf( fieldValue.charAt( i )) == -1 ) {
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + " Aksara yang dibenarkan [" + validKPTBaru + "]";
		}
	}
	return errorMsg ;
}
// ************ End Validation FUNCTIONS *************

// ************ BEGIN OBJECT FUNCTIONS *************
function AddCategory( str_Name ) {
/////////////////////////////////////////////////////////////////
	// Creates a new category object at the next space in the array
	this.obj_Errors[this.obj_Errors.length] = new NewCategory( str_Name );
	this.NewCategory = NewCategory;
}

function NewCategory( str_Name) {
/////////////////////////////////////////////////////////////////
	//  Initializes the category values
	this.str_Name		= str_Name;
	this.str_Error		= "";
}

function AddMessage( str_Msg ) {
/////////////////////////////////////////////////////////////////
	for ( var i = 0; i < this.obj_Errors.length; i++ )  {
		this.obj_Errors[i].str_Error += errorBullet;
		this.obj_Errors[i].str_Error += str_Msg + "\n";
		this.errors = true;
		return true;
	}
}

function ErrorMsg() {
/////////////////////////////////////////////////////////////////
	//  This is the object you create to keep track of errors in the document
	this.obj_Errors		= new Array();
	this.errors			= false;
	this.AddCategory	= AddCategory;
	this.AddMessage		= AddMessage;
	this.DisplayMessage	= DisplayMessage;
}

function DisplayMessage() {
/////////////////////////////////////////////////////////////////
	// Displays the error messages.
	// If none, false is returned and no message is displayed.
	if ( this.errors == false ) {
		return false;
	} else {
		var str_msg = "";
		str_msg += errorLine1 + "\n";
		str_msg += errorLine2 + "\n";
		for ( var i = 0; i < this.obj_Errors.length; i++ ) {			// Go through all of the objects
			if ( this.obj_Errors[i].str_Error != '' ) {					// If errors, write the errors
				str_msg += "\n" + errorLine3 + "\n";
				str_msg += this.obj_Errors[i].str_Error + "\n";
			}
		}
		alert( str_msg );												// Display the error message
		return true;
	}
}
// ************* END OBJECT FUNCTIONS **************
