﻿// -------------------------------------------------------------------------------------------------
// -- JAVASCRIPT LIBRARY (c) 2007 J.Filous --
// -- EDIT WEB FORMS
// -------------------------------------------------------------------------------------------------
var wf_pForm = null;			// pointer na aktuální formulář
var wf_pFocusControl = null		// pointer na ovládací prvek, který má získat focus
var wf_bInvalidForm = false;	// indikace chybně vyplněného formuláře
// -------------------------------------------------------------------------------------------------
// -- REGULAR EXPRESIONs
// -------------------------------------------------------------------------------------------------
// E-mail address
var regexp_EMAIL = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
// Time in format HH:MM, from 00:00 to 23:59
var regexp_TIME_HHMM = /^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/;
// Time in format HH:MM:SS, from 00:00:00 to 23:59:59
var regexp_TIME_HHMMSS = /^([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])$/;
// Password must be at least 8 characters, no more than 20 characters, and must include at least one upper case letter, one lower case letter, and one numeric digit.
var regexp_PASSWORD = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,20}$/;
// MAC address in hex format, for example A9:67:23:00:B3:CC
var regexp_MAC = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/;
// IP address in decimal format, for example 10.60.5.18
var regexp_IP = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
// Login name must be at least 5 characters, no more than 20 characters
var regexp_LOGINNAME = /(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{5,20})$/;
// Positive integer value
var regexp_POSITIVE_NUMBER = /^\d+$/;
// HTTP address
var regexp_HTTP = /^(http:[/]{1}[/]{1}|https:[/]{1}[/]{1}){0,1}[A-Za-z0-9][A-Za-z0-9\-\.]+[A-Za-z0-9]\.[A-Za-z]{2,}[\43-\176]*$/;
// PHONE
var regexp_PHONE_CZ = /^([+]{1}420[0-9]{9})$/;
// DOMAIN
var regexp_DOMAIN = /^[a-z]+([a-z0-9-]*[a-z0-9]+)?(\.([a-z]+([a-z0-9-]*[a-z0-9]+)?)+)*$/;
// FOLDER NAME
var regexp_FOLDER_NAME = /^[^\\\/\?\*\&quot;\'\&gt;\&lt;\:\|]*$/;
// -------------------------------------------------------------------------------------------------
// -- VISUAL FUNCTIONS
// -------------------------------------------------------------------------------------------------
// zvýraznění ovládacího prvku, na který byl přesunut focus
function inpBacklightOn(obj) {
	obj.style.backgroundColor = "#fafad2";
	if (obj.className == "textBox") obj.select();
} 
// zrušení zvýraznění ovládacího prvku, ze kterého byl přesunut focus
function inpBacklightOff(obj) { obj.style.backgroundColor = "#ffffff"; }
// přiřazení obsahu ovládacího prvku typu PASSWORD do atributu TITLE
function inpShowContent(obj) { obj.title = obj.value; }
// -------------------------------------------------------------------------------------------------
// -- STRING MANIPULATION FUNCTIONS
// -------------------------------------------------------------------------------------------------
function LTrim(str) {
	if (str == null) return("");
	if (str.length == 0) return(str);
	if (str.charAt(0) != " ") return(str);
	while (str.charAt(0) == " ") str = str.substring(1, str.length);
	return(str);
}
function RTrim(str) {
	if (str == null) return("");
	if (str.length == 0) return(str);
	if (str.charAt(str.length-1) != " ") return(str);
	while (str.charAt(str.length-1) == " ") str = str.substring(0, str.length-1);
	return(str);
}
function ReplaceChar(str, a, b) {
var intPos = 0;
	if (str == null) return("");
	if (str.length == 0) return(str);
	if (str.indexOf(a, intPos) == -1) return(str);
	while (str.indexOf(a, intPos) != -1) {
		intPos = str.indexOf(a, intPos);
		leftPart = str.substring(0, intPos);
		rightPart = str.substring(intPos+1, str.length);
		str = leftPart + b + rightPart;
		intPos++;
	}
	return(str);
}
function isEmpty(theField) {
	if (theField == null) return(true);
	if (LTrim(RTrim(theField.value)) == "") return(true); else return(false);
}
// -------------------------------------------------------------------------------------------------
// OTHER USEFULL FUNCTIONS
// -------------------------------------------------------------------------------------------------
function make_array(strarr, delimiter) { return strarr.split(delimiter); }
function compactField(theField) { theField.value = ReplaceChar(theField.value, " ", ""); }
function toUpperCase(theField) { theField.value = theField.value.toUpperCase(); }
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -- VALIDATION FUNCTIONS
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
function wf_validate_begin() {
	wf_bInvalidForm = false; wf_pFocusControl = null;
}
function wf_validate_end() {
	if (wf_pFocusControl != null) wf_pFocusControl.focus();
}
function wf_isValid() { return(!wf_bInvalidForm); }
function wf_validate(theAlgorithm, theField, theSecond, theParam, theRequiredCheck) {
var bInvalidInput = false;
	if (!theRequiredCheck && isEmpty(theField)) {
		theField.style.borderColor = "#7f9db9";
		return;
	}
	switch (theAlgorithm) {
		case "EMAIL":
			bInvalidInput = !regexp_EMAIL.test(theField.value);
			break;
		case "LOGINNAME":
			bInvalidInput = !regexp_LOGINNAME.test(theField.value);
			break;
		case "PASSWORD":
			//bInvalidInput = !regexp_PASSWORD.test(theField.value);
			bInvalidInput = wf_validateTextField(theField, 4);	// dočasné
			break;
		case "POSITIVE_NUMBER":
			bInvalidInput = !regexp_POSITIVE_NUMBER.test(theField.value);
			break;
		case "SIMPLE_TEXT_FIELD":
			bInvalidInput = wf_validateTextField(theField, theParam);
			break;
		case "NUMBER_FIELD":
			bInvalidInput = wf_validateNumberField(theField, theParam);
			break;
		case "PHONE":
			bInvalidInput = !regexp_PHONE_CZ.test(theField.value);
			break;
		case "NULL_OPTION":
			bInvalidInput = wf_validateNullOption(theField, theParam);
			break;
		case "DATE_DD.MM.YYYY":
			bInvalidInput = wf_validateDate(theField);
			break;
		case "ICO":
		    bInvalidInput = wf_validateICO(theField);
		    break;
		case "RC":
		    bInvalidInput = wf_validateRC(theField, theSecond, 0);
		    break;
		default:
			alert("Wrong validation rule!");
			break;
	}
	if (bInvalidInput) {
		wf_bInvalidForm = true;
		theField.style.borderColor = "#ff0000";
		if (theSecond != null) theSecond.style.borderColor = "#ff0000";
		if (wf_pFocusControl == null) wf_pFocusControl = theField;
	} else {
		theField.style.borderColor = "#7f9db9";
		if (theSecond != null) theSecond.style.borderColor = "#7f9db9";
	}
}
function wf_validateTextField(theField, theParam) {
	if (isEmpty(theField) || theField.value.length < theParam) return true; else return false;
}
function wf_validatePasswordCompare(theField, theSecond) {
	if (isEmpty(theField) || isEmpty(theSecond)) return true;
	if (theField.value != theSecond.value) return true; else return false;
}
function wf_validateNumberField(theField, theParam) {
	if (isEmpty(theField) || theField.value.length < theParam) return true;
	else {
		return !regexp_POSITIVE_NUMBER.test(theField.value);
//		if (isNaN(parseInt(theField.value))) return true;
	}
	return false;
}
function wf_validateNullOption(theField, theParam) {
	if (theField.value == theParam) return true; else return false;
}
function wf_validateDate(theField) {
var den, mesic, rok, dots, i, pom, tdate;
	// Mininal length of date string is eight characters
	if (theField.value.length < 8) return true;
	// Date string must contains two dots which separate day from month and month from year
	dots = 0;
	for (i = 0; i < theField.value.length; i++) if (theField.value.substring(i,i+1) == ".") dots++;
	if (dots != 2) return true;
	// Date string can contains only digits and dot
	for (i = 0; i < theField.value.length; i++) {
		if (theField.value.substring(i, i+1) < "0" || theField.value.substring(i, i+1) > "9") {
			if (theField.value.substring(i, i+1) != ".") return true;
		}
	}
	// Convert date string to object date and if the date is not changed then is correct
	pom = theField.value.indexOf(".", 0); den = eval(theField.value.substring(0, pom));
	i = pom + 1; pom = theField.value.indexOf(".", i); mesic = eval(theField.value.substring(i, pom)) - 1;
	rok = eval(theField.value.substring(pom + 1, theField.value.length));
	tdate = new Date(rok, mesic, den);
	if (tdate.getDate() != den || tdate.getMonth() != mesic || tdate.getFullYear() != rok) return true;
	return false;
}
function wf_validateICO(theField)
{
    var x = theField.value;
    try
    {
        var a = 0;
        if (x.length == 0) throw 1;
        if (x.length != 8) throw 1;
        var b = x.split('');
        var c = 0;
        for (var i = 0; i < 7; i++) a += (parseInt(b[i]) * (8 - i));
        a = a % 11;
        c = 11 - a;
        if (a == 1) c = 0;
        if (a == 0) c = 1;
        if (a == 10) c = 1;
        if (parseInt(b[7]) != c) throw 1;
    }
    catch (e)
    {
        return true;
    }
    return false;
}
function wf_validateRC(theField1, theField2, age)
{
    var x = theField1.value.toString() + theField2.value.toString();
    try
    {
        if (x.length == 0) throw 1;
        if (x.length < 9) throw 1;
        var year = parseInt(x.substr(0, 2), 10);
        var month = parseInt(x.substr(2, 2), 10);
        var day = parseInt(x.substr(4, 2), 10);
        var ext = parseInt(x.substr(6, 3), 10);
        if ((x.length == 9) && (year < 54)) return false;
        var c = 0;
        if (x.length == 10) c = parseInt(x.substr(9, 1));
        var m = parseInt(x.substr(0, 9)) % 11;
        if (m == 10) m = 0;
        if (m != c) throw 1;
        year += (year < 54) ? 2000 : 1900;
        if ((month > 70) && (year > 2003)) month -= 70;
        else if (month > 50) month -= 50;
        else if ((month > 20) && (year > 2003)) month -= 20;
        var d = new Date();
        if ((year + age) > d.getFullYear()) throw 1;
        if (month == 0) throw 1;
        if (month > 12) throw 1;
        if (day == 0) throw 1;
        if (day > 31) throw 1;
    }
    catch (e)
    {
        return true;
    }
    return false;
}
function getRadioValue(theGroup) {
    for (var i = 0; i < theGroup.length; i++) {
        if (theGroup[i].checked) return theGroup[i].value;
    }
}
// -------------------------------------------------------------------------------------------------
// -- SMLOUVA FYZICKA OSOBA
// -------------------------------------------------------------------------------------------------
function onSubmit_GenPDF_FO(prm) {
    wf_pForm = document.getElementById("frmCONTRACTS");
    wf_validate_begin();
	// ---
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.fo_jmeno, null, 4, true);
    wf_validate("RC", wf_pForm.rc_1, wf_pForm.rc_2, null, true);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.fo_ulice, null, 2, false);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.fo_mesto, null, 2, true);
	wf_validate("NUMBER_FIELD", wf_pForm.fo_psc, null, 5, true);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.fo_mesto2, null, 2, true);
	wf_validate("NUMBER_FIELD", wf_pForm.fo_pocet, null, 1, true);
	// ---
	wf_pForm.TYPE_OF_CONTRACT.value = prm;
    if (prm == "FO_0") wf_pForm.action = "NBS_smlouva_FO_hokejova_plocha.php";
    if (prm == "FO_1") wf_pForm.action = "NBS_smlouva_FO_400m_oval.php";
	// ---
	wf_validate_end();
	if (wf_isValid()) {
	    if (prm == "FO_0") wf_pForm.NUM2TEXT.value = num2text(wf_pForm.fo_pocet.value*1200*5);
	    if (prm == "FO_1") wf_pForm.NUM2TEXT.value = num2text(wf_pForm.fo_pocet.value*40*5);
	    wf_pForm.submit();
	}
}
// -------------------------------------------------------------------------------------------------
// -- SMLOUVA PRAVNICKA OSOBA
// -------------------------------------------------------------------------------------------------
function onSubmit_GenPDF_PO(prm) {
    wf_pForm = document.getElementById("frmCONTRACTS");
    wf_validate_begin();
	// ---
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_firma, null, 4, true);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_zastupce, null, 4, true);
   	wf_validate("ICO", wf_pForm.po_ico, null, null, true);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_jmeno, null, 4, true);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_ulice, null, 2, false);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_mesto, null, 2, true);
	wf_validate("NUMBER_FIELD", wf_pForm.po_psc, null, 5, true);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_mesto2, null, 2, true);
	wf_validate("NUMBER_FIELD", wf_pForm.po_pocet, null, 1, true);
	// ---
	wf_pForm.TYPE_OF_CONTRACT.value = prm;
	if (prm == "PO_0") wf_pForm.action = "NBS_smlouva_PO_hokejova_plocha.php";
	if (prm == "PO_1") wf_pForm.action = "NBS_smlouva_PO_400m_oval.php";
	// ---
	wf_validate_end();
	if (wf_isValid()) {
	    if (prm == "PO_0") wf_pForm.NUM2TEXT.value = num2text(wf_pForm.po_pocet.value*1200*5);
	    if (prm == "PO_1") wf_pForm.NUM2TEXT.value = num2text(wf_pForm.po_pocet.value*40*5);
	    wf_pForm.submit();
	}
}
// -------------------------------------------------------------------------------------------------
// -- SMLOUVA PRAVNICKA OSOBA - ZRIZOVATEL
// -------------------------------------------------------------------------------------------------
function onSubmit_GenPDF_POZ() {
    wf_pForm = document.getElementById("frmCONTRACTS");
    wf_validate_begin();
	// ---
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.poz_nazev, null, 4, true);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.poz_zastupce, null, 4, true);
   	wf_validate("ICO", wf_pForm.poz_ico, null, null, true);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.poz_jmeno, null, 4, true);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.poz_ulice, null, 2, false);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.poz_mesto, null, 2, true);
	wf_validate("NUMBER_FIELD", wf_pForm.poz_psc, null, 5, true);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.poz_mesto2, null, 2, true);
	wf_validate("NUMBER_FIELD", wf_pForm.poz_pocet, null, 1, true);
	// ---
	wf_pForm.TYPE_OF_CONTRACT.value = "POZ";
    wf_pForm.action = "NBS_smlouva_POZ_400m_oval.php";
	// ---
	wf_validate_end();
	if (wf_isValid()) {
	    wf_pForm.NUM2TEXT.value = num2text(wf_pForm.poz_pocet.value*10000);
	    wf_pForm.submit();
	}
}
// -------------------------------------------------------------------------------------------------
// -- TRIBUNA / SMLOUVA FYZICKA OSOBA
// -------------------------------------------------------------------------------------------------
function onSubmit_SaveFO() {
    wf_pForm = document.getElementById("frmCONTRACT");
    wf_validate_begin();
	// ---
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.fo_jmeno, null, 4, true);
	// ---
    wf_validate("RC", wf_pForm.rc_1, wf_pForm.rc_2, null, false);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.fo_ulice, null, 2, false);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.fo_mesto, null, 2, false);
	wf_validate("NUMBER_FIELD", wf_pForm.fo_psc, null, 5, false);
	wf_validate("EMAIL", wf_pForm.fo_email, null, 0, false);
	wf_validate("PHONE", wf_pForm.fo_phone, null, 0, false);
	// ---
	wf_pForm.ACTION.value = "SAVE";
	// ---
	wf_validate_end();
	if (wf_isValid()) wf_pForm.submit();
}
// -------------------------------------------------------------------------------------------------
// -- TRIBUNA / SMLOUVA FIRMA
// -------------------------------------------------------------------------------------------------
function onSubmit_SavePO() {
    wf_pForm = document.getElementById("frmCONTRACT");
    wf_validate_begin();
	// ---
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_firma, null, 4, true);
	// ---
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_zastupce, null, 2, false);
    wf_validate("ICO", wf_pForm.po_ico, null, null, false);
    //wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_dic, null, 2, false);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_jmeno, null, 2, false);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_ulice, null, 2, false);
	wf_validate("SIMPLE_TEXT_FIELD", wf_pForm.po_mesto, null, 2, false);
	wf_validate("NUMBER_FIELD", wf_pForm.po_psc, null, 5, false);
	wf_validate("EMAIL", wf_pForm.po_email, null, 0, false);
	wf_validate("PHONE", wf_pForm.po_phone, null, 0, false);
	// ---
	wf_pForm.ACTION.value = "SAVE";
	// ---
	wf_validate_end();
	if (wf_isValid()) wf_pForm.submit();
}
// -------------------------------------------------------------------------------------------------
// -- TRIBUNA / ZMENA STAVU SMLOUVY
// -------------------------------------------------------------------------------------------------
function onSubmit_SaveContractStatus_1() {
    wf_pForm = document.getElementById("frmCONTRACT");
    wf_validate_begin();
	// ---
	wf_validate("DATE_DD.MM.YYYY", wf_pForm.dat_signed, null, null, true);
    // ---
	wf_pForm.ACTION.value = "SAVE_CONTRACT_STATUS";
	// ---
	wf_validate_end();
	if (wf_isValid()) wf_pForm.submit();
}
function onSubmit_SaveContractStatus_2() {
    wf_pForm = document.getElementById("frmCONTRACT");
    wf_validate_begin();
	// ---
	wf_validate("DATE_DD.MM.YYYY", wf_pForm.dat_paid, null, null, true);
    // ---
	wf_pForm.ACTION.value = "SAVE_CONTRACT_STATUS";
	// ---
	wf_validate_end();
	if (wf_isValid()) wf_pForm.submit();
}
// -------------------------------------------------------------------------------------------------
// -- TRIBUNA / SKOK NA VYBER SEDADEL
// -------------------------------------------------------------------------------------------------
function onSubmit_SelectSeats() {
    wf_pForm = document.getElementById("frmCONTRACT");
	// ---
	wf_pForm.ACTION.value = "SELECT_SEATS";
	// ---
    wf_pForm.submit();
}

