/**
* @version $Id: Kfir Gershon
* @copyright Copyright (C) 2008 Kfir Gershon. All rights reserved.
* @license http://www.studiomor.com
* GNU/GPL
*ניתן להשתמש בקוד , בהצבה של לינק לאתר שלי , תודה
simply insert this style into youre css files:
------------------------------------------------------
	-in css for *:
		span.required			{color:#FF0000;}

	-for each input insert tag call alt, with parm
		<input alt="FREQUIRED" title="this field is REQUIRED"/>
		more example for alt:
			alt="FREQUIRED AREA_0"			>> בדיקה של שדה באיזור 0
			alt="FREQUIRED AREA_1"			>> בדיקה של שדה באיזור 1
			alt="FREQUIRED AREA_0 NUMBER"	>> בדיקה של מספר
			alt="FREQUIRED AREA_0 EMAIL"	>> בדיקה של שדה מייל			
			alt="FREQUIRED AREA_1 SIZE10"   >> בדיקה של סיסמה לדוגמה
	
	-form submit
		<form ... onsubmit="return(kjsForm.checkForm());">
		
	-after form insert this code:			 
			kjsForm.creatRequiredField();
			kjsForm.condition(0);
	
	-div for msg:
		<div id="kclsMsg"></div>
		 
**/
var kjsForm = new function(){
	var tmpArray		=null;
	var tmpCondition	=0;
	this.GetArray 		= function(tagName){return document.getElementsByTagName(''+tagName);}
	this.setType		= function(fldObj){		
		/*
			מחזיר את שיטת הבדיקה הרצויה	
			----------------------------
			EMAIL - בדיקה של מבנה המייל
			NUMBER - בדיקה האם הוזנו רק מספרים בשדה
		*/
		if (fldObj.alt.indexOf("EMAIL")>0)	{return General.checkEmail(fldObj.value);}
		if (fldObj.alt.indexOf("NUMBER")>0)	{return General.checkNum(fldObj.value);}
		if (fldObj.alt.indexOf("SIZE")>0)	{return General.checkSize(fldObj.value,fldObj.alt);}
		return true;
	}
	//מייצר תנאי חיצוני ללולאת הבדיקה כדי ליצור טופס אחד שמורכב מכמה טפסים ויש לבדוק רק חלקים מתוכו
	//לדוגמה טופס שבו נחשפים פרטים עפ"י לחיצה של הגולש ולכן בדיקה של שדה מוסתר תהייה בעיה לגולש למלות אותו :-)
	this.condition 		= function(x){tmpCondition=x;}
	this.getCondition	= function(){return tmpCondition;}
	this.runCheck		= function runCheck(Inputs){		
		for (var Item=0; Item<Inputs.length; Item++){
			try{
				//בדיקה רק באיזור החשוף לגולש
				//if (eval(Inputs[Item].alt.indexOf("AREA_"+tmpCondition))!=0){
				if (Inputs[Item].type!='hiiden'){
					zoneCheck=eval(Inputs[Item].alt.indexOf("AREA_"+tmpCondition))>0;				
					if (zoneCheck){
						General.condition(tmpCondition);
						//משתנה המחזיק את התוצאה האם השדה נדרש להזנה
						ifA=eval(Inputs[Item].alt.indexOf("FREQUIRED"))==0;
						//האם התיבת טקסט שהוגדרה כנדרשת ריקה? מחזיר אמת או שקר
						//שיטת בדיקה פשוטה
						ifB=Inputs[Item].value.length == 0;			
						//אם קיימים שדות חובה
						Inputs[Item].className = "field";
						if (ifA){
							//בודק האם השיטה מחפשת תיבות טקסט או תיבות נגללות
							if (Inputs[Item].value.length>0){ifB=Inputs[Item].selectedIndex==0;}
							//האם תיבת הטקסט ריקה / בוצעה בחירה בתיבת הגלילה
							if (ifB){
								//alert(Inputs[Item].title);
								//במידה והתיבת טקסט ריקה / לא התבצעה בחירה בתיבת גלילה מדווח לגולש 
								//את ההודעה המוחזקת בתג TITLE בשדה הנדרש
								General.printMsg("* - "+Inputs[Item].title);
								//alert(Inputs[Item].title);
								Inputs[Item].className = "fieldRed";
								Inputs[Item].focus();
								//מחזיר תוצאה לבדיקה הבאה
								return false;
							}
							//בודק אפשרויות נוספות על השדה במידה והתכנת דרש זאת EMAIL ect..
							x=kjsForm.setType(Inputs[Item]);
							if (x==false){
								return false;
							}
						}
						
					}
					//יש לבדוק שדה שאינו חובה ומוגדר להזנה של מספרים בלבד
					//exceptional(Inputs[Item]);
				//}
				}
			}catch(err){
				//do nothing
			}
		}
 		return x;
	}
	this.checkForm 			= function(){
		var a=false;
		var b=false;
		formInput=kjsForm.GetArray('input');
		formSelect=kjsForm.GetArray('select');
		
		a=runCheck(formInput);
		if (a){
			b=runCheck(formSelect);
			//alert(b);
		}
	
		return b;
	}
	this.insertAfter 		= function(referenceNode, newNode ){    referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling );}		
	this.setSpan 			= function(){
		for (var Item=0; Item<tmpArray.length; Item++){
			try{
				if (tmpArray[Item].alt.indexOf("FREQUIRED")==0){
					// create an empty element node
					// without an ID, any attributes, or any content
					var sp1 = document.createElement("span");				
					// give it an id attribute called 'newSpan'
					sp1.setAttribute("id", "required"+Item);
					sp1.className="required";
					// create some content for the newly created element.
					var sp1_content = document.createTextNode(" * ");				
					// apply that content to the new element
					sp1.appendChild(sp1_content);
					 
					//parent Node
					var sp2 = tmpArray[Item];
					//var parentItem = sp2.parentNode;				
					kjsForm.insertAfter(sp2,sp1);
				}		 		
			}
			catch(err){
				// because select tag is no alt attribute the browser get err
				//this lines is for select woth out tag alt
			}
		}		
	}	
	this.creatRequiredField = function(){ 
		tmpArray=kjsForm.GetArray('input');
		kjsForm.setSpan();
		tmpArray=kjsForm.GetArray('select');
		kjsForm.setSpan();		
	}
	this.AjaxSubmit 		= function(postObj,fileToRead){
		var getItems;
		for (var Item=0; Item<postObj.length; Item++){
			if (postObj[Item].name.indexOf("_")==-1){getItems=postObj[Item].name+'='+postObj[Item].value+'&'+getItems;}
		}
		Ajax.setDivLoading();
		Ajax.AjaxRequest(fileToRead,getItems);
		return false;
	}
}



