// This function will verify all controls from a given form
function checkError(theForm) {

	var theElements = theForm.elements;
	var theKey;
	var theError,goDown;
	
	var winObject;
	
	theError="";
	for (k=1;k<=theElements.length;k++) {
		theKey=theElements[k-1].name+"_JS";
		goDown=false;
		if (theForm[theKey] !=null) {
			theArray=theForm[theKey].value.split("}");
			// if Campul is empty and is Mandatory 
			if (theArray[0].toLowerCase()=="ma" && theElements[k-1].value=="") {
				goDown=true;
				theError=theError+"Campul "+theForm[theElements[k-1].name].name+" nu poate sa fie gol!\n";
			}
			// if Campul is empty and is optional
			if ((theArray[0].toLowerCase()=="op" && theElements[k-1].value=="") && goDown==false) 
				goDown=true;
			// if Campul is number then is verified like a string with 0..9 and ,.
			if (theArray[1].toLowerCase()=="integer" && goDown==false) {
				goDownNumber=true;
				for (i=1;i<=theElements[k-1].value.length;i++) {
					if ((theElements[k-1].value.charAt(i-1)<"0" || theElements[k-1].value.charAt(i-1)>"9") && theElements[k-1].value.charAt(i-1)!="." && theElements[k-1].value.charAt(i-1)!=",") {
						goDown=true;
						goDownNumber=false;
						theError=theError+"Campul "+theForm[theElements[k-1].name].name+" trebuie sa fie un numar!\n";
						break;
					}
				}
				if (goDownNumber=true) {
					/*if (theElements[k-1].value.charAt(0)=="." || theElements[k-1].value.charAt(0)==",") {
						theError=theError+"Campul "+theForm[theElements[k-1].name].name+" must be an number !"+String.fromCharCode(13);
						goDownNumber=false;
					}*/
					/*if (goDownNumber==true) {
						if (theElements[k-1].value.charAt(theElements[k-1].value.length-1)=="." || theElements[k-1].value.charAt(theElements[k-1].value.length-1)==",") {
							theError=theError+"Campul "+theForm[theElements[k-1].name].name+" must be an number !"+String.fromCharCode(13);
							goDownNumber=false;
						}
					}*/
					/*theNumberArray1=theElements[k-1].value.split(".");
					theNumberArray2=theElements[k-1].value.split(",");
					if ((theNumberArray1.length>2 || theNumberArray2.length>2) && goDownNumber==true) {
						goDownNumber=false;
						goDown=true;
						theError=theError+"Campul "+theForm[theElements[k-1].name].name+" must be an number !"+String.fromCharCode(13);
					}
					if (goDownNumber==true && theNumberArray1.length==2 && theNumberArray2.length==2) {
						goDown=true;
						theError=theError+"Campul "+theForm[theElements[k-1].name].name+" must be an number !"+String.fromCharCode(13);
					}*/
				}
			}
			/*if ((theArray[1].toLowerCase()=="integer" && isNaN(theElements[k-1].value)==true) && goDown==false) {
				goDown=true;
				theError=theError+"Campul "+theForm[theElements[k-1].name].name+" must be an number !"+String.fromCharCode(13);
			}*/
			// if Campul is a date then is verified only the format:mm/dd/yyyy
			if ((theArray[1].toLowerCase()=="date") && goDown==false) {
				goDown=true;
				//goDownDate=false;
				/*if ((theElements[k-1].value.charAt(2)!="/") || (theElements[k-1].value.charAt(5)!="/")) {
					goDownDate=true;
					theError=theError+"Campul "+theForm[theElements[k-1].name].name+" contain an invalid date !"+String.fromCharCode(13);
				}*/
				var splitDate=theElements[k-1].value.split("/");
				if (splitDate.length!=3) {
					splitDate=theElements[k-1].value.split(".");
					if (splitDate.length!=3) {
						splitDate=theElements[k-1].value.split("-");
					}
				}
				if (splitDate.length!=3) {
					theError=theError+"Campul "+theForm[theElements[k-1].name].name+" contine o data invalida!\n";
				}
				else {
					if ((isNaN(splitDate[0])==true) || (isNaN(splitDate[1])==true) || (isNaN(splitDate[2])==true))
						theError=theError+"Campul "+theForm[theElements[k-1].name].name+" contine o data invalida!\n";
				}
			}
			// if Campul is an email then must include '@' and '.'
			if ((theArray[1].toLowerCase()=="email") && goDown==false) {
				goDown=true;
				goDownEmail=false;
				var theEmailArray=theElements[k-1].value.split(",")
				for (i=1;i<=theEmailArray.length;i++) {
					// test if exists "@" and "."
					if ((theEmailArray[i-1].indexOf("@")==-1) || (theEmailArray[i-1].indexOf(".")==-1)) {
						goDownEmail=true;
						theError=theError+"Campul "+theForm[theElements[k-1].name].name+" contine un email invalid!\n";
						break;
					}
					// test if the first char is "@" or "."
					if (((theEmailArray[i-1].charAt(0)=="@") || (theEmailArray[i-1].charAt(0)==".")) && goDownEmail==false) {
						goDownEmail=true;
						theError=theError+"Campul "+theForm[theElements[k-1].name].name+" contine un email invalid!\n";
						break;
					}
					// test if the last char is "@" or "."					
					if (((theEmailArray[i-1].charAt(theEmailArray[i-1].length-1)=="@") || (theEmailArray[i-1].charAt(theEmailArray[i-1].length-1)==".")) && goDownEmail==false) {
						goDownEmail=true;
						theError=theError+"Campul "+theForm[theElements[k-1].name].name+" contine un email invalid!\n";
						break;
					}
					// test if numbers of '@' is 1 on the string
					theEmail=theEmailArray[i-1].split("@");
					if ((theEmail.length!=2) && goDownEmail==false) {
						goDownEmail=true;
						theError=theError+"Campul "+theForm[theElements[k-1].name].name+" contine un email invalid!\n";
						break;
					}
					//test if index of "." <> index of "@" + 1 and <>index of "@" -1
					if (goDownEmail==false) {
						if ((theEmailArray[i-1].indexOf("@")==theEmailArray[i-1].indexOf(".")-1) || (theEmailArray[i-1].indexOf("@")==theEmailArray[i-1].indexOf(".")+1)) {
							goDownEmail=true;
							theError=theError+"Campul "+theForm[theElements[k-1].name].name+" contine un email invalid!\n";
							break;
						}
					}
					// test if index of "." <> index of "." + 1
					if ((theEmailArray[i-1].charAt((theEmailArray[i-1].indexOf(".")))==theEmailArray[i-1].charAt((theEmailArray[i-1].indexOf(".")+1))) && goDownEmail==false) {
						theError=theError+"Campul "+theForm[theElements[k-1].name].name+" contine un email invalid!\n";
						break;
					}
				}
			}
			// if Campul is a credit card...
			// if Campul is string and must test the min numbers or chars and the max numbers of chars
			if ((theArray[1].toLowerCase()=="string") && goDown==false) {
				goDown=true;
				if (theArray.length>=3) {
					if (theArray[2]!="") {
						// must test the min numbers of chars
						if (theElements[k-1].value.length<theArray[2])
							theError=theError+"Campul "+theForm[theElements[k-1].name].name+" trebuie sa aiba minim "+theArray[2]+" caractere!\n";
					}
					if (theArray[3]!="") {
						// must test the min numbers of chars
						if (theElements[k-1].value.length>theArray[3])
							theError=theError+"Campul "+theForm[theElements[k-1].name].name+" trebuie sa aiba maxim "+theArray[3]+" caractere!\n";
					}					
				}
			}
		}
	}
	if (theError !="") {
		alert (theError);
		return false;
	} else{

for (i = 0; i < theForm.length; i++) {
var tempobj = theForm.elements[i];
if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
tempobj.disabled = true;             }


		return true;
		}
}
