﻿
var rxpDate = /(^[0-3]?[0-9]\.[0-1]?[0-9]\.[1-2][0-9]{3}$)|(^[0-3][0-9][0-1][0-9][1-2][0-9]{3}$)/; // (dd.mm.yyyy, d.m.yyyy OR ddmmyyyy)
var rxpTime = /^[0-1][0-9]:[0-5][0-9]$/; // (hh:mm)
var rxpZip = /(^[0-9]{5}$)/; // Zip
var rxpKodPocztowy = /^[0-9]{2}-[0-9]{3}$/;
var rxpPhone = /^\([0-9]{3}\)[0-9]{3}\-[0-9]{4}\s?(\w|\.)*$/; // (xxx)xxx-xxxx or (xxx)xxx-xxxx xxxxxxx for extensions
var rxpSSN = /^[0-9]{3}\-[0-9]{2}\-[0-9]{4}$/; // (xxx-xx-xxxx)
var rxpNumeric = /^[0-9]*$/
var rxpFloat = /^\d+((,|\.)\d+)?$/
var rxpCurrency = /^(\d|\ )+((,|\.)\d\d)?$/
var rxpEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

var toClear; // reference back to a field if it's title needs to clear after a 
			// set amt of time.

function formclick (argString) {
    cfgForm = new DOOM_Config (argString);
	
	this.FormName = cfgForm.Item("FORM") ? cfgForm.Item("FORM") : "";
	this.Form = null;
	this.Fields = Array();
	this.ValidateOnBlur = cfgForm.Item("ONBLUR") ? cfgForm.Item("ONBLUR") : false;
	this.ValidateOnSubmit = true;
	Self = this;
	
	this.Initialize = _Initialize;
	this.ReInitialize = _ReInitialize;
	this.Add = _Add;
	this.Delete = _Delete;
	this.Find = _Find;
	this.HardValidateAll = _HardValidateAll;

	function _Initialize () {
		var initField;
		var oldonsubmit;

		if (Self.FormName == "") Self.FormName = document.forms[0].name;
		if (!(Self.Form = eval ("document." + Self.FormName))) {
			alert ("FormClick Error:\nForm could not be found");
			return false;
		}
		for (var i = 0; i < Self.Fields.length; i++) {
			if (Self.Fields[i].FieldName != "") {
				if (!(initField = eval ("document." + Self.FormName + "." + Self.Fields[i].FieldName))) {
					window.status = "FormClick Error: \n" + Self.Fields[i].FieldName + " field not found.";
				} else {
					Self.Fields[i].Field = initField;
					if (Self.ValidateOnBlur) {
						if (initField.addEventListener) { 
							initField.addEventListener("blur",Self.Fields[i].SoftValidate,false); 
						}   
						else if (initField.attachEvent && !document.all) { 
							initField.attachEvent("onblur",Self.Fields[i].SoftValidate);     
						}
						else initField.onblur = Self.Fields[i].SoftValidate;
					}
				}
			}
		}
		if (Self.ValidateOnSubmit) {
			oldonsubmit = Self.Form.onsubmit;
			if (typeof Self.Form.onsubmit != 'function') {
				if (Self.Form.addEventListener) { 
					Self.Form.addEventListener("submit",Self.HardValidateAll,false); 
				}   
				else if (Self.Form.attachEvent) { 
					Self.Form.attachEvent("onsubmit",Self.HardValidateAll);     
				}
				else Self.Form.onsubmit = Self.HardValidateAll;
			} else {
				Self.Form.onsubmit = function() {
					Self.HardValidateAll(); 
					oldonsubmit();
				}
			}
		}
	}
	function _ReInitialize () {
		var initField;
		
		Self.Form = eval ("document." + Self.FormName);
		for (var i = 0; i < Self.Fields.length; i++) {
			if (Self.Fields[i].FieldName != "") {
				initField = eval ("document." + Self.FormName + "." + Self.Fields[i].FieldName);
				Self.Fields[i].Field = initField;
				if (Self.ValidateOnBlur) {
					initField.onblur = Self.Fields[i].SoftValidate;
				}
			}
		}
	}
	function _Add ( inField, inReq, inReqmsg, inRxp, inRxpmsg ) {
		var newField = new ps_field();
		
		newField.FieldName = inField;
		newField.Req = inReq ? true : false;
		newField.Reqmsg = inReqmsg ? inReqmsg : newField.FieldName + " is a required field.";
		newField.Rxp = inRxp ? inRxp : null;
		newField.Rxpmsg = inRxpmsg ? inRxpmsg : "";
		
		if (_Find(newField.FieldName)) {
			alert ("Form Validator Object Error:\n\n\tAttempted to add " + newField.FieldName + " as a duplicate field.");
		} else {
			Self.Fields[Self.Fields.length++] = newField;
		}
	}
	function _Delete ( strFieldName ) {
		var intIndex;
		
		intIndex = _Find(strFieldName)
		if (intIndex != null) {
			Self.Fields[intIndex].FieldName = "";
			Self.Fields[intIndex].Req = false;
			Self.Fields.splice (intIndex,1);
		} 
	}
	function _Find ( strFieldName ) {
		for (var i = 0; i < Self.Fields.length; i++) {
			if (i in Self.Fields) {
				if (strFieldName == Self.Fields[i].FieldName) {
					return i;
				}
			}
		}
		return null;
	}
	function _HardValidateAll (evt) {
		for (var i = 0; i < Self.Fields.length; i++) {
			if (Self.Fields[i].FieldName != "") {
				if (!Self.Fields[i].HardValidate()) {
					if (window.event) {
						window.event.returnValue = false;
					} else if (evt) {
						evt.preventDefault();
					}
					return false;
				}
			}
		}
	}
	var Self;
}

