
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
jsVersion = 1.1;


/*

Checker = new Validator(Form Id);
Checker.AddCheck("field id", { checktype : "email", empty_valid : false, warning: "Please enter a valid email address"});
Checker.AddCheck("field id", { checktype : "regexp", expression: "^\\d+$", empty_valid : false, warning: "Please enter a valid number" });
Checker.AddCheck("field id", { checktype : "numeric", empty_valid : false, numtype: "fp", warning: "Please enter a valid number" });
Checker.CheckFormAndSubmit();
*/

		var ValidEmpty = 2;			
		
		var Validator = function () {

			this.CheckArray = new Array;
			this.TestMode = true;
			this.SetTestMode = function(Val)
			{
				this.TestMode = Val;
			}
			this.DebugAlert = function(Txt)
			{
				if(true == this.TestMode)
				{
					alert(Txt);
				}
			}

			
			this.testwrite = function()
			{
				document.write("helloagain");
			}
			
			this.AddCheck = function(FieldId, OpArray)
			{
					OpArray['fieldid'] = FieldId;
					this.CheckArray.push(OpArray);
			}
			
			this.GetOps = function(ActionItem)
			{
				return this.CheckArray[ActionItem];
			}			

			this.SetOp = function(ActionItem, Variable, Value) 
			{
				this.CheckArray[ActionItem][Variable] = Value;
			}

			this.GetWarning = function(ActionItem)
			{
				OpArray = this.GetOps(ActionItem);
				if (OpArray["warning"])
				{
					return OpArray["warning"];
				}
				else
				{
					
					if ( ('each'!=ActionItem) && ('undefined'!=ActionItem) && ('eachSlice'!=ActionItem))
					{
						
						DefaultWarning = this.DefaultErrors[OpArray["checktype"]];
						if ('' != DefaultWarning)
						{
							return DefaultWarning;
						}
						else 
						{
							return "there is an error in the form";
						}
					}	
				}
			}
			
			this.CheckIfEmpty = function(ActionItem)
			{
				OpArray = this.GetOps(ActionItem);
				FieldId = OpArray.fieldid;
				Value = document.getElementById(FieldId).value;
				if ('' == Value)
				{
					if (true == OpArray['empty_valid']) 
					{
						return ValidEmpty;
					} else {
						return false;
					}
				}
				else
				{
					return true;
				}
			}
			
			this.CheckEmail = function(ActionItem)
			{
				NotEmpty = this.CheckIfEmpty(ActionItem);
				if (true == NotEmpty) 
				{
					OpArray = this.GetOps(ActionItem);
					FieldId = OpArray.fieldid;
					Field = document.getElementById(FieldId);
					with (Field)
					{
						AtPos=value.indexOf("@");
						DotPos=value.lastIndexOf(".");
						SpacePos = value.indexOf(" ");
						if ( (AtPos<1) || (DotPos-AtPos<2) || (-1 != SpacePos) )
						{
							return false;
						}
						else 
						{
							return true;
						}
					}
					return true;
				} 
				else
				{
					if (ValidEmpty == NotEmpty)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
			
			this.CheckRegExp = function(ActionItem)
			{
				NotEmpty = this.CheckIfEmpty(ActionItem);
				if (true == NotEmpty) 
				{
					OpArray = this.GetOps(ActionItem);
					FieldId = OpArray.fieldid;
					Expression = new RegExp(OpArray['expression']);
					if(Expression.test(document.getElementById(FieldId).value))
					{
						return true;
					} else {
						return false;
					}
				}
				else
				{
					if (ValidEmpty == NotEmpty)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}

			this.CheckPassConfirm = function(ActionItem)
			{
				OpArray = this.GetOps(ActionItem);
				FieldId = OpArray.fieldid
				CheckField = OpArray.confirmfield 
				if (CheckField)
				{
					if (document.getElementById(CheckField))
					{
						if (document.getElementById(CheckField).value == document.getElementById(FieldId).value)
						{
							return true;
						}
						else
						{
							return false;
						}
					}
					else
					{
						this.DebugAlert("Second Password FieldId does not exist");
						return false;
					}
				}
				else
				{
					this.DebugAlert("Confirm Password field not given");
					return false;
				}
			}
			
			
			
			this.CheckEmptyDepends = function(ActionItem)
			{
				OpArray = this.GetOps(ActionItem);
				FieldId = OpArray.fieldid
				DependsField = OpArray.dependsfield 
				if (DependsField)
				{
					if (document.getElementById(DependsField))
					{
						if (OpArray.dependsvalue)
						{
							CheckValue = OpArray.dependsvalue
						}
						else
						{
							CheckValue = '';
						}
						if (CheckValue == document.getElementById(DependsField).value)
						{
							if ('' == document.getElementById(FieldId).value)
							{
								return false;
							}
							else
							{
								return true;
							}
						}
						else
						{
							return true;
						}
					}
					else
					{
						this.DebugAlert("Dependent field does not exist (id wrong?)");
						return false;
					}
				}
				else
				{
					this.DebugAlert("Dependent field not given");
					return false;
				}
			}
			
						this.CheckNumeric = function(ActionItem)
			{
				NotEmpty = this.CheckIfEmpty(ActionItem);
				if (true == NotEmpty) 
				{
					OpArray = this.GetOps(ActionItem);
					if (OpArray['numtype']) {
						switch (OpArray['numtype']) {
						case "fp":
							//error in this regexp
							Expression = "[-+]?[0-9]*\\.?[0-9]+";
							break;
						case "int":
						default:
							Expression = "^\\d+$";
						}
					} else {
						Expression = "^\\d+$";
					}
						
					this.SetOp(ActionItem, "expression", Expression );
					
					if(this.CheckRegExp(ActionItem))
					{
						return true;
					} else {
						return false;
					}
				}
				else
				{
					if (ValidEmpty == NotEmpty)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
			
			
			this.CheckField = function(ActionItem)
			{
				OpArray = this.GetOps(ActionItem);
				CheckType = OpArray["checktype"];

				switch (CheckType)
				{
					case "emptydepends":
						return this.CheckEmptyDepends(ActionItem);
						break;
					case "passconfirm":
						return this.CheckPassConfirm(ActionItem);
						break;
					case "email":
						return this.CheckEmail(ActionItem);
						break;
					case "regexp":
						return this.CheckRegExp(ActionItem);
						break;
					case "numeric":
						return this.CheckNumeric(ActionItem);
						break;
					case "notempty":
						Result = this.CheckIfEmpty(ActionItem);
						return Result;
						break;
					default:
						return false;
				}
				/*switch (CheckType)
				{
					case "email":
						return this.CheckEmail(ActionItem);
						break;
					default:
						return false;
				}*/
				return true;
			}

		this.CheckForm = function()
		{
			for (CheckAction in this.CheckArray)
			{
				
				OpArray = this.GetOps(CheckAction);
				FieldId = OpArray['fieldid'];
				
				if ( ('each'==CheckAction) || ('undefined'==CheckAction) || ('undefined'==typeof(FieldId)) )
				{}
				else
				{
					if (false == this.CheckField(CheckAction))
					{
						
						Warning = this.GetWarning(CheckAction);
						alert(Warning);
						document.getElementById(FieldId).focus();
						return false;
						break;
					}
				}
				
			
				
			}
			
			return true;
		}
		
/*			this.CheckForm = function()
			{
				for (CheckAction in this.CheckArray)
				{
					FieldId = CheckAction.fieldid;
					if (false == document.getElementById(FieldId))
					{
						this.DebugAlert("Element does not exist: "+FieldId);
					}
					if (false == this.CheckField(CheckAction))
					{
						Warning = this.GetWarning(CheckAction);
						alert(Warning);
						document.getElementById(FieldId).focus();
						return false;
						break;
					}
				}
				return true;
		}*/
			
		}

