﻿// funkcja sprawdzająca wszyskie wymagane pola typu select
function checkAllSelect(isGood){
  var selectList = document.getElementsByTagName('select');
  for(i=0; i <selectList.length; i++){
	//alert (selectList[i].name);  
    if (selectList[i].title =='wymagany' && selectList[i].value=="w"){
        temp = selectList[i].name+"IncorrectValue";
        document.getElementById(temp).innerHTML = "Pole wymagane";
        isGood = false;
    }
  }
  return isGood;
}

// funkcja sprawdzająca wszyskie wymagane pola typu text i checkbox
function checkAllInputText(isGood){
  var inputList = document.getElementsByTagName('input');
  for(i=0; i <inputList.length; i++){
	//alert (inputList[i].name);  
    if (inputList[i].title =='wymagany' && inputList[i].value=="" && inputList[i].type=="text"){
        temp = inputList[i].name+"IncorrectValue";
        document.getElementById(temp).innerHTML = "Pole wymagane";
        isGood = false;
    }
    if (inputList[i].title =='wymagany' && !inputList[i].checked && inputList[i].type=="checkbox"){
        temp = inputList[i].name+"IncorrectValue";
        document.getElementById(temp).innerHTML = "Pole wymagane";
        isGood = false;
    }
  }
  return isGood;
}

// funkcja sprawdzająca wszyskie wymagane pola typu textarea
function checkAllTextarea(isGood){
  var inputList = document.getElementsByTagName('textarea');
  for(i=0; i <inputList.length; i++){
	//alert (inputList[i].name);  
    if (inputList[i].title =='wymagany' && inputList[i].value==""){
	    temp = inputList[i].name+"IncorrectValue";
        document.getElementById(temp).innerHTML = "Pole wymagane";
        isGood = false;
    }
  }
  return isGood;
}

// funkcja sprawdzająca wszyskie wymagane pola typu textarea
function checkPassword(isGood){
  var inputList = document.getElementsByTagName('input');
  for(i=0; i <inputList.length && i <= 10; i++){
    if (inputList[i].value==""  && inputList[i].type=="password"){
      temp = inputList[i].name+"IncorrectValue";
      document.getElementById(temp).innerHTML = "Pole wymagane";
      isGood = false;
    }
  }
  
  if(isGood && document.getElementById('formHaslo').value!=document.getElementById('formPowtorzHaslo').value){
      document.getElementById('formHasloIncorrectValue').innerHTML = "Wprowadzone hasła nie zgadzają się";
      isGood = false;
  }
  return isGood;
}


  // sprawdza, czy użytkownik wprowadził poprawny adres email
function isEmailAddress(isGood, formName){
//  var dobryEmail=/^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$/i;
  var dobryEmail=/^[a-z0-9\-\._]+@([a-z0-9\-_]+\.)+[a-z]{2,4}$/i;
  
  val = document.getElementById(formName).value;
  
  if (dobryEmail.test(val)){
    return isGood;
  }
  else{
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Niepoprawny adres e-mail";
    return false;
  }
}

  // sprawdza, czy użytkownik wprowadził poprawny telefon
  
function isPhoneNumber(isGood, formName){
//  var dobryNumer=/[0-9\b]+$/;
  var dobryNumer=/^[0-9()\- ]{7,}$/;
  
  val = document.getElementById(formName).value;
  
  if (dobryNumer.test(val)){
    return isGood;
  }
  else{
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Niepoprawny nr telefonu";
    return false;
  }
}

  // sprawdza, czy użytkownik wprowadził poprawny telefon
  
function isRightFormatedPhoneNumber(isGood, formName, format){
  var dobryNumer = "";
  if (!format){
	dobryNumer=/^([0-9]{3})\-([0-9]{3})\-([0-9]{3})$/;
  }else{
	if (format==1){//stacjonarny
		dobryNumer=/^([0-9]{2})\-([0-9]{7})$/;
	}else if(format==2){//komorkowy
		dobryNumer=/^([0-9]{3})\-([0-9]{3})\-([0-9]{3})$/;
	}else if(format==3){//stacjonarny np z dopisanym wewnetrznym
		dobryNumer=/^([0-9]{2})\-([0-9]{7})(.*)$/;
	}
  }
  
  val = document.getElementById(formName).value;
  
  //alert (dobryNumer+" | "+val);
  
  if (dobryNumer.test(val)){
    return isGood;
  }
  else{
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Niepoprawny nr telefonu";
    return false;
  }
}

  // sprawdza, czy użytkownik wprowadził liczbe / cyfre
  
