function formMultiChange(parentVal, parentId, type){
    newId = parentId + "-" + parentVal;
    childRel = formDependencies['childs'][parentId];


    if (type=='MC' && $("input#"+newId).attr('checked') == true ) {
        for (childRow in childRel ) {
            show(childRel[childRow]);
        }
    } else {
        for (childRow in childRel ) {
            remove(childRel[childRow]);
        }
    }

}

function formChange(parentId, type){

 childRel = formDependencies['childs'][parentId];

if (type=='O' && $("input[name='"+parentId+"'][checked=true]") != null && formDependencies[parentId][$("input[name='"+parentId+"'][checked=true]").val()] != null) {
        for (childRow in formDependencies[parentId][$("input[name='"+parentId+"'][checked=true]").val()] ) {

                    show(childRow);

         //   $("#tag"+childRow).css("display","block");
//            $("#tag"+childRow).fadeIn("slow");
        }    
} else if (type=='O') {
    for (childRow in childRel ) {
        remove(childRel[childRow]);
    }
} else if (type=='C' && $("input#"+parentId).attr('checked') == true) {
    for (childRow in childRel ) {


                    show(childRel[childRow]);

       // $("#tag"+childRel[childRow]).css("display","block");
        //$("#tag"+childRel[childRow]).fadeIn("slow");

    }
} else if (type=='C' && $("input#"+parentId).attr('checked') == false) {
    for (childRow in childRel ) {
        remove(childRel[childRow]);
    }
} else if (type=='S' && formDependencies[parentId][$("#"+parentId).val()] != null) {

    for (childRow in formDependencies[parentId][$("#"+parentId).val()] ) {
         $("#tag"+childRow+" select").empty();
         $("#mirrortag"+childRow+" select option").each(function(){
             if (formDependencies[parentId][$("#"+parentId).val()][$("#tag"+childRow+" select").attr("id")][$(this).val()] != null) {
               
                $("#tag"+childRow+" select").append($(this).clone());
             }
          });
          show(childRow);
    }
}  else if (type=='S') {
    for (selectedRow in formDependencies[parentId]) {
        for (childRow in formDependencies[parentId][selectedRow]) {
            remove(childRow);
        }
    }
}

}

function remove(toRemove) {
    $("#tag"+toRemove).fadeOut("slow");
    clearForm($("#tag"+toRemove))


    if (formDependencies['childs'][toRemove] != null) {
        for (childRow in formDependencies['childs'][toRemove] ) {
            remove(formDependencies['childs'][toRemove][childRow]);
        }
    }
}

function clearInput(elem) {
    elem.value= '';
}


