/**************************************************************/
/*       C L A S S     F O R M     V A L I D A T I O N        */
/**************************************************************/

var objFormValidation = new classFormValidation();

function classFormValidation()
{
	this.rc;
	this.bFocus;
	this.myForm;
	this.fieldList;
	this.bSubmitForm;
	this.objField;
	this.aField      = new Array();
	this.aSubField   = new Array(3);


	classFormValidation.prototype.ValidateForm = function(objParm)
	{
		var fieldIx;
		
		this.myForm = objParm.form;
		this.fieldList = objParm.fieldList;
		this.bSubmitForm = (objParm.bSubmitForm == null ? true : objParm.bSubmitForm);


		this.rc=this.bFocus=0;
		if (this.fieldList!="") this.aField=this.fieldList.split("|");


		for (fieldIx=0; fieldIx<this.aField.length && !this.rc; fieldIx++)
		{
			var i, bMustFill, fieldName, fieldValue;
	
			this.aSubField=this.aField[fieldIx].split(":");
			
			if (this.aSubField[2]=="MustPhone")
				fieldName=this.aSubField[2];
			else
			{
				this.objField = document.getElementById(this.aSubField[0]);
	
				bMustFill=(this.aSubField.length==1 || this.aSubField[1]=="1");  // default is must
				this.aSubField.length>2 ? fieldName=this.aSubField[2] : fieldName=this.aSubField[0];
		
				if (fieldName=="Radio")
				{
					this.objField.value="";
				}
				else
				{
					if (this.objField.length>0)
					{
						for (i=0; i<this.objField.length && (this.objField.value.charAt(i)==' ' || this.objField.value.charAt(i)=="\t" || this.objField.value.charAt(i)=="\n" || this.objField.value.charAt(i)=="\r"); i++);
						if (i==this.objField.length) this.objField.value="";
					}
			
					fieldValue=this.objField.value;
					len=fieldValue.length;
				}
			}

			
			switch (fieldName)
			{
				case "City": 
					if (len==0) 
					{
						if (bMustFill)	this.DisplayError("יש להזין שם עיר");
					}
					else
					{
						for (i=0; i<len && fieldValue.charAt(i)!="'" && fieldValue.charAt(i)!="-" && fieldValue.charAt(i)!='"'; i++);
						if (i<len) this.DisplayError("שם עיר צריך להיות מלא ואינו יכול לכלול מרכאות, גרשיים וקו מפריד");
					}
	
					break;

				case "Date": 
					if (len==0)
					{
						if (bMustFill) this.DisplayError("יש להזין תאריך");
					}				
					else		
						this.rc=this.VerifyDate(fieldValue);
						
					break;
	
				case "FirstName": 
					if (len==0) 
					{
						if (bMustFill)	this.DisplayError("יש להזין שם פרטי");
					}								
					else
					{
						for (i=0; i<len && fieldValue.charAt(i)!="'" && fieldValue.charAt(i)!='"'; i++);
						if (i<len) this.DisplayError("שם אינו יכול לכלול גרשיים או מרכאות");
					}
					
					break;
	
				case "LastName": 
					if (len==0) 
					{
						if (bMustFill)	this.DisplayError("יש להזין שם משפחה");
					}								
					else
					{
						for (i=0; i<len && fieldValue.charAt(i)!="'" && fieldValue.charAt(i)!='"'; i++);
						if (i<len) this.DisplayError("שם משפחה אינו יכול לכלול גרשיים או מרכאות");
					}
					
					break;
	
				case "Name": 
					if (len==0) 
					{
						if (bMustFill)	this.DisplayError("יש להזין שם");
					}								
					else
					{
						for (i=0; i<len && fieldValue.charAt(i)!="'" && fieldValue.charAt(i)!='"'; i++);
						if (i<len) this.DisplayError("שם אינו יכול לכלול גרשיים או מרכאות");
					}
					
					break;
	
				case "Phone": 
					if (len==0) 
					{
						if (bMustFill)	this.DisplayError("יש להזין טלפון");
					}							
					else
					{
						for (i=0; i<len && fieldValue.charAt(i)!="'" && fieldValue.charAt(i)!='"'; i++);
						if (i<len) this.DisplayError("מספר טלפון אינו יכול לכלול גרשיים או מרכאות");
					}
	
					break;
	
	
				case "MustPhone": 
					{
						var i, aPhone = new Array();
						
						aPhone = this.aSubField[0].split("@");
						for (i=0; i<aPhone.length && document.getElementById(aPhone[i]).value<=" "; i++);

						if (i==aPhone.length)
						{
							this.objField=document.getElementById(aPhone[0]);
							this.DisplayError("יש להזין לפחות מספר טלפון אחד");
						}
					}	
					
					break;
	
				case "CCNumber": 
					if (len==0) 
					{
						if (bMustFill)	this.DisplayError("יש להזין מספר כרטיס אשראי");
					}							
					else
					{
						var i, str="";
						
						for (i=0; i<len && (!this.IsNumeric(fieldValue.charAt(i)) || fieldValue.charAt(i)==' '); i++)
						{
							if (fieldValue.charAt(i)!=' ') str+=fieldValue.charAt(i);
						}
						
						if (i<len)                this.DisplayError("מספר כרטיס האשראי שהוזן שגוי");
						//else if (str.length!=16)  this.DisplayError("מספר כרטיס האשראי אינו מכיל 16 ספרות");
						//else if (len!=16)         this.objField.value=str;
						else
							VerifyCCNumber(str, this.myForm.CCCompanyCode.value) ? this.DisplayError("מספר כרטיס האשראי שהוזן שגוי") : this.objField.value=str;
					}
	
					break;
	
				case "CVV": 
					if (len==0) 
					{
						if (bMustFill)	this.DisplayError("יש להזין את 3 הספרות שבגב כרטיס האשראי");
					}							
					else
					{
						var i;
						
						for (i=0; i<len && !this.IsNumeric(fieldValue.charAt(i)); i++);
						
						if (i<len)          this.DisplayError("יש להזין ספרות בלבד");
						else if (i!=3)      this.DisplayError("יש להזין 3 ספרות");
					}
	
					break;
	
				case "ZipCode": 
					if (len==0) 
					{
						if (bMustFill)	this.DisplayError("יש להזין מיקוד");
					}							
					else
					{
						var i;
						
						for (i=0; i<len && !this.IsNumeric(fieldValue.charAt(i)); i++);
						
						if (i<len)          this.DisplayError("יש להזין ספרות בלבד במיקוד");
						else if (i!=5)      this.DisplayError("המיקוד צריך להכיל 5 ספרות");
					}
	
					break;
	
				case "Text": 
					if (len==0) 
					{
						if (bMustFill)	this.DisplayError("יש להזין את הטקסט");
					}							
	
					break;
					
				case "Email":
					if (len==0) 
					{
						if (bMustFill)	this.DisplayError('יש להזין דוא"ל');
					}							
					else
					{				
						var exclude		=	/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
						var check			=	/@[\w\-]+\./;
						var checkend	=	/\.[a-zA-Z]{2,3}$/;
						
						//if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this.myForm.emailAddr.value))
						//	return (true)
					
						if(	(fieldValue.search(exclude)  != -1) ||
					 			(fieldValue.search(check)		 == -1)	||
					 			(fieldValue.search(checkend) == -1))
							this.DisplayError('כתובת הדוא"ל הוזנה בצורה שגויה');
					}
					
				 	break;
					
				case "Comments":
					if (len==0) 
					{
						if (bMustFill) this.DisplayError('יש להזין הערות');
					}							
					else
						this.objField.value=fieldValue.replace(/'/g, "''");
	
				 	break;
	
				case "Number":
					if (len==0) 
					{
						if (bMustFill)	this.DisplayError('יש להזין את השדה');
					}							
					else
					{
						for (i=0; i<len && !this.IsNumeric(fieldValue.charAt(i)); i++);
						if (i<len) this.DisplayError("יש להזין ספרות ואותיות בלבד");
					}
					
				 	break;
	
				case "Radio":
					if (bMustFill)
					{
						var fname;
						
						if (this.aSubField[0]=="SubscriptionType") fname="סוג מנוי";
						for (i=0; i<this.objField.length && !this.objField[i].checked; i++);
						if (i==this.objField.length) this.DisplayError("יש לבחור ערך לשדה '"+fname+"'", 1);
					}
	
				 	break;
	
				default:
					alert("שם לא נמצא: "+fieldName);
			}
		}
	
		if (this.rc==0)
		{
			if (this.bSubmitForm)
			{
				document.body.style.cursor = "wait";
				this.myForm.submit();
			}
		}
	
		return this.rc;
	}


	classFormValidation.prototype.DisplayError = function(msg, swNoFocus)
	{
		this.rc=1;
	
		if (!this.bFocus)
		{
			this.bFocus=1;
			alert(msg);
	
			if (swNoFocus!=1) this.objField.focus();
		}
	}


	classFormValidation.prototype.IsNumeric = function(str)
	{
		var i;
	
		for (i=0; i<str.length && str.charAt(i)>='0' && str.charAt(i)<='9'; i++);
		return i!=str.length;
	}


	classFormValidation.prototype.IsAlpha = function(str)
	{
		var i;
	
		for (i=0; i<str.length && str.charAt(i)>='a' && str.charAt(i)<='z' || str.charAt(i)>='A' && str.charAt(i)<='Z'; i++);
		return i!=str.length;
	}
}
