/******************************************************/
/*** Client Side JavaScript Form Validation Library ***/
/***              Copyright 2006 MBG                ***/
/******************************************************/

/******************************************************/
/*** Generic functions ***/
/******************************************************/

var regexEmail = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";

/* trim leading and trailing white spaces */
function trimSpaces(s) {
	s = s.replace(/^\s*/,'').replace(/\s*$/, '');
	return s;
}

/* information for a field to validate */
function ValidationInfo(ctlID, title, isRequired, regex) {
	this.ctlID = ctlID;
	this.title = title;
	this.isRequired = (isRequired != null) && isRequired;
	this.regex = regex;
}

function Validator_AddField(ctlID, title, isRequired, regex) {
	this.fields[this.fieldCount] = new ValidationInfo(ctlID, title, isRequired, regex);
	this.fieldCount++;
}

function Validator_AddCompareField(ctlID1, title1, ctlID2, title2) {
	var comp = new Object();
	comp.ctlID1 = ctlID1;
	comp.title1 = title1;
	comp.ctlID2 = ctlID2;
	comp.title2 = title2;

	this.compareFields[this.compareFieldCount] = comp;
	this.compareFieldCount++;
}

// Validates the fields and returns the error string
// - returns "" if validation succeeds
function Validator_GetErrors() {
	var errors = "";

	// validate each control
	for (var i in this.fields) {
		var field = this.fields[i];
		var ctl = document.getElementById(field.ctlID);
		if (ctl == null) {
			// try to get indexed control
			var ctl2 = document.getElementById(field.ctlID + "_0");
			if (ctl2 != null &&
				ctl2.tagName == "INPUT" &&
				ctl2.type == "radio") {
				ctl = ctl2;
			} else 	{
				// control not found
				errors += "Control not found: " + field.ctlID + "\n";
				continue;
			}
		}

		if (field.isRequired
			&& ctl.tagName == "INPUT"
			&& ctl.type == "radio") {
			// look for a required radio button list
			var ctls = document.getElementsByName(field.ctlID);
			if (ctls.length > 0) {
				var hasSelected = false;
				for (var j = 0; j < ctls.length; j++) {
					if (ctls[j].checked) {
						hasSelected = true
						break;
					}
				}

				if (!hasSelected)
					errors += "* Lütfen '" + field.title + "' alanında bir seçim yapınız!\n";
			}

			continue;
		}

		var value = ctl.value;

		// autotrim control value
		if (this.autoTrimValues) {
			value = trimSpaces(value);
			ctl.value = value;
		}

		if (field.isRequired && value == "") {
			if (ctl.tagName == "SELECT") // listbox or dropdown
				errors += "* Lütfen '" + field.title + "' alanında bir seçim yapınız!\n";
			else // default to textbox
				errors += "* Lütfen '" + field.title + "' alanını boş bırakmayınız!\n";
		} else if (!field.isRequired && value == "") {
			//
		} else if (field.regex != null) {
			var re = new RegExp("^" + field.regex + "$","i");
			if (value.match(re) == null)
				errors += "* Lütfen '" + field.title + "' alanına doğru bir değer giriniz!\n";
		}
	}

	// validate fields to be compared
	for (var i in this.compareFields) {
		var field = this.compareFields[i];
		var ctl1 = document.getElementById(field.ctlID1);
		var ctl2 = document.getElementById(field.ctlID2);

		// we assume that both fields are required.
		// so we do not give an error if one field is empty.
		if (ctl1.value != "" &&
			ctl2.value != "" &&
			ctl1.value != ctl2.value) {
			errors += "* Lütfen '" + field.title1 + "' ve '" + field.title2 + "' alanlarına aynı değeri giriniz!\n";
		}
	}

	return errors;
}

// Validates the fields
// - returns true on success
// - returns false on failure and displays the errors in an alert
function Validator_Validate() {
	var errors = this.GetErrors();

	if (errors == "") {
		return true;
	} else {
		alert(errors);
		return false;
	}
}

/* validator object */
function Validator() {
	// list of fields to validate
	this.fields = new Array();
	this.fieldCount = 0;

	// list of compare validators
	this.compareFields = new Array();
	this.compareFieldCount = 0;

	// set this to false to disable automatic trimming of control values
	this.autoTrimValues = true;

	this.AddField = Validator_AddField;
	this.AddCompareField = Validator_AddCompareField;
	this.Validate = Validator_Validate;
	this.GetErrors = Validator_GetErrors;
}

