function js_checknumeric(which, errorplaceid) {
	var ValidChars = "0123456789";
	var IsNumber=true;
	var val = which.value;
	var Char;
	var str = '';
	for (i = 0; i < val.length && IsNumber == true; i++) {
		Char = val.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		} else str += Char;
	}
	if (IsNumber == false) {
		//var str_usermessage refers to printLangVar(IWS_LANGVAR_ERR_NUMERICONLY)
		var str_usermessage = 'Sila masukkan input angka sahaja.';
		if (isEnglish()) str_usermessage = 'Please enter numeric inputs only.';

		which.focus();
		which.value = str;
		writeErrorMsg(str_usermessage, errorplaceid);
	} else if (val.length == 0) {
		writeErrorMsg('', errorplaceid);
	} else {
		writeErrorMsg('', errorplaceid);
	}
}

function js_alphabetonly(which, errorplaceid) {
	var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var IsAlphabet=true;
	var val = which.value;
	var Char;
	var str = '';
	for (i = 0; i < val.length && IsAlphabet == true; i++) {
		Char = val.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			IsAlphabet = false;
		} else str += Char;
	}
	if (IsAlphabet == false) {
		//var str_usermessage refers to printLangVar(IWS_LANGVAR_ERR_ALPHABETICONLY)
		var str_usermessage = '';
		if (isMalay()) str_usermessage = 'Sila masukkan input abjad sahaja.';
		else if (isEnglish()) str_usermessage = 'Please enter alphabetic inputs only.';

		which.focus();
		which.value = str;
		writeErrorMsg(str_usermessage, errorplaceid);
	} else if (val.length == 0) {
		writeErrorMsg('', errorplaceid);
	} else {
		writeErrorMsg('', errorplaceid);
	}
}

function js_checkcharsname (obj, errorplaceid) {
		var validChars = "abcdefghijklmnopqrstuvwxyz";
		validChars += validChars.toUpperCase() + "' @/.-";
		var isAlphabet = true;
		var val = obj.value;
		var currentchar;
		var str = '';
		for (i = 0; i < val.length && isAlphabet == true; i++) {
			currentchar = val.charAt(i);
			if (validChars.indexOf(currentchar) == -1) {
				isAlphabet = false;
			} else str += currentchar;
		}
		if (isAlphabet == false) {
        
            //var str_usermessage refers to printLangVar(IWS_LANGVAR_ERR_ALPHABETICONLY)
    		var str_usermessage = '';
    		if (isMalay()) str_usermessage = 'Sila masukkan input abjad sahaja.';
    		else if (isEnglish()) str_usermessage = 'Please enter alphabetic inputs only.';
        
			obj.focus();
			obj.value = str;
			writeErrorMsg(str_usermessage, errorplaceid);
		} else if (val.length == 0) {
			writeErrorMsg('', errorplaceid);
		} else {
			writeErrorMsg('', errorplaceid);
		}
	}

function jump(object, max, next) {
	var len = object.value.length;
	object.maxlength = len;
	var doc = document.getElementById(next);
	if (len == max) {
		doc.focus();
	}
}

function js_alertempty (object, errorplaceid) {
	var result = true;
	if (object.value.length < 1) {
		//var str_usermessage refers to printLangVar(IWS_LANGVAR_ERR_ANSWEREMPTY)
		var str_usermessage = '';
		if (isMalay()) str_usermessage = 'Jawapan kepada soalan peringatan anda <b>mesti</b> diisi.';
		else if (isEnglish()) str_usermessage = 'The answer to your reminder question <b>must</b> be filled.';

		result = false;
		writeErrorMsg(str_usermessage, errorplaceid);
		object.focus();
	}
 	return result;
}