function isNumber(isGood, formName){
  var dobryNumer=/^[0-9]{1,}$/;
  
  val = document.getElementById(formName).value;
  
  if (dobryNumer.test(val)){
    return isGood;
  }
  else{  
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Niepoprawna wartość";
    return false;
  }
}

  // sprawdza, czy użytkownik wprowadził liczbe / liczbe zmiennoprzecinkowa
  
function isFloatNumber(isGood, formName){
  var dobryNumer=/^[0-9,]{1,}$/;
  
  val = document.getElementById(formName).value;
  
  if (dobryNumer.test(val)){
    return isGood;
  }
  else{  
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Niepoprawna wartość (miejsca dziesiętne po przecinku";
    return false;
  }
}

  // sprawdza poprawnosc formatu kodu pocztowego
  
function isPostCode(isGood, formName){
  var dobryKod=/\d{2}-\d{3}$/;
  
  val = document.getElementById(formName).value;
  
  if (dobryKod.test(val)){
    return isGood;
  }
  else{
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Niepoprawny format kodu pocztowego";
    return false;
  }
}

// funkcja sprawdzająca wszyskie wymagane pola typu radio
function checkAdresWysylkiRadio(isGood, formName){
  var inputList = document.getElementsByTagName('input');
  ok = false;   
  for(i=0; i <inputList.length; i++){
    if ((inputList[i].name == formName) && (inputList[i].checked == true)) ok = true;
  }
  if (ok){
	return isGood;  
  }else{
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Musisz zaznaczyć adres wysyłki";
    temp = formName+"2IncorrectValue";
    document.getElementById(temp).innerHTML = "&nbsp;";	
    return false;
  }  
}

// funkcja sprawdzająca wszyskie wymagane pola typu radio
function checkRadio(isGood, formName){
  var inputList = document.getElementsByTagName('input');
  ok = false;   
  for(i=0; i <inputList.length; i++){
    if ((inputList[i].name == formName) && (inputList[i].checked == true)) ok = true;
  }
  if (ok){
	return isGood;  
  }else{
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Musisz zaznaczyć odpowiedź";
    //temp = formName+"2IncorrectValue";
    //document.getElementById(temp).innerHTML = "&nbsp;";	
    return false;
  }  
}

  // sprawdza poprawnosc formatu daty
  
function isDate(isGood, formName){
  var dobryKod=/^\d{4}-\d{2}-\d{2}$/;
  
  val = document.getElementById(formName).value;
  
  if (dobryKod.test(val)){
    return isGood;
  }
  else{
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Niepoprawny format daty";
    return false;
  }
}

  // sprawdza poprawnosc formatu godziny
  
function isTime(isGood, formName){
  var dobryKod=/^\d{2}:\d{2}$/;
  
  val = document.getElementById(formName).value;
  
  if (dobryKod.test(val)){
    return isGood;
  }
  else{
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Niepoprawny format godziny";
    return false;
  }
}

  // sprawdza, czy przynajmniej jedno pole wypelnione 
  
function oneFieldNotEmpty(isGood, formName1, formName2, value){
  
  val1 = document.getElementById(formName1).value;
  val2 = document.getElementById(formName2).value;
  
// alert(val1+" "+val2);
  
  if ((val1 != value) || (val2 != value)){
    return isGood;
  }
  else{
    temp = formName1+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Jedno z pól musi mieć wartość inną niż "+value;
    return false;
  }
}

function checkSelect(isGood, formName){
  var selectField = document.getElementById(formName);
  if (selectField.value=="w"){
    temp = selectField.name+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Pole wymagane";
    isGood = false;
  }
  return isGood;
}

// funkcja sprawdzająca wszyskie wymagane pola typu text i checkbox
function checkAllFormInputText(isGood, formName){
//alert(formName);
  var inputList = eval("document."+formName+".getElementsByTagName('input')");  
  for(i=0; i <inputList.length; i++){
    if (inputList[i].title =='wymagany' && inputList[i].value=="" && inputList[i].type=="text"){
        temp = inputList[i].name+"IncorrectValue";
        document.getElementById(temp).innerHTML = "Pole wymagane";
        isGood = false;
    }
    if (inputList[i].title =='wymagany' && !inputList[i].checked && inputList[i].type=="checkbox"){
        temp = inputList[i].name+"IncorrectValue";
        document.getElementById(temp).innerHTML = "Pole wymagane";
        isGood = false;
    }
  }
  return isGood;
}

// funkcja sprawdzająca pole typu text
function checkInputText(isGood, formName){
//alert(formName);
  var inputField = document.getElementById(formName);  
  if (inputField.value==""){
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Pole wymagane";
    isGood = false;
  }
  return isGood;
}

// sprawdza, czy użytkownik wprowadził poprawny telefon w dobrym formacie
function isFormatedPhoneNumber(isGood, formName){
  var dobryNumer=/([0-9]{3})\-([0-9]{3})\-([0-9]{3})$/;
  
  val = document.getElementById(formName).value;
  
  if (dobryNumer.test(val)){
    return isGood;
  }
  else{
    temp = formName+"IncorrectValue";
    document.getElementById(temp).innerHTML = "Niepoprawny nr telefonu";
    return false;
  }
}

// funkcja ukrywania/pokazywania elementu na stronie
function showHide(elemToShow, elemToHide){
  //alert ("pokazac: "+elemToShow+", ukryc: "+elemToHide);
  var elementsToShow = elemToShow.split(";");
  for(i=0; i< elementsToShow.length; i++){
    document.getElementById(elementsToShow[i]).style.display="block";
  }
  var elementsToHide = elemToHide.split(";");
  for(i=0; i< elementsToHide.length; i++){
    document.getElementById(elementsToHide[i]).style.display="none";
  }  
}


// sprawdza poprawność formularza ankiety oceniajacej
function checkQuestionnaireForm(){
	var spanList = document.getElementsByTagName('span');
	for(i=0; i <spanList.length; i++){
		if (spanList[i].className =='IncorrectValue') spanList[i].innerHTML = "";
	}
	var isGood = true;

	isGood = checkAllInputText(isGood);
	//alert ("sprawdzone inputy");  
	isGood = checkAllTextarea(isGood);
	//alert ("sprawdzone textaree");  

//	isGood = false;

	if (isGood == true){
		return true;
	}else{
		alert('Popraw informacje wprowadzone do formularza');
		return false;
	}
}

//pokazanie formularza przypomnienia hasla
function remindPass(level_id, temp){
	pom = document.getElementById('komunikat');
	if (pom) pom.innerHTML = "";
	document.getElementById('formLogin').style.display="none";
	document.getElementById('formRemind').style.display="block";
}

// sprawdza poprawność formularza zmiany hasla
function checkUpdatePasswordChangeForm(){
	var spanList = document.getElementsByTagName('span');
	for(i=0; i <spanList.length; i++){
		if (spanList[i].className =='incorrectValue') spanList[i].innerHTML = "";
	}

	var isGood = true;

	var inputList = document.getElementsByTagName('input');
	for(i=0; i <inputList.length; i++){
		if (inputList[i].title =='wymagany' && inputList[i].value==""){
			temp = inputList[i].name+"IncorrectValue";
			document.getElementById(temp).innerHTML = "Pole wymagane";
			isGood = false;
		}
	}

	if (document.getElementById('fHaslo').value != document.getElementById('fPowtorzHaslo').value){
		document.getElementById('fHasloIncorrectValue').innerHTML = "Wprowadzone hasła nie zgadzają się";
		document.getElementById('fPowtorzHasloIncorrectValue').innerHTML = "Wprowadzone hasła nie zgadzają się";
		isGood = false;
	}

	//isGood = false;

	if (isGood == true){
		return true;
	}else{
		alert('Popraw informacje wprowadzone do formularza');
		return false;
	}
}

