//<!--<![CDATA[

setHasChanged = false;
String.prototype.trim = function(){return this.replace(/^\s+/,"").replace(/\s+$/,"");}

function FormValidator(){
	this.all = {};
	this.invalidColor = "#FFFFE1";
	this.requiredClass = "required";
	this.setup();
}
FormValidator.prototype.setup = function(){
    
    var a = document.getElementsByTagName("TABLE");
	for (var i=0;i<a.length;i++) 
	    {if(a[i].getAttribute("required"))
	        {this.all[a[i].uniqueID] = new FormElement(a[i], this);}
	    }    
	var a = document.getElementsByTagName("INPUT");
	for (var i=0;i<a.length;i++) this.all[a[i].uniqueID] = new FormElement(a[i], this);
	var a = document.getElementsByTagName("TEXTAREA");
	for (var i=0;i<a.length;i++) this.all[a[i].uniqueID] = new FormElement(a[i], this);
	var a = document.getElementsByTagName("SELECT");
	for (var i=0;i<a.length;i++) this.all[a[i].uniqueID] = new FormElement(a[i], this);
	
	for (o in this.all) this.all[o].setup();
}
FormValidator.prototype.clear = function(){
	for (o in this.all) this.all[o].clear();
}
FormValidator.prototype.validate = function(){
	for (o in this.all){
		if (!this.all[o].validate()) return false;
	}
	return true;
}
FormValidator.prototype.raiseError = function(e, s, t){
	if (e.style.display=='')
	{
	    e.style._backgroundColor = e.style.backgroundColor;
	    e.style.backgroundColor = this.invalidColor;
	    try{e.focus();e.select();}catch(er){}
	    if (typeof(showCallout)=="function"){
		    if (window._callout) window._callout.remove();
		    window._callout = showCallout(e, s, t, "vbExclamation", 4000);
	    }
	}
	
}

var messgenumb = 0;
// returns the message in the language needed depending on the number passed in
FormValidator.prototype.message = function(pNumber){

	switch (pNumber) {
	
		// Validation Title
		case 1 : return lang == "E" ? "Validation Error" : "Erreur de validation"; break;

		// Validation Message
		case 2 : return lang == "E" ? "A required value is missing." : "Une valeur est requis."; break;
		case 3 : return lang == "E" ? "The date entered is not valid. Please use this format: YYYY-MM-DD." : "Le format de date est invalide. Utiliser le format YYYY-MM-DD." ; break;
		case 4 : return lang == "E" ? "This field accepts numeric values only." : "Ce champ accept seulement des valeures numeric." ; break;
		
		// Length Title
		case 5 : return lang == "E" ? "Field length reached" : "Limit de la longeur du champ atteint." ; break;
		
		// Length Message
		case 6 : return lang == "E" ? "A maximum of " + event.srcElement.maxlen + " characters are permitted." : "Seulement " + event.srcElement.maxlen + " characters sont permit dans ce champ." ; break;
		
	    // Not the same message
		case 7 : return lang == "E" ? "The confirmation value does not match." : "La valeur de confirmation diff" + String.fromCharCode(232) + "re de l'original" ; break;	
	
	    // invalide email	
		case 8 : return lang == "E" ? "The email appears invalid." : "Le courriel semble invalide." ; break;	
		
		// invalide phone
		case 9 : return lang == "E" ? "Invalid telephone, please use format: 000-000-0000." : "T" + String.fromCharCode(232) + "l" + String.fromCharCode(232) + "phone invalide, utiliser ce format: 000-000-0000" ; break;	

		// invalide phone
		case 10 : return lang == "E" ? "A minimum of " + messgenumb + " characters is required." : "Un minimum de " + messgenumb + " characters sont requis dans ce champ." ; break;	
				
		// default error
		default : return lang == "E" ? "Error Produced." : "Erreur produite."; break;
	}
}

function FormElement(e, v){
	this.validator = v;
	this.node = e;
	this.uniqueID = e.uniqueID;
	this.required = e.getAttribute("required") || false;
	this.filter = e.getAttribute("filter") || null;
	this.mask = e.getAttribute("mask") || null;
	this.isNumeric = e.getAttribute("isNumeric") || false;
	this.isEmail = e.getAttribute("isEmail") || false;
	this.isTelephone = e.getAttribute("isTelephone") || false;
	this.isDate = e.getAttribute("isDate") || false;
	this.type = this.node.type ? this.node.type : this.node.tagName ;
	this.itemsCheck = e.getAttribute("itemsCheck") || false;
	this.confirmtext = e.getAttribute("confirmtext") || false;
}

