var AllForm = new Object();

function FormItem(in_inputType,in_optional,in_checkType,in_maxLength,in_minLength,in_maxChecked,in_message,in_message2){
	this.inputType = in_inputType; // text,radio,checkbox or select
	this.optional = in_optional; // bit, if true the value is checked if a value is present
	this.checkType = in_checkType; // any, email, integer, float, nospace (enable only if inputType is 'text'))
	this.maxLength = in_maxLength; // int maximum length of the string (enable only if checkType is not 'integer' or 'float')
	this.minLength = in_minLength; // int minimum length of the string (enable only if in_minLength > 0 and checkType is not 'integer' or 'float') 
																//if maxLength == minLength the string has to be maxLength;
	this.maxChecked = in_maxChecked;
	this.message = in_message; // string
	this.message2 = in_message2; // string
}

// checking functions

function isEmail(val) {
  var regex = /^([a-zA-Z0-9_\.\-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
  return regex.test(val);
}

function isInteger(valeur){// v1.01 Patrick TAI 3/7/2000
	match = 0;
	longueur = valeur.length;
	for(var a=0;a<longueur;a++){
		for(var i=0;i<10;i++){
			if(valeur.substring(a,a+1).indexOf(i)!=-1){match = match + 1;}
		}
	}
	if(match != longueur){return false};
	
return true;
}


function isFloat(valeur){// v1.0 Patrick TAI the floating point can be either "." or ","
var caract = new Array("0","1","2","3","4","5","6","7","8","9",",","."), 
		matchs = 0, 
		nbre_decimal = 0,
		longueur = valeur.length,
		separator = false;
	for(var a=0;a<longueur;a++){
		for(var i=0;i<=10;i++){
			if(valeur.substring(a,a+1).indexOf(caract[i])!=-1){
				matchs++;
				if(!separator){if(caract[i]=="," || caract[i]=="."){separator=true;nbre_decimal++;};}
			}
		}
	}
if(!separator)nbre_decimal++;
if(matchs != longueur || matchs == 0 || nbre_decimal>1){return false;};
	
return true;
}

function isCreditcard(object_value)
{
	var white_space = " -";
	var creditcard_string="";
	var check_char;

	for (var i = 0; i < object_value.length; i++)
	{
		check_char = white_space.indexOf(object_value.charAt(i));
		if (check_char < 0)
			creditcard_string += object_value.substring(i, (i + 1));
	}	

	if (creditcard_string.length > 16 || creditcard_string.length < 15)
		return false;

	if (creditcard_string.charAt(0) == "+")
		return false;

	if (!isInteger(creditcard_string))
		return false;

	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for (var i = 0; i < creditcard_string.length; i++)
	{
		tempdigit = eval(creditcard_string.charAt(i));

		if (doubledigit)
		{
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);

			if ((tempdigit / 10) >= 1.0)
				checkdigit++;

			doubledigit = false;
		}
		else
		{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;
}



function isEmpty(str){
	var longr=str.length;
	var test="";
	for (var i=0;i<longr;i++){test+=" ";}
	if(str==test){return true}
	return false
}

var dispMess = "";
function addAlert(message){
	dispMess +=  message +  "\n";
}


// end checking functions
function checkFormItem(FormItemName){
	var theFormItem = AllForm[FormItemName]; // a copy of the object with its properties
	var FormElement = document.forms[FormName][FormItemName]; //a copy of the form element
	var filtered = false;
	switch(theFormItem.inputType) {
		
		case "text":
			var go_on = true;
			if(theFormItem.optional && isEmpty(FormElement.value)){go_on = false;}
			if(go_on){
				
				switch(theFormItem.checkType){
						case "email":
							if(!isEmail(FormElement.value)) {addAlert(theFormItem.message); filtered = true}
						break;
						case "integer":
							if(!isInteger(FormElement.value)) {addAlert(theFormItem.message); filtered = true}
						break;
						case "float":
							if(!isFloat(FormElement.value)) {addAlert(theFormItem.message); filtered = true}
						break;
						case "creditcard":
							if(!isCreditcard(FormElement.value)) {addAlert(theFormItem.message); filtered = true}
						break;
						case "nospace":
							if(FormElement.value.indexOf(' ')!=-1) {addAlert(theFormItem.message); filtered = true}
						break;
				}
				
				if(theFormItem.maxLength == theFormItem.minLength && theFormItem.maxLength > 0 && !filtered){
					if(FormElement.value.length != theFormItem.maxLength){addAlert(theFormItem.message2 + "\n(currenlty "+ FormElement.value.length +" chars)"); filtered = true};
				}
				if(theFormItem.maxLength != theFormItem.minLength && theFormItem.minLength > 0 && !filtered){
					if(theFormItem.maxLength > 0 && FormElement.value.length > theFormItem.maxLength)addAlert(theFormItem.message2 + "\n(currenlty "+ FormElement.value.length +" chars)");
					if(theFormItem.minLength > 0 && FormElement.value.length < theFormItem.minLength)addAlert(theFormItem.message2 + "\n(currenlty "+ FormElement.value.length +" chars)");
				}			
				
			}
		

		break;
		
		case "radio":
			if(!theFormItem.optional){
				var isChecked = false;
				for(var i=0;i < FormElement.length;i++){
					if(FormElement[i].checked){isChecked=true;break}			
				}
				if(!isChecked) {addAlert(theFormItem.message);}
			}
		break;
		
		case "checkbox":
			if(!theFormItem.optional)
			{
				var isChecked = false;
					if(FormElement.length > 0){
						for(var i=0;i < FormElement.length;i++){
							if(FormElement[i].checked){isChecked=true;break}			
						}
					}
					else if(FormElement.checked)isChecked=true;

				if(!isChecked) {addAlert(theFormItem.message);}
			}
		break;
		
		case "select":
			if(!theFormItem.optional){
				if(FormElement.size > 1){
				var isSelected = false;
					for(var i=0;i < FormElement.length;i++){
						if(FormElement[i].selected){isSelected=true;break}			
					}
				}
				else {
					if(FormElement.selectedIndex !=0){
						isSelected=true;
					}
				}
				if(!isSelected) {{addAlert(theFormItem.message);}}
			}
		break;
	}
}


function checkForm(){
	dispMess = '';
	for(el in AllForm){
		checkFormItem(el);
			if(dispMess.length>0){alert(dispMess);return false}
	}


	return true
}
