/************************************************************************

    File    :   validation.js
    Contents:   This file contains general purpose validation routines for us on
                HTML forms.
************************************************************************/


/************************************************************************

    Function    :   Validate
    Author      :   Andy Faulkner
    Date        :   19/11/1998
    Purpose     :   This routine validates fields on a form according to the validation
                    rules defined in the objValidate parameter.

    Parameters  :   frmMainForm     The form in which the fields to be validated reside.
                    objValidate     The object containing validation rules to be applied
                                    to the fields in frmMainForm.
************************************************************************/

/* modified by Conrad Crampton to include the facility to pass the current field as the third
parameter to force the validation of that field. Called as an onBlur event to validate fields
on data entry. */

function Validate (frmMainForm, objValidate , objSingle) {

    var forFocus = "";
    var strMessage="";
    var IsValid = true;
    var FailureList = "The following field(s) failed the validation check: \n\n";



if (objSingle) {

obj=objSingle;
frmMainForm[obj].style.backgroundColor = "white";
    if (isBlank(frmMainForm[obj].value)){
        return false;}

if (objValidate[obj].type == 'text') {
            if (!TextValidate(frmMainForm[obj], objValidate[obj])) {
		IsValid = false;
		strMessage=objValidate[obj].valtext;
		window.status=strMessage;
                frmMainForm[obj].focus();		
							           }
				     }
else 
if (objValidate[obj].type == 'date') {
	    if (!DateValidate(frmMainForm[obj], objValidate[obj])) {
                IsValid = false;
		strMessage=objValidate[obj].valtext;
		window.status=strMessage;
		frmMainForm[obj].focus();
		frmMainForm[obj].style.backgroundColor = "red";
		window.status=strMessage;
							           }
				     }
else 
if (objValidate[obj].type == 'number') {
            if (!NumberValidate(frmMainForm[obj], objValidate[obj])) {
                IsValid = false;
		strMessage=objValidate[obj].valtext;
		window.status=strMessage;
		frmMainForm[obj].style.backgroundColor = "red";
		frmMainForm[obj].focus();
							             }
				       }
else if (objValidate[obj].type == 'time')
		{
			if (!TimeValidate(frmMainForm[obj], objValidate[obj]))
			{
				IsValid = false;
				strMessage=objValidate[obj].valtext;
				window.status=strMessage;
				frmMainForm[obj].style.backgroundColor = "red";
				frmMainForm[obj].focus();
			}
		}
else if (objValidate[obj].type == 'time_style')
		{
			if (!TimeStyleValidate(frmMainForm[obj], objValidate[obj]))
			{
				IsValid = false;
				strMessage=objValidate[obj].valtext;
				window.status=strMessage;
				frmMainForm[obj].style.backgroundColor = "red";
				frmMainForm[obj].focus();
			}
		}
else if (objValidate[obj].type == 'custody_number')
		{
			if (!CustodyValidate(frmMainForm[obj], objValidate[obj]))
			{
				IsValid = false;
				strMessage=objValidate[obj].valtext;
				window.status=strMessage;
				frmMainForm[obj].focus();
			}
		}

else if (objValidate[obj].type == 'crime_number')
		{
			if (!CrimeValidate(frmMainForm[obj], objValidate[obj]))
			{
				IsValid = false;
				strMessage=objValidate[obj].valtext;
				window.status=strMessage;
				frmMainForm[obj].focus();
			}
		}

else if (objValidate[obj].type == 'warrant_46')
		{
			if (!Warrant46Validate(frmMainForm[obj], objValidate[obj]))
			{
				IsValid = false;
				strMessage=objValidate[obj].valtext;
				window.status=strMessage;
				frmMainForm[obj].focus();
			}
		}


}


else {

    for (var obj in objValidate) {
        if (objValidate[obj].type == 'text') {
             if (!TextValidate(frmMainForm[obj], objValidate[obj])) {
         	 IsValid = false;
                 FailureList += "    " + objValidate[obj].valtext + "\n";
		 if (forFocus=="") forFocus=frmMainForm[obj];
            }
        }
        else if (objValidate[obj].type == 'date') {         
	     if (!DateValidate(frmMainForm[obj], objValidate[obj])) {
                 IsValid = false;
                 FailureList += "    " + objValidate[obj].valtext + "\n";
		 if (forFocus=="") forFocus=frmMainForm[obj];
            }
        }
        else if (objValidate[obj].type == 'number') {
             if (!NumberValidate(frmMainForm[obj], objValidate[obj])) {
                 IsValid = false;
                 FailureList += "    " + objValidate[obj].valtext + "\n";
		 if (forFocus=="") forFocus=frmMainForm[obj];
            }
	}

/* list object validates a list box to check that a value other than the first one is selected */
        else if (objValidate[obj].type == 'list') {
             if (!ListValidate(frmMainForm[obj], objValidate[obj])) {
                 IsValid = false;
                 FailureList += "    " + objValidate[obj].valtext + "\n";
		 if (forFocus=="") forFocus=frmMainForm[obj];
            	}
        }
		
		else if (objValidate[obj].type == 'time')
		{
			if (!TimeValidate(frmMainForm[obj], objValidate[obj]))
			{
				IsValid = false;
				FailureList += "    " + objValidate[obj].valtext + "\n";
				if (forFocus == "") forFocus = frmMainForm[obj];
			}
		}

		else if (objValidate[obj].type == 'time_style')
		{
			if (!TimeStyleValidate(frmMainForm[obj], objValidate[obj]))
			{
				IsValid = false;
				FailureList += "    " + objValidate[obj].valtext + "\n";
				if (forFocus == "") forFocus = frmMainForm[obj];
			}
		}
		else if (objValidate[obj].type == 'custody_number')
		{
			if (!CustodyValidate(frmMainForm[obj], objValidate[obj]))
			{
				IsValid = false;
				FailureList += "    " + objValidate[obj].valtext + "\n";
				if (forFocus == "") forFocus = frmMainForm[obj];
			}
		}

		else if (objValidate[obj].type == 'crime_number')
		{
			if (!CrimeValidate(frmMainForm[obj], objValidate[obj]))
			{
				IsValid = false;
				FailureList += "    " + objValidate[obj].valtext + "\n";
				if (forFocus == "") forFocus = frmMainForm[obj];
			}
		}

		else if (objValidate[obj].type == 'warrant_46')
		{
			if (!Warrant46Validate(frmMainForm[obj], objValidate[obj]))
			{
				IsValid = false;
				FailureList += "    " + objValidate[obj].valtext + "\n";
				if (forFocus == "") forFocus = frmMainForm[obj];
			}
		}

		
    }
}




    if (!IsValid){
	if (!objSingle) {
		alert(FailureList);
		forFocus.focus();
		forFocus="";
		return false;
			}
	else {
		window.status=strMessage;
		return false;}
		}
    else {
	if (!objSingle)
//		frmMainForm.submit();
		//alert('Validated OK - Form Submitted');
	return true;
	else

		window.status="";
		}



}

