dojo.require("dijit.Dialog");

// handles validation of form and inputs
function calculator_helper(calcForm, validationRules, onchangeValidation){

	this.validateOnchange = function(){		
		// run the validations
		for (var i = 0; i<onchangeValidation.length; i++) {
			rule = onchangeValidation[i];
			if (rule.type != "Onchange") continue;
				eval(rule.expression);
		}
	};


	this.fieldValidate = function() {
	
		// a list of fields to ignore
		emptyFields = new Array();

		// create javascript variables for each of the form elements
		for (var i=0; i<calcForm.elements.length; i++) {
		
			input = calcForm.elements[i];
			name = calcForm.elements[i].name;
			widget = dijit.byId(name);
			
			if (name == "" || widget == undefined) continue;
			if (input.value == "") {
				emptyFields.push(name);
				eval("var " + name + " = undefined;");
				continue;
			}
			
			if (0 == widget.getValue() || '' != widget.getValue()){
				var tempStr = "var " + name + "=";
				var temp1 = "'" + widget.getValue()+"';";
				if (!isNaN(widget.getValue())){
					temp1 = widget.getValue()+";";
				}
				tempStr = tempStr + temp1;				
				eval(tempStr);
			}
		}
		
		// run the validations
		for (var i = 0; i<validationRules.length; i++) {
			rule = validationRules[i];
			if (rule.type != "NullableExpression" && !canUse(rule.expression, emptyFields)) continue;
		  	if (!eval(rule.expression)){
		  		alert(rule.label);
		  		return false;
		  	}
		}
		
		return true;			
	};
	
	this.validate = function() {
	
		// custom validate all the fields		
		if (!this.fieldValidate()) {
			return;
		}
		
		// simple form validation based on dojo
		widget = dijit.byId(calcForm.id);	
		if (!widget.isValid()) {
			errors = "";
			for (var i=0; i<calcForm.elements.length; i++) {
				el = form.elements[i];
				if (el == undefined || el.name == "") continue;
				di = dijit.byId(el.name);
				if (!di) continue;
				try { 
					if (!di.isValid()) {
						di.displayMessage(di.getErrorMessage(false));
						di.focus();
					} 
				}
				catch(e) {}
			}
		
			alert("Please correct any errors before submitting.");
			return;
		}
		
		// populate dojo values to form
		for (var i=0; i<calcForm.elements.length; i++) {
			el = form.elements[i];
			if (el == undefined || el.name == "") continue;
			di = dijit.byId(el.name);
			if (!di) continue;
			el.value = di.getValue();
		}
		
		// fix for dojo issue
		createAndSubmit(calcForm);
	
	};
	

}

// creates a new form on the fly and submits it
function createAndSubmit(calcForm) {
	
	formName = "form_" + new Date().getTime();
	
	form = document.createElement("FORM");
	form.name = formName;
	form.id = formName;
	form.method = calcForm.method;
	form.action = calcForm.action;
	document.body.appendChild(form);
	
	for (var i=0; i<calcForm.elements.length; i++) {
		el = calcForm.elements[i];
		if (el == undefined || el.name == "" || (el.type == "radio" && !el.checked)) continue;
		newEl = document.createElement("INPUT");
		newEl.type = "hidden";
		newEl.name = el.name;
		newEl.value = el.value;
		form.appendChild(newEl);
	}
	
	form.submit();
	
}

// helper method
function canUse(expression, emptyFields) {
	for (var i=0; i<emptyFields.length; i++) {
		if (expression.indexOf(emptyFields[i]) != -1) return false;
	}
	return true;
}

// validate constraints
function minMax(v, txt, min, max, decimals){
	if (isNaN(v)) {
		return min;
	}
	v = (v*1.0).toFixed(decimals);
	if (v > max){
		v = max;
	}	
	else{
		if (v<min){
			v = min;
		}
	}
	txt.setValue(v);
	
	// *1.0 make sure toFixed() can apply
	v=v*1.0;
	return v;
}

function onSliderChange(slider, dijitEdit, v1, decimalPoints){
	if(isNaN(v1) || v1 == "" || v1 == "NaN"){
		return;
	}
	v1 = (v1*1.0).toFixed(decimalPoints);	
	slider.setValue(v1);
	dijitEdit.setValue(v1);
	
}

