//DEBUG
var REG_DATEFORMAT = 'd.m.Y';
var REG_TIMEFORMAT = 'h:min:s';
var REG_DECIMALPOINTSYMBOL = ',';
var REG_THOUSANDSSEPARATOR = ' ';

// ********************
// Funkce pro overovani dat: nalezeni elementu
// ********************
function getinputs(e,tagname){
	act=e.parentNode;
	while ((act.tagName != 'form') && (act.tagName != 'FORM')){
		act=act.parentNode;
		if ((act.tagName=='body') || (act.tagName=='BODY')) return array();
	}
	tagname=tagname.toUpperCase()
	fel=act.getElementsByTagName(tagname);
	alert (fel[0].name+': '+fel[0].checked+'  '+fel[0].type);
	return false;
	return fel;
}

// ********************
// Funkce pro overovani dat: kontrola prazdnych poli
// ********************

function checkEmptyVal(str,description,message) {
// Otestuje, jestli je v poli vyplnena hodnota (vsechny prvky krome Datefield/Timefield/Checkbox)
// Vraci hlaseni (pokud je prazdne, test je v poradku)
	str = str.replace(/\s*/g,'')
	if(str == "") {
		if (message)
		return message;
		else
		return "'"+description+"' je povinné pole.\n";
	}
	else return "";
}

function checkEmptyHTMLVal(str,description,message){
// Otestuje, jestli neni hodnota v HTML editoru prazdna (je prazna pokud neobsahuje zadny text nebo obrazky)
	str = str.replace(/\s*/g,'');
	//document.write(str);
	//vsechny obrazky v textu se nahradi textem img
	str = str.replace(/<img[^>]*>/g, 'img');
	//document.write(str);
	//odstrani se vsechny tagy
	str = str.replace(/<[^>]*>/g,'');
	//document.write(str);
	if(str == "") {
		if (message)
		return message;
		else
		return "'"+description+"' je povinné pole.\n";
	}
	else return "";
}

function checkEmptyDateVal(str,description,message) {
// Otestuje, jestli je v Datefield poli vyplnena hodnota
// Vraci hlaseni (pokud je prazdne, test je v poradku)
	if ((str == ""))
		if (message)
		return message;
		else
		return "'"+description+"' je povinné pole.\n";
	else return "";
}

function checkEmptyTimeVal(str,description,message) {
// Otestuje, jestli je v Timefield poli vyplnena hodnota
// Vraci hlaseni (pokud je prazdne, test je v poradku)
	if ((str == ""))
		if (message)
		return message;
		else
		return "'"+description+"' je povinné pole.\n";
	else return "";
}

function checkEmptyCheckboxVal(str,description,message) {
// Otestuje, jestli je v poli vyplnena hodnota (vsechny prvky krome Datefield/Timefield)
// Vraci hlaseni (pokud je prazdne, test je v poradku)
	if (str == "" || str == "NULL")
		if (message)
		return message;
		else
		return "'"+description+"' je povinné pole.\n";
	else return "";
}

// ********************
// Funkce pro overovani dat: kontrola datovych typu
// ********************

function checkIntVal(str,description,message) {
// Otestuje, zda je v poli celociselna hodnota (nebo prazdna)
// Plati i zaporna hodnota, nebo cele cislo z oddelovaci tisicu
// Vraci hlaseni (pokud je prazdne, test je v poradku)
	if (str=="") return "";
	var n,i,zerocount=0,badnumber=0,minusalready=0
	str=new String(str);
	n=str.length;
	for(i=0;i<n;i++) {
		if (str.charAt(i)!="-") minusalready=1;
		if ((str.charAt(i)<"0" || str.charAt(i)>"9") && (str.charAt(i)!="-")) badnumber=1;
		if (str.charAt(i)=='-' && minusalready==1) badnumber=1;
	}
	if (badnumber==1) {
		if (message)
		return message;
		else
		return "Hodnota pole '"+description+"' není ve správném formátu (celé číslo).\n";
	} else {
		return "";
	}
}

function checkFloatVal(str,description,message) {
// Otestuje, zda je v poli ciselna (realna) hodnota (nebo prazdna).
// Plati i zaporna hodnota, cele cislo z oddelovaci tisicu, desetinna mista
// Vraci hlaseni (pokud je prazdne, test je v poradku)
	var str,n,i,badnumber=0,pointcount=0,pointlocation,minusalready=0
	str=new String(str);
	n=str.length;
	for(i=0;i<n;i++) {
		if (str.charAt(i)!="-") minusalready=1;
		if (str.charAt(i)==REG_DECIMALPOINTSYMBOL) {
			pointcount++; //pocita carky
			if (pointcount>1) break;
			pointlocation=i;
		} else {
			if ((str.charAt(i)<"0" || str.charAt(i)>"9") && (str.charAt(i)!=REG_THOUSANDSSEPARATOR) && str.charAt(i)!="-" ) badnumber=1;
			if (str.charAt(i)=='-' && minusalready==1) badnumber=1;
		}
	}
	if (pointcount>1) badnumber=1; // dve nebo vice tecky nebo carky - spatne zadano
	if (badnumber==1) {
		if (message)
		return message;
		else
		return "Hodnota pole '"+description+"' není ve správném formátu (číslo).\n";
	}
	return "";
}

