var Validate = function(summaryID)
{
	this.d = new Date();
    this.thisYear = this.d.getFullYear();
    this.tooYoungYear = (this.thisYear-13);
    this.teenagerYear = (this.thisYear-17);    
    this.errors = new Array();
    this.resets = new Array();
	this.errorClass = "errorSpan";
	this.defaultClass = "defaultSpan";
	this.summaryID = summaryID;
    this.setError = function(reportID,message)
    {
		var errorSet = new Object();
		errorSet.reportId = reportID;
		errorSet.message = message;
		this.errors.push(errorSet);	
    };
    this.unsetError = function(reportID,message)
    {
		var resetSet = new Object();
		resetSet.reportId = reportID;
		this.resets.push(resetSet);	
    };
    this.yield = function(flag)
    {
        this.clearErrors();
		this.reportErrors();
		if(flag == true) this.showSummary();
    };
    this.resetFields = function(arg)
    {
        for(var i in arg)
        {
            $(arg[i]).className = this.defaultClass;
        }
    };
    this.reportErrors = function(id,status)
    {
		for(var i =0;i < this.errors.length; i++)
		{
			for(var n in this.errors[i])
			{
				//alert(this.errors[i].reportId);
				var thisNode = this.errors[i].reportId;
				$(thisNode).className = this.errorClass;
			}
		}
    };
	this.clearErrors = function()
	{
		for(var i =0;i < this.resets.length; i++)
		{
			for(var n in this.resets[i])
			{
				//alert(this.resets[i].reportId);
				$(this.resets[i].reportId).className = this.defaultClass;
			}
		}
	};
    this.isValid = function()
    {
		if (this.errors.length > 0)
		{
			return false;	
		}
		else
		{
			this.clearErrors();
			this.clearSummary();
			return true;
		}
    };
	this.showSummary = function()
	{
		var finalSummary = new String();
		for(var i =0;i < this.errors.length; i++)
		{
			finalSummary += this.errors[i].message + "<br />";
		}
		$(this.summaryID).innerHTML = finalSummary;
		return;
	};
	this.clearSummary = function()
	{
		if($(this.summaryID)) $(this.summaryID).innerHTML = "";
	};
	/*CUSTOM VALIDATION*/
	
	this.isTeenager = function(month,day,year)
    {
        return ( (month + day + this.teenagerYear) < (month + day + year) ) ? true:false;
    };
	this.CharCountdown = function(textField,maxValue,spanToUpdate) // parrots the onkeyup
	{      
		try
		{ 
			if($(textField).value.length>maxValue) 
			{	
				$(textField).value = $(textField).value.substr(0, maxValue);
				return false;
			}
			else
			{
				//alert(String(maxValue - $(textField).value.length));
				$(spanToUpdate).innerHTML = String(maxValue - $(textField).value.length);
				return true;
			}
		}
		catch(e)
		{
			alert(e);	
		}
	};
    this.isPreTeen = function(month,day,year)
    {
        return ( (month + day + this.tooYoungYear) < (month + day + year) ) ? true:false;
    };
	
	/*GENERIC VALIDATION*/
	
	this.isEmpty = function(nodeID,reportID,message)
    {
		return ($(nodeID).value == "") ? this.setError(reportID,message) : this.unsetError(reportID,message);
    };
    this.isMatch = function(arg,match,reportID,message)
    {
        if($(arg).value == "") 
		{
			this.setError(reportID,message);
			return;
		}
        if($(arg).value == $(match).value)
		{
			this.unsetError(reportID,message)
		}
		else
		{
			this.setError(reportID,message)
		}
		return;
    };
    this.isZipcode =function(arg,reportID,message)
    {
        //var re = /^\d{5}([\-]\d{4})?$/;
        var re = /^.+$/;    // Changed to allow for international postal codes which do not conform to 5-4 format.
        if (re.test($(arg).value))
		{
			this.unsetError(reportID,message);
		}
		else
		{
			this.setError(reportID,message);
		}
		return;
    };
	this.isReasonablePassword = function(arg,reportID,message)
	{
		//test of punctuation,etc
		var re = /[^A-Za-z0-9]/;
        if (re.test($(arg).value)) 
		{ 
			this.setError(reportID,message);
			return; 
		}
		//test for length
		if($(arg).value.length < 5)
		{
			this.setError(reportID,message);
			return; 
		}
		return;
	}
    this.isSocial = function(value)
    {
        var re = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/;
        if (!re.test(value)) 
		{ 
			this.setError(reportID,message)
			return; 
		}
        var temp = value;
        if (value.indexOf("-") != -1) { temp = (value.split("-")).join(""); }
        if (value.indexOf(" ") != -1) { temp = (value.split(" ")).join(""); }
        if (temp.substring(0, 3) == "000") { this.setError(reportID,message); return; }
        if (temp.substring(3, 5) == "00") { this.setError(reportID,message); return; }
        if (temp.substring(5, 9) == "0000") { this.setError(reportID,message); return; }
        this.unsetError(reportID,message)
		return;
    };
    this.isValidNumber = function(inpString,reportID,message) 
    {
        if(/^[-+]?\d+(\.\d+)?$/.test(inpString))
		{
			this.unsetError(reportID,message);
		}
		else
		{
			this.setError(reportID,message);
		}
    };
    this.isValidDate = function(m,d,y,format,reportID,message) {
       
		if(m==""||d==""||y=="")
		{
			this.setError(reportID,message);
			return;
		}
		if(m == "04" || m == "06" || m == "09" || m == "11")
		{
			if(parseInt(d)>30)
			{
				this.setError(reportID,message);
				return;
			}
		}
		if(m == "02")
		{
			if((parseInt(y)%4 == 0 && parseInt(d) > 29) || (parseInt(y)%4 != 0 && parseInt(d) > 28))
				{
					this.setError(reportID,message);
					return;
				}
		}
		this.unsetError(reportID,message);
		return;
    };
    this.clone = function(myObj)
    {
        //return new Validate;
	    if(typeof(myObj) != 'object') return myObj;
	    if(myObj == null) return myObj;

	    var myNewObj = new Object();

	    for(var i in myObj)
		    myNewObj[i] = this.clone(myObj[i]);

	    return myNewObj;
    };
	this.isEmail = function(arg,reportID,message)
    {
        var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        if(re.test($(arg).value))
		{
			this.unsetError(reportID,message);
		}
		else
		{
			this.setError(reportID,message);
		}
		return;
    };
    this.isChecked = function(nodeID,reportID,message)
    {
		return ($(nodeID).checked ? this.unsetError(reportID,message) : this.setError(reportID,message));
    };
};