function clearForm(form) {
  // iterate over all of the inputs for the form
  // element that was passed in
  $(':input', form).each(function() {
 var type = this.type;
 var tag = this.tagName.toLowerCase(); // normalize case
 // it's ok to reset the value attr of text inputs,
 // password inputs, and textareas
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = "";
 // checkboxes and radios need to have their checked state cleared
 // but should *not* have their 'value' changed
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 // select elements need to have their 'selectedIndex' property set to -1
 // (this works for both single and multiple select elements)
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};


function show(toShow) {
    $("#tag"+toShow).css("display","block");
    $("#tag"+toShow).fadeIn("slow");

    /*if (formDependencies['childs'][toShow] != null) {
        for (childRow in formDependencies['childs'][toShow] ) {
            show(formDependencies['childs'][toShow][childRow]);
        }
    }*/
}



function copyFiledContentTo(srcId, destId)
{

var srcVal = $("#"+srcId).val() ;
if (jQuery.trim($("#"+destId).val()) === ""){
	$("#"+destId).val(srcVal);
}

}


function catChange(){

	var wojewodztwoArray = new Array() ;
	wojewodztwoArray['*']="Wszystkie"; 
	for (var word in dependencies[$("#categories").val()]['w'])
		wojewodztwoArray[word] = word ;

	 /*
		var cityArray = new Array() ;
		cityArray['*']="Wszystkie"; 
		for (var word in dependencies[$("#categories").val()]['w']['mazowieckie']['c'])
			cityArray[word] = word ;*/
			
	$("#param_district").removeOption(/./);
	$("#param_district").addOption({'*':'Wszystkie'});
	$("#param_district").selectOptions('*') ;

	//cityArray.sort();
	//$("#param_city").addOption(cityArray, false);
	//$("#param_city").sortOptions(true) ; 
	$("#param_city").removeOption(/./);
	$("#param_city").addOption({'*':'Wszystkie'});
	$("#param_city").selectOptions('*') ;

	$("#param_wojewodztwo").removeOption(/./);
	$("#param_wojewodztwo").addOption(wojewodztwoArray, false);
	$("#param_wojewodztwo").sortOptions(true) ; 
	$("#param_wojewodztwo").selectOptions('*') ;


		var sellersArray = new Array() ; 
		sellersArray ['*']='Wszyscy' ; 
		for (var word in dependencies[$("#categories").val()]['d'])
			sellersArray[word] = dependencies[$("#categories").val()]['d'][word] ;
			
	$("#sellers").removeOption(/./);
	//sellersArray.sort();
	//sellersArray['*']="Wszyscy"; 
	$("#sellers").addOption(sellersArray, false);
	$("#sellers").sortOptions(true);
	$("#sellers").selectOptions('*') ;



}


function wojewodztwoChange(defaultValue){

var wojValue = defaultValue ; 
if (wojValue == null) wojValue = $("#param_wojewodztwo").val() ; 

if ($("#categories").val() != null && wojValue != null){
	$("#param_city").removeOption(/./);

		if (wojValue == '*' && defaultValue == null) return ; 
	 
		var citiesArray = new Array() ; 
		citiesArray ['*']='Wszystkie' ; 
		for (var word in dependencies[$("#categories").val()]['w'][wojValue]['c'])
			citiesArray[word] = word ;
			
	//districtArray.sort() ;
	//districtArray["*"]="Wszystkie" ;

	$("#param_city").addOption(citiesArray, false);
	$("#param_city").sortOptions(true);
	$("#param_city").selectOptions('*');
	
	$("#param_district").removeOption(/./);
	$("#param_district").addOption({'*':'Wszystkie'});
	$("#param_district").selectOptions('*') ;
	
	$("#param_wojewodztwo").selectOptions(wojValue);	
	
	}
	
}


function cityChange(){

if ($("#categories").val() != null && $("#param_city").val() != null){
	$("#param_district").removeOption(/./);

		if ($("#param_city").val() == '*') return ; 
	 
		var districtArray = new Array() ; 
		districtArray ['*']='Wszystkie' ; 
		for (var word in dependencies[$("#categories").val()]['w'][$("#param_wojewodztwo").val()]['c'][$("#param_city").val()]['d'])
			districtArray[word] = word ;
			
	//districtArray.sort() ;
	//districtArray["*"]="Wszystkie" ;
	$("#param_district").addOption(districtArray, false);
	$("#param_district").sortOptions(true);
	$("#param_district").selectOptions('*');
	
	}
	
}

function onLoad(){
if ($("#searchForm").val() != null) {onSearchLoad(); return; }
if ($("#subproductId").val() != null) onAssignLoad();
}

function onAssignLoad(){
	$("#subproductId").autocomplete(subProducts,  {
		matchContains: true,
		minChars: 0
	});
}

function searchSubproducts(){
//zdarzenie onFocus na subProduct nie oprogramowane
}


function onSearchLoad(){

var city_sel = $("#param_city").selectedValues() ;
var dist_sel = $("#param_district").selectedValues(); 
var cate_sel = $("#categories").selectedValues() ;
var sell_sel = $("#sellers").selectedValues() ;
var woj_sel = $("#param_wojewodztwo").selectedValues() ;


if (!cate_sel[0] ) { cate_sel[0] = 2;  $("#categories").selectOptions(cate_sel) ; }
	catChange() ; 

if (!woj_sel[0] ) { woj_sel[0] = 'mazowieckie';  $("#param_wojewodztwo").selectOptions(woj_sel) ; }
	wojewodztwoChange(woj_sel[0]);

	
	if (city_sel.length > 0 ){
		if (city_sel[0]){
			$('#param_city option').attr('selected', false); 
			$("#param_city").selectOptions(city_sel) ;
			cityChange(); 

			if (dist_sel[0]){
				$('#param_district option').attr('selected', false); 
				$("#param_district").selectOptions(dist_sel) ;
			}else{
			}

		}
	}

	if (sell_sel[0]){
		$('#sellers option').attr('selected', false); 
		$("#sellers").selectOptions(sell_sel) ;
	}


}
