// JavaScript Document

function validateMinLength (fieldName, str_min_len) {
	if (fieldName.value.length>=str_min_len) {
		return true;
	}
	else {
		alert("Minimalna liczba znaków: "+str_min_len);
		return false;
	}
}

function isEmpty (strng) {
	var error = "";
	if (strng.length == 0) {
		error = "Należy wypełnić wszystkie pola oznaczone gwiazdką (*)\n"
	}
	return error;	  
}

function checkUserNotes (strng) {
	var error = "";
	if (strng.length == 0) {
		error = "Nie wprowadzono treści wiadomości\n"
	}
	return error;	  
}

function checkEmail (strng) {
	var error="";
	if (strng == "") {
		error = "Proszę podać adres e-amil\n";
	}
	var emailFilter=/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	if (!(emailFilter.test(strng))) { 
		error = "Proszę podać poprawny adres e-mail\n";
	}
	return error;    
}

function isEmptyPassword (strng) {
	var error = "";
	if (strng.length == 0) {
		error = "Podaj stare hasło\n"
	}
	return error;	  
}

function checkPassword (strng) {
	var error = "";
	if (strng == "") {
		error = "Proszę podać hasło\n";
	}
	
	var illegalChars = /[\W_]/; // tylko litery i cyfry

	if ((strng.length < 6) || (strng.length > 15)) {
		error = "Hasło powinno mieć między 6 a 15 znaków\n";
	}
	else if (illegalChars.test(strng)) {
		error = "Hasło może zawierać tylko małe i duże litery oraz cyfry\n";
	} 
	else if ((strng.search(/[A-Z]+/) < 0) || (strng.search(/[a-z]+/) < 0) || (strng.search(/[0-9]+/) < 0)) {
		error = "Hasło musi zawierać przynajmniej jedną małą i jedną dużą literę oraz jedną cyfrę\n";
	}  
	return error;    
}    

function checkPasswordVerify (strng1, strng2) {
	var error = "";
	if (strng1 != strng2) {
		error = "Hasło i powtórzone hasło nie zgadzają się\n";
	}
	return error;    
}

function isVatId (strng) {
	var error = "";
	var vatIdFilter = /^[0-9]{10}$/
	if ((strng != "")&&(!(vatIdFilter.test(strng)))) { 
		error = "Proszę wpisać NIP w postaci 10 cyfr bez dodatkowych znaków (np. 1231231212)\n"
	}
	return error;
}

function isPostcode (strng) {
	var error = "";
	var postcodeFilter = /^[0-9]{2}-[0-9]{3}$/
	if (strng == "") {
		error = "Proszę podać kod pocztowy\n";
	}
	else if (!(postcodeFilter.test(strng))) { 
		error = "Proszę wpisać kod pocztowy w postaci \"XX-XXX\"\n"
	}
	return error;
}

function isPhone (strng) {
	var error = "";
	var phoneFilter = /^[0-9]{9}$/
	if (strng == "") {
		error = "Proszę podać numer telefonu\n";
	}
	else if (!(phoneFilter.test(strng))) { 
		error = "Proszę wpisać numer telefonu w postaci 9 cyfr bez 0 \"XXXXXXXXX\"\n"
	}
	return error;
}

function isTwoDigitDecimal (strng) {
	var error = "";
	var phoneFilter = /^[1-9][0-9]$/
	if ((strng.length != 0)&&!(phoneFilter.test(strng))) { 
		error = "Proszę podać liczbę z przedziału 0-99\n"
	}
	return error;
}

function isCheckedLegal (chckbx) {
	var error = "";
	if (chckbx.checked) { 
		error = "Proszę zaakceptować treść regulaminu\n"
	}
	return error;
}