FormElement.prototype.setup = function(){
	if (this.required) this.node.className = this.node.className + " " + this.validator.requiredClass;
	if (this.filter){
		this.node.attachEvent("onkeypress", function(){
			var k = String.fromCharCode(event.keyCode);
			var re = new RegExp(filter);
			if (k!="\r" && !re.test(k)) event.returnValue=false;
			event.keyCode=k.charCodeAt(0);
		});
	}

	this.node.attachEvent("onkeypress", function(){
		var e = event.srcElement;
		if (e.maxlen)
		{
			if (e.value.length >= e.maxlen){
				if (typeof(showCallout)=="function"){
					if (window._callout) window._callout.remove();
					window._callout = showCallout(e, this.validator.message(6), this.validator.message(5), "vbInformation", 4000);
					return false;
				}
			}
		}
	});

	this.node.attachEvent("onpaste", function(){
		var e = event.srcElement;
		var sClip = window.clipboardData.getData("Text");
		if (e.maxlen)
		{
			if ((e.value.length + sClip.length) >= e.maxlen){
				if (typeof(showCallout)=="function"){
					if (window._callout) window._callout.remove();
					window._callout = showCallout(e, this.validator.message(6), this.validator.message(5), "vbInformation", 4000);
					return false;
				}
			}
		}

	});
	this.node.attachEvent("onchange", function(){
		var b = validator.all[event.srcElement.uniqueID].validate();
		event.returnValue = b;
		if (b) validator.all[event.srcElement.uniqueID].clear();}
	);
	
	//if (this.node.contentEditable!="false" && this.node.novalidate!="true") this.node.attachEvent("onchange", setHasChanged);
	
	
}
FormElement.prototype.validate = function(){
	if (this.type=="text" || this.type=="textarea" || this.type=="file" || this.type=="password"){
		this.node.value = this.node.value.trim();
	}
	
	if (this.required){
		var b = true;
		if (this.type=="text" || this.type=="textarea" || this.type=="file" || this.type=="password"){
			b = (this.node.value.length!=0);
		
		}
		else if (this.type.toLowerCase().indexOf("select")!=-1)
		{	
			if (this.itemsCheck)
				{if (this.node.options.length <= 0) b = false;}
			else
			{
				if (this.node.selectedIndex==-1) b = false;
				else b = (this.node.options[this.node.selectedIndex].text!="");
			}
		}
		else if (this.type.toLowerCase().indexOf("table")!=-1)
		{
		    b=false;
		    for (var i=0; i < this.node.rows.length;i++) 
   	       {
		        if(this.node.rows[i].firstChild.firstChild.checked){b=true;break;}
		    }
		}
		
		if (!b) 
		{
			this.validator.raiseError(this.node, this.validator.message(2), this.validator.message(1))
			return b;
		}
		
	}

    if (this.node.minlen)
    {
        if (this.node.value.length < this.node.minlen )
        {
            messgenumb = this.node.minlen;
    	    //window._callout = showCallout(e, this.validator.message(10), this.validator.message(1), "vbInformation", 4000);
    	    this.validator.raiseError(this.node, this.validator.message(10), this.validator.message(1));
    	    b=false;
	        return b;
        }
    }
	    
	/*
	if (this.isPostalCode)
	{//k0a 2m0
	   
	}
	*/
	
	    
	//validate date is desired
	if (this.isDate)
	{
		if (this.node.value.trim().length==0) return true;
		// set-up regex for test 0000-00-00 
		var RE = new RegExp("^[0-9]{4}\-([0][0-9]|[1][0-2])\-([0-2][0-9]|[3][0-1])$");
		if(RE.test(this.node.value))
		{ 
			// check for valid date
			var theDay = Math.round(this.node.value.substr(8,2));
			var theMonth = Math.round(this.node.value.substr(5,2));
			var theYear = Math.round(this.node.value.substr(0,4));
			
			if (!(
				!((theYear%4 == 0) && (theDay > 29) && (theMonth == 2)) &&
				!((theYear%4 != 0) && (theDay > 28) && (theMonth == 2)) &&
				!((theDay > 30) && (theMonth == 4 || theMonth == 6 || theMonth == 0 || theMonth == 11))
				))
			{
				this.validator.raiseError(this.node, this.validator.message(3), this.validator.message(1));
				return false;
			}
		}
		else
		{
			this.validator.raiseError(this.node, this.validator.message(3), this.validator.message(1));
				return false;
		}		
	}
	// to fields need to be the same
	if(this.confirmtext)
	{
	    if (this.node.value != document.getElementById(this.confirmtext).value)
	    {
	        this.validator.raiseError(this.node, this.validator.message(7), this.validator.message(1));
		    return false;
	    }
	}

    if (this.isEmail && !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.[A-Za-z]{2,4}$/.test(this.node.value)){
        this.validator.raiseError(this.node, this.validator.message(8), this.validator.message(1));
		return false;
    }

    if (this.node.value != "" )
    {
        if (this.isTelephone && !/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/.test(this.node.value))
        {
            this.validator.raiseError(this.node, this.validator.message(9), this.validator.message(1));
		    return false;
        }
    }

	if (this.isNumeric && (isNaN(new Number(this.node.value)) || !/^[-+]?\d{0,20}(\.\d{0,2})?$/.test(this.node.value))){
		this.validator.raiseError(this.node, this.validator.message(4), this.validator.message(1));
		return false;
	}
	this.clear();
	return true;
}

FormElement.prototype.clear = function(){
	this.node.style.backgroundColor = ""; //this.node.style._backgroundColor;
}



//]]>-->