<!--
/*=====================================================================================================
function 
function assumes name_number for 
arguments:  
			errorMsg: will hold the messages that will be alerted when validation failed
			inputNames: will hold the html form input names(assumes all input names are unique)
			checkType: specify the validation type 
				values 4 checkType: 0:null, -1:not empty, -2:a number, -3:must match previous inputNames array, -4:email address, -5: must be selected,  >0: is max length
			repeat:  to use multiple similar inputs for ex: name_1, name_2, name_3, the value should be the number of repeat or false/null
			required input: is the field that must not be empty in order to check the rest

example:
var inputNames = new Array('name', 'number', 'weight');
var errorMsgs = new Array('Item name required', 'Item number is required', 'weight must be a number');
var checkType = new Array(-1, -1, -2);

<form name="addItems2Cat" action="xSCCP-inventAddItem2.php" method="post" enctype="multipart/form-data" onsubmit="
return validateFormRepeat(inputNames, errorMsgs, checkType, document.getElementsByName('num2Add')[0].value, 'name');
">
*/

function validateForm(inputNames, errorMsg, checkType){
	if(validateFormValidInput(inputNames, errorMsg, checkType) == false){
		return false;
	}
	
	var failed = false;
	for(var i=0; i<errorMsg.length; i++){
	
		var theValue = document.getElementsByName(inputNames[i])[0].value;
		
		switch(checkType[i]){
			case 0:
				continue;
			break;
			
			case -1:
				if(theValue.length == 0)
					failed = true;
			break;
			
			case -2:
				if(theValue != parseFloat(theValue))
					failed = true;
			break;
			
			case -3:
				if(theValue != document.getElementsByName(inputNames[i-1])[0].value)
					failed = true;
			break;
			
			case -4:
				if( (theValue.indexOf('@')==-1) || (theValue.indexOf('.')==-1) )
					failed = true;
			break;
			
			case -5:
				if(document.getElementsByName(inputNames[i])[0].checked == false)
					failed = true;
			break;
			
			default :
				if(theValue.length > checkType[i])
					failed = true;
		}
		
		if(failed == true){
			alert(errorMsg[i]);
			document.getElementsByName(inputNames[i])[0].focus();
			return false;
		}
	}
}


function validateFormRepeat(inputNames, errorMsg, checkType, repeat, requiredInput){
	var failed = false;
	
	if(validateFormValidInput(inputNames, errorMsg, checkType) == false){
		return false;
	}
	if(!repeat>0){
		alert("validateFormRepeat() error, repeat must be greater than 0");
		return false;
	}
	if(requiredInput==null){
		alert("validateFormRepeat() error, requiredInput is missing");
		return false;
	}
	for(var j=0; j<repeat; j++){
		if(document.getElementsByName(requiredInput+'_'+j)[0].value.length==0){	//required input failed, dont check the rest
			continue;
		}
		
		for(var i=0; i<errorMsg.length; i++){
			var theValue = document.getElementsByName(inputNames[i]+'_'+j)[0].value;
			switch(checkType[i]){
				case 0:
					continue;
				break;
				
				case -1:
					if(theValue.length == 0)
						failed = true;
				break;
				
				case -2:
					if(theValue != parseFloat(theValue))
						failed = true;
				break;
				
				case -3:
					if(theValue != document.getElementsByName(inputNames[i-1]+'_'+j)[0].value)
						failed = true;
				break;
				
				case -4:
					if( (theValue.indexOf('@')==-1) || (theValue.indexOf('.')==-1) )
						failed = true;
				break;
				
				default :
					if(theValue.length > checkType[i])
						failed = true;
			}
			if(failed == true){
				alert(errorMsg[i]);
				document.getElementsByName(inputNames[i]+'_'+j)[0].focus();
				return false;
			}
		}
	}
}



function validateFormValidInput(inputNames, errorMsg, checkType){
	
	//validate arguments
	if( (errorMsg == null) || (inputNames == null) || (checkType == null) ){
		alert("inputs for validateForm() is missing");
		return false;
	}
	if (errorMsg.constructor.toString().indexOf("Array") == -1){
		alert("input for validateForm() is wrong, errorMsg is not an array");
		return false;
	}
	if (inputNames.constructor.toString().indexOf("Array") == -1){
		alert("input for validateForm() is wrong, inputNames is not an array");
		return false;
	}
	if (checkType.constructor.toString().indexOf("Array") == -1){
		alert("input for validateForm() is wrong, checkType is not an array");
		return false;
	}
	if( (errorMsg.length != inputNames.length) || (errorMsg.length != checkType.length) ){
		alert("input for validateForm() is wrong, array length dosnt match");
		return false;
	}
	if(errorMsg.length == 0){
		alert("input for validateForm() has not been set");
		return false;
	}
	if(checkType[0] == -3){
		alert("input for validateForm() is wrong, the first element of checkType can not be -3");
		return false;
	}
		return true;
}