﻿function popup_password(userId) {
	var urlVal = "../admin/user_password.php";
	urlVal += "?userId=" + userId;
	//alert(urlVal);
	mywindow = window.open(urlVal, "password", "location=no,status=1,scrollbars=1,width=320,height=200,top=250,left=250");
} 

function confirmToDelete() {
	var returnVal = false;
	returnVal = confirm("This will delete all records related to this user.\nProceed?");
	return returnVal;
}

function confirmDelete() {
	var returnVal = false;
	returnVal = confirm("Are you sure you want to delete this record?");
	return returnVal;
}

function checkField(fieldObj, objName, objType, objRequired) {
	//objType = s:string; n:number; e:email, p:password; i:nric
	//objRequired = c:compulsory; o:optional
	var msg = '';
	var objValue = fieldObj.value;
	
	if(objRequired == 'c') {
		msg += checkFieldIfEmpty(objValue, objName);
	}
	
	if(msg == '' && objValue != '') {
		if(objType == 'i') {	//check for nric
			msg += checkFieldIsNRIC(objValue, objName);
		} else if(objType == 'e') {
			msg += checkFieldIsEmail(objValue, objName);
		} else if(objType == 'p') {
			msg += "\nWill validate password later on";
		} else if(objType == 'n') {
			msg += "\nWill validate numbers later on.";
		} else {
			msg += "";
		}
	}
	
	return msg;

}

function checkFieldIfEmpty(fieldObjValue, fieldObjName) {
	var msg = '';
	if(fieldObjValue == '') {
		msg += "\n~ " + fieldObjName + " is compulsory and cannot be empty";
	}
	
	return msg;
}

//check for NRIC
function checkFieldIsNRIC(fieldObjValue, fieldObjName) {
	var msg = '';
	var errorCounter = 0;
	var nricValLength = fieldObjValue.length;
	
	if(nricValLength == 14) {	
		for(var i=0;i < nricValLength; i++) {
			var new_key = fieldObjValue.charAt(i); 
			
			if(i == 6 || i == 9) {
				if(new_key != '-') { errorCounter++; }
			} else {
				if(isNaN(new_key)) { errorCounter++; }
			}
		}
		
		if(errorCounter > 0) {
			msg += "\n~ Invalid format of " + fieldObjName + ". Example; 999999-99-9999";
		}
	} else {
		msg += "\n~ Invalid format of " + fieldObjName + ". Example; 999999-99-9999";
	}
	
	return msg;
}

//check for email
function checkFieldIsEmail(str, fieldObjName) {
	var msg = '';
	var errorCounter = 0;	
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
		
	if (str.indexOf(at)==-1) { errorCounter++; }
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		errorCounter++;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		errorCounter++;
	}

	if (str.indexOf(at,(lat+1))!=-1) { errorCounter++; }
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		errorCounter++;
	}

	if (str.indexOf(dot,(lat+2))==-1) { errorCounter++; }
	if (str.indexOf(" ")!=-1){ errorCounter++; }

 	if(errorCounter > 0) {
		msg = "\n~ Invalid format of " + fieldObjName;
	}
	
	return msg;					
	
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	if (selObj.options[selObj.selectedIndex].value != ""){
	  window.open(selObj.options[selObj.selectedIndex].value, targ);
	  if (restore) selObj.selectedIndex=0;
	}
}