var sl_valAddUpd = null;
function sl_validateField(type, d, correctIt, additionalUpdate) {
	if (additionalUpdate) { sl_valAddUpd = additionalUpdate; }
	var r = sl_validateValue(type, d.value);
	if (type == "time" || type == "date") {
		setTimeout("sl_checkTimeSE('"+d.id.substr(0,d.id.length-5)+"')", 100);
	}
	if (correctIt) {
		d.style.backgroundColor = "";
		d.value = r.corrected;
		if (sl_valAddUpd) {
			sl_valAddUpd.f(r.corrected, sl_valAddUpd.vars);
		}
		sl_valAddUpd = null;
		return true;
	}
	d.style.backgroundColor = r.success ? "" : "#FFFF00";
	if (r.success) {
		if (r.corrected) {
			d.value = r.corrected;			
		}
	} else {
		sl_validateWarn(document.getElementById('"+d.id+"'), r.text+(r.corrected ? "<br /><br /><a href=\"javascript:;\" onclick=\"sl_validateField('"+type+"',document.getElementById('"+d.id+"'),true);\">CLICK HERE</a> to use the recommended value:<br />"+r.corrected : ""));
	}
	return r.success;
}

function sl_validateValue(type, v) {
	var t = "";
	var c = "";
	var s = true;
	var l = {us_dollar:"a US Dollar value", currency:"currency", numeric:"a number", percentage:"a percentage"};
	if (v.split(/^\s+|\s+$/g).join("") == "") {
		return {success:true, text:'', corrected:''};
	}
	switch (type) {
		case "currency":
		case "us_dollar":
		case "percentage":
		case "numeric":
		

		try {
			eval("var c = (" + sl_calculatorParse(v)+")");
		} catch(e) {
			s = false;
			var n2 = {currency:/[\$\s]+/,us_dollar:/[\$\s]+/,numeric:/[\s]+/,percentage:/[\%\s]+/};
			var n = v.split(n2[type]).join("");
		
			var n = n.split(/[\(\)]/);
			if (n.length == 3) {
				n = "-" + n[1];
			} else {
				n = n[0];
			}
			
			var n2 = n.split(/[^0-9\-\.]+/).join("").split(".").length;
			if (!(s = (n.split(/[^0-9\-\.]+/).length == 1 && n2 >= 1 && n2 <= 2))) {
				t = "'"+v+"' does not seem to be "+l[type]+".";
			}
				
			var n = n.split(/[^0-9\-\.]+/).join("").split(".");
			var c = n[0] + (n[1] ? "."+n[1] : "");
		}
		break;
		
		case "ip":
		var n = v.split(".");
		s = n.length == 4;
		if (!s) t = "An IP address has exactly four values (n.n.n.n)\n";
		
		var s2 = true;
		for (var i = 0; i < n.length; i ++) {
			var n2 = Number(n[i]);
			if (isNaN(n2)) {
				s2 = s = false;
			} else if (n2 > 255 || n2 < 0) {
				s2 = s = false;
			}
		}
		if (!s2) t += "The values of an IP address must be between 0 and 255.";		
		break;
		
		case "phone":
		case "cell":
		var n = v.split(/(extension|ext|x)/i);
		if (n.length == 3) {
			var ext = n[2].split(/[^0-9A-Za-z\s]/).join("").split(/^\s+|\s+$/g).join("");
			v = n[0];
		} else {
			var ext = false;
		}
		
		var l2n = "22233344455566677778889999";
		var v2 = v, v = "";
		for (var i = 0; i < v2.length; i++) {
			var x = v2.charCodeAt(i);
			var l = (x >= 65 && x <= 122 ? ((x & 31) >= 1 && (x & 31) <= 26 ? (x & 31) - 1 : -1) : -1);
			v += l != -1 ? l2n.substr(l,1) : v2.substr(i,1);
		}

		if (v.split(/[^0-9\.\-\(\)\s\+]/).length != 1) {
			t = "This doesn't seem to be a valid phone number.\n";
			s = false;
		}
		
						
		var n = v.split(/[^0-9]/).join("");
		
		if (n.substr(0,1) == "0" && n.substr(1,1) != "0") {
			return v;
		}
		
		if (n.length == 3) {
			var c = n; //emergency
		} else if (n.length == 7) {
			t += "This phone number has no area code.";
			s = false;
		} else if (n.length == 10) {
			var c = "("+n.substr(0,3)+") "+n.substr(3,3)+"-"+n.substr(6,4);
		} else if (n.length >= 11 && n.length <= 13) {
			var n2 = n.length - 10;
			var c = "+"+n.substr(0,n2)+" "+n.substr(n2,3)+"-"+n.substr(n2+3,3)+"-"+n.substr(n2+6,4);
		} else {
			t += "This phone number has an invalid number of digits.";
			s = false;
		}
		
		if (ext) c += " ext " + ext;
		break;
		
		
		case "email":
		var n = v.split(/^\s+|\s+$/g).join("").split("@");
		
		if (n.length == 1) {
			t = "E-mail address needs an @ symbol.";
			s = false;
		} else if (n.length > 2) {
			t = "E-mail address may have only one @ symbol.";
			s = false;
		} else if (n[0].length > 64) {
			t = "The part of the E-mail address before the @ symbol cannot have more than 64 characters.";
		} else {
			var n2 = n[0].split(/[^A-Za-z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^_\`\{\|\}\~\.]+/);
			if (n2.length != 1) {
				t = "Invalid characters in the part before the @ symbol.\n";
				s = false;
			}
			n[0] = n2.join("");
			
			if (n[1].length > 255) {
				t += "The part of the E-mail address after the @ symbol cannot have more than 255 characters.\n";
				s = false;
			}
			
			var n2 = n[1].split(".");
			var s2 = false;
			for (var i = 0; i < n2.length; i++) {
				if (n2[i].length > 63) {
					n2[i] = substr(0,63);
					t += "Any part between dots after the @ symbol cannot have more than 63 characters.\n";
					s = false;
				}
				var n3 = n2[i].split(/[^a-zA-Z0-9\-]+/);
				if (n3.length != 1) {
					s2 = true;
					s = false;
				}
				
				if (n2[i].length == 0) {
					n2.splice(i,1);
					i --;
					t += "Two dots in a row are not allowed.\n";
				} else {
					n2[i] = n3.join("");
				}
			}
			n[1] = n2.join(".");
			if (s2) t += "Invalid characters in the part after the @ symbol.\n";
			c = n.join("@");
		}
		
		break;
		
		case "url":
		var n = v.split("://");
		if (n.length == 1) { n[1] = n[0]; n[0] = "http"; }
		
		var protocol = n[0];
		var url = n[1].split("?");
		var params = url[1];	
		var domain = url[0].split("/");
		var page = domain.length > 1 ? domain.pop() : "";
				
		domain = domain.join("/");
		
		c = protocol + "://" + domain + "/" + page + (params ? "?" + params : "");

		break;
		
		case "date":
		var n = v.split(/[^0-9A-Za-z]/);
		
		if (n.length == 1) {
			n.unshift("1"); n.unshift("1");
			s = false;
			t = "You only specified 1 number.";
		} else if (n.length == 2) {
			n.unshift("1");
			s = false;
			t = "You only specified 2 numbers.";
		} else if (n.length > 3) {
			t = "You specified too many numbers.";
			while (n.length > 3) { n.pop(); }
			s = false;
		}
		c = n.join("/");
		break;
		
		case "time":
		var n = v.split(/[^0-9A-Za-z]/);
		
		if (n.length == 1) {
			n.push("00"); 
		} else if (n.length > 2) {
			t = "You specified too many numbers.";
			while (n.length > 2) { n.pop(); }
			s = false;
		}
		c = n.join(":");
		break;
		
		default: break;
	}
	
	return {success:s, text:t, corrected:c};
}

function sl_validateWarn(d, txt) {
	alert(txt);
}