function isCorrectDeliveryDate (strng) {
	var error = "";
	var tmp = new Date();
	var currentTime = tmp.getTime() + (5 * 60 * 60 * 1000);
	var deliveryTime = parseInt(strng) * 1000;
	if (deliveryTime < currentTime) {
		error = "Prosimy o wybór późniejszego terminu dostawy. Potrzebujemy minimum 5 godzin na skompletowanie zamówienia.\n";
	}
	return error;	
}

function validateContactForm (theForm) {
	var reason = "";
	reason += checkEmail(theForm.user_email.value);
	reason += isEmpty(theForm.msg_body.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateDeliveryAddress (theForm) {
	var reason = "";
	reason += checkEmail(theForm.email.value);
	reason += isEmpty(theForm.firstname.value);
	reason += isEmpty(theForm.lastname.value);
	reason += isEmpty(theForm.street_name.value);
	reason += isEmpty(theForm.street_number.value);
	reason += isPostcode(theForm.postcode.value);
	reason += isEmpty(theForm.city.value);
	reason += isPhone(theForm.phone.value);
	reason += isVatId(theForm.vatid.value);
	reason += isCorrectDeliveryDate(parseInt(theForm.delivery_day.value) + parseInt(theForm.delivery_hour.value));
	reason += isCheckedLegal(theForm.confirmed.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateDeliveryDate (theForm) {
	var reason = "";
	reason += isCorrectDeliveryDate(parseInt(theForm.delivery_day.value) + parseInt(theForm.delivery_hour.value));
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateRegisterForm (theForm) {
	var reason = "";
	reason += checkEmail(theForm.email.value);
	reason += checkPassword(theForm.passwd.value);
	reason += checkPasswordVerify(theForm.passwd.value, theForm.passwd_verify.value);
	reason += isEmpty(theForm.lastname.value);
	reason += isEmpty(theForm.firstname.value);
	reason += isEmpty(theForm.street_name.value);
	reason += isEmpty(theForm.street_number.value);
	reason += isPostcode(theForm.postcode.value);
	reason += isEmpty(theForm.city.value);
	reason += isPhone(theForm.phone.value);
	reason += isVatId(theForm.vatid.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateUserForm (theForm) {
	var reason = "";
	reason += checkEmail(theForm.email.value);
	reason += checkPassword(theForm.passwd.value);
	reason += isEmpty(theForm.lastname.value);
	reason += isEmpty(theForm.firstname.value);
	reason += isEmpty(theForm.street_name.value);
	reason += isEmpty(theForm.street_number.value);
	reason += isPostcode(theForm.postcode.value);
	reason += isEmpty(theForm.city.value);
	reason += isPhone(theForm.phone.value);
	reason += isVatId(theForm.vatid.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateOrderAddressForm (theForm) {
	var reason = "";
	reason += checkEmail(theForm.email.value);
	reason += isEmpty(theForm.lastname.value);
	reason += isEmpty(theForm.firstname.value);
	reason += isEmpty(theForm.street_name.value);
	reason += isEmpty(theForm.street_number.value);
	reason += isPostcode(theForm.postcode.value);
	reason += isEmpty(theForm.city.value);
	reason += isPhone(theForm.phone.value);
	reason += isVatId(theForm.vatid.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function categoryValidator (theForm) {
	if (theForm.c.value>0) {
		return true;
	}
	else {
		alert("Wybierz kategorię!");
		return false;
	}
}

function productgroupValidator (theForm) {
	if (theForm.p.value>0) {
		return true;
	}
	else {
		alert("Wybierz grupę produktową!");
		return false;
	}
}

function validateProducgroupSelector (theForm, submitButton) {
	if (submitButton == "show_category") return categoryValidator (theForm);
	if (submitButton == "show_productgroup") return productgroupValidator (theForm);
	return false;
}

function validatePasswordChange (theForm) {
	var reason = "";
	reason += isEmpty(theForm.passwd.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateUserPasswordChange (theForm) {
	var reason = "";
	reason += isEmptyPassword(theForm.passwd_old.value);
	reason += checkPassword(theForm.passwd.value);
	reason += checkPasswordVerify(theForm.passwd.value, theForm.passwd_verify.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateUserAdd (theForm) {
	var reason = "";
	reason += checkEmail(theForm.email.value);
	reason += isEmpty(theForm.passwd.value);
	reason += isEmpty(theForm.company.value);
	reason += isVatId(theForm.vatid.value);
	reason += isEmpty(theForm.firstname.value);
	reason += isEmpty(theForm.lastname.value);
	reason += isEmpty(theForm.street_name.value);
	reason += isEmpty(theForm.street_number.value);
	reason += isPostcode(theForm.postcode.value);
	reason += isEmpty(theForm.city.value);
	reason += isPhone(theForm.phone.value);
	reason += isTwoDigitDecimal(theForm.user_discount.value);
	reason += isTwoDigitDecimal(theForm.due_date.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateUserUpdate (theForm) {
	var reason = "";
	reason += isEmpty(theForm.lastname.value);
	reason += isEmpty(theForm.firstname.value);
	reason += isEmpty(theForm.street_name.value);
	reason += isEmpty(theForm.street_number.value);
	reason += isPostcode(theForm.postcode.value);
	reason += isEmpty(theForm.city.value);
	reason += isPhone(theForm.phone.value);
	reason += checkEmail(theForm.email.value);
	reason += isVatId(theForm.vatid.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateUserUpdateAdmin (theForm) {
	var reason = "";
	reason += isEmpty(theForm.company.value);
	reason += isVatId(theForm.vatid.value);
	reason += isEmpty(theForm.lastname.value);
	reason += isEmpty(theForm.firstname.value);
	reason += isEmpty(theForm.street_name.value);
	reason += isEmpty(theForm.street_number.value);
	reason += isPostcode(theForm.postcode.value);
	reason += isEmpty(theForm.city.value);
	reason += isPhone(theForm.phone.value);
	reason += checkEmail(theForm.email.value);
	reason += isTwoDigitDecimal(theForm.user_discount.value);
	reason += isTwoDigitDecimal(theForm.due_date.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateUserEdit (theForm, submitButton) {
	if (submitButton == "add_profile") return validateUserAdd (theForm);
	if (submitButton == "update_profile") return validateUserUpdate (theForm);
	if (submitButton == "update_user_profile") return validateUserUpdateAdmin (theForm);
	if (submitButton == "passwd_change") return validatePasswordChange (theForm);
	if (submitButton == "user_passwd_change") return validateUserPasswordChange (theForm);
	return false;
}

function validateUserNotesForm (theForm) {
	var reason = "";
	reason += checkUserNotes(theForm.user_notes.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateUserAddress (theForm) {
	var reason = "";
	reason += isEmpty(theForm.addr_lastname.value);
	reason += isEmpty(theForm.addr_firstname.value);
	reason += isEmpty(theForm.addr_street_name.value);
	reason += isEmpty(theForm.addr_street_number.value);
	reason += isPostcode(theForm.addr_postcode.value);
	reason += isEmpty(theForm.addr_city.value);
	reason += isPhone(theForm.addr_phone.value);
	if (reason != "") {
		alert(reason);
		return false;
	}
	return true;
}

function validateUserAddressForm (theForm, submitButton) {
	if (submitButton == "addr_remove") return true;
	if (submitButton == "update_useraddress") return validateUserAddress (theForm);
	return false;
}

function checkPipe (pipe_arr, pipe_id, pipe_len) {
	Array.prototype.inArray = function(v) {
	  for(var i in this){if(this[i] == v){return true;}}return false;
	}
	if (pipe_arr.inArray(pipe_id)) {
		if ((pipe_len % 6) == 0) {
			return true;
		}
		else {
			alert ('Podana ilość nie jest wielokrotnością 6');
			return false;
		}
	}
	else {
		return true;
	}
}