// Script: ddform (Drag and Drop Form) v1.00
// Company: Interaxis
// Author: dynamicreport.com
// Website: http://dynamicreport.com/popup.html
// License: Commercial (original author info. must remain in all files)


function filter_data ()
{
	this.f_validate = f_validate;
	this.f_clr = f_clr;
	this.f_fill = f_fill;
	this.f_fix_real = f_fix_real;
	this.f_is_strongpass = f_is_strongpass;

	function f_validate (sform, sbtn)
	{
		var sabbrv = "f_";
		var snotice = "fn_";
		var sctrl = sobj = sparam = pw = "";
		var ctrl = obj = notice = btn = attrib = param = null;
		var result, req, bneg;
		var fresult = true;
		var frm = document.getElementById (sform);
		for (var i = 0; i < frm.elements.length; i++)
		{
			pw = "";
			result = bneg = true;
			req = false;
			sctrl = frm.elements[i].name;
			index = sctrl.indexOf (sabbrv);
			if (index != 0)
				continue;
			sobj = sctrl.substring (sabbrv.length);
			sctrl = document.getElementById (sctrl).value;
			obj = document.getElementById (sobj);
			notice = document.getElementById (snotice + sobj);
			sobj = obj.value;
			sctrl = sctrl.toLowerCase ();
			sctrl = sctrl.replace (/\s/g, ""); // remove white space
			ctrl = sctrl.split (',');
			for (var j = 0; j < ctrl.length; j++)
			{
				sparam = ctrl[j];
				switch (sparam)
				{
					case "required":
						req = true;
						break;
					case "word":
						if (!f_is_word (sobj))
							result = false;
						break;
					case "alphanum":
						if (!f_is_alnum (sobj))
							result = false;
						break;
					case "alpha":
						if (!f_is_alpha (sobj))
							result = false;
						break;
					case "numeric":
						if (!f_is_num (sobj))
							result = false;
						break;
					case "email":
						if (!f_is_email (sobj))
							result = false;
						break;
					case "phone":
						if (!f_is_email (sobj))
							result = false;
						break;
					case "zipcode":
						if (sobj.length < 5 || !f_is_num (sobj))
							result = false;
						break;
					case "postalcode":
						if (sobj.length < 7 || !f_is_alnum (sobj))
							result = false;
						break;
					case "pzcode":
						if (sobj.length < 5 || !f_is_alnum (sobj))
							result = false;
						break;
					case "user":
						if (!f_is_user (sobj))
							result = false;
						break;
					case "password":
						pw = f_is_strongpass (sobj);
						break;
					case "selection":
						req = true;
						if (obj.selectedIndex <= 0)
						{
							result = false;
							sobj = "";
						}
						break;
					default:
						param = sparam.split (':'); // obtain parameter attribute / value pair
						if (param.length <= 1)
							break;
						switch (param[0])
						{
							case "len": // minimum length
								if (sobj.length < parseInt (param[1]))
									result = false;
								break;
							case "amount+": // positive real numbers
								bneg = false;
							case "amount": // real numbers
								obj.value = f_fix_real (sobj, parseInt (param[1]), param.length > 2 ? param[2] : null, bneg);
								break;
							case "date":
								if (!f_is_d (param[1], sobj))
									result = false;
								break;
						}
						break;
				}
			}
			if ((req && f_is_blank (sobj)) || (!result && !f_is_blank (sobj)))
			{
				fresult = false;
				if (notice) // set invalid input notice
					notice.innerHTML = "[Invalid " + (obj.alt ? obj.alt : obj.title) + "]";
			}
			else if (pw) // password field - display password strength
				notice.innerHTML = pw;
			else if (notice) // reset invalid input notice
				notice.innerHTML = "";
		}
		btn = document.getElementById (sbtn);
		if (fresult && btn) // avoid double submission of form
			btn.disabled = true;
		return fresult;
	}

	function f_is_strongpass (str, snobj)
	{
		var strongRegex = new RegExp ("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
		var mediumRegex = new RegExp ("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
		var enoughRegex = new RegExp ("(?=.{6,}).*", "g");
		var res = nobj = null;
		if (strongRegex.test (str))
			res = '<span class = "strong">[Strong pass]</span>';
		else if (mediumRegex.test (str))
			res = '<span class = "nominal">[Nominal pass]</span>';
		else
			res = '<span class = "weak">[Weak pass]</span>';
		if (snobj)
			nobj = document.getElementById (snobj);
		if (nobj)
			nobj.innerHTML = res;
		return res;
	}

	function f_fix_real (str, dec, min, max, bneg)
	{
		var bDec = 0; var val = "";
		var strf = ""; var neg = ""; var i = 0;
		if (isNaN (min))
			min = 0;
		if (str == "") return parseFloat (min).toFixed (dec);
		for (i; i < str.length; i++)
		{
			val = str.charAt (i);
			if (val == '.')
			{
				if (strf == "" && bneg && val == '-')
					neg = '-';
				if (!bDec) { strf += val; bDec = 1; }
			}
			else if (val >= '0' && val <= '9')
				strf += val;
		}
		strf = (strf == "" || parseFloat (strf) < parseFloat (min) ? parseFloat (min) : neg + strf); // check minimum value limit
		if (parseFloat (strf) > parseFloat (max)) // check maximum value limit
			strf = parseFloat (max);
		return parseFloat (strf).toFixed (dec);
	}

	function f_is_num (str)
	{
		for (var i = 0; i < str.length; i++)
		{
			val = str.charCodeAt (i);
			if (val < 48 || val > 57)
				return false;
		}
		return true;
	}

	function f_is_alpha (str)
	{
		for (var i = 0; i < str.length; i++)
		{
			val = str.charCodeAt (i) | 32;
			if (val < 97 || val > 122)
				return false;
		}
		return true;
	}
	
	function f_is_alnum (str)
	{
		for (var i = 0; i < str.length; i++)
		{
			val = str.charCodeAt (i) | 32;
			if ((val >= 97 && val <= 122) ||
				(val >= 48 && val <= 57));
			else // not in range of alphabet or numbers
				return false;
		}
		return true;
	}

	function f_is_user (str)
	{
		for (var i = 0; i < str.length; i++)
		{ // allow alpha-numeric characters and the underscore
			val = str.charCodeAt (i) | 32;
			if ((val >= 97 && val <= 122) ||
				(val >= 48 && val <= 57) ||
				val == 95);
			else // not in range of the above
				return false;
		}
		return true;
	}

	function f_is_word (str)
	{
		for (var i = 0; i < str.length; i++)
		{ // allow alpha-numeric characters, single quote, period, parenthesis, dash, space
			val = str.charCodeAt (i) | 32;
			if ((val >= 97 && val <= 122) ||
				(val >= 48 && val <= 57) ||
				val == 32 || val == 39 ||
				val == 46 || val == 45 ||
				val == 40 || val == 41);
			else // not in range of the above
				return false;
		}
		return true;
	}
	
	function f_is_blank (str)
	{
		var re = new RegExp("^(\\s){0,}$");
		if (str.match(re))
			return true;
		return false;
	}
	
	function f_is_email (str)
	{
		var is_email_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/; 
		return String (str).search (is_email_re) != -1; 
	}

	function f_is_phone (str)
	{ // US phone pattern. E.g. (999) 999-9999 or (999)999-9999
		var objRegExp = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
		return objRegExp.test (str);
	}

	function f_is_d (smask, str)
	{
		var rex = "";
		for (i = 0; i < smask.length; i++)
		{
			ch = smask.charAt (i);
			switch (ch)
			{
				case 'm': // month 01-12
					rex += "(0[1-9]|1[012])";
					break;
				case 'd': // day 01-31 (take leap year into account)
					rex += "(0[1-9]|[12][0-9]|3[01])";
					break;
				case 'y': // year 1900-2099
					rex += "(19|20)\\d\\d";
					break;
				case 'H': // hour of 24 hour clock (00-23)
					rex += "([0][0-9]|1[0-9]|2[0-3])";
					break;
				case 'h': // hour of 12 hour clock (01-12)
					rex += "([0][1-9]|1[0-2])";
					break;
				case 'm': // minutes (00-59)
				case 's': // seconds (00-59)
					rex += "([0-5][0-9])";
					break;
				case 'a': // AM or PM case-insensitive indication
					rex += "(AM|PM|am|pm)";
					break;
				case ' ': // whitespace (any amount ignored)
					rex += "\\s{0,}";
					break;
				default: // ignore all
					rex += ch;
					break;
			}
		}
		rex = "^(" + rex + ")$"; // final regular expression composition
		var re = new RegExp (rex);
		if (str.match (re))
			return true;
		return false;
	}

	function f_clr (fld, txt)
	{
		if (fld.value == txt)
			fld.value = "";
	}

	function f_fill (fld, txt)
	{
		if (fld.value == "")
			fld.value = txt;
	} 
}
var fvalinp = new filter_data ();


function ddform ()
{
	this.bshow = true;
	this.days = 1;

	this.load = load;
	this.show = show;
	this.get_cookie = get_cookie;
	this.put_cookie = put_cookie;

	this.cbinit = function ()
	{
		alert ("Missing ddform function definition for object instance initialization: cbinit ();");
	};

	function load (once, key, days)
	{
		var simg = "";
		if (days)
			this.days = days;
		if (once)
			this.bshow = this.get_cookie (key) > 50 ? false : true;
		if (!this.bshow)
			return false; // not the first visit according to cookie so forget the popup
	}

	function show ()
	{
		if (!this.bshow)
			return false; // not the first visit so forget the popup
		this.cbinit (); // custom callback prototype for popup initialization
		return true;
	}

	function get_cookie (name)
	{ // searches cookie with a specific name on the visitor's hard drive.
		var cookiecontent = '0';
		if (document.cookie.length > 0)
		{ // look for cookie on visitor's hard drive
			var cookiename = name + '=';
			var cookiebegin = document.cookie.indexOf (cookiename);
			var cookieend = 0;
			if (cookiebegin > -1)
			{
				cookiebegin += cookiename.length;
				cookieend = document.cookie.indexOf (";", cookiebegin);
				if (cookieend < cookiebegin)
					cookieend = document.cookie.length;
				cookiecontent = document.cookie.substring (cookiebegin,cookieend);
			}
		}
		var value = parseInt (cookiecontent) + 1;
		this.put_cookie (name, value);
		return value;
	}

	function put_cookie (n, v)
	{
		var exp = '';
		if (this.days > 0)
		{
			var now = new Date ();
			then = now.getTime () + (this.days * 24 * 60 * 60 * 1000); // time: ms
			now.setTime (then);
			exp = '; expires=' +
			now.toGMTString ();
		}
		document.cookie = n + "=" + v + '; path=/' + exp;
	}
}

var dd_form = new ddform ();

