//object constructor
function formValidator(formname,debug){
	//properties
	this.debug = debug? true:false;
	this.formname = formname;
	this.commandQueue = new Array();
	this.commandItemCount = 0;
}
//prototype
//internal helper functions
formValidator.prototype.errorHandling = errorHandling;
formValidator.prototype.addCommandQueueItem = addCommandQueueItem;
formValidator.prototype.execCommandQueueItem = execCommandQueueItem;
formValidator.prototype.checkForm = checkForm;
		
//functions
formValidator.prototype.textboxEmpty = textboxEmpty;	
formValidator.prototype.textboxEmptyF = textboxEmptyF;	
formValidator.prototype.textboxEmail = textboxEmail;	
formValidator.prototype.textboxEmailF = textboxEmailF;
formValidator.prototype.radioChecked = radioChecked;	
formValidator.prototype.radioCheckedF = radioCheckedF;
formValidator.prototype.checkboxChecked = checkboxChecked;	
formValidator.prototype.dropdownSelected = dropdownSelected;
formValidator.prototype.dropdownSelectedF = dropdownSelectedF;
formValidator.prototype.compareValues = compareValues;
formValidator.prototype.compareValuesF = compareValuesF;
formValidator.prototype.characterCheck = characterCheck;
formValidator.prototype.characterCheckF = characterCheckF;
formValidator.prototype.fieldLength = fieldLength;
formValidator.prototype.fieldLengthF = fieldLengthF;
formValidator.prototype.compareValuesZero = compareValuesZero;
formValidator.prototype.compareValuesZeroF = compareValuesZeroF;
//implementation
//internal helper functions
function errorHandling(message){
	alert(message);
	//document.forms[this.formname].onsubmit='return false';
	return true; 
}

function execCommandQueueItem(queueItem){
	return this.commandQueue[queueItem][0](this.commandQueue[queueItem][1]);
}

function addCommandQueueItem(functionname,parameterlist){
	this.commandQueue[this.commandItemCount] = new Array(functionname,parameterlist); // parameterlist is an array
	this.commandItemCount++;
}
function checkForm(){
	this.debug ? window.onerror = this.errorHandling:void(0);
	for(var i = 0;i<this.commandItemCount;i++){
		if(!this.execCommandQueueItem(i)){this.debug ? window.onerror = null : void(0);return false;}
	}
	this.debug ? window.onerror = null : void(0);
	return true;
}
//functions
/****************** TextboxEmpty ******************/
function textboxEmpty(formname,formItemName,errorMsg,defaultText){
	this.addCommandQueueItem(this.textboxEmptyF,arguments);
}
function textboxEmptyF(params){//formname,formItemName,errorMsg,defaultText
	var formname=params[0],formItemName=params[1],errorMsg=params[2],defaultText=params[3];
	if(defaultText='undefined'){
		defaultText='';
	}
	
	var strValue = document.forms[formname].elements[formItemName].value.length>0 && document.forms[formname].elements[formItemName].value!=defaultText;
	if (! strValue){	
		alert(errorMsg);
		document.forms[formname].elements[formItemName].focus();
		return false;
	}
	else{
		return true;
	}
}
/****************** TextboxEmail ******************/
function textboxEmail(formname,formItemName,errorMsg){	
	this.addCommandQueueItem(this.textboxEmailF,arguments);
}

function textboxEmailF(params){
	var formname=params[0],formItemName = params[1],errorMsg = params[2];
	var strValue = document.forms[formname].elements[formItemName].value;
	if (! strValue.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) {	
		alert(errorMsg);
		document.forms[formname].elements[formItemName].focus();
		return false;
	}else{
		return true
	}
}
/****************** RadioChecked ******************/
function radioChecked(formName,formItemName,errorMsg){
	this.addCommandQueueItem(this.radioCheckedF,arguments);
}

function radioCheckedF(params){//formName,formItemName,errorMsg 
       var field = document.forms[params[0]].elements[params[1]]; 
       if (!field.length){ 
           if(field.checked){
		   		return true;
			}
			else{
				 alert(params[2]);
				 return false;
			}
       }
       for(var i = 0; i < field.length; i++){if(field[i].checked) return true;} 
       alert(params[2]); 
       return false; 
  }
/****************** CheckboxChecked ******************/
function checkboxChecked(formName,formItemName,errorMsg){
	this.addCommandQueueItem(this.radioCheckedF,arguments);
}
/****************** DropdownSelected ******************/
function dropdownSelected(formName,formItemName,errorMsg){
	this.addCommandQueueItem(this.dropdownSelectedF,arguments);
}
function dropdownSelectedF(params){//formName,formItemName,errorMsg
	var field = document.forms[params[0]].elements[params[1]];
	if(field.selectedIndex==0){
		alert(params[2]);
		field.focus();
		return false;
	}
	return true;
}
/****************** compareValues ******************/
function compareValues(formName,formItemName1,formItemName2,errorMsg){
	this.addCommandQueueItem(this.compareValuesF,arguments);
}

function compareValuesF(params){ //formName,formItemName1,formItemName2,errorMsg,resetfields;
	reset=params[4];
	if(!(document.forms[params[0]].elements[params[1]].value == document.forms[params[0]].elements[params[2]].value)){
		alert(params[3]);
		if(reset){
			document.forms[params[0]].elements[params[1]].value ='';
			document.forms[params[0]].elements[params[2]].value ='';
		}
		document.forms[params[0]].elements[params[1]].focus();
		return false;
	}
	return true;
}

function compareValuesZero(formName,formItemName1,formItemName2,errorMsg){
	this.addCommandQueueItem(this.compareValuesZeroF,arguments);
}

function compareValuesZeroF(params){ //formName,formItemName1,formItemName2,errorMsg,resetfields;
	reset=params[4];
	if(document.forms[params[0]].elements[params[1]].value == document.forms[params[0]].elements[params[2]].value){
		if (document.forms[params[0]].elements[params[2]].value==0) {
			alert(params[3]);
			document.forms[params[0]].elements[params[1]].focus();
			return false;
		}
	} else {
		return true;
	}
}
/****************** characterCheck ******************/
function characterCheck(formName,formItemName1,validCharacterList,errorMsg)
{
    this.addCommandQueueItem(this.characterCheckF,arguments);
}
function characterCheckF(param)//formName,formItemName,validCharacterList,errorMsg
{
    var Chars = param[2];
	var formFieldvalue = document.forms[param[0]].elements[param[1]].value;
	for (var i = 0; i < formFieldvalue.length; i++)
	{
		if (Chars.indexOf(formFieldvalue.charAt(i)) == -1)
		{
			alert(param[3]);
			document.forms[param[0]].elements[param[1]].focus();
			return false;
		}
	}
	return true;
}

function fieldLength(formName,formItemName1,fromlength,tolength,errorMsg)
{
    this.addCommandQueueItem(this.fieldLengthF,arguments);
}

function fieldLengthF(param)//formName,formItemName,fromlength,tolength,errorMsg
{
	var formFieldvalue = document.forms[param[0]].elements[param[1]].value;
	if(param[2]< formFieldvalue.length || param[3]> formFieldvalue.length){
		alert(param[4]);
		document.forms[param[0]].elements[param[1]].focus();
		return false;
	}
	return true;
}
