﻿//------------------------------------------------------------------------------------
// Deveoper:    Wayne Clarke
// Date:        8 June 2007
// Purpose:     Validates the value of a date field textbox
//------------------------------------------------------------------------------------
// Amendments:  Dev     Date        Summary
//------------------------------------------------------------------------------------
//              TUM     11-Jul-08   Added boolean arguament so that validation
//                                  can occur when onchange is fired and pre-postback
//------------------------------------------------------------------------------------
function ValidateDate(ctlInput,blnOnChange) 
{
    var blnValidFlag = new Boolean(false);
	var strValue = new String();
    var rexDateFormat = new RegExp(/^\d{1,2}\/\d{1,2}\/\d{2,4}$/);

    // Prepare value
	strValue = ctlInput.value;
    strValue = strValue.replace(/\./g, '/');
	
	if (strValue == '')
	{
	    if (blnOnChange)
	    {
	        blnValidFlag = true;
	    }
	    else
	    {
	        blnValidFlag = false;
	    }
	}
	else
	{
		if (rexDateFormat.test(strValue))
		{
		    if (blnOnChange)
		    {
                var intDay = new Number(strValue.split("/")[0]);
                var intMonth = new Number(strValue.split("/")[1]);
                var intYear = new Number(strValue.split("/")[2]);
                if (intYear <= 49) intYear += 2000;
                if ((intYear >= 50) && (intYear <= 99)) intYear += 1900;

                var dtDate = new Date(intYear, intMonth-1, intDay);
                if ((dtDate.getMonth()+1 == intMonth) && (dtDate.getDate() == intDay) && (dtDate.getFullYear() == intYear))
                {
        	        blnValidFlag = true;
        	        strValue = PadNumber(intDay, 2, '0') + '/' + PadNumber(intMonth, 2, '0') + '/' + intYear.toString();
                }
            }
            else
            {
                blnValidFlag = true;
            }
		}
	}
	if (blnValidFlag == false)
	{
	    if (blnOnChange)
	    {
            window.alert('The value specified is not appropriate.');
            ctlInput.focus();
            return false;
        }
        else
        {
            return false;
        }
    }
    else
    {
        ctlInput.value = strValue;
        return true;
    }
}

//------------------------------------------------------------------------------
// Deveoper:    Wayne Clarke
// Date:        8 June 2007
// Purpose:     Pads a number with specified number of specified leading characters
//-------------------------------------------------------------------------------
function PadNumber(intNumber, intLength, strChar)
{
    var strNumber = new String()
    strNumber = '' + intNumber;
    while (strNumber.length < intLength)
        strNumber = strChar + strNumber;
    return strNumber;
}

//-----------------------------------------------------------------------------------
// Deveoper:    Tom Miller
// Date:        14 April 2008
// Purpose:     Validates that the value of a control only contains numeric values.
//------------------------------------------------------------------------------------
// Amendments:  Dev     Date        Summary
//------------------------------------------------------------------------------------
//              TUM     14-Jul-08   Added boolean arguament so that validation
//                                  can occur when onchange is fired and pre-postback
//------------------------------------------------------------------------------------
function ValidateTelephone(ctlInput,blnOnChange)
{
    var strValidChars = '0123456789 ';
    var blnIsNumber = true;
    var strNumber;
    
    if (ctlInput.value != '')
    {
        for (i = 0; i < ctlInput.value.length && blnIsNumber == true; i++)
        {
            strNumber = ctlInput.value.charAt(i);
            if (strValidChars.indexOf(strNumber) == -1)
            {
                blnIsNumber = false;
            }
        }
        
        if (blnIsNumber == false)
        {
            if (blnOnChange)
            {
                window.alert('Please enter a valid telephone number.');
                ctlInput.focus();
                return false;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return true;
        }
    }
    else
    {
        if (blnOnChange)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

//------------------------------------------------------------------------------
// Deveoper:    Tom Miller
// Date:        14 April 2008
// Purpose:     Validates that the value is a suitable email address.
//-------------------------------------------------------------------------------
function ValidateEmail(ctlInput)
{
    var strEmailAddress = ctlInput.value;
    var intFirstChar = strEmailAddress.indexOf('@');
    var intSecondChar = strEmailAddress.lastIndexOf('.');
    var blnValid = true;
    
    if (ctlInput.value != '')
    {
        if (intFirstChar == -1 || intSecondChar == -1)
        {
            blnValid = false;
        }
        
        if (intSecondChar < intFirstChar)
        {
            blnValid = false;
        }
        
        if (intSecondChar - intFirstChar == 1)
        {
            blnValid = false;
        }
        
        if (blnValid == false)
        {
            window.alert('The email address you have entered is not valid.');
            ctlInput.focus();
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return true;
    }
}

function showPointer(element)
{
    element.style.cursor = 'pointer';
}

function showDefault(element)
{
    element.style.cursor = 'default';
}
