/*
example use:
if(!isEmpty(value))
{
	isValidNameString(value,"should only contain alphanumeric characters");
	checkStrLen(value, 23, "The Group description is too long!");
}

if(checkValid())
{
    submit();
}

*/

var error = false;
var errorMessage = "Please rectify the following errors:\n\n";

// called after all validation
function checkValid()
{
    if(error)
    {
        alert(errorMessage);
        //error
    }
    var result = error;
    error = false;
    errorMessage = "";
    return !result;    
}

function addMsg(msg) 
{
    error = true;
    errorMessage += msg + '\n';   
}
        
// is valid email
function isEmail(value,msg) 
{
    
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(value))) 
    { 
        if(msg != null)
        {
            error = true;
            errorMessage += msg + '\n';   
        }
        return false;   
    }
    else 
    {
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
        if (value.match(illegalChars)) 
        {
            if(msg != null)
            {
                error = true;
                errorMessage += msg + '\n';   
            }
            return false;   
        }
    }
    return true;
}

function isValidDate(value,msg) 
{ 
    if (!isDateValid(value,"DMY")) 
    { 
        if(msg != null)
        {
            error = true;
            errorMessage += msg + '\n';   
        }
        return false;   
    }
    return true;
}

function isDateValid(dateStr, format) 
{
    if (format == null) 
    { 
        format = "MDY"; 
    }
    format = format.toUpperCase();
    if (format.length != 3) 
    { 
        format = "MDY"; 
    }
    if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) 
    { 
        format = "MDY"; 
    }
    if (format.substring(0, 1) == "Y") // If the year is first
    { 
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
    } 
    else if (format.substring(1, 2) == "Y") // If the year is second
    { 
        var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
        var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
    } 
    else // The year must be third
    { 
        var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
        var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
    }
    // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
    if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) 
    { 
        return false; 
    }
    // Split into 3 parts based on what the divider was
    var parts = dateStr.split(RegExp.$1); 
    // Check to see if the 3 parts end up making a valid date
    if (format.substring(0, 1) == "M") 
    { 
        var mm = parts[0]; 
    } 
    else if (format.substring(1, 2) == "M") 
    { 
        var mm = parts[1]; 
    } 
    else 
    { 
        var mm = parts[2]; 
    }
    if (format.substring(0, 1) == "D") 
    { 
        var dd = parts[0]; 
    } 
    else if (format.substring(1, 2) == "D") 
    { 
        var dd = parts[1]; 
    }
    else 
    { 
        var dd = parts[2]; 
    }
    if (format.substring(0, 1) == "Y") 
    { 
        var yy = parts[0]; 
    } 
    else if (format.substring(1, 2) == "Y") 
    { 
        var yy = parts[1]; 
    } 
    else 
    { 
        var yy = parts[2]; 
    }
    if (parseFloat(yy) <= 50) 
    { 
        yy = (parseFloat(yy) + 2000).toString(); 
    }
    if (parseFloat(yy) <= 99) 
    { 
        yy = (parseFloat(yy) + 1900).toString(); 
    }
    var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
    if (parseFloat(dd) != dt.getDate()) 
    { 
        return false; 
    }
    if (parseFloat(mm)-1 != dt.getMonth()) 
    { 
        return false; 
    }
    return true;
}

function isWord(value,msg) 
{
    var alphaNumeric = /[^a-zA-Z0-9_]/;
    if (alphaNumeric.test(value))
    { 
        if(msg != null)
        {
            error = true;
            errorMessage += msg + '\n';   
        }
        return false;
    }
    return true;
}

function hasValue(value)
{
    return((value != null) && (value.length != 0));
}

// is value empty or contains only whitespace or return chars
function isEmpty(value,msg)
{
    var whitespace = " \t\n\r";
    if(hasValue(value))
    {
        // Search through string's characters one by one
        // until we find a non-whitespace character.
        for (var i = 0; i < value.length; i++)
        {
            // Check that current character isn't whitespace.
            var c = value.charAt(i);    
            if (whitespace.indexOf(c) == -1) return false;
        }                         
    }
    // All characters are whitespace or value is empty.
    if(msg != null)
    {
        error = true;
        errorMessage += msg + '\n';   
    }
    return true;
}
// is valid number
function isNumber(value,msg) 
{
    var numberFilter=/[0-9]/;
    if (numberFilter.test(value))
    { 
        if(msg != null)
        {
            error = true;
            errorMessage += msg + '\n';   
        }
        return false;   
    }
    return true;
}

// is valid telephone number
// - including international code chars
function isTelephoneNumber(value,msg) 
{
    var numberFilter=/[^0-9 +)(]/;
    if ((numberFilter.test(value))) 
    { 
        if(msg != null)
        {
            error = true;
            errorMessage += msg + '\n';   
        }
        return false;   
    }
    return true;
}

function valueMisMatch(value1,value2,msg) 
{
    if(value1 == value2)
    {
        if(msg != null)
        {
            error = true;
            errorMessage += msg + '\n';   
        }
        return false;   
    }
    return true;
}

function valuesMatch(value1,value2,msg) 
{
    if(value1 != value2)
    {
        if(msg != null)
        {
            error = true;
            errorMessage += msg + '\n';   
        }
        return false;   
    }
    return true;
}

// contains given testvalue
function containsValue(value,testValue,msg) 
{
    var ndx = value.search(testValue);
    if(ndx == -1)
    {
        // not found testValue
        if(msg != null)
        {
            error = true;
            errorMessage += msg + '\n';   
        }
        return false;         
    }
    return true;
}

function isMaxLength(str, maxlen, msg)
{
	if(str.length > maxlen)
	{
		error = true;
		errorMessage += msg + '\n';
		return false;
	}
	return true;
}

function isMinLength(str, minlen, msg)
{
	if(str.length < minlen)
	{
		error = true;
		errorMessage += msg + '\n';
		return false;
	}
	return true;
}
