var gkbAllowQuiet = true;

function VerifyPadNumber( xrDecimal, xnPlaces )
{
	var sValue = "" + Round( xrDecimal, xnPlaces );
	var asParts = sValue.split( "." );
	
	if( asParts.length < 2 )
		asParts[ 1 ] = "";

	while( asParts[ 1 ].length < xnPlaces )
		asParts[ 1 ] = asParts[ 1 ] + "0";

	sValue = asParts[ 0 ] + "." + asParts[ 1 ];
	return sValue;
}

function VerifyRound( xnValue, xnPrecision )
{
	var nValue = xnValue;
	var nFactor = Math.pow( 10, xnPrecision );
	var nSign = ( nValue < 0.0 )? -1.0: 1.0;
	var rRound = nSign * 0.5;
	nValue *= nFactor;
	nValue += rRound;
	nValue = ( nSign > 0 )? Math.floor( nValue ): Math.ceil( nValue );
	return ( nValue / nFactor );
}

function ScanForm( xoForm, xsField )
{
	var nLength = xoForm.elements.length;
	var nIndex;
	
	for( nIndex = 0; nIndex < nLength; nIndex++ )
	{
		if( xoForm.elements[ nIndex ].name == xsField )
			return nIndex;
	}

	return -1;
}

function ScanFormAll( xoForm, xsField )
{
	var nLength = xoForm.elements.length;
	var anResult = new Array();

	var nIndex;
	
	for( nIndex = 0; nIndex < nLength; nIndex++ )
	{
		if( xoForm.elements[ nIndex ].name == xsField )
			anResult[ anResult.length ] = nIndex;
	}

	return anResult;
}

function AlternateMandatory( xbOK, xsField, xsMessage )
{
	var sTarget = xsField + "Error";
	var oTarget = document.getElementById( sTarget );

	if( xbOK == false )
	{
		if( oTarget != null && gkbAllowQuiet == true )
			oTarget.innerHTML = "<font color=\"red\"><center>Mandatory</center></font>";
		else
			alert( xsMessage );
	}
	else if( oTarget != null && gkbAllowQuiet == true )
		oTarget.innerHTML = "&nbsp;";
	
}

function BasicValidation( xoForm, xsField )
{
	var nIndex = ScanForm( xoForm, xsField );

	if( nIndex < 0 )
	{
		alert( "The form field " + xsField + " does not exist!" );
		return -1;
	}

	var sValue = "" + xoForm.elements[ nIndex ].value;

	if( sValue == "undefined" )
	{
		alert( "Field value is given as \"undefined\"!" );
		return -1;
	}
	
	return nIndex;
}

function ValidGeneral( xoForm, xsField, xbMandatory, xnRestriction )
{
	var nIndex = BasicValidation( xoForm, xsField );

	if( nIndex < 0 )
		return false;

	var sValue = "" + xoForm.elements[ nIndex ].value;

	// If mandatory, length cannot be 0

	if( xbMandatory == true )
	{
		var bOK = ( sValue.length > 0 );
		AlternateMandatory( bOK, xsField, "This field must be filled in!" );
	}
	
	if( xbMandatory == false && sValue.length < 1 )	// Nothing to check
		return true;

	if( xnRestriction == 0 )
	{
		// Check for invalid characters
		
		if( CharacterSearch( sValue ) == true )
		{
			alert( "The field contains illegal characters. i.e " + sIllegal ); //\\!#$%^&*()+=~`/?':;}{][<>\"" );
			return false;
		}
	}

	if( xnRestriction == 1 )
	{
		// Check for invalid characters

		if( sValue.indexOf( ">" ) >= 0 || sValue.indexOf( "<" ) >= 0 )		
		{
			alert( "The field may contain HTML tags. You cannot use the '<' and '>' characters." );
			return false;
		}
	}

	// General tests for text field have been passed

	return true;
}

function ValidText( xoForm, xsField, xbMandatory )
{
	return ValidGeneral( xoForm, xsField, xbMandatory, 0 );
}

