//**************************************************
var fieldName;

function checkForm(collElements,arrReqElements,sLang, arrFieldNames){
	//collElements - collection of all controls in a given form
	//arrReqElements  - array of names of all required controls in a given form
	var validationRes;
	validationRes = validateForm(collElements,arrReqElements);
		
	var CrLf = String.fromCharCode(10, 13);
	var sAlertMsg;
		
	if (validationRes == 1)	{
		if (sLang == 'en')
			sAlertMsg = 'Please, enter your ' + arrFieldNames[fieldName] + '.';
		if (sLang == 'bg')
			sAlertMsg = 'Please, enter your ' + arrFieldNames[fieldName] + '.';
		window.alert(sAlertMsg);
		return false;
	}
	if (validationRes == 2){
		if (sLang == 'en')
			window.alert('Please enter a valid e-mail address.'+ CrLf + 'For example: office@qbe-sofia.bg');
		if (sLang == 'bg')
			window.alert('Please enter a valid e-mail address.'+ CrLf + 'For example: office@qbe-sofia.bg');
		return false;
	}
		
	if (validationRes == 3){
		return true;
	} 
}  
//***********************************************
function validateForm(collElements,arrReqElements){
	var i;
	for (i = 0; i < collElements.length; i++){
		var el = collElements[i];
		var name = el.name;
		var val = el.value;
			
		if (el.type == 'text' || el.type == 'textarea' || el.type == 'password'){
			if (val != null && isEmptyField(val) && (fieldName = isInArr(name, arrReqElements)) != -1){
				el.focus();
				return 1;	
			}
			if (name.search(/Email/)!=-1 && !isEmptyField(val) && !isEmail(val) && (fieldName = isInArr(name, arrReqElements)) != -1){	
				el.focus();
				return 2;
			}
		}
		if (el.type.search(/select/)!=-1 && isEmptyField(el.options[el.selectedIndex].text) && (fieldName = isInArr(name, arrReqElements)) != -1){
			el.focus();			
			return 1;	
		} 
	}	
	return 3;				
  }
//***********************************************	 
function isEmptyField(str){
	var k;
	for (k = 0; k < str.length; k++){
	    var chr = str.substring(k, k + 1);
		if (chr != ' '){
		  return false;
		}
	}
	return true;
 }
//***********************************************	  
function isInArr(name, Arr){
	if (Arr == '') return false; 
	for (var j = 0; j < Arr.length; j++){
		var name1 = Arr[j];
		if (name == name1){
			return j;
		}
	}	
	return -1;		
}
//***********************************************	  	  
function isEmail(strEmail){
	if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else	
		return false;
} 
//************************************************