// -*- coding: utf-8 -*-

//shows and hides proper operator-selects
function onFilterFieldChange(itemindex,sel) {
    var alltypes = ['n','i','t','d'];
    var objname = "o"+itemindex+"_";
    var obj;
    var id;
    if (sel && sel.selectedIndex > -1) {
	for (var i = 0; i < 4; i++) {
	    obj = document.getElementById(objname+alltypes[i]);
	    if (alltypes[i] == ftypes[sel.selectedIndex]) {
		obj.style.visibility = 'visible';
		obj.style.display = 'block';		
	    } else {
		obj.style.visibility = 'hidden';
		obj.style.display = 'none';
	    }
	}
	obj = document.forms['mainForm'].elements["v" + itemindex + "_value"];
	if (ftypes[sel.selectedIndex] == 'e') {
	    obj.style.visibility = 'hidden';
	    obj.style.display = 'none';
	    for (var j = 0; j < ftypes.length; j++) {
		if (ftypes[j] == 'e') {
		    obj = document.forms['mainForm'].elements["v" + itemindex + "_" + sel.options[j].value];
		    if (j == sel.selectedIndex) {
			obj.style.visibility = 'visible';
			obj.style.display = 'block';		
		    } else {
			obj.style.visibility = 'hidden';
			obj.style.display = 'none';
		    }
		}
	    }
	} else {
	    obj.style.visibility = 'visible';
	    obj.style.display = 'block';
	    if (ftypes[sel.selectedIndex] == 'n') {
		obj.value = '';
	    }
	    for (var j = 0; j < ftypes.length; j++) {
		if (ftypes[j] == 'e') {
		    obj = document.forms['mainForm'].elements["v" + itemindex + "_" + sel.options[j].value];
		    obj.style.visibility = 'hidden';
		    obj.style.display = 'none';
		}
	    }
	}
    }
}

//cleans all filter
function onFilterReset() {
    var field;
    var obj;
    var i = 1;
    field = document.forms['mainForm'].elements["f" + i + "_sel"];
    while (field) {
	field.selectedIndex = 0;
	onFilterFieldChange(i,field);
	obj = document.forms['mainForm'].elements["v" + i + "_value"];
	obj.value = "";

	i++;
	field = document.forms['mainForm'].elements["f" + i + "_sel"];
    }
    document.forms['mainForm'].elements["lo1"][0].checked = true;
    document.forms['mainForm'].elements["lo2"][0].checked = true;
}

//runs filter
function onFilterApply() {
    var hid;
    var field;
    var oper;
    var enm;
    var i = 1;
    if (filterIsValid()) {
	field = document.forms['mainForm'].elements["f" + i + "_sel"];
	while (field) {
	    //applied field
	    hid = document.forms['mainForm'].elements["f" + i];
	    hid.value = field.options[field.selectedIndex].value;
	    //applied operator
	    hid = document.forms['mainForm'].elements["o" + i];
	    oper = document.forms['mainForm'].elements["o" + i + "_" + ftypes[field.selectedIndex]];//visible operators
	    hid.value = oper.options[oper.selectedIndex].value;
	    //applied value
	    hid = document.forms['mainForm'].elements["v" + i];
	    if (ftypes[field.selectedIndex] == 'e') {
		enm = document.forms['mainForm'].elements["v" + i + "_" + field.options[field.selectedIndex].value];
		hid.value = enm.options[enm.selectedIndex].value;
	    } else {
		hid.value = document.forms['mainForm'].elements["v" + i + "_value"].value;
	    }
	    
	    i++;
	    field = document.forms['mainForm'].elements["f" + i + "_sel"];
	}
	//set sorting criteria and paging to default values
	document.forms['mainForm'].elements['x_offset'].value = 0;
	document.forms['mainForm'].elements['x_sort'].value = '';

	//and finally submit filter
	document.forms['mainForm'].submit();
    }
}

