addNamespace("KY.Web.Validation");

Object.extend(KY.Web.Validation, {
	Show: function(obj) {
		var text = "";
		//DebugWindow('KY.Web.Validatio:Show:', text);
		var iLeft = 0;
		var iTop = 0;
		if (!obj.getAttribute("validMsg")) { return; }
		text = obj.getAttribute("validMsg");
		iLeft = MS.Position.getLeft(obj) + obj.offsetWidth + 40;
		iTop = MS.Position.getTop(obj) + 10;
		this.Set(text, iLeft, iTop);
	},
	ShowMsg: function(obj, text, position, offset, offsetHeight) {
		if(typeof offset=='undefined') offset = 40;
		if(typeof offsetHeight=='undefined') offsetHeight = 0;
		if(typeof position=='undefined') position = 0; //0:right, 1:bottom, 2:left, 3:top
		var iLeft = 0;
		var iTop = 0;
		var sMsg = text!=''?text:!obj.getAttribute("validMsg")?obj.getAttribute("validMsg"):'';
		switch(position){
			case 1: //bottom
				iLeft = MS.Position.getLeft(obj);
				iTop = MS.Position.getTop(obj) + MS.Position.getBounds(obj).height + offset;
				break;
			case 2: //left
				iLeft = MS.Position.getLeft(obj) - MS.Position.getBounds($("tooltip")).width - offset;
				iTop = MS.Position.getTop(obj) + MS.Position.getBounds(obj).height;
				break;
			case 3://top
				iLeft = MS.Position.getLeft(obj);
				iTop = MS.Position.getTop(obj) - MS.Position.getBounds($("tooltip")).height - offset;
				break;
			default://right
				iLeft = MS.Position.getLeft(obj) + obj.offsetWidth + offset;
				iTop = MS.Position.getTop(obj) + offsetHeight;
		}
		this.Set(sMsg, iLeft, iTop);
	},
	ShowImg: function(obj, valid) {
		if (!$(obj.id + "_valImage") && !valid) {
			var text = "";
			if (obj.getAttribute("validMsg")) text = obj.getAttribute("validMsg");
			var div = document.createElement("div");
			div.id = obj.id + "_valImage";
			div.style.visibility = "hidden";
			div.innerHTML = "<img src='/resim/tooltip/hint2.gif' border='0' alt='"+text+"'/>";
			var par1 = MS.Position.getParent(obj);
			if (par1) {
				par1.appendChild(div);
			}
			else {
				document.body.appendChild(div);
			}
		}
		if ($(obj.id + "_valImage"))
		{
			iLeft = MS.Position.getLeft(obj) + obj.offsetWidth + 5;
			iTop = MS.Position.getTop(obj) + ((obj.offsetHeight - 20) / 2);
			//var rLoc = MS.Position.getLocation(obj);
			//iTop = rLoc.top + ((obj.offsetHeight - 20) / 2);
			MS.Position.setLocation($(obj.id + "_valImage"), {left:iLeft,top:iTop});
		}
		if (valid && $(obj.id + "_valImage")) { $(obj.id + "_valImage").style.visibility = "hidden"; }
		else if (!valid) { $(obj.id + "_valImage").style.visibility = "visible"; }
	},
	Validate: function(obj) {
		var sFunc = "";
		var aFunc = null;
		if (!obj.getAttribute("validFunctions")) { return true; }

		sFunc = obj.getAttribute("validFunctions");

		if (sFunc == "") { this.Hide(); return true; }
		aFunc = eval(sFunc);

		var i = 0;
		for (i = 0; i < aFunc.length; i++ ) {
			var sResult = eval(aFunc[i].replace("this", "$('" + obj.id + "')"));
			this.ShowImg(obj, sResult == "" ? true : false);
			if (sResult != "") {
				this.ShowMsg(obj, sResult);
				return sResult;
			}
		}

		this.Hide();
		return "";
	},
	ValidateForm: function(obj, WithPost, ReturnScript) {
		var b;
		var sErrHtml = "";
		var i = 0;
		var f = document.forms[obj];
		var par = "";

		if (!f) { return; }
		if (f.tagName.toLowerCase() != "form") { return; }
		b = true;
		sErrHtml = "<table width='100%' border='0' cellspacing='0' cellpadding='0' style='border:1 #990000 solid;background-color:#FFFFFF;color:red;font-weight:bold;'>"
		var aObjs = Array();
		for (i = 0; i < f.elements.length; i++) {
			if (f.elements[i].name == "parentObj") {
				par = f.elements[i].value;
			}
			if (!f.elements[i].getAttribute("validFunctions")) { continue; }
			var s = this.Validate(f.elements[i]);
			if (s != "") {
				aObjs.push(f.elements[i].id);
				sErrHtml += "<tr><td height='20px' width='20px'><img src='/resim/tooltip/hint2.gif' border='0' alt=''/></td><td style='border:1 #990000 solid;background-color:#FFFFFF;color:red;font-weight:bold;'>"+ s + "</td></tr>";
				b = false;
			}
		}
		sErrHtml += "</table>";
		if (!b) {
			if ($("tooltip_summary")) {
				$("tooltip_summary").innerHTML = sErrHtml;
			}
		}
		else {
			if ($("tooltip_summary")) {
				$("tooltip_summary").innerHTML = "";
			}
		}
		this.Hide();
		if (b && WithPost) {
			PostNavigateForm(obj, par, par == "" ? false : true, ReturnScript);
			return false;
		}

		for (i = 0; i < aObjs.length; i++) {
			this.ShowImg($(aObjs[i]), false);
		}

		return b;
	},
	Hide: function() { 
		this.Set("", -1000, -1000); 
	},
	Set: function(text, x, y) {
		var tipContent = $("tooltipcontent");
		tipContent.innerHTML = text;

		var tip = $("tooltip");
		tip.style.zIndex = 5900;
		var tipEnd = $("tooltipend");
		tip.style.left = x;
		tip.style.top = y - (MS.Position.getTop(tipEnd) + 3 - MS.Position.getTop(tip))/2;
		MS.Position.setLocation(tip, {left:x,top:y - (MS.Position.getTop(tipEnd) + 3 - MS.Position.getTop(tip))/2});
	}

}, false);