/***************************************************************************
	function : CrimeValidate
	author : conrad
	date : 19/10/99
	purpose : validates crime number fields 

	parameters : objCrime    The crime record field to be validated
		     objValidate the validation object to be applied to it.

***************************************************************************/
function CrimeValidate(objCrime, objValidate) 
{

	// regexp matches any letter [ABCDEFGHJL] followed by Z/ any one of 6 numbers followed by / two more
	var regexp = /^([ABCDEFGHJL]Z\/[0-9]{1,6}\/[90][0-9])$/gi;

	var result = objCrime.value.match(regexp);
	
	if (isBlank(objCrime.value))
        return(objValidate.blank);
		
	// if either of the regexp's match then return true from function otherwise return false.
	if (result != null)
		return(true);
	else 
		return(false);
	
	return (true);
}


/***************************************************************************
	function : CustodyValidate
	author : conrad
	date : 19/10/99
	purpose : validates custody number fields 

	parameters : objCustody    The custody record field to be validated
		     objValidate the validation object to be applied to it.

***************************************************************************/
function CustodyValidate(objCustody, objValidate) 
{

	// regexp matches ABCDEFGHJL then Z/ then must be same as first then JKLMN/one of 6 numbers then year
	var regexp = /^(([ABCDEFGHJL])Z\/\2[JKLMN]\/[0-9]{1,6}\/[90][0-9])$/gi;

	var result = objCustody.value.match(regexp);
	
	if (isBlank(objCustody.value))
        return(objValidate.blank);
		
	// if either of the regexp's match then return true from function otherwise return false.
	if (result != null)
		return(true);
	else 
		return(false);
	
	return (true);
}

/***************************************************************************
	function : Warrant46Validate
	author : JLCB
	date : 17/01/2003
	purpose : validates PNC Warrant 46/ code fields 

	parameters : objWarrant46    The record field to be validated
		     objValidate the validation object to be applied to it.

***************************************************************************/
function Warrant46Validate(objWarrant46, objValidate) 
{

	// regexp matches 2 digits plus "/" and two letters.
	var regexp = /^((\d\d)\/\w\w)$/gi;

	var result = objWarrant46.value.match(regexp);
	
	if (isBlank(objWarrant46.value))
        return(objValidate.blank);
		
	// if either of the regexp's match then return true from function otherwise return false.
	if (result != null)
		return(true);
	else 
		return(false);
	
	return (true);
}


/***************************************************************************
	function : TimeValidate
	author : conrad
	date : 19/10/99
	purpose : validates time fields 

	parameters : objTime    The time field to be validated
		     objValidate the validation object to be applied to it.

***************************************************************************/