function ValidXHTML( xoForm, xsField, xbMandatory )
{
	return ValidGeneral( xoForm, xsField, xbMandatory, 1 );
}

function ValidFree( xoForm, xsField, xbMandatory )
{
	return ValidGeneral( xoForm, xsField, xbMandatory, 2 );
}

function ValidInt( xoForm, xsField, xbMandatory, xbNegative )
{
	var nIndex = BasicValidation( xoForm, xsField );
	
	if( nIndex < 0 )
		return false;

	var sValue = "" + xoForm.elements[ nIndex ].value;
	
	if( xbMandatory == true )
	{
		var bOK =  ( sValue.length > 0 );
		AlternateMandatory( bOK, xsField, "This field must be filled in!" );
	}

	var nInt = parseInt( sValue );
	
	if( isNaN( nInt ) && sValue != "" )
	{
		alert( "The value entered was not an integer!" );
		xoForm.elements[ nIndex ].value = "";
		return false;
	}

	if( xbNegative == false )
	{
		if( nInt < 0 )
		{
			alert( "The value entered cannot be less than 0!" );
			xoForm.elements[ nIndex ].value = "";
			return false;
		}
	}

	// General tests for integer field have been passed

	return true;
}

function ValidFloat( xoForm, xsField, xbMandatory, xbNegative, xnPrecision )
{
	var nIndex = BasicValidation( xoForm, xsField );
	
	if( nIndex < 0 )
		return false;

	var sValue = "" + xoForm.elements[ nIndex ].value;

	if( xbMandatory == true )
	{
		var bOK =  ( sValue.length > 0 );
		AlternateMandatory( bOK, xsField, "This field must be filled in!" );
	}

	var nFloat = parseFloat( sValue );
	
	if( isNaN( nFloat ) )
	{
		//alert( "The value entered was not a number!" );
		//xoForm.elements[ nIndex ].value = "0.0";
		return false;
	}
	
	if( xbNegative == false )
	{
		if( nFloat < 0.0 )
		{
			alert( "The value entered cannot be less than 0!" );
			//xoForm.elements[ nIndex ].value = "0.0";
			return false;
		}
	}

	var aSplit = sValue.split( "." );
	
	while( aSplit.length < 2 )
		aSplit[ aSplit.length ] = "";
	
	if( aSplit[ 1 ].length > xnPrecision )
	{
		if( xnPrecision >= 0 )
		{
			alert( "The number you entered had more than " + xnPrecision + " decimal places. The value is being rounded off." );
			nFloat = VerifyRound( nFloat, xnPrecision );
			sValue = "" + nFloat;
			aSplit = sValue.split( "." );
			xoForm.elements[ nIndex ].value = sValue;
		}			
	}

	while( aSplit[ 1 ].length < xnPrecision )
	{
		aSplit[ 1 ] += "0";
		sValue = aSplit[ 0 ] + "." + aSplit[ 1 ];
		xoForm.elements[ nIndex ].value = sValue;
	}

	// General tests for float field have been passed

	return true;
}

// function CheckRadio()
// Written by Sean Henders
// 2003-10-03
// Ensures that mandatory radio buttons are valid, and ensures the "mandatory" alert
// gets turned off if the radio is selected.
function CheckRadio( oForm, sField )
{ 
	ValidRadio( oForm, sField, 1, true );
	
	var fFieldToClear = document.getElementById( sField + "Error" );
		
	if( eval( oForm.name + "." + sField + "[ 0 ]" ).checked || eval( oForm.name + "." + sField + "[ 1 ]" ).checked  )
	{
		fFieldToClear.innerHTML = "&nbsp;";
	}
		
	return true;
}

