
function JimetFormValidatorObj() { 
 	this.jimetAjaxObj = null;
 	this.toggleDivId = null;
 	this.formId = null;
 	this.formObj = null;
 	this.action = null;
 	this.postData = null;
 	this.url = appRoot+'/FormHandlerServlet.do';
 	this.errMsg = null;
 	this.resultMsg = null;
 	this.endFunction = null;
 	this.resultObjId = -1;
 	this.resultObjName = null;
 	this.loaderIcon = null;
 	this.loadingDiv = null;
 	this.disabledFormElements = new Array();
 	
 	this.disableAllFormElements = function(objId)
 	{
 		var els = document.getElementById(objId);
 		for(i=0;i<els.length;i++){
 			if( (els[i].disabled != true) )
 			{
 				els[i].disabled = true;
 				this.disabledFormElements[i] = els[i]; 			
 			}
 			
 		}
 		//document.getElementById(formId).blur();
 	}

 	this.enableAllFormElements = function(obj)
 	{
 		var els = obj;//document.getElementById(formId);
 		for(i=0;i<els.length;i++)
 			if(els[i]&&els[i]!=null)
 			els[i].disabled = false;
 		//document.getElementById(formId).blur();
 	}
 	
 	
  	this.finPosition = function(obj) {
		var curleft = curtop = 0;

		var jq = jQuery(document.getElementById(this.toggleDivId)); 
		var position = jq.offset(); 

		curleft = position.left;
		curtop = position.top;
		return [curleft,curtop];
	}
		
 	this.init = function(formId,divId){

 			if (!document.getElementById(formId)){
 				this.errMsg = 'Not a valid form Id';
 				return false;
 			}

 				this.formId = formId;
 				this.toggleDivId = divId;

 				this.disabledFormElements = new Array();
 				
 				var loadIc = document.createElement('div');
 		 		var loadDiv = document.createElement('div');
 		 		
 		 		loadIc.style.cssText = 'position:absolute;display:none;';
 		 		loadDiv.className = 'loadingDiv';
 		 		
 				var loadImg = document.createElement('img');
 				loadImg.src = '/images/loading.gif';

 				
 				document.getElementById("formContainer").appendChild(loadIc);
 				document.getElementById("formContainer").appendChild(loadDiv);
 				
 				
 				loadIc.appendChild(loadImg);

 				this.loaderIcon = loadIc;
 				this.loadingDiv = loadDiv;
 				
 				
 				this.formObj = document.getElementById(this.formId);
 				this.jimetAjaxObj = new JimetAjaxObj();
 				this.jimetAjaxObj.setTimeout(30000);
 				this.jimetAjaxObj.setIsSubmit(true);
 				//alert ("FORM ID : " + this.formId + "DIV ID : " + this.toggleDivId +"FORM OBJ : " +this.formObj);

 		
 		return this.formObj;
 	};
 	
 	this.doValidate = function(formId,divId){
		if (!this.init(formId,divId)){
 			alert(this.errMsg);
			return; 	 	 
	 	}
	 	
	 	if(this.formObj.id!=formId)
	 	{
 				this.formId = formId;
 				this.toggleDivId = divId; 			
 				this.formObj = document.getElementById(formId);
	 	}
	 	this.formObj = document.getElementById(formId);
	 	
		this.toggleLoading(true);
		//alert(document.getElementById(this.toggleDivId+'Loading').style.display);
		this.disableAllFormElements(this.formId);		
	 	formEls = this.formObj.elements;
	 	
	 	var checkboxOrRadioLooped = new Array();
	 	
		this.postData = "formId" + "="+ encodeURIComponent(this.formId)+"&";
		this.postData += "action" + "="+ encodeURIComponent(this.formObj.action.value);
	
		for(i=0; i<formEls.length; i++) 
		{
			//alert(checkboxOrRadioLooped.length);
			if( formEls[i].type == 'text' || formEls[i].type == 'password' || formEls[i].type == 'checkbox' || formEls[i].type == 'radio' || formEls[i].type == 'hidden' || formEls[i].type == 'textarea' || formEls[i].type == 'select-one' || formEls[i].type == 'select-multiple')
			{
				if(formEls[i].type == 'hidden')
				{
			 		this.postData += "&"+formEls[i].name + "="+ encodeURIComponent(formEls[i].value);
			 		this.postData += "&"+formEls[i].name + "HtmlType=" + encodeURIComponent(formEls[i].type);					
				}
				else if( formEls[i].getAttribute('validator') )
				{
					args = formEls[i].getAttribute('validator').split(";");
										
					if(formEls[i].type == 'text' || formEls[i].type == 'password' || formEls[i].type == 'select-one' || formEls[i].type == 'textarea')
				 	{
				 		this.postData += "&"+formEls[i].name + "="+ encodeURIComponent(formEls[i].value);
				 		this.postData += "&"+formEls[i].name + "HtmlType=" + encodeURIComponent(formEls[i].type);
			
				 		this.postData += "&"+formEls[i].name + "Required=" + encodeURIComponent(args[0]);
				 		this.postData += "&"+formEls[i].name + "ValidationType=" + encodeURIComponent(args[1]);
				 		this.postData += "&"+formEls[i].name + "MinLength=" + encodeURIComponent(args[2]);
				 		this.postData += "&"+formEls[i].name + "MaxLength=" + encodeURIComponent(args[3]);
				 		this.postData += "&"+formEls[i].name + "RealLifeName=" + encodeURIComponent(args[4]);
			 		}
			 		else if(formEls[i].type == 'checkbox' || formEls[i].type == 'radio')
			 		{
		 				inputs = document.getElementsByName(formEls[i].name);
//		 				alert(inputs.length);
			 			var checkedInputs = new Array();			
//			 			alert(formEls[i].name +';'+ checkboxOrRadioLooped);
			 			var looped = false;
			 			
			 			for(f=0;f<checkboxOrRadioLooped.length;f++)
			 			{
			 				if(formEls[i].name==checkboxOrRadioLooped[f])
			 				{
			 					looped = true;
			 					break;
			 				}
			 			}
			 			
			 			
			 			if(!(looped))
			 			{
			 				for(k=0;k<inputs.length;k++)
			 				{	
//alert(inputs[k].parentNode.innerHTML + ':' + inputs[k].checked);
			 					if(inputs[k].checked)
			 					{
							 		checkedInputs.push(inputs[k]);
							 		checkboxOrRadioLooped.push(formEls[i].name);
			 					}
			 				}

			 				if(checkedInputs.length>1)
			 				{
			 					for(k=0;k<checkedInputs.length;k++)
			 					{
							 		this.postData += "&"+formEls[i].name + "="+ encodeURIComponent(checkedInputs[k].value);
							 		this.postData += "&"+formEls[i].name + "HtmlType=" + encodeURIComponent(checkedInputs[k].type);
							 		this.postData += "&"+formEls[i].name + "Required=" + encodeURIComponent(args[0]);
							 		this.postData += "&"+formEls[i].name + "ValidationType=" + encodeURIComponent(args[1]);
							 		this.postData += "&"+formEls[i].name + "MinLength=" + encodeURIComponent(args[2]);
							 		this.postData += "&"+formEls[i].name + "MaxLength=" + encodeURIComponent(args[3]);
							 		this.postData += "&"+formEls[i].name + "RealLifeName=" + encodeURIComponent(args[4]);			 				
							 	}
			 				}
			 				else 
				 			{
				 						var st = +checkedInputs.length==1?checkedInputs[0].value:'';
								 		this.postData += "&"+formEls[i].name + "="+st;
								 										 		this.postData += "&"+formEls[i].name + "HtmlType=text";
								 		this.postData += "&"+formEls[i].name + "Required=" + encodeURIComponent(args[0]);
								 		this.postData += "&"+formEls[i].name + "ValidationType=" + encodeURIComponent(args[1]);
								 		this.postData += "&"+formEls[i].name + "MinLength=" + encodeURIComponent(args[2]);
								 		this.postData += "&"+formEls[i].name + "MaxLength=" + encodeURIComponent(args[3]);
								 		this.postData += "&"+formEls[i].name + "RealLifeName=" + encodeURIComponent(args[4]);
								 		checkboxOrRadioLooped.push(formEls[i].name);
								 		//checkboxOrRadioLooped = formEls[i].name;
				 			}
			 				
			 			}
			 			
			 		}
			 		else if(formEls[i].type == 'select-multiple')
			 		{
			 			var multiOptions = formEls[i].options;

			 			if(formEls[i].name.match("SelectedValues"+"$")=="SelectedValues")
			 			{
			 				for(k=0;k<multiOptions.length;k++)
			 					multiOptions[k].selected = true;
			 			}

			 			var selectedMultiples = new Array();
			 			for(k=0;k<multiOptions.length;k++)
			 			{
			 				if(multiOptions[k].selected)
			 				{

							 		selectedMultiples.push(multiOptions[k]);
			 				}
			 			}
			 			
			 			if(!(selectedMultiples.length>1))
			 			{			
			 						var st = selectedMultiples.length==1?selectedMultiples[0].value:'';
							 		this.postData += "&"+formEls[i].name + "="+st;
							 		this.postData += "&"+formEls[i].name + "HtmlType=text";
							 		this.postData += "&"+formEls[i].name + "Required=" + encodeURIComponent(args[0]);
							 		this.postData += "&"+formEls[i].name + "ValidationType=" + encodeURIComponent(args[1]);
							 		this.postData += "&"+formEls[i].name + "MinLength=" + encodeURIComponent(args[2]);
							 		this.postData += "&"+formEls[i].name + "MaxLength=" + encodeURIComponent(args[3]);
							 		this.postData += "&"+formEls[i].name + "RealLifeName=" + encodeURIComponent(args[4]);			 			
			 			}
			 			else
			 			{
			 				for(k=0;k<selectedMultiples.length;k++) 
			 				{
							 		this.postData += "&"+formEls[i].name + "="+ encodeURIComponent(selectedMultiples[k].value);
							 		this.postData += "&"+formEls[i].name + "HtmlType=" + encodeURIComponent(formEls[i].type);
							 		this.postData += "&"+formEls[i].name + "Required=" + encodeURIComponent(args[0]);
							 		this.postData += "&"+formEls[i].name + "ValidationType=" + encodeURIComponent(args[1]);
							 		this.postData += "&"+formEls[i].name + "MinLength=" + encodeURIComponent(args[2]);
							 		this.postData += "&"+formEls[i].name + "MaxLength=" + encodeURIComponent(args[3]);
							 		this.postData += "&"+formEls[i].name + "RealLifeName=" + encodeURIComponent(args[4]);			 			
			 				}
			 			}
			 			
			 		}			 		
			 		
				}
				
			//alert(this.formObj.elements[i].type);
			}
			
		}

//alert(this.postData);
//		this.postData = this.postData.replace(/%25/g,'%BD'); //send % char as ½
//		this.postData = this.postData.replace(/%2B/g,'%BC'); //send + char as ¼

		var self = this;
		this.jimetAjaxObj.setPostData(this.postData);
		this.formValidationResult = function(result){
			//alert(result);
			if(result=='ajaxError'){
				self.toggleLoading();
				ajaxOnWork = false;
				self.enableAllFormElements(self.disabledFormElements);
				return;
			}
			
			var numOfErrors = 0;

			var qs = new Querystring(decodeURI(result));
				
			if(qs.get('objectId'))
				self.resultObjId = qs.get('objectId'); 
			
			if(qs.get('objectName'))
				self.resultObjName = qs.get('objectName'); 

//			alert("validatorden:"+self.resultObjId);
			
			self.formObj = document.getElementById(qs.get("formId"));
			for(i=0; i<self.formObj.elements.length; i++) 
			{  
				if(document.getElementById(self.formObj.elements[i].name+'ErrMsg')){
					document.getElementById(self.formObj.elements[i].name+'ErrMsg').innerHTML = '';   
					document.getElementById(self.formObj.elements[i].name+'ErrMsg').style.display = 'none';
				}   
			}	

			for(i=0; i<self.formObj.elements.length; i++) 
			{        
				if(document.getElementById(self.formObj.elements[i].name+'ErrMsg') && ( (self.formObj.elements[i].getAttribute('validator')) || (self.formObj.elements[i].name=='it') || (self.formObj.elements[i].name=='combobox3') ) )
				{
					if(qs.get(self.formObj.elements[i].name+"ErrMsg") != 'ok') 
			   		{ //alert(this.formObj.elements[i].name+"ErrMsg = " +qs.get(this.formObj.elements[i].name+"ErrMsg"));
			   			document.getElementById(self.formObj.elements[i].name+'ErrMsg').style.display = 'block';
				   		document.getElementById(self.formObj.elements[i].name+'ErrMsg').innerHTML = qs.get(self.formObj.elements[i].name+"ErrMsg");        
						numOfErrors++;
					}
				}
			}
				
			if (qs.get("resultErrMsg"))
				numOfErrors++;	
			//alert (numOfErrors+" : " +qs.get("resultErrMsg")+" : " +qs.get("resultMsg"))
			self.enableAllFormElements(self.disabledFormElements);
			self.toggleLoading(true);
			
			ajaxOnWork = false;
			if (self.endFunction)
				self.endFunction(numOfErrors,qs.get("resultErrMsg"),qs.get("resultMsg"));

			self.toggleLoading(true);
			
			self.toggleLoading();

			this.executeScript(result);			
		};

		this.jimetAjaxObj.doPost(this.url,this.formValidationResult);
	
 	};


	this.executeScript = function(content) 
	{
//		alert(resp);// <script src=source.js olanlanlar? ay?klayal?m
    var src = new RegExp('<script.*?src=".*?"');
    var repl = new RegExp('<script.*?src="');
    var matches = src.exec(content);

	/**ilerde jsin i?inden script ?al??t?rmak istersek kullan?l?r
	   if (matches != null)
	    {
	        for (i = 0; i < matches.length; i++)
	        {
	            // source dosyas?n?n ad?
	            var scriptSrc = matches[i].replace(repl, '');
	            scriptSrc = scriptSrc.substring(0, scriptSrc.length-1);
	
	        }
	    }
	*/
	repl = new RegExp('<script.*?src=".*?".*?</script>');
    content = content.replace(repl, '');

    // inline scriptler i?in

    // script commentlerini kald?r
    repl = new RegExp('//.*?$', 'gm');
    content = content.replace(repl, '\n');

    // enterlar? clean
    repl = new RegExp('[\n\r]', 'g');
    content = content.replace(repl, ' ');

    //<script> i?indeki her?eyi match et
    src = new RegExp('<script.*?</script>', 'g');
    matches = content.match(src);


    if (matches != null)
    {
        for (i = 0; i < matches.length; i++)
        {
            // ba?taki tag? kald?r
            var repl = new RegExp('<script.*?>', 'gm');
            var script = matches[i].replace(repl, '');

            // sondaki tag? kald?r
            repl = new RegExp('</script>', 'gm');
            script = script.replace(repl, '');

            // comutlar? ?al??t?r
            setTimeout(script, 250);
        }
    }
	

	} 	
	
	this.setEndFunction = function(endFunc){
		this.endFunction = endFunc;
	};
	
	this.toggleLoading = function(show){
		
		var mainDiv = document.getElementById(this.toggleDivId);
		
		var pos = this.finPosition(mainDiv);
		
		if (mainDiv && this.loadingDiv){



			this.loaderIcon.style.position = 'absolute';
			
			if(show){
				this.loaderIcon.style.display = 'block';
				this.loaderIcon.className = 'none';
				this.loadingDiv.style.display = 'block';
				
				this.loadingDiv.style.zIndex = document.getElementById(this.toggleDivId).style.zIndex+1;
				this.loaderIcon.style.zIndex = this.loadingDiv.style.zIndex+1;
				this.loaderIcon.style.left = (pos[0]+(mainDiv.offsetWidth/2))-(this.loaderIcon.offsetWidth/2) + 'px';
				this.loaderIcon.style.top = pos[1]+((mainDiv.offsetHeight/2)) + 'px';
				
				this.loadingDiv.style.position = 'absolute';

				this.loadingDiv.style.height = (mainDiv.offsetHeight<this.loaderIcon.offsetHeight?this.loaderIcon.offsetHeight:mainDiv.offsetHeight) + 'px';
				this.loadingDiv.style.width = mainDiv.offsetWidth+ 'px';
				this.loadingDiv.style.left = pos[0]+ 'px';
				this.loadingDiv.style.top = pos[1]+ 'px';				
				
			}
			else{
				
				jQuery(this.loaderIcon).animate({opacity: 0}, "slow",function(){
					this.style.display = 'none';
					this.className = 'none';
					document.getElementById("formContainer").removeChild(this);

				});
				
				jQuery(this.loadingDiv).animate({opacity: 0}, "slow",function(){
					this.style.display = 'none';
					document.getElementById("formContainer").removeChild(this);

				}); 


			}
			
			

	

		}

	};
}
