function checkSearchForm() { return checkInput("search-clause","Podaj szukane wyra\u017Cenie"); }
function checkAdvancedSearchForm() { return checkInput("search-clause-adv","Podaj szukane wyra\u017Cenie"); }

function checkContactForm()
{
	return checkEmail("contact_email","Podaj prawid\u0142owy adres email!")
	&& checkTextarea("contact_message","Podaj tre\u015B\u0107 wiadomo\u015Bci!");
}

function checkContestForm(form)
{
	return checkTextarea("contest_answer","Podaj odpowied\u017A")
	&& checkInput("contest_first_name","Podaj imi\u0119!")
	&& checkInput("contest_last_name","Podaj nazwisko!")
	&& checkInput("contest_street","Podaj ulic\u0119 i numer domu")
	&& checkInput("contest_post_code","Podaj kod pocztowy")
	&& checkInput("contest_city","Podaj miasto")
//T#10019 && checkInput("contest_pesel","Podaj PESEL")
	&& checkEmail("contest_email","Podaj prawid\u0142owy adres email!")
	&& checkInput("contest_phone","Podaj telefon")
	&& checkRadio(form, "contest_agree","Musisz wyrazi\u0107 zgod\u0119 na przetwarzanie danych osobowych!","contest_agree");
}

function checkOpinionForm()
{
	return checkTextarea("opinion_message","Podaj tre\u015B\u0107 wiadomo\u015Bci!");
}


/* ----------------------------------------------------------------------
	DEFAULT VALIDATION FORMS
---------------------------------------------------------------------- */
function getFormElementValue(inputId)
{
	var input = $JM(inputId);
	if(!input)
	{
		alert( "Input " + inputId + " not found!");
		return null;
	}

	return input.value;
}

function checkInput(inputId, errorMessage)
{
	var input = $JM(inputId);
	if(!input)
	{
		alert( "Input " + inputId + " not found!");
		return false;
	}

	return ( input.value == "" ) ? focusFailedInput(inputId,errorMessage) : true;
}

function compareFields(fieldId1, fieldId2, errorMessage)
{
	var field1 = $JM(fieldId1);
	if(!field1)
	{
		alert( "Element " + fieldId1 + " not found!");		
		return false;
	}

	var field2 = $JM(fieldId2);
	if(!field2)
	{
		alert( "Element " + fieldId2 + " not found!");		
		return false;
	}

	if( field1.value != field2.value )
    	return focusFailedInput(fieldId2,errorMessage);
     
    return true;
}

function checkTextarea(inputId, errorMessage)
{
	var input = $JM(inputId);
	if(!input)
	{
		alert( "Textarea " + inputId + " not found!");		
		return false;
	}

	if( input.value.length <= 3 )
    	return focusFailedInput(inputId,errorMessage);
     
    return true;
}

function checkEmail(inputId,errorMessage)
{
	var input = $JM(inputId);
	if(!input)
	{
		alert( "Input " + inputId + " not found!");
		return false;
	}

    if (!isValidEmail(input.value)) 
    {
        focusFailedInput(inputId,errorMessage);
        return false;
    }
    return true;
}

function isValidEmail(email)
{
 	var template = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/;
  	if (template.test(email) == false) return false;
	return true;
}

function clearError()
{
	var labels = document.getElementsByTagName("label");
	
	for(var i = 0; i < labels.length; i++ )
	{
		var label = labels[i];
		removeClass(label,"error");
	}
	return true;
}

function focusFailedInput(inputId, errorMessage)
{
	var labels = document.getElementsByTagName("label");

	var tmplabel;
	// set error class to correct label and remove error class from others
	for(var i = 0; i < labels.length; i++ )
	{
		var label = labels[i];
		removeClass(label,"error");
		// if anything will be wrong, remove break statement
		// KCI -> KCI i think it is wrong 'couse it coulnd not loop to the end
		if( label.htmlFor == inputId ) { label.className += " error"; break; }
	}
	
	var element = $JM(inputId);
	if(errorMessage)
		myAlert(errorMessage,element);
		
	return false;
}

function checkRadio(form, input, errorMessage, inputId)
{
	var tmpForm = $JM(form);
	if(tmpForm)
		form = tmpForm;
		
	if(!form[input])
	{
		alert( "Element " + input + " not found!");		
		return false;
	}
	
	if(!form[input].length) // 1 input
	{
		if(form[input].checked) return true;
		return focusFailedInput(inputId,errorMessage);
	}
	
	var i = 0;
	for( i ; i < form[input].length; i++ )
		if(form[input][i].checked == true) break;

	if( i == form[input].length )
    	return focusFailedInput(inputId,errorMessage);
     
    return true;
}

function groupFields(name,id,cnt,message)
{
	var count = parseInt(cnt);
	for( var i = 1; i <= count; i++ )
		if($JM(id + "_" + i,message).value == "") return true;
	return focusFailedInput(id + "_1",message);
}

function groupEmails(name,id,cnt,message)
{
	var template = /^[0-9a-z]+[0-9a-z._-]*\@[0-9a-z]+[0-9a-z._-]*\.[0-9a-z]{2,}$/i;
	var count = parseInt(cnt);
	for( var i = 1; i <= count; i++ )
		if(template.test($JM(id + "_" + i,message).value) == true) return true;
	return focusFailedInput(id + "_1",message);
}

function checkPESEL(inputId, errorMessage)
{
	var input = $JM(inputId);
	if(!input)
	{
		alert( "Input " + inputId + " not found!");
		return false;
	}

	if( !validatePESEL(input.value) )
    	return focusFailedInput(inputId,errorMessage);
    return true;
}

function validatePESEL(pesel)
{
	// basic check
	if (pesel.length != 11) return false;
	
	// check date
	var month = parseInt(pesel.slice(2,4),10); 
	if (month == 0 || month > 12) return false;
	var day = parseInt(pesel.slice(4,6),10);
	if (day == 0 || day > 31) return false;
	var year = parseInt(pesel.slice(0,2),10) + 1900;
    
    if (month > 20 && month < 40) { year += 100; }
    else if (month > 80) { year -= 100; }
    else if (month > 60) { year += 300; }
    else if (month > 40) { year += 200; }
    
    var bornString =  ((month<10?"0":"")+month) + "/" + ((day<10?"0":"")+day) + "/" + (""+year) ;
    // nice regexp ;)
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    if(!bornString.match(RegExPattern))
        return false;
        
	// calculate crc
	var steps = new Array(1, 3, 7, 9);
	var crc = 0;
	for (var i = 0; i < 10; i++)
		crc += steps[i % 4] * parseInt(pesel[i]);
	crc = 10 - (crc % 10);
	if (crc == 10) crc = 0;
	
	return crc == parseInt(pesel[10]);	
}

function checkPostCode(inputId, errorMessage)
{
	if(!checkInput(inputId, errorMessage)) return false;
	
	var value = $JM(inputId).value;
	return (!value.match(/^\d{2}-\d{3}$/)) ? focusFailedInput(inputId,errorMessage) : true;
}