//function js_checkalphanumeric(which, lang) {
//	var ValidChars = "0123456789";
//	var IsNumber=true;
//	var val = which.value;
//	var Char;
//	var str = '';
//	for (i = 0; i < val.length && IsNumber == true; i++) {
//		Char = val.charAt(i);
//		if (ValidChars.indexOf(Char) == -1) {
//			IsNumber = false;
//		} else str += Char;
//	}
//	if (IsNumber == false) {
//		if (lang == 0) var txt = "Sila isi angka sahaja!"
//		else if (lang == 1) var txt = "Please fill in numeric values only!"
//		alert(txt);
//		which.focus();
//		which.value = str;
//	}
//}
//

function hiliteElement(id, color) {
	document.getElementById(id).style.background = color;
}

function disableElement(id) {
	document.getElementById(id).disabled = true;
}

function enableElement(id) {
	document.getElementById(id).disabled = false;
}

function checkEPFNoLength(object, acctype, errorplaceid) {
	var contents = object.value;
	var len;
	var str_usermessage;

	if (acctype == '1') {//member
		len = 8;
		//var str_usermessage refers to printLangVar(IWS_LANGVAR_ERR_MEMNOLENGTH)
		if (isMalay()) str_usermessage = 'Nombor Ahli mesti mempunyai 8 angka.';
		else if (isEnglish()) str_usermessage = 'Member Number must be 8 digits.';

	} else if (acctype == '2') {//employer
		len = 9;
		//var str_usermessage refers to printLangVar(IWS_LANGVAR_ERR_EMPNOLENGTH)
		if (isMalay()) str_usermessage = 'Nombor Majikan mestilah mempunyai 9 angka.';
		else if (isEnglish()) str_usermessage = 'Employer Number must be 9 digits.';
	}
	if (contents.length > 0 && contents.length < len) {
		b = '';
		for (v=0;v < (len-contents.length); v++) { b += "0";}
		object.value = b + contents;
		writeErrorMsg('', errorplaceid);
	}
}

function checkMinLength (object, minlength, fielddescription, errorplaceid) {
	var contents = object.value;
	var str_usermessage;

	//var str_usermessage refers to printLangVar(IWS_LANGVAR_ERR_INPUTMINLENGTH)
	if (isMalay()) str_usermessage = '%inputdesc% tidak boleh kurang daripada %inputlength% aksara.';
	else if (isEnglish()) str_usermessage = '%inputdesc% cannot be less than %inputlength% characters.';

	str_usermessage = str_usermessage.replace(/%inputlength%/, minlength);
	str_usermessage = str_usermessage.replace(/%inputdesc%/, fielddescription);
	if (contents.length > 0 && contents.length < minlength) {
		writeErrorMsg(str_usermessage, errorplaceid);
	} else writeErrorMsg('', errorplaceid);
}

function writeErrorMsg(msg, placementid) {
	document.getElementById(placementid).innerHTML = '<div style="font-size:8pt;color:#F10A49; background-color:#FBDBDB">' + msg + '</div>';
}

function hiliteArea (areaid, hilitecolor) {
	document.getElementById(areaid).style.background = hilitecolor;
	document.getElementById(areaid).disabled = false;
}

function dimArea (areaid, dimcolor) {
	document.getElementById(areaid).style.background = dimcolor;
	document.getElementById(areaid).disabled = true;
}

function chgBorderColor (objectid, thecolor) {
	document.getElementById(objectid).style.borderstyle = 'inset';
	document.getElementById(objectid).style.border = thecolor;
}

//emphasize an area by changing the color of its background & border
function emphasize1 (objectId, color1, bdrcolor1, otherObjectId, color2, bdrcolor2) {
	hiliteArea (objectId, color1);
	chgBorderColor (objectId, bdrcolor1);
	dimArea (otherObjectId, color2);
	chgBorderColor (otherObjectId, bdrcolor2);
}

function hilitedim (objToHilite, objIdToDim) {
	document.getElementById(objToHilite).className = 'activeArea';
	document.getElementById(objIdToDim).className = 'inactiveArea';
}

//disable an element. id2 and so on are optional
function dAble (id1, id2, id3, id4, id5) {
	document.getElementById(id1).disabled = true;
	if (id2) document.getElementById(id2).disabled = true;
	if (id3) document.getElementById(id3).disabled = true;
	if (id4) document.getElementById(id4).disabled = true;
	if (id5) document.getElementById(id5).disabled = true;
}

