var inpsToValidate;
var vAlertMsg;
var vMsgSpanId;
var vInputOkClass;
var vInputBadClass;

// ** InputValidate class **
function InpV(name, rule, msg, shortMsg, required)
{
    var _this = this;

    this._id=name;
    this._rule=rule;
    this._msg=msg;
	this._shortMsg=shortMsg;
    this._required=required;
    this._url="/Skins/RichardsProjects/Scripts/validationAjax.aspx"
    this._ajax = new Ajax(this._url, function(r){_this.handleValidateResponse(r);});
    this._errMsqSpan = name + "ErrMsg";
}
InpV.prototype._id = null;
InpV.prototype._inp = null;
InpV.prototype._rule = null;
InpV.prototype._msg = null;
InpV.prototype._shortMsg = null
InpV.prototype._shortMsg = null
InpV.prototype._required = null;
InpV._url = null;
InpV.prototype._ajax = null;
InpV.prototype._errMsqSpan = null;
InpV.prototype._ok = true;

// ** InputCompare class
function InpC(name1, name2, operator, msg)
{
    var _this = this;
    
    this._id=name1;
    this._id2=name2;
    this._operator=operator;
    this._msg=msg;
    this._url = "/_scripts/valCompareAjax.aspx"
    this._ajax = new Ajax(this._url, function(r){_this.handleValidateResponse(r);});
    this._errMsqSpan = name1 + "_" + name2 + "ErrMsg";
}
InpC.prototype._id = null;
InpC.prototype._id2 = null;
InpC.prototype._inp = null;
InpC.prototype._inp2 = null;
InpC.prototype._operator = null;
InpC.prototype._msg = null;
InpC._url = null;
InpC.prototype._ajax = null;
InpC.prototype._errMsqSpan = null;
InpC.prototype._ok = true;

// ** InputCompare class
function InpL(name, min, max, goodMsg, badMsg)
{ 
    this._id=name;
    this._min = min;
    this._max = max;
    this._goodMsg=goodMsg;
    this._badMsg = badMsg;
    this._errMsqSpan = name + "LengthMsg";
}
InpL.prototype._id = null;
InpL.prototype._goodMsg = null;
InpL.prototype._badMsg = null;
InpL.prototype._min = null;
InpL.prototype._max = null;
InpL.prototype._errMsqSpan = null;
InpL.prototype._ok = true;

function printMsg(theMsg)
{
	var msgSpan = document.getElementById(this._errMsqSpan);
	msgSpan.innerHTML = theMsg;
}
InpV.prototype.printMsg = printMsg;
InpC.prototype.printMsg = printMsg;
InpL.prototype.printMsg = printMsg;

function attachValidateHandlers()
{
    //alert("adding blur handlers");
	if (document.getElementsByTagName)//make sure we're on newer browser
	{
		for (x in inpsToValidate)
		{
			var oInput = document.getElementById(inpsToValidate[x]._id);
			// Attach onblur event to each input
			if(oInput != null)
			{
			    // Validate
			    if(inpsToValidate[x]._rule != null)
			    {
			        // Join input to it's controler
			        oInput.inpV = inpsToValidate[x];
			        inpsToValidate[x]._inp = oInput;
			        // Add event handler
			        oInput.onblur = function(){eval("validateMe(this);")};
			    }
			    // Compare
			    else if(inpsToValidate[x]._operator != null)
			    {
			        // Get compare input
			        var oInput2 = document.getElementById(inpsToValidate[x]._id2);
			        
			        // Join input to it's controler
			        oInput.inpC = inpsToValidate[x];
			        oInput2.inpC = inpsToValidate[x];
			        inpsToValidate[x]._inp = oInput;
			        inpsToValidate[x]._inp2 = oInput2;
			        
			        // Attach onblur event to 1st input
			        oInput.onblur = function(){eval("validateMe(this);")};
			        
			        // Attach onblur event to 2nd input
			        if(oInput2 != null)
			        {
			            oInput2.onblur = function(){eval("validateMe(this);")};
			        }
			    }
			    // Length
			    else if(inpsToValidate[x]._max != null)
			    {
			        // Attach on press event
			        eval("oInput.onkeyup = function(){return inpsToValidate[" + x + "].chkLength(this.value);}");
			        inpsToValidate[x].showMsg(inpsToValidate[x]._goodMsg, 0);
			    }
			}	
		}
		
		// Attach validate() to the form
		document.forms[0].onsubmit = function(){return validate();}
	}
}

