var aFields = new Array();

function validate_required(field,alerttxt){
	with (field){
		if (value==null||value==""){
			alert(alerttxt);return false;
		}else{
			return true
		}
	}
}

//Validaes form
function validate_form(thisform,aFields){
    var bool = false;
	if(aFields[0] != ""){
		if(!required(thisform,aFields[0])){
            bool = true;
			return false;
		}
	}
    if(aFields[1] != ""){
        if(!fieldNumeric(thisform,aFields[1])&&bool==false){
            bool = true;
            return false;
        }
    }
    if(aFields[2] != ""){
        if(!checkemail(thisform,aFields[2])&&bool==false){
            return false;
        }
    }
	return true;
}

//Check to see of all the required fields are filded in
function required(thisform,aFields){
	var c = aFields.length;
	for(i=0;i<c;i++){
		with(thisform){
			if (validate_required(aFields[i]['name'],aFields[i]['alert'])==false){
				aFields[i]['name'].focus();return false;
			}
		}
	}
	return true;
}

function disableField(form,fieldToEnable,currentField){
	if(document.form.currentField.value == ""){
		document.form.fieldToEnable.disabled = true;
	}else{
		document.form.fieldToEnable.disabled = false;
	}
}

function fieldNumeric(thisform,aFields){
	var c = aFields.length;
	for(i=0;i<c;i++){
		with(thisform){
			var field = aFields[i];
			if (isNumeric(aFields[i]['name'].value) == false){
				field['name'].focus();
				alert(field['alert']);
				return false;
			}else{
				return true;	
			}
		}
	}
}

//checks emails
function checkemail(thisform,aFields){
    var c = aFields.length;
    var filter=/^.+@.+\..{2,3}$/;
    for(i=0;i<c;i++){
        with(thisform){
            var email = aFields[i];
            if (filter.test(email.value)){
                email.focus();
                return true;
            }else{
                alert("Please input a valid email address!");
                return false;
            }
        }
    }
    return testresults;
}

var formFunctions = {
    getFormValues:function(id){
        var vars = {};
        //Gets all the input elements value and name from a given form.
        $('#'+id+' input').each(function(i){
            if($(this).attr('name')!=''){
                if($(this).attr('type')=='radio'){
                    if($(this).attr('checked')){
                        vars[$(this).attr('name')] = $(this).val();
                    }
                }else if($(this).attr('type')=='checkbox'){
                    if($(this).attr('checked')){
                        vars[$(this).attr('name')] = 1;
                    }else{
                        vars[$(this).attr('name')] = 0;
                    }
                }else{
                    vars[$(this).attr('name')] = $(this).val();
                }
            }
        });
        //Gets all the select elements value and name from a given form.
        $('#'+id+' select').each(function(i){
            vars[$(this).attr('name')] = $(this).val();
        });
        $('#'+id+' textarea').each(function(i){
            vars[$(this).attr('name')] = $(this).val();
        });
        return vars;
    },
    clearFormValues:function(id){
        $('#'+id+' input').each(function(i){
            if($(this).attr('type')=='button' || $(this).attr('type')=='submit'){
                //Do nothing
            }else if($(this).attr('type')=='radio'){
                $(this).attr('checked','');
            }else if($(this).attr('type')=='checkbox'){
                $(this).attr('checked','');
            }else{
                $(this).val('');
            }
           
        });
        $('#'+id+' > select').each(function(i){
            $(this).val('');
        });
        $('#'+id+' > textarea').each(function(i){
            $(this).val('');
        });
    }
}