function TimeValidate(objTime, objValidate) 
{

	// regexp matches 0 or 1 followed by 0 to 9 then : then 0 to 5 then 0 to 9
	// to cater for 24 hr clock up to 19:59
	var regexp = /^([01]{1}[0-9]{1}:[0-5]{1}[0-9]{1})$/g;

	// regexp1 matches 2 followed by 0 to 3 then : then 0 to 5 then 0 to 9
	// to cater for 24 hr clock 20:00 to 23:59
	var regexp1 = /^([2]{1}[0-3]{1}:[0-5]{1}[0-9]{1})$/g;

	var result = objTime.value.match(regexp);
	var result1 = objTime.value.match(regexp1);
	
	if (isBlank(objTime.value))
        return(objValidate.blank);
		
	// if either of the regexp's match then return true from function otherwise return false.
	if ((result != null) || (result1 != null))
		return(true);
	else 
		return(false);
	
	return (true);
}


function TimeStyleValidate(objTime, objValidate) 
{

	// regexp matches 0 or 1 followed by 0 to 9 then : then 0 to 5 then 0 to 9
	// to cater for 24 hr clock up to 19:59
	var regexp = /^(\-?[0-9]{2}:[0-5]{1}[0-9]{1})$/g;

	var result = objTime.value.match(regexp);
	
	if (isBlank(objTime.value))
        return(objValidate.blank);
		
	// if either of the regexp's match then return true from function otherwise return false.
	if ((result != null))
		return(true);
	else 
		return(false);
	
	return (true);
}


/************************************************************************
    Function    :   TextValidate
    Author      :   Andy Faulkner
    Date        :   19/11/1998
    Purpose     :   General purpose text validation routine.

    Parameters  :   objText     The text field to be validated.
************************************************************************/

function TextValidate(objText, objValidate) {
    if (isBlank(objText.value))
        return(objValidate.blank);

    if (typeof(objText.value) == 'string') {
        return true;
    }
    else
        return false;
}

/***************************************************************************
	function : ListValidate
	author : conrad
	date : 21/7/99
	purpose : validates option lists to check not top item in list

	parameters : objList    The option list to be validated
		     objValidate the validation object to be applied to it.

***************************************************************************/

function ListValidate(objList, objValidate) 
{
    if (objList.selectedIndex == 0)
        return(objValidate.blank);
    else 
	return (true);
}





/************************************************************************
    Function    :   NumberValidate
    Author      :   Andy Faulkner
    Date        :   19/11/1998
    Purpose     :   Validates number fields according to rules.
    
    Parameters  :   objDate     The number field to be validated.
                    objValidate The validation object to be applied to it.

************************************************************************/
function NumberValidate(objNumber, objValidate) {
    if (isBlank(objNumber.value))
        return(objValidate.blank);

    if (isNumberString(objNumber.value)) {
        return(objNumber.value >= objValidate.lower && objNumber.value <= objValidate.upper);
    }
    else {
        return(false);
    }
}



/************************************************************************
    Function    :   DateValidate
    Author      :   Andy Faulkner
    Date        :   19/11/1998
    Purpose     :   Validates date fields according to rules.
    
    Parameters  :   objDate     The date field to be validated.
                    objValidate The validation object to be applied to it.

************************************************************************/

function DateValidate(objDate, objValidate) {
    if (isBlank(objDate.value))
        return(objValidate.blank);

    if (typeof(objDate.value) == 'string') {
	
	var retCheckDate = CheckDate(objDate.value)
        var dt = Date.parse(retCheckDate);
        if (retCheckDate != 0) {
            return(dt >= objValidate.lower && dt <= objValidate.upper);
        }
    }

    return(false);
}


/************************************************************************
    Function    :   CheckDate
    Author      :   Andy Faulkner
    Date        :   19/11/1998
    Purpose     :   Checks that a date is valid and then reformats
                    a date string in DD/MM/YYYY form to one in
                    MM/DD/YYYY.
    
    Parameters  :   objDate     The date field to be validated.
                    objValidate The validation object to be applied to it.

************************************************************************/

function CheckDate(strDate) {

    var datebits = parser(strDate, '/');
    if (datebits.length != 3)
        return (0);

    var day = datebits[1];
    var mon = datebits[2];
    var year = datebits[3];

    if (year.length !=4) 
	return (0);

    if ((day.length > 2) || (day.length < 1) || (mon.length > 2) || (mon.length < 1))
	return (0);

    if (mon < 1 || mon > 12) 
        return(0);

    var maxday = 0;
    if (mon == 4 || mon == 6 || mon == 9 || mon == 11)
        maxday = 30;
    else if (mon == 2) {
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    maxday = 29;
                }
                else
                    maxday = 28;
            }
            else
                maxday = 29;
        }
        else
            maxday = 28;
    }
    else
        maxday = 31;

    if (day < 1 || day > maxday)
        return(0);
        
    var strUSDate = mon + "/" + day + "/" + year;
    return(strUSDate);
}

