var cursor = 0;
var maxFields = 5;
var displayMsg = false;

function submitEmail(formID) {
	var form = $(formID);
	var valForm = validate_form(form);
	
	if (valForm == 'OK') {
		displayMsg = true;
		form.submit();
		$('delphiEmail').innerHTML = $('sendingEmailText').innerHTML;
	}
	else if (valForm == 'FIELDS') {
		alert($('formAlert').innerHTML);
	}
	else if (valForm == 'REG') {
		alert($('formAlert2').innerHTML);
	}
	else if (valForm == 'FIELDSEMAIL') {
		alert($('formAlert3').innerHTML);
	}
	else if (valForm == 'EMAIL') {
		alert($('formAlert4').innerHTML);
	}
}

function displayMessage() {
	if (displayMsg) {
		//iframeDocument = $('submitFrame').contentDocument;
		//alert(parent.frames["submitFrame"].document.body.innerHTML);//iframeDocument.getElementById('MSG').innerHTML);
		var response = parent.frames["submitFrame"].document.body.innerHTML;
		if (response == 'MSG_SENT') {
			//alert('Wysłano Zgłoszenie');
			$('delphiEmail').innerHTML = $('sendingEmailText_MSG_SENT').innerHTML;
		}
		else if (response == 'MSG_SIZE_ERROR') {
			//alert('Wiadomość nie została wysłana. Łączny rozmiar plików przekroczył limit 10MB');
			$('delphiEmail').innerHTML = $('sendingEmailText_MSG_SIZE_ERROR').innerHTML;
		}
		else if (response == 'MSG_ERROR') {
			//alert('Wiadomość nie została wysłana');
			$('delphiEmail').innerHTML = $('sendingEmailText_MSG_ERROR').innerHTML;
		}
		else if (response == 'MSG_CAPTCHA_ERROR') {
			$('delphiEmail').innerHTML = $('sendingEmailText_MSG_CAPTCHA_ERROR').innerHTML;
		}
		else {
			$('delphiEmail').innerHTML = $('sendingEmailText_MSG_ERROR').innerHTML;
		}
	}
}

function addInput() {
	if (cursor < maxFields) {
	  $('attachment_'+cursor).setStyle({display: 'inline'});
	  $('attachment_'+cursor).disabled = false;
	  cursor++;
	}
}

function deleteInput() {
	if (cursor > 0) {
		cursor--;
	  $('attachment_'+cursor).setStyle({display: 'none'});
	  $('attachment_'+cursor).disabled = true;
	}
}

function validate_required(field)
{
	with (field)
	{
		if (value==null||value==""||value=="0") {
			return false;
		}
		else {
			return true;
		}
	}
}

function validate_email(field)
{
	with (field)
	{
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) {
			return false;
		}
		else {
			return true;
		}
	}
}

function validate_form(thisform)
{
	with (thisform)
	{
		isName = validate_required(name);
		isSurname = validate_required(surname);
		isRefnum = validate_required(refnum);
		isEmail = validate_email(email);
		isPhone = validate_required(phone);
		
		if (!isName || !isSurname || !isEmail || !isPhone || !isRefnum) {
			//return 'FIELDS';
			if ((!isName || !isSurname || !isPhone || !isRefnum) && !isEmail) {
				return 'FIELDSEMAIL';
			}
			else if ((!isName || !isSurname || !isPhone || !isRefnum) && isEmail) {
				return 'FIELDS';
			}
			else if (!isEmail && (isName && isSurname && isPhone && isRefnum)) {
				return 'EMAIL';
			}
		}
		else if (!$('acceptReg').checked) {
			return 'REG';
		}
		else {
			return 'OK';
		}
	}
}

// Zliczanie
function zliczaj(nr_f, nr_l, nr_t) {
  with(document.getElementById('contactForm')) {
   var tekst = $("boxText").value; // cały text z pola textarea
   var dl_tresc = $("boxText").value.length; // długoś tekstu z textarea
   var maxlen = 500; // maxymalna dozwolona długość
   var prawdziwa = 0; // licznik znaków
   var entery = 0;

    for (i=0; i<dl_tresc; i++) {
       if (tekst.charAt(i) == "\n")  {
         prawdziwa++;
         entery++;
           if (navigator.appName != "Netscape") i++;
       }
      prawdziwa++;
    } // koniec for i
    $("boxCount").value= maxlen - prawdziwa; // wyświetl komunikat o licznie znaków
    if (prawdziwa>maxlen) {
       if (navigator.appName != "Netscape")
         $("boxText").value = $("boxText").value.substring(0,maxlen);
       else
         $("boxText").value = $("boxText").value.substring(0,maxlen-entery);
      $("boxCount").value = maxlen - maxlen;
   	}
   	
   	if ($("boxCount").value == 0) {
   		$("boxCount").style.backgroundColor = "#FFBAC2";
   	}
   	else {
   		$("boxCount").style.backgroundColor = "#EFEFEF";
   	}
  }   // koniec with
}
