
var userMax   = 50; //max length of a username
var minLength = 2;  //min length of a password
var maxLength = 15; //max length of a password

function ValidEmail(strEmail){
//MD recoded Nov 05,2009 to be used in the next release
/*
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (strEmail.match(emailRegEx))
	{	
			//strEmail.focus();
			return true;

	}

	alert("Please enter a valid email address.");
	return false;  */
		
	if (strEmail.length >= userMax){		
		//invalid Email length
		return false;
	} else {
		if (strEmail.match(/^([a-zA-Z0-9_\-\.]+)@{1}([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,})$/)){
			//valid
			return true;
		} else {
			//invalid
			return false;
		}
	}
	
	
}

function ValidateEmailWithLangauage(strEmail,EnglishFlag){
//MD Created Nov 05,2009

	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (strEmail.match(emailRegEx))
	{	
			//strEmail.focus();
			return true;

	}

	if (EnglishFlag)
		alert("Please enter a valid email address.");
	else
		alert("Veuillez entrer une adresse de courriel valable.");
		
	return false;
	
}

function DoubleEmailValidation(objEmail1,objEmail2,EnglishFlag){
//MD created Nov 05,2009
	var strEmail1= objEmail1.value;
	var strEmail2= objEmail2.value;
	
	if (ValidateEmailWithLangauage(strEmail1,EnglishFlag) == false)
	{
		objEmail1.focus();
		return false;
		
		
	}
		
	if (ValidateEmailWithLangauage(strEmail2,EnglishFlag) == false)
	{
		objEmail2.focus();		
		return false;
		
	}		
	

	if (strEmail1 != strEmail2)
	{
		if (EnglishFlag)
			alert("The two email addresses you have entered do not match.");
		else
			alert("Les deux adresses courriels que vous avez entres ne correspondent pas.");
			
			
			objEmail2.focus();			
			return false;

	}
	
	return true;
	
}