//validates filter
function filterIsValid() {
    var ret = true;
    var i = 1;
    var field = document.forms['mainForm'].elements['f'+i+"_sel"];
    var oper;
    var rowarr = new Array();
    var conf;
    var valobj;

    //next part for the case, that operator can have undefined value

    //      while (field) {
    // 	if (field.selectedIndex > 0) {
    // 	    oper = document.forms['mainForm'].elements["o" + i + "_" + ftypes[field.selectedIndex]];
    // 	    if (oper.selectedIndex <= 0) {
    // 		rowarr[rowarr.length] = i;
    // 	    };
    // 	};
    // 	i++;
    // 	field = document.forms['mainForm'].elements['f'+i+"_sel"];
    //     }
    //     if (rowarr.length > 0) {
    // 	if (rowarr.length > 1) {
    // 	    conf = 'Řádky ' + rowarr[0]; 
    // 	    for (var j = 1; j < rowarr.length - 1; j++) {
    // 		conf = conf + ', ' + rowarr[j];
    // 	    }
    // 	    conf = conf + ' a ' + rowarr[rowarr.length-1] + ' nejsou zcela vyplněny. Chcete spustit filtr bez použití těchto řádků ?';
    // 	    ret = self.confirm(conf) && ret;
    // 	} else {
    // 	    conf = 'Řádek ' + rowarr[0] + ' není zcela vyplněn. Chcete spustit filtr bez použití tohoto řádku ?';
    // 	    ret = self.confirm(conf) && ret;
    // 	}
    //     }
    //     if (!ret) return false;
    //     i = 1;
    //    field = document.forms['mainForm'].elements['f'+i+"_sel"];

    while (field) {
	if (field.selectedIndex > 0) {
	    oper = document.forms['mainForm'].elements["o" + i + "_" + ftypes[field.selectedIndex]];
	    //if (oper.selectedIndex > 0) {
		if (ftypes[field.selectedIndex] == 'e') {
		    valobj = null;
		} else {
		    valobj = document.forms['mainForm'].elements["v" + i + "_value"];
		}
		ret = fvalidate[field.selectedIndex](i,valobj) && ret;//fvalidate are validating functions, i is filter row index
		//};
	};
	i++;
	field = document.forms['mainForm'].elements['f'+i+"_sel"];
    }
    return ret;
}

//sorting of columns

function onSort(colindex) {
    //0..default, colindex..ASC, -colindex..DESC
    var srt = document.forms['mainForm'].elements["x_sort"];
    if (Number(srt.value) == Number(colindex)) {
	srt.value = (-1)*colindex;
    } else {
	srt.value = colindex;
    }
    document.forms['mainForm'].submit();
}

// validating functions 

function strNotBlank(input,alstr1,alstr2) {
    var str = input.value;
    if (str == '') {
	self.alert(alstr1);
	return false;
    };
    str = str.replace(/([ ,]*)/g,'');
    if ((alstr2 != '') && (str == '')) {
	self.alert(alstr2);
	return false;
    };
    return true;
}

function strNotEqual(input1,input2,alstr1,alstr2) {
    var str1 = input1.value;
    var str2 = input2.value;
    if (str1 == str2) {
	self.alert(alstr1);
	return false;
    };
    str1 = str1.replace(/([ ,]*)/g,'');
    str2 = str2.replace(/([ ,]*)/g,'');
    if ((alstr2 != '') && (str1 == str2)) {
	self.alert(alstr2);
	return false;
    };
    return true;
}

function dateIsDDMMYYYY(input,separator,alstr1,alstr2) {
    var str = input.value.replace(/([ ,]*)/g,'');
    if (alstr2 == '') {
	if (str == '') {
	    input.value = str;
	    return true;
	};
    } else {
	if (str == '') {
	    self.alert(alstr2);
	    return false;
	};
    };
    var arr = str.split(separator);
    var dat = new Date(arr[2], arr[1]-1, arr[0]);
    if (!   ((arr[2] == dat.getFullYear()) 
	  && (arr[1]-1 == dat.getMonth()) 
	  && (arr[0] == dat.getDate()))) {
	self.alert(alstr1);
	return false;
    };
    if (dat.getDate() < 10) {
	str = '0' + dat.getDate();
    } else {
	str = dat.getDate();
    }
    if (dat.getMonth() < 9) {
	str = str + '.0' + Number(dat.getMonth()+1);
    } else {
	str = str + '.' + Number(dat.getMonth()+1);
    }
    str = str + '.' + dat.getFullYear();
    input.value = str;
    return true;
}

function strIsFloat(input,alstr1,alstr2) {
    //assumes ' ' or ',' as marginal sep.
    var str = input.value.replace(/([ ,]*)/g,'');
    if (alstr2 == '') {
	if (str == '') {
	    input.value = str;
	    return true;
	};
    } else {
	if (str == '') {
	    self.alert(alstr2);
	    return false;
	};
    };
    var num = parseFloat(str);
    if (num != str) {
	self.alert(alstr1);
	return false;
    };
    input.value = str;
    return true;
}

function strIsInt4(input,isNotIntAlert,isBlankAlert,isBigAlert,isSmallAlert) {
    //assumes '.' as decimal sep. and ' ' or ',' as marginal sep.
    var str = input.value.replace(/([ ,]*)/g,'');
    if (isBlankAlert == '') {
	if (str == '') {
	    input.value = str;
	    return true;
	};
    } else {
	if (str == '') {
	    self.alert(isBlankAlert);
	    return false;
	};
    };
    var num = parseInt(str,10);//10 is radix
    if (num != str || ("_"+str) != ("_"+str.replace(/(\.)/g,''))) {
	self.alert(isNotIntAlert);
	return false;
    };
    if (isBigAlert != '' && num > 2147483647) {
	self.alert(isBigAlert);
	return false;
    };
    if (isSmallAlert != '' && num < -2147483648) {
	self.alert(isSmallAlert);
	return false;
    };
    input.value = str;
    return true;
}

function reset(t) {
    document.mainForm.reset();
}