// -------------------------------------------------------------------------------------------------
// -- PREVOD CASTKY NA SLOVNI VYJADRENI
// -------------------------------------------------------------------------------------------------
function num2text(value) { 
    return base(part(value,3),3) + mld(part(value,3)) + base(part(value,2),2) + mil(part(value,2)) + base(part(value,1),1) + tis(part(value,1)) + base(part(value,0),0); 
} 

function part(value,section) { 
    value = value + ""; 
    return value.substring(value.length-3-section*3,value.length-section*3); 
}

function base(value,part) { 
    var outputStr = ""; 
    var dict2 = new Array("deset", "jedenáct", "dvanáct", "třináct", "čtrnáct", "patnáct", "šestnáct", "sedmnáct", "osmnáct", "devatenáct"); 
    var dict = new Array(); 
    dict[0] = new Array("", "jedna", "dva", "tři", "čtyři", "pět", "šest", "sedm", "osm", "devět"); 
    dict[1] = new Array("", "", "dvacet", "třicet", "čtyřicet", "padesát", "šedesát", "sedmdesát", "osmdesát", "devadesát"); 
    dict[2] = new Array("", "sto", "dvěstě", "třista", "čtyřista", "pětset", "šestset", "sedmset", "osmset", "devětset"); 

    for (var x=0; x <= value.length-1; x++) { 
        if ((x == value.length-2) && (value.charAt(x)=="1")) { 
            outputStr += dict2[value.charAt(x+1)]; 
            break; 
        } 
        else { 
            if (part == 1 && value.charAt(x)=="1" && x == value.length-1) { 
                outputStr += "jeden"; // výjmka jeden tisíc 
            } 
            else if (part == 2 && value.charAt(x)=="1" && x == value.length-1) { 
                outputStr += "jeden"; // výjmka jeden milion 
            } 
            else if (part == 3 && value.charAt(x)=="2" && x == value.length-1) { 
                outputStr += "dvě"; // výjmka dvě miliardy 
            } 
            else { 
                outputStr += dict[value.length-1-x][value.charAt(x)]; 
            } 
        } 
    }

    return outputStr; 
} 

function tis(value) { 
    value = value - 0; 
    var appendix = new Array("", "tisíc", "tisíce", "tisíce", "tisíce", "tisíc", "tisíc", "tisíc", "tisíc", "tisíc"); 
    if (value <= 9) return appendix[value];
    else return "tisíc";
} 

function mil(value) { 
    value = value - 0; 
    var appendix = new Array("", "milion", "miliony", "miliony", "miliony", "milionů", "milionů", "milionů", "milionů", "milionů"); 
    if (value <= 9) return appendix[value];
    else return "milionů";
} 

function mld(value){ 
    value = value - 0; 
    var appendix = new Array("", "miliarda", "miliardy", "miliardy", "miliardy", "miliard", "miliard", "miliard", "miliard", "miliard"); 
    if (value <= 9) return appendix[value];
    else return "miliard";
} 
