$(document).ready( function(){
	
	
function cleanDisabled(){

	$(this).parents('div.form_element:first,div.fromto:first,div.form_element_flatCheckbox:first');
	
	$("div.form_element_group").each(function(e){
		
			var myGroup = $(this);
			var countElem = 0;
			
			$(this).children("div.form_element").each(function(e){
				
				if(($(this).css('display'))!='none'){
					countElem ++;
				}
			})
			if(countElem>0){
				$(this).css('display', 'block');
			}else{
				$(this).css('display', 'none');
			}

	});
}


	//----------------------- FROM Assign Dependencies----------------------------------------------------------------

	
	$("form[jsdepend='true']").each(function(e){

		$(this).removeClass("bewaw_form_init");			
		$("input[depend],select[depend],textarea").each(function(e){

				var childInput = $(this);
					
				var parentDiv = $(this).parents('div.form_element:first,div.fromto:first,div.form_element_flatCheckbox:first');
				
					$(parentDiv).css("display","none");
					
					if ($.evalJSON($(this).attr('depend') )[0] !== undefined) {
						
						var arrMultiDepend = $.evalJSON($(this).attr('depend') ) ;						
											
						$(arrMultiDepend).each(function(keyDepend){
						
							var MultiDependOn = arrMultiDepend[keyDepend].dependon;							
							
							$('#' + MultiDependOn).change(function(){
			
								var arrCompareResults = [] ;
								
								// check ALL MULTIPLE conditionals		
								$(arrMultiDepend).each(function(subkeyDepend){	
										
										var parentInputValue ='';
										
										if ($('#'+arrMultiDepend[subkeyDepend].dependon).attr('type') !== 'checkbox') {
											parentInputValue = $('#'+arrMultiDepend[subkeyDepend].dependon).val();
										}
										else {
											parentInputValue = $('#'+arrMultiDepend[subkeyDepend].dependon).attr('checked');	
										}										
										
										if (in_array('*', arrMultiDepend[subkeyDepend].arrtocompare) && parentInputValue!='' && parentInputValue!='null' && parentInputValue!=null ) {			
													
										}else{
												
											if (!in_array(parentInputValue, arrMultiDepend[subkeyDepend].arrtocompare)) {
												
													arrCompareResults[subkeyDepend] = false;
												}
												
											}	
								});
								
								var childName = childInput.attr('name');	
										
									if (!in_array(false, arrCompareResults)) {
										
										$("input[name=" + childName + "]").removeAttr("disabled");
										$(childInput).removeAttr("disabled");
										$(childInput).change();
										$(parentDiv).css("display", "block");
										
									}
									else {
		
										$("input[name=" + childName + "]").attr('disabled', 'disabled');
										$(childInput).attr('disabled', 'disabled');
										$(childInput).change();
										$(parentDiv).css("display", "none");
									}
								
							});
						});
					
					}else{
											
						var arrDepend = $(this).attr('depend');
						
						var SingleDependOn = $.evalJSON(arrDepend).dependon;
						var SingleArrToCompare = $.evalJSON(arrDepend).arrtocompare;

						$('#' + SingleDependOn).change(function(){				
							
							// check parent type
							if ($(this).attr('type') !== 'checkbox') {
								var parentInputValue = $(this).val();
							}
							else {
								var parentInputValue = $(this).attr('checked');
							}
							
							var childName = childInput.attr('name');							
							// check conditionals
							
							if(childName == 'sector1'){
								alert(dump(SingleArrToCompare));
							}
							
							
							if(in_array('*', SingleArrToCompare ) && parentInputValue!='' && parentInputValue!='null' && parentInputValue!=null ){
								
								$("input[name="+childName+"]").removeAttr("disabled");
								$(childInput).removeAttr("disabled");
								$(childInput).change();
								$(parentDiv).css("display", "block");	
							
							}else{
							
								if (in_array(parentInputValue, SingleArrToCompare)) {
									
									$("input[name="+childName+"]").removeAttr("disabled");
									$(childInput).removeAttr("disabled");
									$(parentDiv).css("display", "block");
									
									$(childInput).change();
									
								}
								else {
	
									$("input[name="+childName+"]").attr('disabled','disabled');
									$(childInput).attr('disabled','disabled');
									$(childInput).change();
									$(parentDiv).css("display", "none");
								}
							
								
								
								
							}
							
						});
				}
		
		//hide empty groups !
		}).change(function(){
			
			/*
			if (console && "firebug" in console) {
			  	
         		 console.log('change !');
         	}
			*/
			cleanDisabled();
				
		})
		
		
		;
		
	});
	//----------------------- FROM Assign MultiSelect Fill ----------------------------------------------------------------
	
	$("select[submulti]").each(function(e){
			
			var childInput = $(this);
			
			var arrSubMulti = $.evalJSON($(this).attr('subMulti') );		
						
			$('#'+arrSubMulti['parentId']).change(function () {
			
				var parentValue = $(this).val();
			
				if (parentValue !== null && parentValue!='') {
					
					var jsonParentValue =  $.toJSON(parentValue);
					
					$.getJSON(arrSubMulti['url'], {'parentValue': jsonParentValue,'parentId': arrSubMulti['parentId'],ajax: 'true'}, function(data){	
									
									var xhtml ='';
									if(data['']!=''){	
										for (key in data) {
											xhtml += '<option value = "' + key + '" >' + data[key] + '</option>';
										}									 	
									}
									$(childInput).html(xhtml);
									$(childInput).change();
							});
					
				}
			});	

	});	
	/*
		var selected = new Array();
	 
    			// safe them when you click the mouse
				$("#$parentInput").change(function () {
	
							if (arrValue === undefined) {
								var arrValue = {};
							}
		
							arrValue.$parentInput = $(this).val();
							
							jsonParentValue =  $.toJSON(arrValue);
														
							var xhtml = '';
							
							if($(this).val() !== null ){
															
								$.getJSON('$url',{'parentValue':jsonParentValue ,'parentID':parentID, ajax: 'true'},function(data){
																			
										for (key in data) {
											
											xhtml += '<option value = "' + key + '" >' + data[key] + '</option>';
										
										}
									
									if(xhtml != ''){
										
										$("#$strName").removeAttr("disabled");
									
									}else{
										
										$("#$strName").attr("disabled","disabled");
									
									}
								 	$("#$strName").html(xhtml);
									$("#$strName").change();	
								})
							
							}else{
								
								$("#$strName").attr("disabled","disabled");
								$("#$strName").html(xhtml);
								$("#$strName").change();	
								
				}
	}) ;
	*/			
	
	
	
	
	//----------------------- FROM Assign INFOTEXT Dependencies----------------------------------------------------------------
	
	
	$("form[jsdepend='true']").each(function(e){
		$(this).removeClass("bewaw_form_init");

		$("input[dependinfo],select[dependinfo],textarea[dependinfo]").each(function(e){

				var childInput = $(this);	
					
				var parentDiv = $(this).parents('div.form_element:first,div.fromto:first,div.form_element_flatCheckbox:first');
				
				var childInfotext = $(parentDiv).children('img.infoText:first') ;
				
				$(childInfotext).css("display", "none");
						
					if ($.evalJSON($(this).attr('dependinfo') )[0] !== undefined) {
						
						var arrMultiDepend = $.evalJSON($(this).attr('dependinfo') ) ;						
											
						$(arrMultiDepend).each(function(keyDepend){
						
							var MultiDependOn = arrMultiDepend[keyDepend].dependon;							
							
							$('#' + MultiDependOn).change(function(){	
								
								var arrCompareResults = [] ;
								
								// check ALL MULTIPLE conditionals
								$(arrMultiDepend).each(function(subkeyDepend){	
										
										if ($('#'+arrMultiDepend[subkeyDepend].dependon).attr('type') !== 'checkbox') {
											var parentInputValue = $('#'+arrMultiDepend[subkeyDepend].dependon).val();
										}
										else {
											var parentInputValue = $('#'+arrMultiDepend[subkeyDepend].dependon).attr('checked');	
										}

										if (!in_array(parentInputValue,  arrMultiDepend[subkeyDepend].arrtocompare) ) {
											arrCompareResults[subkeyDepend] = false;
										}	
								});
								var childName = childInput.attr('name');	
																								
								if (!in_array(false, arrCompareResults)) {
									$(childInfotext).css("display", "inline-block");
								
								}else {

									$(childInfotext).css("display", "none");
								}
								
								
							});
						});
					
					}else{
											
						var arrDepend = $(this).attr('dependinfo');
						
						var SingleDependOn = $.evalJSON(arrDepend).dependon;
						var SingleArrToCompare = $.evalJSON(arrDepend).arrtocompare;

						$('#' + SingleDependOn).change(function(){				
							
							// check parent type
							if ($(this).attr('type') !== 'checkbox') {
								var parentInputValue = $(this).val();
							}
							else {
								var parentInputValue = $(this).attr('checked');
							}
							
							var childName = childInput.attr('name');							
							// check conditionals
							if (in_array(parentInputValue, SingleArrToCompare)) {							
								$(childInfotext).css("display", "inline-block");	
							}
							else {
								$(childInfotext).css("display", "none");
							}
							
						});
				}					
		});
	});
	
	
	//----------------------- FROM Assign Select Dependencies----------------------------------------------------------------
		
		$("select[dependselect]").each(function(e){
			
			var childInput = $(this);
			var childInputID = childInput.attr('id') ;
			
			var selectedItem = $(childInput).val();
			/*if (console && "firebug" in console) {
				var myvar =  $(childInput).val();
				console.log(myvar);
			}*/
			
			//save init value
			var arrInitValue = new Array();
			$("#"+childInputID+" option").each(function(opt){
				currVal = $(this).attr('value');
				currText = $(this).text();
				if (currVal.length > 0) {
					arrInitValue[opt] = new Array();
					arrInitValue[opt]['text'] = currText;
					arrInitValue[opt]['id'] = parseInt(currVal,10);
				}
			});

			var arrDependSelect = $.evalJSON($(this).attr('dependselect') );
			
			var parentID = '' ;

			for (key in arrDependSelect) {
				 parentID = key;
			};

				$('#' + parentID).change(function(){
					
					var parentInputValue = $(this).val();
					
					var ArrToCompare = arrDependSelect[parentID][parentInputValue];
					
					//select each options of childinput
					$(childInput).empty();
					
					curr_id = jQuery.trim($(this).val());

 					if(childInput.attr('fixedoptions') ){
												
						var arrfixedoptions = $.evalJSON($(childInput).attr('fixedoptions') );

						for (key in arrfixedoptions) {
								var label = arrfixedoptions[key];
								var value = key;
								$(childInput).append('<option value="'+value+'">'+label+'</option>');
						};
					}else{
								$(childInput).append('<option value=""></option>');
					}
					
					for (key in arrInitValue) {
						if (in_array(arrInitValue[key]['id'], ArrToCompare)) {
							if(selectedItem==arrInitValue[key]['id']){
								currSelected = 'selected="selected"';
							}else{
								currSelected = '';
							}
							$(childInput).append('<option value="'+arrInitValue[key]['id']+'"'+currSelected+'>'+arrInitValue[key]['text']+'</option>');
						
						}
					};
				
				$(childInput).change();				
				//console.log('change refill');
				});
	
			
		})


//----------------------- FROM Assign GET JSON DATA ----------------------------------------------------------------
	
//	$("form[jsjsondata='true']").each(function(e){
	
		$("input[getjson],select[getjson]").each(function(e){
	
				var arrGetJson = $(this).attr('getjson') ;
				var toFillInput = $(this);
								
				var listenOn = 		$.evalJSON(arrGetJson).listenon ;
				var url = 		$.evalJSON(arrGetJson).url ;
				
				
				/*
				var table = 		$.evalJSON(arrGetJson).table ;
				var linkField = 	$.evalJSON(arrGetJson).linkField ;
				var labelField = 	$.evalJSON(arrGetJson).labelField ;
				var valueField = 	$.evalJSON(arrGetJson).valueField ;
				var joinSelect = 	$.evalJSON(arrGetJson).joinSelect ;
				*/
				
				$('#'+listenOn).change(function () {
						
						
						if($(this).val().length>1){
							var parentVal =  $.toJSON($(this).val());
						}else{
							var parentVal = $(this).val();
						}
						//var listenOnInputValue = $(this).val();
						
						//alert(dump(listenOnInputValue) );
						
						$.getJSON(url,{'parentValue':parentVal },function(data){
									
									if( $(toFillInput).attr("multiple")== undefined ){
										var xhtml = '';
									}else{
										var xhtml = '<option value = "" ></option>';
									}

									for (key in data) {
										xhtml += '<option value = "' + key + '" >' + data[key] + '</option>';
									}
							
								if(xhtml != '<option value = "" ></option>'|| xhtml != '' ){
									$(toFillInput).removeAttr("disabled");
								
								}else{
									$(toFillInput).attr("disabled","disabled");
								
								}
								
						
						$(toFillInput).html(xhtml);		
						$(toFillInput).change();	
						});		
								
								
				});
		});
	$("#id_action").change();
	//$("#id_type").change();
	});
//});


