

// Postcode Validation Function
// Requires postcode textboxes to be named:
//   txtPostCode1 & txtPostCode2 
function ValidatePostcode(source, args) {
	args.IsValid = true;
	
	try
	{
		var postcode1 = document.getElementById(m_txtPostCode1).value;
	    var postcode2 = document.getElementById(m_txtPostCode2).value;
    	
	    var postcode = postcode1.toUpperCase() + " " + postcode2.toUpperCase();
    	
	    if( postcode1.length > 1 && postcode2.length > 1 )
	    {
		    if(!postcode.match(/^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$/))
		    {
			    args.IsValid = false;
		    }	
	    }
	}
	catch(e)
	{
	    alert(e.description);
	    alert("There is a problem with the clientside validation");
	    args.IsValid = false;
	}
}

// Name:   IsNumeric
// Author: leeg@eqtr.com - [LG]
// Desc:   Checks if input is numeric
function IsNumeric(source, args)
{
    //var objRegExp  = /^[0-9]+/;
    
    var objRegExp  = /^[0-9]*$/;

    args.IsValid = objRegExp.test(args.Value);
}

// Name:   IsNumericWithSpaces
// Author: leeg@eqtr.com - [LG]
// Desc:   Checks if input is numeric
function IsNumericWithSpaces(source, args)
{
    //var objRegExp  = /^[0-9]+/;
    
    var objRegExp  = /^[0-9 ]*$/;

    args.IsValid = objRegExp.test(args.Value);
}


// Name:   ValidateFullPostcode
// Author: leeg@eqtr.com - [LG]
// Desc:   Validates a full postcode
function ValidateFullPostcode(source, args)
{
    args.IsValid = true;

    sPostcode = args.Value.toUpperCase();
	if(sPostcode.length > 1)
    {
	    if(!sPostcode.match(/^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$/))
	    {
		    args.IsValid = false;
	    }	
    }

}


// Name:   ValidateFullPostcode
// Author: leeg@eqtr.com - [LG]
// Desc:   Validates a full postcode
function ValidateSortcode(source, args)
{
    var txtSortCode1 = document.getElementById( m_txtSortCode1 ).value;
    var txtSortCode2 = document.getElementById( m_txtSortCode2 ).value;
    var txtSortCode3 = document.getElementById( m_txtSortCode3 ).value;
    
    var sPostcode = txtSortCode1 + txtSortCode2 + txtSortCode3;

    var objRegExp  = /^[0-9]{6}/;

    args.IsValid = objRegExp.test(sPostcode);
}


// Name:   ValidatePhoneNumbers
// Author: leeg@eqtr.com - [LG]
// Desc:   Checks if a daytime or an evening number has been entered
function ValidatePhoneNumbers(source, args)
{
    args.IsValid = false;
        
        
	try
	{
		var daytimePhone = document.getElementById(m_txtDaytimePhone).value;
	    var eveningPhone = document.getElementById(m_txtEveningPhone).value;
    	    	
	    if(daytimePhone.length > 0 || eveningPhone.length > 0)
	    {
            args.IsValid = true;
	    }
	}
	catch(e)
	{
	    alert(e.description);
	    alert("There is a problem with the clientside validation");
	    args.IsValid = true;
	}

}


// Name:   ValidateHouseNameNumber
// Author: leeg@eqtr.com - [LG]
// Desc:   Checks if a house name OR number has been entered  
function ValidateHouseNameNumber(source, args)
{
    args.IsValid = false;

	try
	{
		var houseName = document.getElementById(m_txtHouseName).value;
	    var houseNumber = document.getElementById(m_txtHouseNumber).value;
    	    	
	    if(houseName.length > 0 || houseNumber.length > 0)
	    {
            args.IsValid = true;
	    }
	}
	catch(e)
	{
	    alert(e.description);
	    alert("There is a problem with the clientside validation");
	    args.IsValid = true;
	}

}



function ValidateAuthorisation(source, args)
{
    args.IsValid = document.getElementById(m_chkAuthorised).checked;    
}

function ValidateTandC(source, args)
{
    args.IsValid = document.getElementById(m_chkTermsAndConditions).checked;    
}

function ValidateConfirmation(source, args)
{
    args.IsValid = document.getElementById(m_chkConfirmation).checked;    
}


// Name:   ValidateSNumber
// Author: leeg@eqtr.com - [LG]
// Desc:   Checks for the existance of at least the first S Number (On request a quote page)
function ValidateSNumber(source, args)
{
    var iSuccessCount = 0;
    for( var iCounter = 1; iCounter <= 7; iCounter++ )
    {
        var value = document.getElementById( eval("m_txtSNumber1_" + iCounter) ).value;

        if( value == "" || isNaN(value) || value < 0 )
        {
            break;
        }
        iSuccessCount++
    }
        
    args.IsValid = (iSuccessCount == 7);
}

// Name:   ValidateDate
// Author: leeg@eqtr.com - [LG]
// Desc:   
function ValidateDate(source, args)
{

    var bValid = false;

    
    var txtEndDate1 = document.getElementById( m_txtEndDate1 );
    var txtEndDate2 = document.getElementById( m_txtEndDate2 );
    var txtEndDate3 = document.getElementById( m_txtEndDate3 );
    

    if( txtEndDate1 != undefined && txtEndDate2 != undefined  && txtEndDate3 != undefined )
    {
        if( txtEndDate1.value.length > 0 || txtEndDate2.value.length > 0 || txtEndDate3.value.length > 0 )
        {
            var sDate = txtEndDate1.value + "/" + txtEndDate2.value + "/" + txtEndDate3.value;
            var reDate = /^(?:(?:0?[1-9]|1\d|2[0-8])(\/|-)(?:0?[1-9]|1[0-2]))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:31(\/|-)(?:0?[13578]|1[02]))|(?:(?:29|30)(\/|-)(?:0?[1,3-9]|1[0-2])))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(29(\/|-)0?2)(\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?(?:0[48]|[2468][048]|[13579][26]))$/;
            bValid = reDate.test( sDate );
        }
        else
        {
            bValid = true;
        }
    

    }
    else
    {
        alert( "Textboxes undefined" );
    }



    
    args.IsValid = bValid;
}


// Name:   ValConfirmExistingAccount
// Author: leeg@eqtr.com - [LG]
// Desc:   Validates whether a user has indicated if they are an existing customer or not
//         Used on Direct Debit Signup Page
function ValConfirmExistingAccount(source, args) {
	args.IsValid = false;
	
	var dd1 = document.getElementById(m_rdoExistingCustomer + "_0");
	var dd2 = document.getElementById(m_rdoExistingCustomer + "_1");

    if( dd1.checked || dd2.checked )
    {
        args.IsValid = true;
    }

	
	return args;
}




