//
//	Define Validate class constructor
//

function Validate() {}


Validate.prototype.isEmpty = function(string)
{
	if( string == ""){
		return true;	
	} else {
		return false;
	}
}

Validate.prototype.isOnlyAlphaNumeric = function(string)
{
	var invalidCharactersRegExp = /[^a-zæøå,\.\-\d ]/i;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	return isValid;
}


Validate.prototype.isOnlyAlphaNumericNoSpace = function(string)
{
   var invalidCharactersRegExp = /[^a-zæøå\d]/i;
   var isValid = !(invalidCharactersRegExp.test(string));
      
   return isValid;
}



Validate.prototype.isOnlyAlphabetic = function(string)
{
	invalidCharactersRegExp = /[^a-zæøå ]/i;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	return isValid;
}

Validate.prototype.isOnlyNumeric = function(string)
{
	var invalidCharactersRegExp = /[^\d]/;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	return isValid;
}

Validate.prototype.isValidInteger = function(string)
{
	var invalidCharactersRegExp = /[^\d\-]/;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	return isValid;
}

Validate.prototype.isValidFloatingPoint = function(string)
{
	var invalidCharactersRegExp = /[^\d\.,\-]/;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	return isValid;
}

Validate.prototype.isValidAge = function(age)
{
	var isValid = false;
	if (this.isOnlyNumeric(age))
	{
		isValid = (parseInt(age) > 0 && parseInt(age) < 140)
	}
	
	return isValid;
}

Validate.prototype.isValidPassword = function(password)
{
	var invalidCharactersRegExp = /[^a-zæøå\d]/i
	var isValid = !(invalidCharactersRegExp.test(password));
	if (isValid)
	{
		isValid = (password.length >= 8 && password.length <= 16);
	}
	return isValid;
	
}



Validate.prototype.isValidTelephoneNum = function(telephoneNum)
{
   var validFormatRegExp = /^(\+\d{2} ?)?(\d{2} ?\d{2} ?\d{2} ?\d{2} ?)$/i;
//Remember to put this regular expression on one line.
   var isValid = validFormatRegExp.test(telephoneNum);
   return isValid;
   
}




Validate.prototype.isValidPostalCode = function(postalCode)
{// US / UK version
 //   var validFormat = /^(\d{5}(-\d{4})?|[a-z]{1,2}[\da-z]{1,2} ?\d[a-z][a-z])$/i
   var validFormat = /^(\d{4,5}|[a-z]{2,3}[ -]*\d{4,5})$/i
   var isValid = validFormat.test(postalCode);
   return isValid;
}




Validate.prototype.isValidEmail = function(email)
{
   var validFormatRegExp = /^\w(\.?\S)*@\w(\.?[-\w])*\.([a-z]{3}(\.[a-z]{2})?|[a-z]{2}(\.[a-z]{2})?)$/i
   var isValid = validFormatRegExp.test(email);
   return isValid;
}




Validate.prototype.isValidDate = function(day, month,year)
{
	var isValid = true;

	var enteredDate = new Date(day + " " + month + " " + year);
	if (enteredDate.getDate() != day)
	{
		isValid = false;
	}
	return isValid;
}




