﻿// JScript File

function fnCheckForEmpty(strFieldName, strErrorMessage)
{
    strValue = fnGetFieldValue(strFieldName)
    if (strValue == "") 
        return fnDisplayErrMsg(strFieldName, strErrorMessage);
    else
        return fnHideErrMsg(strFieldName);  
}

function fnCheckForEmailChars(strFieldName, txt) 
{    
	obj = document.getElementById(strFieldName);
	var charlist = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@._-";

	if ( fnCheckForChars( strFieldName, txt, charlist ) == false )
		return fnDisplayErrMsg(strFieldName, txt); 
	else
		return fnHideErrMsg(strFieldName);
}

function fnCheckForEmailFormat(strFieldName,txt)
{
	obj = document.getElementById(strFieldName);

	if (!fnEmailCheck(strFieldName))
		return fnDisplayErrMsg(strFieldName, txt); 
	else
		return fnHideErrMsg(strFieldName); 
}

function fnCheckForChars( strFieldName, txt, charlist )
{
	var obj = document.getElementById(strFieldName);
	
	if ( isChars(obj.value, charlist ) == false )
		return fnDisplayErrMsg(strFieldName, txt );
	else
		return fnHideErrMsg(strFieldName);
}

function isChars(string,charlist) 
{    
	if (!string) return false;
	var Chars = charlist;    
	for (var i = 0; i < string.length; i++) 
	{		if (Chars.indexOf(string.charAt(i)) == -1) { return false }		}
	return true;
} 

function fnEmailCheck(strFieldName) 
{
	obj = document.getElementById(strFieldName);
	emailStr = obj.value;
		
	//if (emailStr=="")
	//{
	//	fnDisplayErrMsg(obj, arrLangText[31]);
	//	return false;
	//} 

	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */

	var checkTLD = 1;

	/* The following is the list of known TLDs that an e-mail address must end with. */

	//var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|COM|NET|ORG|EDU|INT|MIL|GOV|ARPA|BIZ|AERO|NAME|COOP|INFO|PRO|MUSEUM)$/;

	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */

	var emailPat=/^(.+)@(.+)$/;

	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */

	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/

	var validChars="\[^\\s" + specialChars + "\]";

	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */

	var quotedUser="(\"[^\"]*\")";

	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */

	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

	/* The following string represents an atom (basically a series of non-special characters.) */

	var atom=validChars + '+';

	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */

	var word="(" + atom + "|" + quotedUser + ")";

	// The following pattern describes the structure of the user

	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */

	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	/* Finally, let's start trying to figure out if the supplied address is valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		// Too many/few @'s or something; basically, this address doesn't even fit the general mould of a valid e-mail address. 
		//fnDisplayErrMsg(obj, arrLangText[40]);
		strEmailAlertMsg = "Please check the format of your email address. It should be in the following format: myname@domainname.com";
		return false;
	}
	else
	{
//	    if(strFieldName == 'txtFriend1EmailID')
//	    {
//	        
//	    }
//		fnHideErrMsg(obj.id);		
    }

	var user=matchArray[1];
	var domain=matchArray[2];

	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			//fnDisplayErrMsg(obj, arrLangText[41]);
			strEmailAlertMsg = "Please check your email username for invalid characters. Only alphanumeric characters should be used along with @ and . (dot)";
			return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			//fnDisplayErrMsg(obj, arrLangText[41]);
			strEmailAlertMsg = "Please check your email username for invalid characters. Only alphanumeric characters should be used along with @ and . (dot)";
			return false;
	   }
	}

	// See if "user" is valid 

	if (user.match(userPat)==null) {
		// user is not valid
		//fnDisplayErrMsg(obj, arrLangText[40]);
		strEmailAlertMsg = "Please check your email username for invalid characters. Only alphanumeric characters should be used along with @ and . (dot)";
		return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	/* disabled
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				fnDisplayErrMsg(obj, arrLangText[36]);
				return false;
		   }
		}
		return true;
	}
	*/

	// Domain is symbolic name.  Check if it's valid.
	 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			//fnDisplayErrMsg(obj, arrLangText[40]);
			strEmailAlertMsg = "Please check the format of your email address. It should be in the following format: myname@domainname.com";
			return false;
	   }
	}

	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		//fnDisplayErrMsg(obj, arrLangText[40]);
		strEmailAlertMsg = "Please check the format of your email address. It should be in the following format: myname@domainname.com";
		return false;
	}

	// Make sure there's a host name preceding the domain.

	if (len<2) {
		//fnDisplayErrMsg(obj, arrLangText[40]);
		strEmailAlertMsg = "Please check the format of your email address. It should be in the following format: myname@domainname.com";
		return false;
	}

	//fnHideErrMsg(obj.id);

	// If we've gotten this far, everything's valid!
	return true;
}

