﻿/* Client-side validation using regular expressions */

var placeholderId = "ctl00_ContentPlaceHolder1_";

function IsNaturalNumber(str)
{
    var pattern = /^(0)|([1-9][0-9]*)$/;
    return pattern.test(str);
}

function IsWholeNumber(str)
{
    var pattern = /[^0-9]/;
    return !pattern.test(str);
}

function IsInteger(str)
{
    var objNotIntPattern = /[^0-9-]/;
    var objIntPattern = /^-[0-9]+$|^[0-9]+$/
    return !objNotIntPattern.test(str) && objIntPattern.test(str);
}

function IsPositiveNumber(str)
{
    var objNotPositivePattern = /[^0-9.]/;
    var objPositivePattern = /^[.][0-9]+$|[0-9]*[.]*[0-9]+$/;
    var objTwoDotPattern = /[0-9]*[.][0-9]*[.][0-9]*/;
    return !objNotPositivePattern.test(str) &&
           objPositivePattern.test(str) &&
           !objTwoDotPattern.test(str);
}

function IsNumber(str)
{
    return !isNaN(str);
}

function IsAlpha(str)
{
    var objAlphaPattern = /[^a-zA-Z]/;
    return !objAlphaPattern.test(str);
}

function IsAlphaNumeric(str)
{
    var objAlphaNumericPattern = /[^a-zA-Z0-9]/;
    return !objAlphaNumericPattern.test(str);
}

function IsDate(str) 
{
    format = "DMY";
    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(str) == false) && (reg2.test(str) == false) ) 
    { 
        return false; 
    }
    
    var parts = str.split(RegExp.$1); // Split into 3 parts based on what the divider was
    // Check to see if the 3 parts end up making a valid date
    var mm = parts[1]; 
    var dd = parts[0];
    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 IsDateAfter(str, str2)
{
    if( !IsDate(str) || !IsDate(str2) )
    {
        return false;
    }
    var Date1 = new Date(str);
    var Date2 = new Date(str2);
    return Date1 > Date2
}

function IsDateBefore(str, str2)
{
    if( !IsDate(str) || !IsDate(str2) )
    {
        return false;
    }
    var Date1 = new Date(str);
    var Date2 = new Date(str2);
    return Date1 < Date2
}

function IsEmail(str)
{
    var objEmailPattern = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
    return objEmailPattern.test(str)    
}

function IsEmpty(str)
{
    return trim(str).length == 0 || str == null;
}

function Validate(str, func)
{
    if(func.toString().toLowerCase() == "isnumber") return IsNumber(str.toString());
    else if(func.toString().toLowerCase() == "isdate") return IsDate(str.toString());
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}



function ShowError(ctrl, errorText, errorImgUrl)
{
    errorIconId = ctrl.id.toString().replace(placeholderId, "") + "ErrorIcon"
    errorIcon = document.getElementById(errorIconId);
    if(errorIcon == null)
    {
        ctrl.parentNode.innerHTML += "<img src='" + errorImgUrl + "' id='" + errorIconId + "' title='" + errorText + "' alt='" + errorText + "' class='ErrorIcon'>";
    }
    else
    {
        document.getElementById(errorIconId).style.display = "inline";
        document.getElementById(errorIconId).alt = errorText;
        document.getElementById(errorIconId).title = errorText;
    }
}

function HideError(ctrl)
{
    errorIconId = ctrl.id.toString().replace(placeholderId, "") + "ErrorIcon"
    errorIcon = document.getElementById(errorIconId);   
    if(errorIcon != null) errorIcon.style.display = "none";    
}