function checkLoginForm()
{
	return checkEmail("login_email","Podaj adres email") &&
		checkInput("login_password","Podaj swoje haslo");
}

function checkPasswordForm()
{
	return checkEmail("form_email","Podaj adres email");
}

function checkRegisterForm(form)
{
	var check = checkEmail("form_email","Podaj adres email")
	&& checkInput("form_password","Podaj swoje haslo")
	&& checkFieldLength("form_password",6,"Haslo musi skladac sie z minimum 6 znakow")
	&& compareFields("form_password","form_password_commit","Podane hasla sie nie zgadzaja")
	&& checkInput("form_parent_client_id","Podaj identyifkator klienta polecajacego")
	&& checkInput("form_first_name","Podaj imie")
	&& checkInput("form_last_name","Podaj nazwisko")
	&& checkInput("form_phone","Podaj telefon")
	&& checkInput("form_address","Podaj adres")
	&& checkInput("form_city","Podaj miasto")
	&& checkInput("form_post_code","Podaj kod pocztowy")
	&& checkInput("form_post_code","Podaj kod pocztowy")
	&& checkRadio(form, "account_type", "Wybierz typ konta", "form_account_type_0");
	
	if(!check) return false;
	
	if($("form_account_type_0").checked)
	check = true;
	//	check = checkInput("form_card_id","Podaj numer i serie dowodu osobistego")
	//		&& checkInput("form_pesel","Podaj PESEL");
	else
		check = checkInput("form_company_name","Podaj nazwe firmy")
			&& checkInput("form_nip","Podaj NIP")
			&& checkInput("form_regon","Podaj REGON");
	if(!check) return false;
	return checkRadio(form,"accept","Zaakceptuj regulamin","form_accept")	
}

function checkDataForm(form)
{
	var check;
	if($("form_password").value.length > 0 || $("form_password_commit").value.length > 0)
	{
		check = checkInput("form_password","Podaj swoje haslo")
		&& checkFieldLength("form_password",6,"Haslo musi skladac sie z minimum 6 znakow")
		&& compareFields("form_password","form_password_commit","Podane hasla sie nie zgadzaja")
		if(!check) return false;
	}
	
	check = checkInput("form_first_name","Podaj imie")
	&& checkInput("form_last_name","Podaj nazwisko")
	&& checkInput("form_phone","Podaj telefon")
	&& checkInput("form_address","Podaj adres")
	&& checkInput("form_city","Podaj miasto")
	&& checkInput("form_post_code","Podaj kod pocztowy")
	&& checkInput("form_post_code","Podaj kod pocztowy")
	&& checkRadio(form, "account_type", "Wybierz typ konta", "form_account_type_0");
	
	if(!check) return false;
	
	if($("form_account_type_0").checked)
		check = true;
	//	return checkInput("form_card_id","Podaj numer i serie dowodu osobistego")
	//		&& checkInput("form_pesel","Podaj PESEL");
	else
		return checkInput("form_company_name","Podaj nazwe firmy")
			&& checkInput("form_nip","Podaj NIP")
			&& checkInput("form_regon","Podaj REGON");
}

function checkTelecodeForm(form)
{
	var check = checkRadio(form, "gsm", "Wybierz telekod", "orange_25")
	&& checkInput("form_phone","Podaj numer telefonu")
	&& checkInput("form_phone_confirm","Potwierdz numer telefonu")
	&& compareFields("form_phone","form_phone_confirm","Podane numery telefonow sie nie zgadzaja");
	if(!check) return false;
	
	var anonymousClientEmail = $("form_client_email");
	if(!anonymousClientEmail) return true;
	return checkInput("form_client_email","Podaj adres email");
}

function checkOnlineForm(form)
{
	var check = checkRadio(form, "gsm", "Wybierz siec", "form_orange")
	&& checkInput("form_amount","Podaj kwote doladowania")
	&& checkInput("form_phone","Podaj numer telefonu")
	&& checkInput("form_phone_confirm","Potwierdz numer telefonu")
	&& compareFields("form_phone","form_phone_confirm","Podane numery telefonow sie nie zgadzaja");
	
	if(!check) return false;
	
	var anonymousClientEmail = $("form_client_email");
	if(!anonymousClientEmail) return true;
	return checkInput("form_client_email","Podaj adres email");
}


function checkComplaintForm()
{
	return checkInput("form_type","Wybierz przyczyne reklamacji")
	&& checkTextarea("form_describe","Podaj opis problemu");
}

/* ----------------------------------------------------------------------
	DEFAULT VALIDATION FORMS
---------------------------------------------------------------------- */
function checkInput(inputId, errorMessage)
{
	var input = document.getElementById(inputId);
	if(!input)
	{
		alert( "Input " + inputId + " not found!");
		return false;
	}

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

function checkFieldLength(inputId, length, errorMessage)
{
	var input = document.getElementById(inputId);
	if(!input)
	{
		alert( "Input " + inputId + " not found!");
		return false;
	}

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

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

	var field2 = document.getElementById(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 = document.getElementById(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 = document.getElementById(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 = /^[0-9a-z]+[0-9a-z._-]*\@[0-9a-z]+[0-9a-z._-]*\.[0-9a-z]{2,}$/i;
  	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 = document.getElementById(inputId);
	if(errorMessage)
	{
		alert(errorMessage);
		if(element) element.focus();
	}
		
	return false;
}

function checkRadio(form, input, errorMessage, inputId)
{
	var tmpForm = document.getElementById(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;
}
