	
	var arrValid = [];
	var arrFields = [];
	var arrValues = [];
	
	function doAllAjax(){
		alert(arrValid);
		$.each($("input, select, textarea"), function(i,v) {
			var theTag = v.tagName;
			var theElement = $(v);
			var theValue = theElement.val();
			var fieldName = theElement.attr("name")
			//alert($(v).attr("name"));
			//if($(v).focus
		    if(!inArray(fieldName, arrValid)){
				if(!inArray(fieldName, arrFields)){
					arrFields.push(fieldName);
					arrValues.push(theValue);
				}

				//doAjax(fieldName, theValue);
			}
				   
		});
			
		// Now 
		$.getJSON(
			"/override/formsajax.asp?fields=" + String(arrFields) + "&values=" + String(arrValues),
			function(result)	{
				resultSet = eval(result);
				alert(resultSet.response);
			}
		)
		
	}
	
	
	// call with 
	//   fieldName : name of field to be validated
	//   validate : type of validation number, text
	//   extras : an object with extra items e.g. max, min
	function doAjax(fieldName, value)	{

		//$('#pDebugOut').html($("#" + fieldName).val());

		var nSplice;
		  
		$.getJSON(
			"/lib/formbuster_ajax.asp?tpl=" + strXmlTpl + "&field=" + fieldName + "&value=" + $("#" + fieldName).val(),
			function(result)	{
				// Turn our Json string back to Javascript
				resultSet = eval(result);
				//alert(resultSet.err);
				/*
				if (resultSet.valid == '1') 	{	valid = true }	else	{ valid = false	};
				message = resultSet.mes;
				*/
				if (resultSet.mes=='VALID')		{
					resetValidation(fieldName);
					$('#div_' + fieldName).removeClass("error");
					$('#mes_' + fieldName).html("");
					//$('#' + fieldName).css('border-color', '#09FF3D');
					//$('#mes_' + fieldName).css('color', '#09FF3D');
					//$('#' + fieldName).css('border-width', '2px;');
					//icon = "<img src='img/good.gif' alt='OK'/>";
					pushOneValue(fieldName);
				}	else	{
					$('#div_' + fieldName).addClass("error");
					//$('#' + fieldName).parent().append (resultSet.err);
					$('#mes_' + fieldName).html(resultSet.err);
					//$('#mes_' + fieldName).css('color', 'red');
					//$('#' + fieldName).css('border-width', '2px;');
					//icon = "<img src='img/bad.gif' alt='Error'/>";
									
					spliceByValue(fieldName);
				}

				// COULD BE USEFUL!
				//$('#pDebugOut').html(String(arrValid));
				
				
			}

		) 

	}


	function ajaxValidate(ele, strValidator){
		
		doAjax(ele.name, ele.value);
	}


	// Switch off the ajax styles
	function resetValidation(fieldName)	{
		
		// Not sure it should reset at all - too much jumping around?
		
		//$('#' + fieldName).parent().css('border', 'none');
		//$('#' + 'mes_' + fieldName).html('') ;
	}


	// need to check if one box is ticked to see if others need validated
	function isThisFieldRequired(fieldToCheck, valueToLookFor)	{
		if ($('#' + fieldToCheck).checked == valueToLookFor)	{
			return '1';
		}	else	{
			return '0';
		}
	}


	// like above but with option box
	function isThisFieldRequiredOpt(fieldToCheck, valueToLookFor)	{
		if ($('#' + fieldToCheck).value == valueToLookFor)	{
			return '1';
		}	else	{
			return '0';
		}
	}
	
	
	
function spliceByValue(val, arr) {

    var idx = -1;
    for (i = 0; i < arrValid.length; i++) {
        if (arrValid[i].value == val) {
			alert(arrValid[i].value);
            idx = i;
        }
    }
	if(idx>-1)
    	arrValid.splice(idx, 1);
}

function pushOneValue(val) {

	var bPush = true;
	
    for (i = 0; i < arrValid.length; i++) {
        if (arrValid[i].value == val) {
            bPush = false;
        }
    }

	if(bPush)
    	arrValid.push(val);
}

function pushOneFieldname(val) {

	var bPush = true;
	
    for (i = 0; i < arrFields.length; i++) {
        if (arrFields[i].value == val) {
            bPush = false;
        }
    }

	if(bPush)
    	arrFields.push(val);
}


function inArray(what, where) {
    var a = false;
    for (var i = 0; i < where.length; i++) {
        if (what == where[i]) {
            a = true;
            break;
        }
    }
    return a;

}