function ps_field () {
	this.FieldName = "";
	this.Field = null;
	this.Req = false;
	this.Reqmsg = "";
	this.Rxp = null;
	this.Rxpmsg = "";
	this.CurMsg = "";
	
	Self = this;
	
	this.SoftValidate = _SoftValidate;
	this.HardValidate = _HardValidate;
	
	function _SoftValidate () {
		if (Self.Req) {
			if (Self.Field.value == "") {
				Self.CurMsg = Self.Reqmsg ? Self.Reqmsg : Self.Field.title;
				setWndMsg (Self.CurMsg, Self.Field);
				return false;
			}
		}
		if (Self.Rxp && Self.Field.value.length > 0) {
			if (Self.Field.value.search( Self.Rxp ) != 0) {
				Self.CurMsg = Self.Rxpmsg ? Self.Rxpmsg : Self.Field.title;
				setWndMsg (Self.CurMsg, Self.Field);
				return false;
			}
		}
		Self.Field.style.backgroundColor = "#FFFFFF";
		window.status = "";
		return true;
	}
	function _HardValidate () {
		if (Self.SoftValidate())
			return true;
		else {
			alert (Self.CurMsg);
			Self.Field.focus();
			return false;
		}
	}
	
	var Self;
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		if (window.addEventListener) { 
			window.addEventListener("load",func,false); 
		}   
		else if (window.attachEvent) { 
			window.attachEvent("onload",func);     
		}
		else window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function setWndMsg (inMsg, inField) {
	window.status = inMsg
	if (inField) {
		inField.style.backgroundColor = "#EEBBBB";
		inField.title = inMsg;
		toClear = inField;
	}
	setTimeout( clearMsg, 10000 );
}

function clearMsg () {
	window.status = ''; 
	if (toClear) toClear.title = "";
	toClear = null;
}

// DOOM_Config (argString)
//  argString = String of ;-separated option key/value pairs
// Methods
//  Item (argKey)
//   Returns the string value of the Option identified by argKey
//  Parse (argStr, argConfig)
//   Parses the specified string, converting to boolean if necessary
//  The idea behind DOOM_Config is to allow configuration properties to 
//   be set in a more user-friendly manner.  Params are sent in a string
//   like CSS with a selector and then a value.  This can then be used
//   to set the object values in the calling object by accessing the
//   Item method, using the parameter name as the key.
function DOOM_Config (argString)
{
	this.pv_Option = new Array();
	this.pv_Key = new Array();
	
	this.Parse = Parse;
	this.Item = Item;
	
	Parse( argString, this );
	
	function Parse (argStr, argConfig)
	{
		var i, aryOptions;
		var strKey, strVal;
		if (argStr) aryOptions = argStr.split(";");
		else return;
		var strKey, strValue, intEq;
		
		for (i = 0; i < aryOptions.length; i++)
		{
			intEq = aryOptions[i].indexOf(":");
			if (intEq > 0)
			{
				strKey = aryOptions[i].substring(0,intEq).toUpperCase();
				strVal = aryOptions[i].substring(intEq + 1, aryOptions[i].length);
				
				if (strVal.toLowerCase() == "true") strVal = true;
				else if (strVal.toLowerCase() == "false") strVal = false;
				
				argConfig.pv_Key[i] = strKey;
				argConfig.pv_Option[i] = strVal;
			}
		}
	}
	function Item (argKey)
	{
		var i;
		
		for (i = 0; i < this.pv_Key.length; i++)
		{
			if (this.pv_Key[i] == argKey)
			{
				return this.pv_Option[i];
			}
		}
		return null;
	}
}