function checkDateVal(str,description,message) {
// Otestuje, zda je v poli datumova hodnota (nebo prazdna).
// Vraci hlaseni (pokud je prazdne, test je v poradku)
	var str2="",i,n,slash=0,na=0,nb=0,nc=0,str2error=0, dd="",mm="",yyyy=""
	str=new String(str);
	n=str.length;
	if(REG_DATEFORMAT=='m/d/Y') pointsymbol="/";
	else if(REG_DATEFORMAT=='d.m.Y') pointsymbol=".";
	else if(REG_DATEFORMAT=='Y-m-d') pointsymbol="-";
	else return 'Application error: Incorrect date format';
	if (str=="") return "";
	for(i=0;i<n;i++) {
		if (((str.charAt(i)>="0" && str.charAt(i)<="9") || str.charAt(i)==pointsymbol)) {
			if (str.charAt(i)==pointsymbol) slash++;
			else {
				if (slash==0) {
					na++;
					if (REG_DATEFORMAT=='m/d/Y') mm+=str.charAt(i);
					else dd+=str.charAt(i);
				}
				if (slash==1) {
					nb++;
					if (REG_DATEFORMAT=='m/d/Y') dd+=str.charAt(i);
					else mm+=str.charAt(i);
				}
				if (slash==2) {
					nc++;
					yyyy+=str.charAt(i);
				}
			}
		} else str2error=1;
	}
	if (slash>2) str2error=1;
	if (mm>12) str2error=1;
	if ((mm==1 || mm==3 ||mm==5||mm==7||mm==8||mm==10||mm==12) && dd>31) str2error=1;
	if ((mm==4 ||mm==6||mm==9||mm==11) && dd>30) str2error=1;
	if (mm==2 && dd>29 && !(yyyy%4)) str2error=1;
	if (mm==2 && dd>28 && yyyy%4) str2error=1;
	if (nc!=4) str2error=1;
	if (str2error && str!="")
		{if (message)
		return message;
		else
		return "Hodnota pole '"+description+"' není ve správném formátu (datum).\n";
		}
	return "";
}

function checkTimeVal(str,description,message) {
// Otestuje, zda je v poli casova hodnota (nebo prazdna).
// Vraci hlaseni (pokud je prazdne, test je v poradku)
	var str2="",i,n,slash=0,na=0,nb=0,nc=0,str2error=0, hh="",mm="";
	var firstpoint=0,secondpoint=0,hour='',minute='',second='';
	str=new String(str);
	n=str.length;
	if (n==0) return '';
	if(REG_TIMEFORMAT=='h:min:s') {
		pointsymbol=":";
		firstpoint = str.indexOf(pointsymbol);
		if(firstpoint==-1) str2error=1;
		hour=str.substr(0,firstpoint);
		secondpoint = str.substr(firstpoint+1).indexOf(pointsymbol)+firstpoint+1;
		minute=str.substr(firstpoint+1,secondpoint-firstpoint-1);
		second=str.substr(secondpoint+1);
		if(secondpoint==-1) str2error=1;
		if(hour>24) str2error=1;
		if(minute>59) str2error=1;
		if(second>59) str2error=1;
	} else return 'Application error: Incorrect time format';
	if (str2error==1) {
		if (message)
		return message;
		else
		return "Hodnota pole '"+description+"' není ve správném formátu (čas).\n";
		}
		else return '';
}

// ********************
// Funkce pro overovani dat: kontrola rozmezi hodnot
// ********************

function checkIntervalVal(str,description,message,minimum,maximum) {
	str=str.replace(REG_THOUSANDSSEPARATOR,'');
	str = str.replace(',','.');
	if (str=="") return "";
	if (!(str <= maximum && str >= minimum))
		{if (message)
		return message;
		else
		return "Hodnota pole '"+description+"' musí být v rozmezí ("+minimum+";"+maximum+").\n";
		}
	else return "";
}

function checkMaxVal(str,description,message,maximum) {
	str=str.replace(REG_THOUSANDSSEPARATOR,'');
	str = str.replace(',','.');
	if (str=="") return "";
	if(isNaN(str)){return "Hodnota pole '"+description+"' musí být číslo.\n";}
	if (!(str.length <= maximum))
		{if (message)
		return message;
		else
		return "Hodnota pole '"+description+"' musí být menší než "+maximum+".\n";
		}
	else return "";
}


function checkMinVal(str,description,message,minimum) {
	str=str.replace(REG_THOUSANDSSEPARATOR,'');
	str = str.replace(',','.');
	if (str=="") return "";
	if(isNaN(str)){return "Hodnota pole '"+description+"' musí být číslo.\n";}
	if (!(str.length >= minimum))
		{if (message)
		return message;
		else
		return "Hodnota pole '"+description+"' musí být větší než "+minimum+".\n";
		}
	else return "";
}

function checkMaxLength(str,description,message,maximum) {
	if (str=="") return "Hodnota pole '"+description+"' je povinné pole.\n";  
	var s = new String(str);
	if (!(s.length <= maximum)){
		if (message)
			return message;
		else
			return "Hodnota pole '"+description+"' je příliš dlouhá (max. "+maximum+" znaků)\n";
	}
	
	return "";
}
function checkCorrectEmail(str,description,message){
    //var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (!filter.test(str)) {
        if (message)
            return message;
        else
            return "'"+description+"' není ve správném formátu.\n";
    }else{
        return "";
    }
}

function checkEmails(str,str_re,description,message){
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (!filter.test(str)) {
        if (message)
            return message;
        else
            return "'"+description+"' není ve správném formátu.\n";
    }else{
        return "";
    }
}
function checkNumeric(str,description,message){
    if(isNaN(str)){
       if (message)
            return message;
        else
            return "'"+description+"' není ve správném formátu.\n";
    }else{
        return '';
    }
}
