// ------------------------------------
// Validation object.
// ------------------------------------

function ClassValidation()
{ 
	this.tipElements = ['input', 'select', 'textarea'];		// Array of allowable elements for validation tooltips
	this.obj = Object;										// Current element that youre hovering over
	this.tip = Object;										// The actual toolTip DIV itself
	this.OffsetX = 10;
	this.formObj = Object;
	
	this.Initialize = function ()
	{
		if (!document.getElementById || !document.createElement || !document.getElementsByTagName )
		{
			return;
		}
		var i,j;
		this.tip = document.createElement('div');
		this.tip.id = 'toolTip';
		this.tip.className = 'tooltip_container';
		this.tip.style.width = 272 + 'px';
		document.getElementsByTagName('body')[0].appendChild(this.tip);

		this.tip.style.top = '0';
		this.tip.style.visibility = 'hidden';
		
		var tipLen = this.tipElements.length;
		for ( i=0; i<tipLen; i++ )
		{
			var current = document.getElementsByTagName(this.tipElements[i]);
			var curLen = current.length;
			for ( j=0; j<curLen; j++ )
			{
				if( current[j].title)
				{
					AddEvent(current[j], "mouseover", function(evt) { return ValidationOver(evt); }, true);
					AddEvent(current[j], "mouseout", function(evt) { return ValidationOut(evt); }, true);
					
					var validateData = current[j].title.split(';');
					if(validateData.length > 1)
					{
						AddEvent(current[j], "blur", function(evt) { return Validate(evt); }, true);
						current[j].setAttribute('valType', validateData[1]);
						current[j].setAttribute('valMessage', validateData[2]);	
					}
					current[j].setAttribute('tip', validateData[0]);
					current[j].removeAttribute('title');
				}
			}
		}
	}
	
	this.TipOut = function ()
	{
		Validation.tip.style.visibility = 'hidden';
	}

	this.TipOver = function (e)
	{
		Validation.obj = this;
		Validation.ShowTip();
	}	
	
	this.ShowBorder = function ()
	{
		this.obj.style.borderColor = '#ff0000';
	}
	
	this.RemoveBorder = function ()
	{
		this.obj.style.borderColor = '';
	}
	
	this.ValidateForm = function (formId)
	{
		this.formObj = document.getElementById(formId);
		var tipLen = this.tipElements.length;
		var validationMessage = '';
		for ( i=0; i<tipLen; i++ )
		{
			var current = this.formObj.getElementsByTagName(this.tipElements[i]);
			var curLen = current.length;
			for ( j=0; j<curLen; j++ )
			{	
				this.obj = current[j];
				if(this.DoValidate())
				{
					this.ShowBorder();
					validationMessage += this.obj.getAttribute('valMessage') + '<br />';
				}
				else
				{
					this.RemoveBorder();
				}				
			}
		}
		
		return this.CreateFeedback(validationMessage);
	}
	
	this.CreateFeedback = function (validationMessage)
	{
		if(validationMessage != '')
		{
			// Create feedback element
			if(!document.getElementById('feedbackAlgin'))
			{
				var feedBackAligner = document.createElement('div');
				feedBackAligner.setAttribute('id', 'feedbackAlgin');
				feedBackAligner.style.paddingLeft = '5px';
				feedBackAligner.setAttribute('align', 'left');
				
				var feedBack = document.createElement('div');
				feedBack.setAttribute('id', 'feedback');
				feedBack.style.textAlign = 'left';
				feedBack.style.width = '582px';
				feedBack.style.fontFamily = 'Trebuchet MS';
				feedBack.style.fontSize = '13px';
				feedBack.style.fontWeight = 'bold';
				feedBack.style.color = '#cc0000';
				feedBack.style.paddingLeft = '5px';
				feedBack.style.paddingTop = '3px';
				feedBack.style.paddingBottom = '3px';
				feedBack.style.borderStyle = 'dashed';
				feedBack.style.borderColor = '#cc0000';
				feedBack.style.borderWidth = '1px';
				feedBack.style.backgroundColor = '#ffffff';
				feedBackAligner.appendChild(feedBack);
				
				this.formObj.parentNode.insertBefore(feedBackAligner, this.formObj);
				
				var TabHeight = Tabs.GetTabHeight();
				if(TabHeight)
				{
					TabHeight = this.GetFeedbackHeight() + TabHeight;
					Tabs.SetTabHeight(TabHeight);
				}
			}
			document.getElementById('feedback').innerHTML = validationMessage;
			return false;
		}
		else
		{
			if(document.getElementById('feedbackAlgin'))
			{
				this.formObj.parentNode.removeChild(document.getElementById('feedbackAlgin'));
				
				var TabHeight = Tabs.GetTabHeight();
				TabHeight = TabHeight - this.GetFeedbackHeight();
				Tabs.SetTabHeight(TabHeight);
			}
			return true;
		}
	}
	
	this.GetFeedbackHeight = function ()
	{
		var feedbackObj = document.getElementById('feedbackAlgin');
		if(feedbackObj)
		{
			return feedbackObj.offsetHeight;
		}
		else
		{
			return false;
		}
	}
	
	this.DoValidate = function ()
	{
		var validateType = this.obj.getAttribute('valType');
		switch(validateType)
		{
			case 'required' :
				if(this.IsEmpty()) { return true; } else { return false; }
				break;
			case 'numeric' :
				if(this.IsNumeric()) { return true; } else { return false; }
				break;
			case 'email' :
				if(this.EmailValidator()) { return true; } else { return false; }
				break;
		}
	}
  
	this.IsEmpty = function ()
	{
		if(this.obj.value.length != 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	this.IsNumeric = function ()
	{
		if(this.obj.value.match(/^[0-9]+$/))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	this.IsAlphabet = function ()
	{
		if(this.obj.value.match(/^[a-zA-Z]+$/))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	this.IsAlphanumeric = function ()
	{
		if(this.obj.value.match(/^[0-9a-zA-Z]+$/))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	this.LengthRestriction = function (min_val, max_val)
	{
		var uInput = this.obj.value;
		
		if(uInput.length >= min_val && uInput.length <= max_val)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	this.MadeSelection = function (val)
	{
		if(this.obj.value == val)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	this.EmailValidator = function ()
	{
		if(this.obj.value.match(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	this.PostcodeValidator = function ()
	{
		if(this.obj.value.match(/^[0-9]{4}\s{0,1}[a-zA-Z]{2}$/))
		{
			return false;
		}
		else
		{
			return true;
		}
	}

  	this.ShowTip = function ()
	{
		var Ttop, Tleft, ClientWidth, ClientHeight, ScrollTop;
		var TipWidth, TipHeight;
		
		if(this.obj.getAttribute('tip'))
		{
			if (this.obj.getAttribute('tip').length == 0)
			{
				return;
			}  
		}
		else
		{
			return;
		}
		
		Tleft = findPosX(this.obj);		// offsetLeft;
		Ttop = findPosY(this.obj);		// offsetTop;
		TipWidth = this.tip.offsetWidth;
		TipHeight = this.tip.offsetHeight;
		
		if (self.innerHeight)
		{
			// all except Explorer
			ClientWidth = self.innerWidth;
			ClientHeight = self.innerHeight;
			ScrollTop = self.scrollTop;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
		{
			ClientWidth = document.documentElement.clientWidth;
			ClientHeight = document.documentElement.clientHeight;
			ScrollTop = document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			// other Explorers
			ClientWidth = document.body.clientWidth;
			ClientHeight = document.body.clientHeight;
			ScrollTop = document.body.scrollTop;
		}
		
		var sHtml = '\
				<div class="tooltip_left"></div>\
				<div class="tooltip_right" style="width:257px;">\
					<div class="tooltip">'+this.obj.getAttribute('tip')+'</div>\
				</div>';
				
		this.tip.innerHTML = sHtml; 

		Tleft = Tleft + parseInt(this.obj.style.width) + this.OffsetX;
		if ((Tleft + TipWidth) > ClientWidth) Tleft = ClientWidth - TipWidth;
		
		if (Tleft < 1) Tleft = 1;
		if (Ttop < 1) Ttop = 1;
		if (Ttop < ScrollTop) Ttop = ScrollTop + (TipHeight + 5);
		
		this.tip.style.left = Tleft + 'px';
		this.tip.style.top = Ttop + 'px';
		this.tip.style.visibility = 'visible';
	}
	
  	this.ShowMessage = function ()
	{
		var Ttop, Tleft, ClientWidth, ClientHeight, ScrollTop;
		var TipWidth, TipHeight;

		if (this.obj.getAttribute('valMessage').length == 0)
		{
			return;
		}  
		
		Tleft = findPosX(this.obj);		// offsetLeft;
		Ttop = findPosY(this.obj);		// offsetTop;
		TipWidth = this.tip.offsetWidth;
		TipHeight = this.tip.offsetHeight;
		
		if (self.innerHeight)
		{
			// all except Explorer
			ClientWidth = self.innerWidth;
			ClientHeight = self.innerHeight;
			ScrollTop = self.scrollTop;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
		{
			ClientWidth = document.documentElement.clientWidth;
			ClientHeight = document.documentElement.clientHeight;
			ScrollTop = document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			// other Explorers
			ClientWidth = document.body.clientWidth;
			ClientHeight = document.body.clientHeight;
			ScrollTop = document.body.scrollTop;
		}
		
		var sHtml = '\
				<div class="tooltip_left"></div>\
				<div class="tooltip_right" style="width:257px;">\
					<div class="tooltip">'+this.obj.getAttribute('valMessage')+'</div>\
				</div>';
				
		this.tip.innerHTML = sHtml; 

		Tleft = Tleft + parseInt(this.obj.style.width) + this.OffsetX;
		if ((Tleft + TipWidth) > ClientWidth) Tleft = ClientWidth - TipWidth;
		if (Tleft < 1) Tleft = 1;
		if (Ttop < 1) Ttop = 1;
		if (Ttop < ScrollTop) Ttop = ScrollTop + (TipHeight + 5);
		this.tip.style.left = Tleft + 'px';
		this.tip.style.top = Ttop + 'px';
		this.tip.style.visibility = 'visible';
	}

}

function ValidationOut ()
{
	Validation.tip.style.visibility = 'hidden';
}

function ValidationOver (evt)
{
	Validation.obj = (evt.target) ? evt.target : evt.srcElement;
	Validation.ShowTip();
}

function Validate (evt)
{
	Validation.obj = (evt.target) ? evt.target : evt.srcElement;
	if(Validation.DoValidate())
	{
		Validation.ShowBorder();
		Validation.ShowMessage();
	}
	else
	{
		Validation.RemoveBorder();
	}
}

// Form Validation Functions

function getFormErrors(form) {
   var errors = new Array();
   
   // loop thru all form elements
   for (var elementIndex = 0; elementIndex < form.elements.length; elementIndex++) {
      var element = form.elements[elementIndex];
     
      // text and textarea types
      if (element.type == "text" || element.type == "textarea") {
         element.value = trimWhitespace(element.value)
         
         // required element
         if (element.required  && element.value == '') {
			 errors[errors.length] = element.requiredError;
         }
         
         // maximum length
         else if (element.maxlength && isValidLength(element.value, 0, element.maxlength) == false) {
            errors[errors.length] = element.maxlengthError;
         }

         // minimum length
         else if (element.minlength && isValidLength(element.value, element.minlength, element.minlength) == false) {
            errors[errors.length] = element.minlengthError;
         }
         
         // pattern (credit card number, email address, zip or postal code, alphanumeric, numeric)
         else if (element.pattern) {
            if ( ( (element.pattern.toLowerCase() == 'visa' || element.pattern.toLowerCase() == 'mastercard' || element.pattern.toLowerCase() == 'american express' || element.pattern.toLowerCase() == 'diners club' || element.pattern.toLowerCase() == 'discover' || element.pattern.toLowerCase() == 'enroute' || element.pattern.toLowerCase() == 'jcb' || element.pattern.toLowerCase() == 'credit card') && isValidCreditCard(element.value, element.pattern) == false) ||
                  (element.pattern.toLowerCase() == 'email' && isValidEmailStrict(element.value) == false) ||
                  (element.pattern.toLowerCase() == 'zip or postal code' && isValidZipcode(element.value) == false && isValidPostalcode(element.value) == false) ||
                  (element.pattern.toLowerCase() == 'zipcode' && isValidZipcode(element.value) == false) ||
                  (element.pattern.toLowerCase() == 'postal code' && isValidPostalcode(element.value) == false) ||
                  (element.pattern.toLowerCase() == 'us phone number' && 
                     ( (element.prefix && element.suffix && isValidUSPhoneNumber(element.value, form[element.prefix].value, form[element.suffix].value) == false) || 
                        (!element.prefix && !element.suffix && isValidUSPhoneNumber(element.value) == false) ) ) ||
                  (element.pattern.toLowerCase() == 'alphanumeric' && isAlphanumeric(element.value, true) == false) ||
                  (element.pattern.toLowerCase() == 'numeric' && isNumeric(element.value, true) == false) ||
                  (element.pattern.toLowerCase() == 'alphabetic' && isAlphabetic(element.value, true) == false) ) {
               errors[errors.length] = element.patternError;
            }
         }
      }
      
      // password 
      else if (element.type == "password") {
         
         // required element
         if (element.required  && element.value == '') {
            errors[errors.length] = element.requiredError;
         }
         
         // maximum length
         else if (element.maxlength && isValidLength(element.value, 0, element.maxlength) == false) {
            errors[errors.length] = element.maxLengthError;
         }

         // minimum length
         else if (element.minlength && isValidLength(element.value, element.minlength, 255) == false) {//Number.MAX_VALUE
            errors[errors.length] = element.minLengthError;
         }
      }
      
      // file upload
      if (element.type == "file") {
         
         // required element
         if (element.required  && element.value == '') {
            errors[errors.length] = element.requiredError;
			// Extension checker modified by BaZ
			 if (element.extension) {
				if((element.value.lastIndexOf(".jpg")==-1) && (element.value.lastIndexOf(".gif")==-1) && (element.value.lastIndexOf(".swf")==-1)) {
					errors[errors.length] = element.extensionError;
				}
			}
         }
	}
      
      // select
      else if (element.type == "select-one" || element.type == "select-multiple" || element.type == "select") {

         // required element
         if (element.required && element.selectedIndex == -1) {
            errors[errors.length] = element.requiredError;
         }
         
		 // disallow empty value selection
         else if (element.disallowEmptyValue && element.options[element.selectedIndex].value == '') {
            errors[errors.length] = element.disallowEmptyValueError;
         }

      }
      
      // radio buttons
      else if (element.type == "radio") {
         var radiogroup = form.elements[element.name];
         
         // required element
         if (radiogroup.required && radiogroup.length) {
            var checkedRadioButton = -1;
            for (var radioIndex = 0; radioIndex < radiogroup.length; radioIndex++) {
               if (radiogroup[radioIndex].checked == true) {
                  checkedRadioButton = radioIndex;
                  break;
               }
            }
            if (checkedRadioButton == -1 && !radiogroup.tested) {
               errors[errors.length] = radiogroup.requiredError;
               radiogroup.tested = true;
            }
         }
         
         radiogroup = null;
      }
   }
	return errors;
}

// Checks if 2 passwords are the same. Returns boolean
function verifyPass(whichElm1, whichElm2) {

	var tElm1 = document.getElementById(whichElm1);
	var tElm2 = document.getElementById(whichElm2);
	
	// Return when one is empty
	if(tElm1.value == "" || tElm2.value == "") { return false; }
	// Compare
	if(tElm1.value != tElm2.value) {
		return true;
	} else {
		return false;
	}
}

// Check that the number of characters in a string is between a max and a min
function isValidLength(string, min, max) {
	if (string.length < min || string.length > max) return false;
	else return true;
}

// Check that a credit card number is valid based using the LUHN formula (mod10 is 0)
function isValidCreditCard(number) {
	number = '' + number;
	
	if (number.length > 16 || number.length < 13 ) return false;
	else if (getMod10(number) != 0) return false;
	else if (arguments[1]) {
		var type = arguments[1];
		var first2digits = number.substring(0, 2);
		var first4digits = number.substring(0, 4);
		
		if (type.toLowerCase() == 'visa' && number.substring(0, 1) == 4 &&
			(number.length == 16 || number.length == 13 )) return true;
		else if (type.toLowerCase() == 'mastercard' && number.length == 16 &&
			(first2digits == '51' || first2digits == '52' || first2digits == '53' || first2digits == '54' || first2digits == '55')) return true;
		else if (type.toLowerCase() == 'american express' && number.length == 15 && 
			(first2digits == '34' || first2digits == '37')) return true;
		else if (type.toLowerCase() == 'diners club' && number.length == 14 && 
			(first2digits == '30' || first2digits == '36' || first2digits == '38')) return true;
		else if (type.toLowerCase() == 'discover' && number.length == 16 && first4digits == '6011') return true;
		else if (type.toLowerCase() == 'enroute' && number.length == 15 && 
			(first4digits == '2014' || first4digits == '2149')) return true;
		else if (type.toLowerCase() == 'jcb' && number.length == 16 &&
			(first4digits == '3088' || first4digits == '3096' || first4digits == '3112' || first4digits == '3158' || first4digits == '3337' || first4digits == '3528')) return true;
		
    // if the above card types are all the ones that the site accepts, change the line below to 'else return false'
    else return true;
	}
	else return true;
}

// Check that an email address is valid based on RFC 821 (?)
function isValidEmail(address) {
	if (address != '' && address.search) {
      if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
      else return false;
	}
	
   // allow empty strings to return true - screen these with either a 'required' test or a 'length' test
   else return true;
}

// Check that an email address has the form something@something.something
// This is a stricter standard than RFC 821 (?) which allows addresses like postmaster@localhost
function isValidEmailStrict(address) {
	if (isValidEmail(address) == false) return false;
	var domain = address.substring(address.indexOf('@') + 1);
	if (domain.indexOf('.') == -1) return false;
	if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) return false;
	return true;
}

// Check that a US zip code is valid
function isValidZipcode(zipcode) {
	zipcode = removeSpaces(zipcode);
	if (!(zipcode.length == 5 || zipcode.length == 9 || zipcode.length == 10)) return false;
   if ((zipcode.length == 5 || zipcode.length == 9) && !isNumeric(zipcode)) return false;
   if (zipcode.length == 10 && zipcode.search && zipcode.search(/^\d{5}-\d{4}$/) == -1) return false;
   return true;
}

// Check that a Canadian postal code is valid
function isValidPostalcode(postalcode) {
	if (postalcode.search) {
		postalcode = removeSpaces(postalcode);
		if (postalcode.length == 6 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;
		else if (postalcode.length == 7 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]-\d[a-zA-Z]\d$/) != -1) return true;
		else return false;
	}
	return true;
}

// Check that a US or Canadian phone number is valid
function isValidUSPhoneNumber(areaCode, prefixNumber, suffixNumber) {
   if (arguments.length == 1) {
      var phoneNumber = arguments[0];
      phoneNumber = phoneNumber.replace(/\D+/g, '');
      var length = phoneNumber.length;
      if (phoneNumber.length >= 7) {
         var areaCode = phoneNumber.substring(0, length-7);
         var prefixNumber = phoneNumber.substring(length-7, length-4);
         var suffixNumber = phoneNumber.substring(length-4);
      }
      else return false;
   }
   else if (arguments.length == 3) {
      var areaCode = arguments[0];
      var prefixNumber = arguments[1];
      var suffixNumber = arguments[2];
   }
   else return true;

   if (areaCode.length != 3 || !isNumeric(areaCode) || prefixNumber.length != 3 || !isNumeric(prefixNumber) || suffixNumber.length != 4 || !isNumeric(suffixNumber)) return false;
   return true;
}

// Check that a string contains only letters and numbers
function isAlphanumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) return false;
	}
	return true;
}

// Check that a string contains only letters
function isAlphabetic(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) return false;
	}
	return true;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
	}
	return true;
}

// Remove characters that might cause security problems from a string 
function removeBadCharacters(string) {
	if (string.replace) {
		string.replace(/[<>\"\'%;\)\(&\+]/, '');
	}
	return string;
}

// Remove all spaces from a string
function removeSpaces(string) {
	var newString = '';
	for (var i = 0; i < string.length; i++) {
		if (string.charAt(i) != ' ') newString += string.charAt(i);
	}
	return newString;
}

// Remove leading and trailing whitespace from a string
function trimWhitespace(string) {
	var newString  = '';
	var substring  = '';
	beginningFound = false;
	
	// copy characters over to a new string
	// retain whitespace characters if they are between other characters
	for (var i = 0; i < string.length; i++) {
		
		// copy non-whitespace characters
		if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {
			
			// if the temporary string contains some whitespace characters, copy them first
			if (substring != '') {
				newString += substring;
				substring = '';
			}
			newString += string.charAt(i);
			if (beginningFound == false) beginningFound = true;
		}
		
		// hold whitespace characters in a temporary string if they follow a non-whitespace character
		else if (beginningFound == true) substring += string.charAt(i);
	}
	return newString;
}

// Returns a checksum digit for a number using mod 10
function getMod10(number) {
	
	// convert number to a string and check that it contains only digits
	// return -1 for illegal input
	number = '' + number;
	number = removeSpaces(number);
	if (!isNumeric(number)) return -1;
	
	// calculate checksum using mod10
	var checksum = 0;
	for (var i = number.length - 1; i >= 0; i--) {
		var isOdd = ((number.length - i) % 2 != 0) ? true : false;
		digit = number.charAt(i);
		
		if (isOdd) checksum += parseInt(digit);
		else {
			var evenDigit = parseInt(digit) * 2;
			if (evenDigit >= 10) checksum += 1 + (evenDigit - 10);
			else checksum += evenDigit;
		}
	}
	return (checksum % 10);
}