//enable an element. id2 and so on are optional
function eAble (id1, id2, id3, id4, id5) {
	document.getElementById(id1).disabled = false;
	if (id2) document.getElementById(id2).disabled = false;
	if (id3) document.getElementById(id3).disabled = false;
	if (id4) document.getElementById(id4).disabled = false;
	if (id5) document.getElementById(id5).disabled = false;
}

function jump(object, max, next) {
	var len = object.value.length;
	object.maxlength = len;
	var doc = document.getElementById(next);
	if (len == max) {
		doc.focus();
	}
}

function join_newic (newic1, newic2, newic3, target) {
	var newic1 = document.getElementById(newic1).value;
	var newic2 = document.getElementById(newic2).value;
	var newic3 = document.getElementById(newic3).value;
	var newic = newic1+newic2+newic3;
	document.getElementById(target).value = newic;
}

function join_phonenumber(prefixID, numberID, targetID) {
	var countrycode = getObject(prefixID).value;
	var number = getObject(numberID).value;
	var fullphonenumber = countrycode+'-'+number;
	if (fullphonenumber == '-') fullphonenumber = '';
//	if (getObject(extensionID).value.length > 0)  fullphonenumber += '-'+extensionID;
	//if (extensionID != '')  fullphonenumber += '-'+getObject(extensionID).value;
	getObject(targetID).value = fullphonenumber;
}

function hiliteElement(id, color) {
	document.getElementById(id).style.background = color;
}

function disableElement(id) {
	document.getElementById(id).disabled = true;
}

function enableElement(id) {
	document.getElementById(id).disabled = false;
}

function checkOldIc(object, errorplaceid) {
//	<?php
//		$invalidFrmtOldIc = str_replace ('%inputname%', printLangVar(IWS_LANGVAR_STR_ICNO).' ('.printLangVar(IWS_LANGVAR_STR_OLD).')', printLangVar(IWS_LANGVAR_ERR_INVALIDCHAR));
//	?>

	var ValidNos = "0123456789";
	var ValidChars = "aAkKhHbBgGiIpPmMrRtT";

	//var str_usermessage = "<?=$invalidFrmtOldIc?>";
	//var str_usermessage refers to printLangVar(IWS_LANGVAR_STR_ICNO)printLangVar(IWS_LANGVAR_STR_OLD) printLangVar(IWS_LANGVAR_ERR_INVALIDCHAR)

	if (isMalay()) str_usermessage = 'Aksara tidak sah bagi Nombor Kad Pengenalan Lama.';
	else if (isEnglish()) str_usermessage = 'Invalid character for Identification Card Nombor Old.';

	var str_usermessage = "";
	var IsValid = true;
	var val = object.value;
	var Char;
	var str = '';

	for (i = 0; i < val.length && IsValid == true; i++) {
		Char = val.charAt(i);
		if (i == 0) {
			if (ValidChars.indexOf(Char) == -1 && ValidNos.indexOf(Char) == -1) {
				IsValid = false;
			}
		} else {
			if (ValidNos.indexOf(Char) == -1) {
				IsValid = false;
			}
		}
		
		if (i == 7 && ValidChars.indexOf(val.charAt(0)) == -1) {			
			IsValid = false;			
		}
		
		if (IsValid == true) str += Char;
	}	
	
	if (IsValid == false) {
		object.focus();		
		writeErrorMsg(str_usermessage, errorplaceid);				
	} else {
		writeErrorMsg('', errorplaceid);
	}
	object.value = str.toUpperCase();
}

function checkNewICDate(object, errorplaceid) {
//	<?php
//		$invalidFrmtNewIc = printLangVar(IWS_LANGVAR_ERR_NEWICINVALID);
//	?>
	var str_usermessage = 'Nombor K/P Baru tidak sah.';
	if (isEnglish()) str_usermessage = 'Invalid New IC Number.';
	if (object.value.length > 0) checkYYMMDD(object, errorplaceid, str_usermessage);
}