Validate.prototype.checkFormValid = function(theForm,theDocument)
{
	var isWholeFormValid = true;
	var isValid = true;
	var theElement;
	var isToBeValidatedElementRegExp = /(_Compulsory)|(_NotCompulsory)/i;
	var isCompulsoryRegExp = /(_Compulsory)/i;
	var validDataTypeRegExp = /_[a-zæøåA-ZÆØÅ]+$/i;
	var invalidDataType;
	var elementName;
	var errorDivId;
	var isCompulsoryElement;
	var isToBeCheckedElement;
	var isTextBoxElement;
	
// Check Text boxes completed and/or correct data type	
	for (var formElementCounter = 0; formElementCounter < theForm.length; formElementCounter++)
	{
		theElement = theForm.elements[formElementCounter];
		elementName = new String(theElement.name);

		isCompulsoryElement = isCompulsoryRegExp.test(elementName);
		
		isToBeValidatedElement = isToBeValidatedElementRegExp.test(elementName);
		
		if (isToBeValidatedElement)
		{
			errorDivId = new String(theElement.name);
			errorDivId = errorDivId.slice(3,errorDivId.indexOf("_")) + "Error";
			this.hideErrorDiv(errorDivId,theDocument);
					
			isTextBoxElement =  theElement.type == "text" || 
								theElement.type == "password" || 
								theElement.type == "file";
			
			if ( isTextBoxElement )
			{
				isValid = this.isTextElementValid(theElement,
												  theDocument,
												  validDataTypeRegExp,
												  isCompulsoryElement);
				
				if ( !isValid )
				{
					this.showErrorDiv(errorDivId,theDocument);
					theElement.focus();
					isWholeFormValid = false;
				}
			} //Check Compulsory Radio Buttons Completed
			else if (theElement.type == "radio")
			{

				if (isCompulsoryElement)
				{
					elementName = theElement.name;
					theElement = theForm.elements[theElement.name];
					isValid = this.isOneRadioButtonInGroupSelected(theElement);

					if (isValid == false)
					{
						this.showErrorDiv(errorDivId,theDocument);		
						isWholeFormValid = false;			
					}

					do 
					{
						formElementCounter++;
						theElement = theForm.elements[formElementCounter];
					}
					while (theElement.name == elementName && formElementCounter < theForm.length)
						
					formElementCounter--;
				}
			}
		}
	}

	return isWholeFormValid;
}

Validate.prototype.isTextElementValid = function(theElement,
												theDocument,
												validDataTypeRegExp, 
												isCompulsoryElement)
{

	var isValid = true;
	var validDataType;
	
	if (isCompulsoryElement && theElement.value == "")
	{
		isValid = false;	
	}
	else
	{				
		validDataTypeRegExp.exec(theElement.name);
		validDataType = new String(RegExp.lastMatch)
		validDataType = validDataType.toLowerCase();
		isValid = this.isElementDataValid(theElement.value,validDataType)
	}	
	
	return isValid;
}

Validate.prototype.isElementDataValid = function(elementValue, validDataType)
{
	var isValid = false;
	switch (validDataType)
	{

		case "_alphanumeric":
			isValid = this.isOnlyAlphaNumeric(elementValue);
			break;
	
		case "_alphabetic":
			isValid = this.isOnlyAlphabetic(elementValue);
			break;

		case "_numeric":
			isValid = this.isOnlyNumeric(elementValue);
			break;

		case "_integer":
			isValid = this.isValidInteger(elementValue);
			break;
			
		case "_floatingpoint":
			isValid = this.isValidFloatingPoint(elementValue);
			break;

		case "_age":
			isValid = isValidAge(elementValue);
			break;
			
		case "_password":
			isValid = isValidPassword(elementValue);
			break;

		case "_telephone":
			isValid = isValidTelephoneNum(elementValue);
			break;				
	
		case "_postcode":	
			isValid = this.isValidPostalCode(elementValue);
			break;
	
		case "_email":
			isValid = this.isValidEmail(elementValue);
			break;
			
		default:
			alert("Error unidentified element data type");
	}
			
	return isValid;
}



Validate.prototype.isOneRadioButtonInGroupSelected = function(theElement)
{

	var radioCounter;
	var isValid = false;
	for (radioCounter = theElement.length - 1; radioCounter >= 0; radioCounter--)
	{

		isValid = theElement[radioCounter].checked;
		if (isValid)
		{
			break;
		}
	}

	return isValid;

}


Validate.prototype.showErrorDiv = function (  errorDescripDivId, 
								theDocument)
{
  if (document.layers)
  {
 	theDocument.layers[errorDescripDivId].visibility = "visible";
  }
  else if (document.all)
  {
	theDocument.all(errorDescripDivId).style.visibility = "visible"
  }
  else
  {
    theDocument.getElementById(errorDescripDivId).style.visibility = "visible"
  }
}


Validate.prototype.hideErrorDiv = function (errorDescripDivId, theDocument)
{
  if (theDocument.layers)
  {
	theDocument.layers[errorDescripDivId].visibility = "hidden";
  }
  else if (document.all)
  {
	theDocument.all(errorDescripDivId).style.visibility = "hidden"
  }
  else
  {
 	theDocument.getElementById(errorDescripDivId).style.visibility = "hidden"	
  }
}