function ValidRadio( xoForm, xsField, xnMinimum, xbExact )
{
	var nIndex = BasicValidation( xoForm, xsField );
	
	if( nIndex < 0 )
		return false;

	var anSet = ScanFormAll( xoForm, xsField );

	var nCount = 0;

	for( nIndex = 0; nIndex < anSet.length; nIndex++ )
	{
		var bChecked = xoForm.elements[ anSet[ nIndex ] ].checked;
		
		if( bChecked == true )
			nCount++;
	}

	var sPlural = "";
	
	if( xnMinimum > 1 )
		sPlural = "s";

	if( nCount < xnMinimum )
	{
		var sTarget = xsField + "Error";
		var oTarget = document.getElementById( sTarget );

		if( xbExact == true )
		{
			AlternateMandatory( false, xsField, "You must make exactly " + xnMinimum + " selection" + sPlural + "." );
			return false;
		}
		else if( oTarget != null)
			AlternateMandatory( true, xsField, "" );
		else
		{
			alert( "You must make at least " + xnMinimum + " selection" + sPlural + "." );
			return false;
		}
	}

	if( xbExact == true )
	{
		if( nCount > xnMinimum )
		{
			AlternateMandatory( true, xsField, "You must make exactly " + xnMinimum + " selection" + sPlural + "." );
			return false;
		}
	}

	return true;
}

function ValidEMail( xoForm, xsField, xbMandatory )
{
	var nIndex = BasicValidation( xoForm, xsField );
	
	if( nIndex < 0 )
		return false;

	var sValue = "" + xoForm.elements[ nIndex ].value;
	
	if( xbMandatory == true )
	{
		var bOK =  ( sValue.length > 0 );
		AlternateMandatory( bOK, xsField, "This field must be filled in!" );
	}
	
	// If mandatory, length cannot be 0

	if( xbMandatory == false && sValue.length < 1 )	// Nothing to check
		return true;

	if( CheckEMail( sValue ) == true )
	{
		alert( "E-Mail is not in correct format." );
		return false;
	}

	// General tests for email field have been passed
	
	return true;
}

function ValidPhone( xoForm, xsField, xbMandatory, xbNAmerica, sDescription )
{
	var nIndex = BasicValidation( xoForm, xsField );
	
	if( nIndex < 0 )
		return false;

	var sValue = "" + xoForm.elements[ nIndex ].value;
	var nExtension;
	var sExtension = "";
	var sField = xsField.substring( 1, xsField.length );
	
	sValue = sValue.toLowerCase();

	nExtension = sValue.search( "ext" );
	
	if( nExtension <= 0 )
	{
		nExtension = sValue.search( "x" );
		
		if( nExtension > 0 )
		{
			sExtension = sValue.substring( nExtension, sValue.length );
			sValue = sValue.substring( 0, nExtension );
			if( isNaN( sExtension.substring( 1, sExtension.length ) ) == true )
			{
				alert( sDescription + " number entered is not valid." );
				return false;
			}
			sExtension = "x " + StripAllOccurrences( sExtension.substring( 1, sExtension.length ), " " );
		}
	}
	else
	{
		sExtension = sValue.substring( nExtension, sValue.length );
		sValue = sValue.substring( 0, nExtension );
		
		if( isNaN( sExtension.substring( 3, sExtension.length ) ) == true )
		{
			alert( sDescription + " number entered is not valid." );
			return false;
		}
		sExtension = "EXT " + StripAllOccurrences( sExtension.substring( 3, sExtension.length ), " " );
	}
	
	if( xbMandatory == true )
	{
		var bOK =  ( sValue.length > 0 );
		AlternateMandatory( bOK, xsField, "This field must be filled in!" );
	}

	// If mandatory, length cannot be 0

	if( xbMandatory == false && sValue.length < 1 )	// Nothing to check
		return true;

	if( xbNAmerica == true )
	{
		sValue = ICNormalize_NAPhoneNumber( sValue );
		
		if( ICValidate_NAPhoneNumber( sValue ) == false )
		{
			alert( sDescription + " number entered is not valid." );
			return false;
		}
       
		xoForm.elements[ nIndex ].value = sValue + " " + sExtension;
	}
	else
	{
		if( CharacterSearch( sValue ) == true )
		{
			alert( "The field contains illegal characters. i.e \\\"<>" );
			return false;
		}
	}

	// General tests for phone field have been passed

	return true;
}