function checkYYMMDD(object, errorplaceid, errStr) {
	function y2k(number) { return (number < 1000) ? number + 1900 : number; }
	var yymmdd = object.value;
	var str_usermessage = errStr;
    var isValid = true;
    var yr;
	var mnth;
	var dy;
    year  = yymmdd.substring(0,2);
    month = yymmdd.substring(2,4);
    day   = yymmdd.substring(4,6);
	var note1;
    var str = '';
    var test;
    var note = 'note';
	if (year.length == 2) yr = String(year);
	else if (year.length == 1) yr = String(year) + '0';

	if (month.length == 2) mnth = String(month);
	else if (month.length == 1) {
		if (Number(month) > 0) mnth = String(month) + '0';
		else mnth = String(month) + '1';
	}
	else if (month.length == 0) mnth = '01';

	if (day.length == 2) dy = String(day);
	else if (day.length == 1) {
		if (Number(day) > 0) dy = String(day) + '0';
		else dy = String(day) + '1';
	}
	else if (day.length == 0) dy = '01';

	if (Number (yr) > 6) yr = (Number(yr) + 1900);
	else yr = (Number(yr) + 2000);
    mnth = Number(mnth) - 1;

    test = new Date(yr,mnth,dy);
	if ((y2k(test.getYear()) != yr) || (mnth != test.getMonth()) || (dy != test.getDate())) {
    	isValid = false;
    }
    for (var i=0; i<(yymmdd.length-1); i++) {
		var char = yymmdd.charAt(i);
    	str += char;
	}
	if (isValid == false) {
		object.focus();
		writeErrorMsg(str_usermessage, errorplaceid);
		object.value = str;
	}
}

function setElementDisplayMode (object, mode) {
	object.style.display = mode;
}

function hideIt (elementId) {
	var object = document.getElementById(elementId);
	setElementDisplayMode (object, 'none');
}

function unhideIt (elementId) {
	var object = document.getElementById(elementId);
	setElementDisplayMode (object, 'block');
}

function enablethis (objId) {
	var which = objId.substr(3);
	try {
		var items = getFieldArray();
		var str = '';
		for (i=0;i<items.length;i++) {
			var prefix = new Array("lbl","txt");
			for (a=0;a<prefix.length;a++) {
				var theId = prefix[a] + items[i];
				if (items[i] == which) {
					var focusedObj = null;
					var focusedObj;
					if (items[i] == 'NewICNo' && prefix[a] == 'txt') {
						setElementDisableStatus (theId + '1', false);
						setElementDisableStatus (theId + '2', false);
						setElementDisableStatus (theId + '3', false);
						focusedObj = getObject(theId + '1');
					} else {
						setElementDisableStatus (theId, false);
						focusedObj = getObject(theId);
					}
					document.getElementById(objId).checked = true;
					focusedObj.focus();
					str += items[i];
				} else {
					if (items[i] == 'NewICNo' && prefix[a] == 'txt') {
						setElementDisableStatus (theId + '1', true);
						setElementDisableStatus (theId + '2', true);
						setElementDisableStatus (theId + '3', true);
					} else {
						setElementDisableStatus (theId, true);
					}
					var theradioId = 'rdo' + items[i];
					document.getElementById(theradioId ).checked = false;
				}
			}
		}
	} catch (err) {
	}
}

function setElementDisableStatus(elementID, status) {
	document.getElementById(elementID).disabled = status;
//	if (!document.getElementById(elementID).focus()) {
//		//alert('asdas');
//		document.getElementById(elementID).value = '';
//	}
}

function toggleCheckBox(checkBoxId, actionWhenChecked, actionWhenUnchecked) {
	try {
		var checkBoxObj = document.getElementById(checkBoxId);
		if (checkBoxObj.checked == true) {
			checkBoxObj.checked = false;
			eval(actionWhenUnchecked);
		} else if (checkBoxObj.checked == false){
			checkBoxObj.checked = true;
			eval(actionWhenChecked);
		}
	} catch(err) {
		var txt = 'There seems to be a problem:\n';
		alert (txt + err);
	}
}