function fnCheckForPhoneFormat(strFieldName, strErrorMessage)
{
	obj = document.getElementById(strFieldName);
	var charlist = "0123456789()-+ ";

	if ( fnCheckForChars( strFieldName, strErrorMessage, charlist ) == false )
		return fnDisplayErrMsg(strFieldName, strErrorMessage); 
	else
		return fnHideErrMsg(strFieldName);
}

function fnCheckForCountryList(strFieldName, strErrorMessage)
{
    strValue = fnGetFieldValue(strFieldName)
        
    if (strValue == 0) 
        return fnDisplayErrMsg(strFieldName, strErrorMessage);
    else
        return fnHideErrMsg(strFieldName);  
}

function fnGetFieldValue(ID)
{
    return document.getElementById(ID).value;
}

function fnDisplayErrMsg(strFieldName, strErrorMessage, errObj)
{	    

    objField = document.getElementById(strFieldName)
  
	if (typeof errObj == 'object')	
	{	   
	    errObjId = document.getElementById(errObj.id);	   
	}
	else
	{	    
		errObjId = document.getElementById('err_' + objField.id);			
	}
			
	try
	{
	    if (strErrorMessage != "")
	    {
		    //errObjId.innerHTML = "<img src='images/red_arrow.gif' valign='middle' align='absmiddle' />" + strErrorMessage;
		    errObjId.innerHTML = strErrorMessage;
		}
		// errObjId.style.display = "inline";		
	}
	catch (e)
	{
		alert("Error encountered: " + e.message + " --  Please check error display placeholder of " + objField.id)
	}

	if (strErrorField == "") strErrorField = objField.id;

	// strEmailAlertMsg = "";  // Reset Email Error Message
    
	return false;
}

function fnHideErrMsg(strFieldName)
{
    var strErrorField = "";
	objField = document.getElementById(strFieldName)
	errObj = document.getElementById("err_" + strFieldName);
	
	if (errObj == undefined) 
	{
		alert("Error message place holder for " + strFieldName + " (err_" + strFieldName + ") is not found" )
		return; // hide silent if not defined
	}

	try
	{
		// errObj.innerHTML = "";
		// errObj.style.display = "none";
	}
	catch (e)
	{
		alert("Error encountered: " + e.message + " --  Please check error display placeholder of " + objField.id)
	}

	return true;
}

function fnCheckForNRICFormat(strFieldName, txt)
{
	if (!CheckNRICFormat(strFieldName, txt))
		return fnDisplayErrMsg(strFieldName, txt); 
	else
		return fnHideErrMsg(strFieldName); 
}

// -------------------------------------------------------------------
// CHECK FOR NRIC
// -------------------------------------------------------------------

function CheckNRICFormat(strFieldName, txt)
{
	var obj = document.getElementById(strFieldName);
	var len = obj.value.length;
	
	for (i=0; i<=len-1;i++ )
	{
		if ( (i == 0) || (i == len-1) )
		{
			if (isAlpha( obj.value.charAt(i) ) == false)
				return false;
		}
		else
		{			
			if ( isNum( obj.value.charAt(i) ) == false )
				return false
		}
	}

	return true;	
}

// -------------------------------------------------------------------
// CHECK FOR ALPHANUMERIC
// -------------------------------------------------------------------

function fnCheckForAlphaNumeric( strFieldName, txt )
{
	obj = document.getElementById(strFieldName);

	if ( isAlphaNumeric( obj.value ) == false )
		return fnDisplayErrMsg(strFieldName, txt); 
	else
		return fnHideErrMsg(strFieldName);
}

// -------------------------------------------------------------------
// CHECK FOR ALPHABETS
// -------------------------------------------------------------------

function isAlpha(string) 
{    
	if (!string) return false;
	var Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";    
	for (var i = 0; i < string.length; i++) 
	{		
		if (Chars.indexOf(string.charAt(i)) == -1) 
			return false;
	}
	return true;
} 



// -------------------------------------------------------------------
// CHECK FOR NUMERIC
// -------------------------------------------------------------------

function isNum(string) 
{    
	if (!string) return false;
	var Chars = "0123456789";    
	for (var i = 0; i < string.length; i++) 
	{		
		if (Chars.indexOf(string.charAt(i)) == -1)
			return false;
	}
	return true;
} 
// -------------------------------------------------------------------
// CHECK FOR ALPHANUMERIC
// -------------------------------------------------------------------

function isAlphaNumeric(string) 
{    
	if (!string) return false;
	var Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	for (var i = 0; i < string.length; i++) 
	{		
		if (Chars.indexOf(string.charAt(i)) == -1)
			return false;
	}
	return true;
} 

function getRadioSelectedValue(radioList)
{
    var options = radioList.getElementsByTagName('input');    
    for(i=0;i<options.length;i++)
    {
        var opt = options[i];        
        if(opt.checked)
        {            
            return true;
        }        
    }
    return false;
}