function ErrorCheck (){this.fields = new Array();}
//0=alphanumeric
//1=numeric
//2=email
//3=text
//4=state drop down
ErrorCheck.prototype.addField = function (name, type){
	this.fields.push (new fieldObj (name,type));	
}


//Declares a custom object to couple
//field name and field type:
//0=alphanumeric
//1=numeric
//2=email
//3=text
//4=state drop down
function fieldObj (name,type){ 
	this.name=name;
	this.type=type;
}

//submitForm 
//Arguments: form - the form object
//Returns: pop-up window if errors encountered in form.
// Checks form using askextension's form fields
ErrorCheck.prototype.submitForm = function (form){
	var elements=this.fields;
	var message="The following errors were found, please fix:\n\n";
	var i=0;
	var errCode=0;
	var error=false;
	for (i=0; i<elements.length; i++){
		errCode=this.validValue (elements[i]);
		if (errCode==0){
			message=message+"   *"+elements[i].name+" is empty."+"\n";
			error=true;	
		}else if (errCode==-1){
			message=message+"   *"+elements[i].name+" contains invalid characters."+"\n";
			error=true;	
		}else if (errCode==-2){
			message=message+"   *"+elements[i].name+" has invalid format."+"\n";
			error=true;	
		}
	}
	//user didn't enter the information correctly

	if (error){
		alert (message);
		return false;
	}
	else //we are ready to submit
		form.submit();
		
}
//validValue
//Arguments: obj (form field object to scrutinize with value and name variables)
//Returns:  0 for empty field
//         -1 for invalid characters in field
//         -2 for invalid format
//          1 for correct format and not empty field (good entry.) 
//function that attempts to detect empty and faulty formatting of the fields.
ErrorCheck.prototype.validValue = function (obj){
	obj.ref = document.getElementById (obj.name);
	var i=0;
	var j=0;
	for (i=0; i<this.fields.length; i++){
		if (obj.name==this.fields[i].name){
			if (obj.ref.value.length==0){
				return 0;
			}
			
			if (this.fields[i].type==1){ //numeric
				for (j=0; j<obj.ref.value.length; j++){
					if (isNaN (obj.ref.value.charAt (j)) && 
				            obj.ref.value.charAt (j) != '-') //checking for '-' in case of a long zip
						return -1;
				}
			}else if (this.fields[i].type==2){ //email
				//checking for xxx@xxx.xxx format.
				if ((obj.ref.value.indexOf('\@')!=-1) &&
				    (obj.ref.value.indexOf('\.')!=-1)){ //we have both an @ and a .
						//if (the . is after the @) that's correct syntax
						if (obj.ref.value.lastIndexOf ('\@') < obj.ref.value.lastIndexOf ('\.'))
							return 1;
						else
							return -2;
				//is there an @ or a . (then invalid format)
				}else if (obj.ref.value.indexOf('\.')!=-1 || obj.ref.value.indexOf('\@')!=-1 ||
						  obj.ref.value.length>0){
					return -2;
				//email has to be empty if nothing was cought
				}else{ 
					return 0;
				}
			}else if (this.fields[i].type==4){//date drop down
				if (obj.ref.value=="AA")
					return 0;
			}
			
		}
	}
	return 1;
}