function isChecked(checkBoxId) {
	var checkBoxObj = document.getElementById(checkBoxId);
	return checkBoxObj.checked;
}

function getDisplayState (elementId, condition, stateIfTrue, stateIfFalse) {
	var object = getObject(elementId);
	if (eval(condition)) {
		object.style.display = stateIfTrue;
	} else {
		object.style.display = stateIfFalse;
	}
}

function getObject (id) {
	try {
		var Obj = document.getElementById(id);
		return Obj;
	} catch(err) {
		var txt = 'There seems to be a problem:\n';
		alert (txt + err);
	}
}

function uncheckIt (boxId) {
	if (isChecked(boxId) == true)
		getObject(boxId).checked = false;
}

function checkIt (boxId) {
	if (isChecked(boxId) == false)
		getObject(boxId).checked = true;
}
/**
* Used for: 1) i-Akaun Activation Page
**/
function setActivationForm(checkBoxId, submittedOptionId) {
	enablethis(submittedOptionId);
	if (isChecked(checkBoxId) == true) {
		unhideIt('activationForm');
	} else {
		hideIt('activationForm');
	}
}

function getOptionToQueryBy (idArray) {
	//expected type is array
	var radioFound = false;
	for (var i=0; i<idArray.length && radioFound == false; i++) {
		var id = idArray[i];
		if (document.getElementById('rdo'+id).checked == true) radioFound = true;
	}
	return id;
}
function findValueToPass () {
	//expected type is array
	var fieldArray = getFieldArray();
	var id = getOptionToQueryBy(fieldArray);
	var valueToPass = document.getElementById('txt'+id).value;
	//alert (valueToPass)
	return valueToPass;
}
function getFieldArray () {
	var fieldArray = new Array('MemberNo','EmployerNo','NewICNo','OldICNo');
	return fieldArray;
}

function postThis () {
	var idArray = new Array();
	var fieldArray = getFieldArray ();
	var id  = getOptionToQueryBy (fieldArray);
	idArray[0] = id;
	idArray[1] = 'txt' + id;
	idArray[2] = 'txtPINNo';
	makePostRequest('activation', 'validateActivation', idArray, 'statusIndicator', 'resultIndicator')
}

function resetFormInputs (elementIDArray) {
	for (var i=0; i<elementIDArray.length; i++) {
		var elementID = elementIDArray[i];
		document.getElementById(elementID).value='';
	}
}

function validate_email(which,errorplaceid) {
	str = which.value
	//if ((str.indexOf(".") > 2) && (str.indexOf("@") > 0) || str == ''){	
	if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		writeErrorMsg('', errorplaceid);
	} else {
		//var str_usermessage refers to printLangVar(IWS_LANGVAR_ERR_INVALIDEMAILADDRESS)
		var str_usermessage = 'Kesilapan pada alamat email.';
		if (isEnglish()) str_usermessage = 'Invalid email address.';
		which.value = str;
		writeErrorMsg(str_usermessage, errorplaceid);
		which.focus();
	}
}

function test() {
    alert ('hai!');
}

function doEPFNoControl (objId, usertype) {
	var theObject = getObject(objId);
	var theTipId = 'iwsHTMLCommonTip_epfNo' + objId;
	var maxlength = 8;
	alert(theObject.value.length)
	if (theObject.value.length > maxlength) theObject.value = theObject.value.substring(0, maxlength);
	theObject.setAttribute ('maxlength', 8);
	theObject.onkeyup = function fn () { js_checknumeric (theObject, theTipId); }
	theObject.onmouseup = function fn () { js_checknumeric (theObject, theTipId); }
	theObject.onmouseup = function fn () { checkEPFNoLength (theObject, usertype, theTipId); }
	theObject.onblur = function fn () { js_checknumeric (theObject, theTipId); }
	theObject.onblur = function fn () { checkEPFNoLength (theObject, usertype, theTipId); }
}

function checkICNo() {
	// 
}

//
//function assignEventListener () {
//}