function IsVisible(obj){
	if (obj == null)
		return true;	
	if (obj.currentStyle.display == "none" || obj.currentStyle.visibility == "hidden")
		return false;
			
	return IsVisible(obj.parentElement)
}

function FXWB_ValidateField(source, arguments){
	try{
		arguments.IsValid = true;
		
		//*****************************************************
		var validationEnabled = true;
		if (typeof(source.validationtogglefunction) == "string"){
			eval("validationEnabled = " + source.validationtogglefunction + ";");
		}
		//*****************************************************
		
		var oSrc = document.all[source.validatedcontrol];
		if( oSrc == null) return;
		
		// check if the control is visible to validate it
		if( !IsVisible(oSrc) ) return;
		
if (validationEnabled == true){
		// validate control
		var maskID = source.id.substring(0, source.id.indexOf('_cfv'));
		var oLabel = document.all[maskID +'_lbl'];
		switch(source.controltype){
		case 'text': 
			if(oSrc.value != ''){
				switch(source.datatype){
				case 'Email':
					var rxEmail = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
					var matchesEmail = rxEmail.exec(oSrc.value);
					arguments.IsValid = (matchesEmail != null && oSrc.value == matchesEmail[0]);
				break;
				case 'DateTime':
					var oToCompare = document.all[source.controltocompare];
					if (oToCompare != null){
						if ( IsValidDate(oSrc.value) && IsValidDate(oToCompare.value) ){
							var dSrc = new Date(oSrc.value)
							var dTrg = new Date(oToCompare.value)
							arguments.IsValid = eval("(new Date('" + dSrc.toDateString() + "') " +source.operator + " new Date('" + dTrg.toDateString() + "'))")
						} else {
							arguments.IsValid = false
						}
					} else {
						arguments.IsValid = IsValidDate(oSrc.value);
					}
				break; 
				case 'Numeric':
					arguments.IsValid = IsNum(oSrc.value);
					break; 
					case 'Currency':
					if(IsNum(oSrc.value)){
						arguments.IsValid = ((oSrc.value*1) >= 0)
					}else{
						arguments.IsValid = false;
					}
				break; 
				case 'UInt':
					if(IsNum(oSrc.value)){
						if ((oSrc.value*1) >= 0){
							arguments.IsValid = !(("" + (oSrc.value * 1) + "").split(".").length > 1)
						}else{
							arguments.IsValid = false
						}
					}else{
						arguments.IsValid = false;
					}
				break; 
				case 'RegExp':
				  alert(validationexpression);
				  var rxEmail = new RegExp(validationexpression); // /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
					var matchesEmail = rxEmail.exec(oSrc.value);
					arguments.IsValid = (matchesEmail != null && oSrc.value == matchesEmail[0]);
				break;  
				default:
					arguments.IsValid = (oSrc.value != '');
				}
			}else{
				arguments.IsValid = (!source.isRequired);
			}
		break; 
		case 'select': 
			if(oSrc.options.length > 0){
				arguments.IsValid = (oSrc.options[oSrc.selectedIndex].value != source.inittalvalue);
			}else{
				arguments.IsValid = (!source.isRequired);
			}
		break; 
		case 'userpicker': 
			arguments.IsValid = !((!source.isRequired) && (oSrc.value.length > 0));
		break; 
		}
}//validationEnabled		
				
		// render controls
		var oSrcLabel = document.all[source.validatedcontrollabel];
		var oErrorMessageLabel, star;
		
		if (typeof(source.validatorerrormessagelabel) == "string") var oErrorMessageLabel = document.getElementById(findLabel(source.validatorerrormessagelabel));
		if (typeof(source.highlightstar) == "string" && source.highlightstar == "True") star = document.getElementById(source.starid);
					
		if(arguments.IsValid){
			oSrcLabel.style.color='';
			oSrcLabel.title='';
			
			if (typeof(oErrorMessageLabel) == "object" &&
					oErrorMessageLabel.innerHTML.indexOf(source.errormessage) != -1 ){
					oErrorMessageLabel.innerHTML = cut_string(oErrorMessageLabel.innerHTML,source.errormessage+"<br/>"); 
			}
			
			if (typeof(star) == "object")	star.style.color = '';
		
			setFormIsValid(true);
		
		}else{
			oSrcLabel.style.color='red';
			oSrcLabel.title=source.errormessage;
			
			if (typeof(oErrorMessageLabel) == "object" && 
					oErrorMessageLabel.innerHTML.indexOf(source.errormessage) == -1){
					oErrorMessageLabel.innerHTML = oErrorMessageLabel.innerHTML + source.errormessage+"<br/>";
			}
			
			if (typeof(star) == "object")	star.style.color = 'red';
			
			setFormIsValid(false);
				
			// move to error control
			var rng = document.body.createTextRange();
			if (rng!=null) {
				rng.moveToElementText(oSrc);
				rng.scrollIntoView();
			}
		}
	}catch(e){
		//alert(e.message);
	}
}

function findLabel(name_part){
	var labels = document.getElementsByTagName('span');
	for (i=0; i<labels.length; i++){
		if (labels[i].id.indexOf(name_part) != -1){
			return labels[i].id;
		}
	}
	return "";
}

function cut_string(source_string, string_to_cut){
	var start_pos = source_string.indexOf(string_to_cut);
	var end_pos = start_pos + string_to_cut.length;
	return_string = source_string.substring(0,start_pos) + source_string.substring(end_pos,source_string.length);
	return return_string;
}


function setFormIsValid(is_valid){
	try {
		current_form = document.forms['Form1'];
		var form_elements = document.forms['Form1'].elements;
		var fv = form_elements[form_elements.length-1]
		if (fv.name != "is_valid"){
			var e = document.createElement("INPUT") ;
			e.setAttribute("type","hidden");
			e.setAttribute("id","is_valid"); 
			e.setAttribute("name","is_valid");
			e.setAttribute("value",true);
			current_form.appendChild(e) ;
			fv = e;
		}
		if (is_valid == true && fv.value == false) return;
		fv.value = is_valid;
	} catch (ex) {}	
}