﻿function Len(str){
	return String(str).length;
}

function Mid(str, start, len){
	if ((start < 0) || (len < 0))
	return "";
	var iEnd, iLen = String(str).length;
	if (start + len > iLen)
		iEnd = iLen;
	else
		iEnd = start + len;
		return String(str).substring(start,iEnd);
	}

function CheckNumeric(tStr){
	var i;
	var n;
	var err=0;
	var cnt=0;
	var a = new String(tStr);
	//var iLength = a.Length;
	iLength = Len(tStr)
	for (i=0; i <= iLength-1; i++){
		n = a.substring(i, i+1);
		if (n!="0" && n!="1" && n!="2" && n!="3" && n!="4" && n!="5" && n!="6" && n!="7" && n!="8" && n!="9" && n!=".")
			err = 1;
		if (n==".")
			cnt++;
		}
	if (err == 1 || cnt > 1)
		return(1);
	}

function validatenric(tStr)
{
	var sTemp=Mid(tStr,0,1);

    if(sTemp == "S" || sTemp == "T" || sTemp == "F" || sTemp == "G")
    {

	    var sUniArr = new Array("", "A", "B", "C", "D", "E", "F", "G", "H", "I", "Z", "J");

	    var sFinArr = new Array("", "K", "L", "M", "N", "P", "Q", "R", "T", "U", "W", "X");

	    if (Len(tStr) != 9)
	    {
		    return false;
	    }

	    var sPrefix = Mid(tStr, 0, 1);

	    if (CheckNumeric(sPrefix) != 1)
	    {
		    return false;
	    }

	    sPrefix = sPrefix.toUpperCase();

	    if (sPrefix != "S" && sPrefix != "T" && sPrefix != "F" && sPrefix != "G")
	    {
		    return false;
	    }

	    for (iIndex=1; iIndex <= 7; iIndex++)
	    {
		    if (CheckNumeric(Mid(tStr, iIndex, 1)) == 1)
		    {
			    return false;
		    }
	    }

      lSum = (parseInt(Mid(tStr, 1, 1))) * 2 + (parseInt(Mid(tStr, 2, 1)) * 7) + 
    (parseInt(Mid(tStr, 3, 1)) * 6) + (parseInt(Mid(tStr, 4, 1)) * 5) + 
    (parseInt(Mid(tStr, 5, 1)) * 4) + (parseInt(Mid(tStr, 6, 1)) * 3) + 
    (parseInt(Mid(tStr, 7, 1)) * 2);

      if (sPrefix=="T" || sPrefix=="G")
      {
		    lSum = lSum + 4
      }

      iIndex = 11 - (lSum % 11)

      if (sPrefix=="S" || sPrefix=="T")
      {
		    sCheck = sUniArr[iIndex]
      }
      else
      {
        if (sPrefix=="F" || sPrefix=="G")
        {
          sCheck = sFinArr[iIndex];
        }
        else
        {
			    return false;
        }
      }

      if (Mid(tStr.toUpperCase(), 8, 1) != sCheck)
      {
		    return false;
	    }
    }
    else
    {
	    return true;
    }
	return true;
}





function validateuin(tStr, czStatus)
{
	/*----------------------------------------------------------------
	tStr - Identity Number
	czStatus - Citizenship Status 
	
	          Example
	          =======
	          CZ(Singapore Citizen), PR(Permanent Resident)
	          EP(Employment Pass), DP(Dependent Pass), SL(Student with LOC), WP(Work Permit), SP (S Pass)
			CZ & PR use NRIC check method
			EP, DP, SL , WP and SP use FIN check method
	----------------------------------------------------------------*/

	var sUniArr = new Array("", "A", "B", "C", "D", "E", "F", "G", "H", "I", "Z", "J");

	var sFinArr = new Array("", "K", "L", "M", "N", "P", "Q", "R", "T", "U", "W", "X");

	if (Len(tStr) != 9)
	{
		return false;
	}

	var sPrefix = Mid(tStr, 0, 1);

	if (CheckNumeric(sPrefix) != 1)
	{
		return false;
	}

	tStatus = "";
	if (czStatus == "CZ" || czStatus == "PR") tStatus = "NRIC";
	if (czStatus == "EP" || czStatus == "DP" || czStatus == "SL" || czStatus == "WP" || czStatus == "SP") tStatus = "FIN";

	sPrefix = sPrefix.toUpperCase();
	
	//if Identity is NRIC and Identity prefix is not "S" or "T" -> invalid Identity Number
	if (tStatus == "NRIC" && sPrefix != "S" && sPrefix != "T")
	{
		return false;
	}

	//if Identity is FIN and Identity prefix is not "F" or "G" -> invalid Identity Number
	if (tStatus == "FIN" && sPrefix != "F" && sPrefix != "G")
	{
		return false;
	}

	for (iIndex=1; iIndex <= 7; iIndex++)
	{
		if (CheckNumeric(Mid(tStr, iIndex, 1)) == 1)
		{
			return false;
		}
	}

	lSum = (parseInt(Mid(tStr, 1, 1))) * 2 + (parseInt(Mid(tStr, 2, 1)) * 7) + 
(parseInt(Mid(tStr, 3, 1)) * 6) + (parseInt(Mid(tStr, 4, 1)) * 5) + 
(parseInt(Mid(tStr, 5, 1)) * 4) + (parseInt(Mid(tStr, 6, 1)) * 3) + 
(parseInt(Mid(tStr, 7, 1)) * 2);


	//for newer generation, those Identity Number issued on 21st Century, do the following
	if (sPrefix == "T" || sPrefix == "G")
	{
		lSum = lSum + 4
	}

	iIndex = 11 - (lSum % 11)

	//if (sPrefix=="S" || sPrefix=="T")
	if (tStatus=="NRIC")
	{
		sCheck = sUniArr[iIndex]
	}
	else
	{
		//if (sPrefix=="F" || sPrefix=="G")
		if (tStatus=="FIN")
		{
			sCheck = sFinArr[iIndex];
		}
		else
		{
			return false;
		}
	}

	if (Mid(tStr.toUpperCase(), 8, 1) != sCheck)
	{
		return false;
	}

	return true;
}

function isBlank(s)
{
	var c;

	for(var i=0;i<s.length;i++)
	{
		c = s.charAt(i);
		if ((c != ' ')&&(c != '\n')&&(c != '\t'))
			return false;
	}
	return true;
}


function isBlank(s)
{
	var c;

	for(var i=0;i<s.length;i++)
	{
		c = s.charAt(i);
		if ((c != ' ')&&(c != '\n')&&(c != '\t'))
			return false;
	}
	return true;
}


//Validate dd/mm/yyyy date format
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1910;
var maxYear=2200;

function isInteger(s)
{
	var i;
	for (i = 0; i < s.length; i++){
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag)
{
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n)
{
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear

	if (dtStr.length < 10) {
		alert("The date format should be : dd/mm/yyyy");
		return false;
	}

	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)

	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}

	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)

	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}

	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter/select a valid month")
		return false
	}

	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter/select a valid day")
		return false
	}

	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}

	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter/select a valid date")
		return false
	}
	return true
}