addNamespace("KY.Web.Validation.Function");
Object.extend(KY.Web.Validation.Function, {
	StrLen: function(obj, minLen, maxLen, text) {
		if (obj == null) { return text; }
		return (obj.value.length >= minLen && obj.value.length <= maxLen ? "" : text);
	},
	PasswordMatch: function(obj1, obj2, text) {
		return (obj1.value == obj2.value ? "" : text);
	},
	RequiredCheckBox: function(obj, text) {
		if (obj == null) { return text; }
		if (obj.type.toLowerCase() != "checkbox") { return text; }
		return (obj.checked ? "" : text);
	},
	RequiredObject: function(obj, defaultValue, text) {
		if (obj == null) { return text; }
		return (obj.value == defaultValue ? text : "");
	},
	RequiredTextBox: function(obj, text) {
		if (obj == null) { return text; }
		if (obj.type.toLowerCase() != "text" && obj.type.toLowerCase() != "password") { return text; }
		return (obj.value != "" ? "" : text);
	},
	Between: function (obj, minValue, maxValue, text) {
		if (obj == null) { return text; }
		return (eval(obj.value) >= minValue && eval(obj.value) <= maxValue ? "" : text);
	},
	RegEx: function (obj, regex, text) {
		var re = new RegExp(regex);
		return (re.test(obj.value) ? "" : text);
	},
	RequiredEmail: function(obj, text) {
		var re = new RegExp('^ *(([^<>()[\\]\\.,;:@"\\s]+(\\.[^<>()[\\]\\.,;:@"\\s]+)*)|(".+"))@((\\[([0-9]{1,3}\\.){3}[0-9]{1,3}\\])|(([a-zA-Z0-9]+[\\.\\-]?)*[a-zA-Z0-9]\\.(([a-zA-Z]{2,3})|(aero|coop|info|museum|name|mobi|arpa|asia|jobs|travel|post)))) *$');
		return (obj.value.match(re) != null ? "" : text);
	}
}, false);