// Handles ajax response
function handleValidateResponse(vResponse)
{
	    if(vResponse == 0)
	    {
		    // Display error message
			if(this._shortMsg != '' && this._shortMsg != null)
			{
				this.printMsg(this._shortMsg);
			}
			else
			{
				this.printMsg(this._msg);
			}
			
			// Store input's failure
			this._ok = false;
			
			//change input's display class
			if(vInputBadClass != null)
			{
				this._inp.className = vInputBadClass;
				if(this._inp2 != null)this._inp2.className = vInputBadClass;
			}
	    }
	    else
	    {
			// Delete the current message
			this.printMsg("&nbsp;");

			// Store input's success
		    this._ok = true;
		    
		    //change input's display class
		    if(vInputBadClass != null)this._inp.className = vInputOkClass;
	    }
}
InpV.prototype.handleValidateResponse = handleValidateResponse;
InpC.prototype.handleValidateResponse = handleValidateResponse;

function validateMe(inp)
{
    // Has a validate control
    if(inp.inpV != null)
    {
        inp.inpV._ok = true; // in case of network probs assume correct first.
        
        //sends the rule and value to aspx page to be validated
	    inp.inpV._ajax.sendAjaxQuery("val=" + inp.value + "&rule=" + inp.inpV._rule + "&req=" + inp.inpV._required)
    }
    // Has a compare control
    if(inp.inpC != null)
    {
        inp.inpC._ok = true; // in case of network probs assume correct first.
        
        val1 = inp.inpC._inp.value;
        val2 = inp.inpC._inp2.value;
	    //sends the values and operator to aspx page to be validated (only if neither values are blank)
	    if(val1 != "" && val2 != "")
	    {
	        inp.inpC._ajax.sendAjaxQuery("val1=" + val1 + "&op=" + inp.inpC._operator + "&val2=" + val2)
	    }
	    // if both are blank removes compare error
	    else if(val1 == "" && val2 == "")
	    {
	        document.getElementById(inp.inpC._errMsqSpan).innerHTML = "&nbsp;";
	    }
    }
}

InpL.prototype.chkLength = function(inpValue)
{
    var valid = (inpValue.length >= this._min && inpValue.length <= this._max);
    
    if(valid)
    {
        this.showMsg(this._goodMsg, inpValue.length);
        document.getElementById(this._errMsqSpan).className = "ok";
        this._ok = true;
    }
    else
    {
        this.showMsg(this._badMsg, inpValue.length);
        document.getElementById(this._errMsqSpan).className = "error";
        this._ok = false;
    }
}

InpL.prototype.showMsg = function(msg, inpLength)
{
    msg = msg.replace("<max>", this._max);
    msg = msg.replace("<typed>", inpLength);
    msg = msg.replace("<over>", inpLength - this._max);
    msg = msg.replace("<left>", this._max - inpLength);
    document.getElementById(this._errMsqSpan).innerHTML = msg;
}

// onsubmit event function
function validate()
{
	var anyErrors = false;
	
	for (x in inpsToValidate)
	{
		if(!inpsToValidate[x]._ok)
		{
			anyErrors = true;

			// Write long error message (except for length validators)
			if(inpsToValidate[x]._msg != null)inpsToValidate[x].printMsg(inpsToValidate[x]._msg);
		}
		// if required field blank
		else if(inpsToValidate[x]._required == true && inpsToValidate[x]._inp.value == '')
		{
			anyErrors = true;

			// Write long error message
			inpsToValidate[x].printMsg(inpsToValidate[x]._msg);
			
			//change input's display class
			if(vInputBadClass != null)inpsToValidate[x]._inp.className = vInputBadClass;
			
			// Store failure
			inpsToValidate[x]._ok = false;
		}
	}
	
	if (anyErrors)
	{
		//if there are any errors give a message
		alert (vAlertMsg);
		if(vMsgSpanId!=null)
		{
		    document.getElementById(vMsgSpanId).innerHTML = vAlertMsg;
		}
		return false;
	}
	else return true;
}