/* List of validation functions */
/*
	Note: In all these functions, pass the second argument as 'true'
	      to allow empty string as valid data for that particular form field.
	      If the second argument is not passed, empty string are considered
	      to be INVALID entries. 
*/

var min_nmln = 2;
var max_nmln = 30;

/* Strict validation for alphabets and spaces*/
function isValidName(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
        //re = RegExp("^[a-zA-Z\ ]+$");
        //re1 = RegExp("^[a-zA-Z0-9_\ \-\.\']+$");
        //re2 = RegExp("[\_\.\-\']{2}");
		//re1 = RegExp("^[a-zA-Z0-9][a-zA-Z0-9_'\ \-\.]+$");
	re1 = RegExp("^[0-9a-zA-Z][a-zA-Z0-9_'\ \.\+\*\-]+$"); 
	re2 = RegExp("[_'\ \.\*\+\-]{2}");
	if (!re1.test(str) || re2.test(str))
                return false;
        else
                return true;
}
/* Strict validation for alphabets only*/
function isValidAlpha(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
	re = RegExp("^[a-zA-Z]+$");
        if (!re.test(str))
                return false;
        else
                return true;
}
/* Strict validation for alphanumeric values only */
function isValidAlphaNumeric(str, allowEmpty)
{
        if (allowEmpty && str == '')
		return true;
	re = RegExp("^[a-zA-Z][a-zA-Z0-9]*$");
        if (!re.test(str))
                return false;
        else
                return true;
}
/* Strict validation for numeric values only */
function isValidNumeric(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
        re = RegExp("^[1-9][0-9]*$");
        if (!re.test(str))
                return false;
        else
                return true;
}
/* Strict validation for Username */
function isValidUser(str, allowEmpty)
{
	if (allowEmpty && str == '')
               return true;
        //re = RegExp("^[a-zA-Z][a-zA-Z0-9\_\.]*$");
        re1 = RegExp("^[0-9a-zA-Z][a-zA-Z0-9\_\.\-]*$");
        re2 = RegExp("[\_\+\.\*\-]{2}");
        if (!re1.test(str) || re2.test(str))
                return false;
        else
                return true;
}
function isValidUserLen(str, allowEmpty)
{
	if (allowEmpty && str == '')
               return true;
        var len = str.length;
        if(len < min_nmln || len > max_nmln)
		return false;
	else
		return true;
}
function isValidFirstUserLen(str, allowEmpty)
{
        if (allowEmpty && str == '')
               return true;
        var len = str.length;
        if(len > max_nmln)
                return false;
        else
                return true;
}
function isValidLastUserLen(str, allowEmpty)
{
        if (allowEmpty && str == '')
               return true;
        var len = str.length;
        if(len > max_nmln)
                return false;
        else
                return true;
}
/* The following function deliberately allows special characters, supressing only '`' */
/* Relaxed validation for Password */
function isValidPassword(str, allowEmpty)
{
	if (allowEmpty && str == '')
                return true;
	var pattern=/[a-zA-z]{1,}/;
	flag1=pattern.test(str);
	var pattern=/[0-9]{1,}/;
	flag2=pattern.test(str);
	var pattern=/[!@#%&*()\-_+=\/:;.`\^\$\{\}<>?|\[\]]{1,}/;
	flag3=pattern.test(str);
	var pattern=/[~'",\\]{1,}/;
	flag4=pattern.test(str);
	if(flag1 == false || flag2 == false || flag3 == false || flag4 == true){
		return false;
	}
	else
	{
		return true;
	}
	//return true;
	/*if (allowEmpty && str == '')
                return true;
	re = RegExp("^[^'\`\\\\/\ ~\"]+$");
        if (!re.test(str))
                return false;
        else{
                re = RegExp("[0-9]+");
                re1 = RegExp("^[a-zA-Z0-9]*$");
                if(re.test(str) && !re1.test(str)){
                        return true;
                }else{
                        return false;
                }
	}*/
	/*
	var re=/[~,`'"\\/]/;
        if (re.test(str))
                return false;
        else
                return true;*/

}
function isValidPassLen(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
        var min_pass_length = 4;
        var max_pass_length = 8;
        var len = str.length;
        if(len <  min_pass_length || len > max_pass_length)
		return false
	else
		return true;
}
/* Added for SLEMS 10.3 */
function isValidGroupLen(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
        var len = str.length;
        if(len <  min_nmln || len > max_nmln)
        return false;
    else
        return true;
}


/* The following function deliberately allows special characters, supressing only '`' */
/* Relaxed validation for comments, titles, messages, etc */
function isValidComment(str, allowEmpty)
{
		//Number, Underscore, Alphabets, Space, dot, /, #, Comma, -, Single Quote, Double quote
        if (allowEmpty && str == '')
                return true;
        re = RegExp("^[^\`]+$");
        if (!re.test(str))
                return false;
        else
                return true;
}

function isValidAddress(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
        //re = RegExp("^[A-Za-z0-9#][A-Za-z0-9#\s\.\,\-\'\"]+$");
        //re = new RegExp("^[\ ]*[A-Za-z0-9#\\n\\r][A-Za-z0-9#,_\-\\n\\r\ \"\'\.\/]+$" );
		re = new RegExp("^[\ ]*[A-Za-z0-9#\\n\\r][A-Za-z0-9\-#,\\n\\r\ \'\"\.\(\)\/\_]*$" );
		//re1 = new RegExp("[\-#,\\n\\r\'\"\.\/\_]{2}" );
		if (!re.test(str))// || re1.test(str))
                return false;
		else
                return true;
}

/* Relaxed validation for hostname */
function isValidHostname(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
        if (!isValidDomain(str))
	{
		if (!isValidIP(str))
	                return false;
		else
			return true;
	}
        else
                return true;
}

/* Relaxed validation for domain name */
function isValidDomain(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
	//re = RegExp("^[a-zA-Z][a-zA-Z0-9\.\-]*[a-zA-Z0-9]$");
	re1 = /^([a-zA-Z]([a-zA-Z0-9\-]*[a-zA-Z0-9])?\.)*[a-zA-Z0-9]*[a-zA-Z]$/;
        re2 = /[\-]{2}/;
        if (!re1.test(str) || re2.test(str))
                return false;
        else
                return true;
}

/* Relaxed validation for IP */
function isValidIP(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
	re = RegExp("^[0-9]{1,3}[\.][0-9]{1,3}[\.][0-9]{1,3}[\.][0-9]{1,3}$");
	if (!re.test(str))
                return false;
        else
                return true;
}

/* Relaxed validation for domain name */
function isValidCookieDomain(str, allowEmpty)
{
	if (allowEmpty && str == '')
		return true;
        re = RegExp("^[\.][a-zA-Z0-9\.\-]*[a-zA-Z0-9]$");
        if (!re.test(str))
                return false;
        else
                return true;
}

/* Strict validation for email id */
function isValidEmail(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
	//re = RegExp("^[a-zA-Z][a-zA-Z0-9\_\.]*[\@][a-zA-Z][a-zA-Z0-9\.\-]*[\.][a-zA-Z\.]+$");
	re = RegExp("^[0-9a-zA-Z][a-zA-Z0-9\_\.\-]*[\@][a-zA-Z][a-zA-Z0-9\.\-]*[\.][a-zA-Z\.]+$");
	re2 = RegExp("[\-\_\.\@]{2}");
        if (!re.test(str) || re2.test(str))
                return false;
        else
                return true;
}

/* Relaxed validation for only alphabets */
function isValidFilePath(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
	re = /^[\\\.\/\:a-zA-Z0-9\ ]*$/;
        if (!re.test(str))
                return false;
        else
                return true;
}

/* Extension of file name with preceeding dot (eg. => .txt : .gif : .avi */
function getExtension(str)
{
	var idx;
	if ((idx = str.lastIndexOf('.')) == -1)
		return false;
	else
		var txt = str.substr(idx);
		if (txt == '.')
			return false;
		else
			return txt;
}

/* Trims the leading and trailing white spaces */
function trim(value)
{
	var temp = value;
	var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
	if (obj.test(temp)) {temp = temp.replace(obj, '$2'); }
	var obj = / +/g;
	temp = temp.replace(obj, " ");
	if (temp == " ") { temp = ""; }
		return temp;
}

/*Added below function for bugid 0003396 By Prabahar*/
/*Begin*/
function isValidMinComment(str, allowEmpty)
{
	if (allowEmpty && str == '')
                return true;
        //re = /^[a-zA-Z][a-zA-Z0-9\ \_\-\?\.\']+$/;
	re = /^[a-zA-Z][a-zA-Z0-9,\ \_\-\?\.\'\&\(\)\/]+$/;
	re1 = /[,\_\-\?\.\'\&\(\)\/]{2}/;
        if (!re.test(str) || re1.test(str))
                return false;
        else
                return true;
}

function isValidDisplayName(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
        re = /^[a-zA-Z0-9][a-zA-Z0-9_\*\-\+\ \.\']*$/;
		re1 = /[_\*\-\+\.\']{2}/;
        if (!re.test(str) || re1.test(str))
                return false;
        else
                return true;
}
function isValidDisplayName_new(str, allowEmpty)
{
        if (allowEmpty && str == '')
                return true;
        re = /^[a-zA-Z'&\/][a-zA-Z0-9_\*\+\ \.\'&\/]*$/;
                re1 = /[_\*\+\.\']{2}/;
        if (!re.test(str) || re1.test(str))
                return false;
        else
                return true;
}

/*Added for Slems 10.3*/
function isValidDispNameLen(str, allowEmpty)
{
    if (allowEmpty && str == '')
               return true;
        var len = str.length;
        if(len > 2*(max_nmln))
        return false;
    else
        return true;
}

//Changes for SLEMS 10.3 Validation
/* Strict validation for Phone Number only */                                                                                           
function isPhoneNumber(str, allowEmpty) 
{
	if (allowEmpty && str == '')
		return true;
	str = trim(str);
	
	        if(str.length < 5 || str.length > 20) //:+
		return false; 	//:+

	re = RegExp("^[0-9\+\(][0-9\+\ \(\)\-]*$");
	re1 = /[\(\)]{2}/;
        re2 = /[\+\-\)]{2}/;
	if (!re.test(str) || re1.test(str) || re2.test(str))
		return false;
	else
		return true;
}

/* Strict validation for ZIP(PIN) Number only */                                                                                                  
function isValidPinNumber(str, allowEmpty)      
{
	if (allowEmpty && str == '')
		return true;
	re = RegExp("^[a-zA-Z0-9][a-zA-Z0-9\-\ ]*$");     
        re1 = /[\-]{2}/;
        if (!re.test(str) || re1.test(str))
		return false;
	else
		return true;
}
function isValidCity(str, allowEmpty)      
{
	if (allowEmpty && str == '')
		return true;
	re = RegExp("^[a-zA-Z][A-Za-z\ \.]*$");      
	re1 = /[\.]{2}/;
        if (!re.test(str) || re1.test(str))
		return false;
	else
		return true;
}
function isValidState(str, allowEmpty)      
{
	if (allowEmpty && str == '')
		return true;
	re1 = RegExp("^[a-zA-Z][A-Za-z\ \.\&]*$");      
        re2 =  /[\.]{2}/;
        re3 =  /[\&]{2}/;
        if (!re1.test(str) || re2.test(str) || re3.test(str))
		return false;
	else
		return true;
}
function isValidCountry(str, allowEmpty)      
{
	if (allowEmpty && str == '')
		return true;
	re = RegExp("^[a-zA-Z][A-Za-z\ \&]*$");      
	re1 = /[\&]{2}/;
        if (!re.test(str) || re1.test(str))
		return false;
	else
		return true;
}

function isValidCompanyName(str, allowEmpty)      
{
	if (allowEmpty && str == '')
		return true;
	re = RegExp("^[a-zA-Z0-9][A-Za-z0-9\ \@\&\.\,\'\\\/]*$");      
	if (!re.test(str))
		return false;
	else
		return true;
}


function validate_html(str,stralert)
{
	
	
	 if(str.match(/([\<])([^\>]{1,})*([\>])/i)!=null)
	{
			alert(stralert + " Contains HTML Tag");
			return false;
	}
	
	return true;
	

} 
/*End*/

