var Validate = function(summaryID)
{
    this.errors = new Array();
    this.resets = new Array();
	this.errorClass = "errorSpan";
	this.defaultClass = "defaultSpan";
	this.summaryID = summaryID;
	this.setStyles = function(defaultClass,errorClass)
	{
		this.defaultClass = defaultClass;
		this.errorClass = errorClass;
	}
    this.setError = function(reportID,message)
    {
        //alert("setting "+reportID);
        var errorSet = new Object();
		errorSet.reportId = reportID;
		errorSet.message = message;
		this.errors.push(errorSet);
		return false;
    };
    this.unsetError = function(reportID,message)
    {
		//alert("unsetting "+reportID);
    	var resetSet = new Object();
		resetSet.reportId = reportID;
		this.resets.push(resetSet);
		// save this id in an array to serialize later on
		return true;
    };
    this.yield = function(flag,success)
    {
        this.clearErrors();
		this.reportErrors();
		if(flag == true)
		{
		    success ? this.showSummaryNonError() : this.showSummary();
		}
    };
    this.peetsYield = function(flag,success)
    {
        this.clearErrors();
		this.reportErrors();
		if(flag == true)
		{
		    success ? this.showPeetsSummaryNonError() : this.showPeetsSummary();
		}
    };
    this.peetsYieldSuccess = function(section)
    {
        this.clearErrors();
	    this.showSummaryPeetsNonError(section);
    };
    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])
			{
				if($(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()
	{
		Utility.turnOn(this.summaryID);
		var finalSummary = new String();
		finalSummary = "<div class=\"peetsErrorPanel\"><h3 class=\"errorHeader\"><img src=\"/images/error/notification.gif\" style=\"vertical-align:middle;\" /> The following errors have occured</h3><div class=\"errorBody\">";
	    for(var i =0;i < this.errors.length; i++)
		{
			finalSummary += (i+1)+". "+this.errors[i].message + "<br />";
		}
		finalSummary += "</div></div>";
		$(this.summaryID).innerHTML = finalSummary;
		return;
	};
	this.showPeetsSummary = function()
	{
		var finalSummary = new String();
		finalSummary = "<div class='PeetsErrorPanel' style='color:#ebe1ac;line-height:15px;font-size:11px;font-style:normal;padding-bottom:10px;'>";
		finalSummary = "<b>Please correct the following:</b><br />";
	    for(var i = 0; i < this.errors.length; i++)
		{
			finalSummary += "&middot; "+this.errors[i].message + "<br />";
		}
		finalSummary += "</div>";
		finalSummary += "<img src='/images/misc/px.gif' widtb='1' height='10' /><br />";
		$(this.summaryID).innerHTML = finalSummary;
		Utility.fadeIn(this.summaryID);
		return;
	};	
	this.showSummaryNonError = function()
	{
		var finalSummary = new String();
		finalSummary = "<div class=\"peetsErrorPanel\"><h3 class=\"errorHeaderNoError\"><img src=\"/images/error/ok.gif\" style=\"vertical-align:middle;\" /> Success!</h3><div class=\"errorBodyNoError\">";
	    for(var i =0;i < this.errors.length; i++)
		{
			finalSummary += this.errors[i].message + "<br />";
		}
		finalSummary += "</div></div>";
		$(this.summaryID).innerHTML = finalSummary;
		Utility.turnOn(this.summaryID);
		return;
	};
	this.showSummaryPeetsNonError = function(section,message)
	{
		//Utility.turnOn(this.summaryID);
		var finalSummary = "<b>";
		switch(section)
		{
			case "SendPageToFriend":
				finalSummary += "Email has been sent. You may send another or <a href='javascript:Modal.hide();'>close this form</a>.";
			break;
			case "TellAFriend":
				finalSummary += "Your email has been sent. You may send more or <a href='/learn_share/'>Learn &amp; Share</a>.";
			break;
			case "SubmitReview":
				finalSummary += "Thank you for your review! <a href='javascript:ReviewWindow.hide();'>close this window</a>.";
			break;
			case "CouponSuccess":
				finalSummary += "Your information has been saved.<br />You will be redirected to your Coupon in a few moments.";
			break;
			case "LoveOfPeets":
				finalSummary += "Thank you for submitting your story.  <a href='javascript:Modal.hide();'>Close this form</a> to return to Learn &amp; Share.";
			break;
			case "SendFriendCampaign":
				finalSummary += "Your Email has been sent. Feel free to send more!";
			break;
			case "ExpertsQuestions":
				finalSummary += "Thank you for submitting a question. <br />We will review the question and either answer it directly <br />via email or post the answer on peetscoffee.com.<br />You may submit another question or <a href='javascript:Modal.hide();'>close this window</a>.";
			break;
			case "ExpertsComments":
				finalSummary += "Thank you for submitting a comment. <a href='javascript:Modal.hide();'>Close this form</a> to return to Learn &amp; Share.";
			break;
			case "EmailSignup":
				finalSummary += "Thank you for signing up for Peet&rsquo;s Coffee email newsletter! <br /><a href='javascript:Modal.hide();'>Close this form</a> to go back to peetscoffee.com."
			break;
		}
		finalSummary += "</b><br /><img src='/images/misc/px.gif' widtb='1' height='10' /><br />";
		$(this.summaryID).innerHTML = finalSummary;
		Utility.fadeIn(this.summaryID);
		return;
	};	
	this.clearSummary = function()
	{
		Utility.turnOff(this.summaryID);
	    if($(this.summaryID)) $(this.summaryID).innerHTML = "";
	};
	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);
		}
	};

	/*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})?$/;
        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 length = 5;
		var re = /[^A-Za-z0-9]/;
        if (re.test($(arg).value))
		{
			this.setError(reportID,message);
			return;
		}
		//test for length
		if($(arg).value.length < length)
		{
			this.setError(reportID,message);
			return;
		}
		return;
	};
	this.generalError = function(reportID,message)
	{
	    for(var i=0;i<message.length;i++)
	    {
	        this.setError(reportID,message[i]);
	    }
	};
	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.isNumericRange = function(arg1,arg2,reportID,message)
    {
        if($(arg1).value>$(arg2).value)
        {
            this.setError(reportID,message);
        }
        else
        {
            this.unsetError(reportID,message);
        }
    };
    this.isValidDate = function(m,d,y,format,reportID,message)
    {
        m = $(m).value;
        d = $(d).value;
        y = $(y).value;
		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(d)>28)
			{
				if(parseInt(y)%4 == 0 && parseInt(d) < 30)
				{
					this.setError(reportID,message);
					return;
				}
			}
		}
		this.unsetError(reportID,message);
		return;
    };
    this.isValidDateRange = function(date1,date2,reportID,message)
    {
        if(!date1 || !date2)
        {
            return;
        }
        if(date1>date2)
        {
            this.setError(reportID,message);
        }
        else
        {
            this.unsetError(reportID,message);
        }
    };
    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.reset = function()
    {
         this.clearErrors();
         this.clearSummary();
    };
};