function ValidURL(strURL){

if (strURL.indexOf(".")<0){
		
		//required characters in url address
		return false;
	}
	if (strURL.match(/[`@!#$%\*\(\)+=\|\{\}\[\]'":;\?><,]/)){
				 
		//invalid characters in an email address
		return false;
	}
	
	return true;
}

function ValidUserName(strUserName){

	if (strUserName.match(/[^a-zA-Z0-9@._]/)){
				 
		//invalid characters in Username
		return false;
	}
	if (strUserName.length >= userMax || strUserName.length < minLength){
		
		//invalid Username length
		return false;
	}
	return true;
}

function ValidUserNameNew(strUserName){


	if (strUserName.length >= userMax || strUserName.length < minLength){
		
		//invalid Username length
		return false;
	}
	return true;
}

function ValidChannelName(strChannelName){

	if (strChannelName.match(/[^a-zA-Z0-9@._-]/)){
				 
		//invalid characters in Username
		return false;
	}
	
	return true;
}



function LegalLocalPhone(strPhoneNumber){
	
	var strVal;
	
	strVal = strPhoneNumber;
	if (strPhoneNumber.length != strVal.length) {
		return false;
	}
	
	if (strVal.length < 10) {
		 return false;
	}
	if (strVal.length > 10) {
		 return false;
	}
	
	if ((strVal.indexOf("0")== 0)){
		return false;
	}
	
	return true;	
}

function LegalModem(strSerialNumber){
	
	var strVal;
	
	strVal = strSerialNumber;
	strVal = strVal.replace (/[^0-9A-Fa-f]/g,"");
	
	if (strSerialNumber.length != strVal.length) {
		return false;
	}
	
	if (strVal.length < 7  || strVal.length > 12) {
		 return false;
	}
	
	return true;	
}

function LegalPassword(strPsswd){
	
	var strVal;
	strVal = strPsswd;
	
	strVal = strVal.replace(/[^a-zA-Z0-9]/g,"");
	if (strPsswd.length != strVal.length) { 
		
		return false;
	}
	if (strPsswd.length < minLength) { 
		
		return false;
	}
	return true;
}	


function LegalPasswordOld(strPsswd){
	
	if ((strPsswd.length < minLength)||(strPsswd.length > maxLength)){
		//invalid password length
		return false;
	}
	return true;
}

function ValidCustomerName(strCName){
	
	if ((strCName.length < 1)||(strCName.length > 50)){
		//invalid Customer Name length
		return false;
	}
	return true;
}

function ValidAccountNumber2(strAcctNo){
	intVal = strAcctNo;
	
	intVal = intVal.replace(/[^0-9]/g,"");
	if ( (intVal != strAcctNo) || (strAcctNo.length < 5) || (strAcctNo.length > 7) ) {
		return false;
	}
	return true;
}

function ValidPartAccountNumber(strAcctNo){
	intVal = strAcctNo;
	
	intVal = intVal.replace(/[^0-9]/g,"");
	if ( (intVal != strAcctNo) || (strAcctNo.length < 5) || (strAcctNo.length > 8) ) {
		return false;
	}
	return true;
}


function ValidDescription(strDesc){
	
	if ((strDesc.length < 1)||(strDesc.length > 500)){
		//invalid length
		return false;
	}
	return true;
}


function ValidAddress(strAddress){
	
	if ((strAddress.length < 1)||(strAddress.length > 60)){
		//invalid Address length
		return false;
	}
	return true;
}

function ValidChoice(strChoice){
	
	if (strChoice=="None"){
		return false;
	}
	return true;
}

function ValidAccountNumber(strAcct){
	
	if (strAcct !== ""){
	
		if ( (strAcct.length < 5) || (strAcct.length > 11) || (parseInt(strAcct,10) < 10000) ) {
			return false;
		}
	}
	return true;
}

function ValidSuiteNumber(strSuite){
	
	if (strSuite == "NA")
	{
		return true;
	}
	
	if ((strSuite !== "") || (strSuite !== "NA"))
	{
		if ((strSuite.length < 1) || (strSuite.length > 5)) 
		{
			return false;
		}
	}
	
	return true;
}

function ValidPostalCode(strPCode){
	
	if (strPCode.length > 6){
		strPCode = strPCode.replace(/[^a-zA-Z0-9]/g,"");
		if (strPCode.length > 6){
			return false;
		}
	}
	if (strPCode.match(/[A-Z]\d[A-Z]\d[A-Z]\d/)){
		return true;
	}
	return false;
}

function ValidCorporateNumber(strCorpNumb){
	
	if (strCorpNumb.length > 14){
		strCorpNumb = strCorpNumb.replace(/[^a-zA-Z0-9]/g,"");
		if (strCorpNumb.length > 14){
			return false;
		}
	}
	if (strCorpNumb.match(/\d\d\d\d\d\d\d\d\d[A-Z][A-Z]\d\d\d/)){
		return true;
	}
	return false;
}

function AmountREQ(strAMT){
	
	var strVal;
	
	strVal = strAMT;
	if (strAMT.length != strVal.length) {
		return false;
	}
	
	if (strVal.length < 2) {
		 return false;
	}
	
	if ((strVal.indexOf("0")== 0)){
		return false;
	}
	
	return true;
}

function LegalPIN(strPIN){
	
	var intVal = strPIN;
	
	intVal = intVal.replace(/[^0-9]/g,"");
	if ( (intVal != strPIN) || (strPIN.length != 4) || (parseInt(strPIN,10) < 1000) || (parseInt(strPIN,10) > 9999) ) {
		return false;
	}
	return true;
}



function LegalFriendlyName(strFName){
	var strTemp = strFName;
	
	if (strTemp.length > 30){
		return false;
	}
	
	strTemp = strTemp.replace(/[^a-zA-Z0-9 @]/g,"");
	if (strTemp!= strFName){
		return false;
	}
	return true;
}

function ValidQty(intQty){

	if (intQty.match(/[^0-9]/)){
				 
		//invalid characters in intQty
		return false;
	}
	if (intQty.length < 1) {
		 return false;
	}
	return true;
}

// Returns true if character c is a digit (0 .. 9).
function isDigit (c)

{
    return ((c >= "0") && (c <= "9"))
}

// Check whether string s is empty.
function isEmpty(s)

{
    return ((s == null) || (s.length == 0))
}

function isInteger (strInput)
{
   var i;

    // Search through string's char one by one until we find a non-numeric char
    // When we do, return false; if we don't, return true

    for (i = 0; i < strInput.length; i++)
    {   
        // Check that current character is number.
        var c = strInput.charAt(i);

        if (!isDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}

function isValidTime(timeStr)
{
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	
	if (matchArray == null)
	{
	   return false;
	}

	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="")
	{  
	   second = null;
	}
	
	if (ampm=="")
	{
	   ampm = null
	}

	if (hour < 0 || hour > 23)
	{
	   return false;
	}

	if (hour <= 12 && ampm == null)
	{
	   return false;
	}

	if (hour > 12 && ampm != null)
	{
	   return false;
	}

	if (minute < 0 || minute > 59)
	{
	   return false;
	}

	if (second != null && (second < 0 || second > 59))
	{
	   return false;
	}

	return true;
}



function ValidPart4DigitAccountNumber(strAcctNo){
//Validating for 4 digit Account number
	var intVal;
	var bReturn;
	intVal = strAcctNo;
	bReturn = false;
	
	intVal = intVal.replace(/[^0-9]/g,"");
	if ( (intVal != strAcctNo) || (strAcctNo.length < 3) || (strAcctNo.length > 4) ) 
		bReturn= false;
	else
		bReturn= true;
		
		
		return bReturn;

}