function ValidFax( xoForm, xsField, xbMandatory, xbNAmerica )
{
	var nIndex = BasicValidation( xoForm, xsField );
	
	if( nIndex < 0 )
		return false;

	var sValue = "" + xoForm.elements[ nIndex ].value;
	
	if( xbMandatory == true )
	{
		var bOK =  ( sValue.length > 0 );
		AlternateMandatory( bOK, xsField, "This field must be filled in!" );
	}

	// If mandatory, length cannot be 0

	if( xbMandatory == false && sValue.length < 1 )	// Nothing to check
		return true;

	if( xbNAmerica == true )
	{
		sValue = ICNormalize_NAPhoneNumber( sValue );
		/*
		if( ICValidate_NAPhoneNumber( sValue ) == false )
		{
			alert( "Fax number entered is not valid." );
			return false;
		}
        */
		xoForm.elements[ nIndex ].value = sValue;
	}
	else
	{
		if( CharacterSearch( sValue ) == true )
		{
			alert( "The field contains illegal characters. i.e \"!#$%^&*()+=~`/?':;}{][<>\"" );
			return false;
		}
	}

	// General tests for phone field have been passed

	return true;
}

function ValidPostal( xoForm, xsField, xbMandatory, xbNAmerica )
{ 
	var nIndex = BasicValidation( xoForm, xsField );
	
	if( nIndex < 0 )
		return false;

	var sValue = "" + xoForm.elements[ nIndex ].value;
	
	if( xbMandatory == true )
	{
		var bOK =  ( sValue.length > 0 );
		AlternateMandatory( bOK, xsField, "This field must be filled in!" );
	}

	// If mandatory, length cannot be 0

	if( xbMandatory == false && sValue.length < 1 )	// Nothing to check
		return true;

	if( xbNAmerica == true )
	{
		sValue = ICNormalize_CanadianPostalCode( sValue );
		
		if( ICValidate_CanadianPostalCode( sValue ) == false )
		{
			alert( "Postal Code entered is not valid." );
			return false;
		}

		xoForm.elements[ nIndex ].value = sValue;
	}
	else
	{
		if( CharacterSearch( sValue ) == true )
		{
			alert( "The field contains illegal characters. i.e \"!#$%^&*()+=~`/?':;}{][<>\"" );
			return false;
		}
	}

	// General tests for phone field have been passed

	return true;
}

function capitAll( input )
{	
	var str= input; //.toLowerCase();
	var Rx= /(^|\s)([a-z])/g;
	str= str.replace(Rx,function(m,p1,p2)
	{
		return p1+p2.toUpperCase();
	});
	return str;
}
    
function MaxTextArea( fieldName, maxLength, labelName )
{    
    $( fieldName ).keypress(function(e)    
    {   
        var keycode = e.keyCode ? e.keyCode : e.which;
        
        if( keycode == 8 || keycode == 46 )
            return;
            
        if ($(this).val().length >= maxLength) 
        {       
            e.preventDefault();
        }    
    });
    
    $( fieldName ).keyup( function()
    {
        if( labelName.length > 0 )
        {
            var total = maxLength - parseInt( $( this ).val().length );
            $( labelName ).html( "<b>" + total + "</b>" );
        }
    });
    
    if( fieldName.length > 0 )
    {
        var total = maxLength - parseInt( $( fieldName ).val().length );
        
        $( labelName ).html( "<b>" + total + "</b>" );
    }
      
//    $( fieldName ).keyup( function( e )
//    {  
//        var total = maxLength - parseInt( $( this ).val().length );
//        if ($(this).val().length >= maxLength)
//        {
//            $(this).text($(this).text().substring(0, maxLength));

//        }
//        $( labelName ).html( "<b>" + total + "</b>" );
//    });
}


