/**
 * Javascript library functions
 *
 * @author Lukas Pohl <dr.zoidberg1969 AT yahoo DOT com>
 * @license Beerware
 */


/**
 * @param integer
 */
function vyplnFormControl(id) {
	var FormElement = document.getElementById(id);

	var NahodneCislo = Math.round((Math.random() * 20000)) * 8513;

	if (FormElement != undefined){
		FormElement.value = NahodneCislo;
	}
}


/**
 * Step by step show file input forms, so they all wont be showed empty at same time
 * @param String - Form ID example:priloha_3 priloha_div_3
 * @param Integer - File row count
 * @param Integer - Max file rows
 */
function fileInputClick(id,num,elemNum) {
	j=1;
	for(i=num;i>=1;i--)
	{
		element=document.getElementById(id+"_"+i);
		if(!element.value) j++;
		if(j==2) return;
	}
	if(num!=elemNum)
	{
		num++;
		element=document.getElementById(id+"_div_"+num);
			element.style.display="inline";
			element.style.height="auto";
	}
}



var mailFields = new Array("email", "e-mail");

/**
 * GENERAL FUNCTION TO SUBMIT A FORM
 * onsubmit="return (submitForm(this,new Array('nazev', 'ulice', 'mesto', 'psc', 'email','popis'), true));"
 * 
 * @var String myForm - name of the form to submit
 * @var Array mustFill - array with ID's of form which must be filled
 * @checkMail - if true, check input form with id from array mailFields (above)
 *		and check if its valid mail format. This flag doesnt check filling control
 *		Filling control must be set in mustFill array !
 * @checkPasswds - if true, check passwdord field if they are filled the same
 *
 */
function submitForm(myForm,mustFill, checkMail, checkPasswds) {
	var error=false;
	var firstFocus = true;
	for ( var i in mustFill)
	{
		if(!document.getElementById(mustFill[i])) {
			alert("Error, element with id '"+mustFill[i]+"' doesnt exist");
			return false;
		}
		if(document.getElementById(mustFill[i]).value=="") {
			document.getElementById(mustFill[i]).style.borderColor="red";
			if(firstFocus) {
				document.getElementById(mustFill[i]).focus();//focus last error input
				firstFocus = false;
			}
			error=true;
		} else {
			document.getElementById(mustFill[i]).style.borderColor="";
		}
			//alert( mustFill[i] );
	}
	if(error) {
		setErrorMsg("Musíte zadat všechna povinná pole (*)");
		return false;
	} else
	{
		if(checkMail) {
			for ( var i in mailFields) {
				var mailInput = document.getElementById(mailFields[i]);
				if (mailInput == undefined) continue;
				if(mailInput.value!="" && !isMail(mailInput.value)) {
					mailInput.focus();
					mailInput.style.borderColor="red";
					setErrorMsg("Musíte zadat mail ve správném formátu");
					return false;
				}
			}
		}

        if(checkPasswds) {
            var pass1 = document.getElementById("password1");
            if(!pass1) {
                allert("id='password1' not defined");
            }
            var pass2 = document.getElementById("password2");
            if(!pass2) {
                allert("id='password2' not defined");
            }
            if(pass1.value!=pass2.value) {
                pass1.focus();
                pass1.style.borderColor="red";
                pass2.style.borderColor="red";
                setErrorMsg("Hesla nesouhlasí");
                return false;
            }
        }

		return true;
//		myForm.submit();
	}
}

function setErrorMsg(label) {
	var warningElement=document.getElementById("jsError");
	if(!warningElement) {
		alert("Please put in your code:<div id='jsError'></div>");
	}
	warningElement.innerHTML=label;
	warningElement.className="error";
}

function isMail(email) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return true;
	}
	else {
		return false;
	}
}

