//############################## JAVA SCRIPTS FILE
//### ver. 1.13 [2008-12-18]
//###
//### Copyright by CN Design Marcin Czechowski
//###
//### cndesign.pl   biuro@cndesign.pl
//###
//### All rights reserved.
//############################################



//************************* Wyskakujace okno

function displayWindow(url, width, height)
{
 var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=0,scrollbars=yes,menubar=no' );
}



//************************* Liczenie znakow i ograniczenie textarea

function charleft(pole,max)
{
 document.getElementById('x').innerHTML = max - pole.value.length + 1;
 if (pole.value.length > max) pole.value = pole.value.substr(0,max);
}


//************************* Podmiana duzego zdjecia

function swap_image(image)
{
 document.getElementById('big_image').innerHTML = "<img src=" + image + " alt='' border=0>";
}


//************************* Pokazywanie / ukrywanie

otwarty = 0;
function show_hide(id)
{
 if(id != null)
 {
  if (otwarty > 0)
  {
   poprzedni = document.getElementById('pos_'+otwarty);
   poprzedni.style.display = 'none';
  }

  el = document.getElementById('pos_'+id);

  if(!el)
   return;
   el.style.display = 'block';
   otwarty = id;
 }
}


function show_or_hide(id)
{
 if(id != null)
 {
  el = document.getElementById('pos_'+id);

  if(!el)
   return;
   el.style.display = el.style.display == 'block' ? 'none' : 'block';
 }
}



//************************* Wstawianie tekstu

function storeCaret(textEl)
{
 if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}


function wstaw(text, zm)
{
  var gdzie = document.getElementById(zm); 
 if (gdzie.createTextRange && gdzie.caretPos)
 {
	var caretPos = gdzie.caretPos;
	caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	gdzie.focus();
 }
 else if (gdzie.selectionStart != undefined)
 {
	gdzie.value = gdzie.value.substring(0, gdzie.selectionStart) + text + gdzie.value.substring(gdzie.selectionStart);
	gdzie.focus();
 }
 else
 {
	gdzie.value += text;
	gdzie.focus();
 }
}


//************************************************************************
//************************* RAMKA EDYCJI KATEGORII

function CMS_cat_options(act, id, cat_name, script_prefix, l)
{
 var cat_box = document.getElementById('cat_' + id);

 if (act == 1)
 {
  cat_box.innerHTML = "<a href='?ad=" + script_prefix + "&amp;id_cat=" + id + "&amp;l=" + l + "'>" + cat_name + "</a> <a href='?co=gallery&amp;act=edit_gal&amp;id=" + id + "'><img src='gfx/edit.gif' alt='' border=0></a>";
 }
 else if (act == 2)
 {
  cat_box.innerHTML = "<a href='?ad=" + script_prefix + "&amp;id_cat=" + id + "&amp;l=" + l + "'>" + cat_name + "</a>";
 }

}



//************************************************************************
//************************* OBSŁUGA ZAKŁADEK

function selectContent(containerId, contentIndex, przycisk)
{
 // menu i zaznaczenie przycisku
 var pojemnik_menu = przycisk.parentNode;
 var elementy_menu = pojemnik_menu.getElementsByTagName("*");
 var ile_menu = elementy_menu.length;

 for ( var i = 0; i < ile_menu; i++ )
 {
  elementy_menu[i].className = 'off';
 }

 przycisk.className = 'on';

 // content
 var content = document.getElementById(containerId);
 var elementy_content = content.getElementsByTagName("div");
 var ile_content = elementy_content.length;

 for ( var i = 0; i < ile_content; i++ )
 {
  elementy_content[i].className = 'zakladka';
 }

 //pokazuje wybrany element
 elementy_content[contentIndex].className = 'selected';

		
 return false;
}



//************************************************************************
//************************* WALIDACJA FORMULARZY

function error(tekst)
{
 if (errfound) return;
	window.alert(tekst);
errfound = true;
}


//************************* FORMULARZ KONTAKTOWY

function validate_contact_pl()
{
 errfound = false;
 var bledy = "";

 with(document.contact)
 {
  if(sender_name.value == "") bledy += "* Proszę podać IMIĘ i/lub NAZWISKO.\n";
  if(sender_email.value == "") bledy += "* Podaj swój ADRES E-MAIL.\n";
  if(sender_email.value != "" && (sender_email.value.length < 4 || sender_email.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/) == null)) bledy += "* Podany ADRES E-MAIL wydaje się być nieprawidłowy!\n";
  if(subject.value == "") bledy += "* Podaj TEMAT wiadomości.\n";
  if(text.value == "") bledy += "* Wpisz TREŚĆ wiadomości.\n";

  if(bledy != "") error(bledy);
 }
return !errfound;
}


function validate_contact_en()
{
 errfound = false;
 var bledy = "";

 with(document.contact)
 {
  if(sender_name.value == "") bledy += "* Please fill in NAME AND SURNAME field.\n";
  if(sender_email.value == "") bledy += "* Please fill in E-MAIL ADDRESS field.\n";
  if(sender_email.value != "" && (sender_email.value.length < 4 || sender_email.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/) == null)) bledy += "* Your E-MAIL ADDRESS seems to be incorrect!\n";
  if(subject.value == "") bledy += "* Please fill in SUBJECT field.\n";
  if(text.value == "") bledy += "* Please fill in TEXT field.\n";

  if(bledy != "") error(bledy);
 }
return !errfound;
}


function validate_contact_ru()
{
 errfound = false;
 var bledy = "";

 with(document.contact)
 {
  if(sender_name.value == "") bledy += "* Please fill in NAME AND SURNAME field.\n";
  if(sender_email.value == "") bledy += "* Please fill in E-MAIL ADDRESS field.\n";
  if(sender_email.value != "" && (sender_email.value.length < 4 || sender_email.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/) == null)) bledy += "* Your E-MAIL ADDRESS seems to be incorrect!\n";
  if(subject.value == "") bledy += "* Please fill in SUBJECT field.\n";
  if(text.value == "") bledy += "* Please fill in TEXT field.\n";

  if(bledy != "") error(bledy);
 }
return !errfound;
}



//************************* DODAWANIE KOMENTARZA

function validate_com_logout_pl()
{
 errfound = false;
 var bledy = "";

 with(document.com_add)
 {
  if(nick.value == "") bledy += "* Proszę podać NICK.\n";
  if(text.value == "") bledy += "* Wpisz TREŚĆ komentarza.\n";

  if(bledy != "") error(bledy);
 }
return !errfound;
}


function validate_com_logout_en()
{
 errfound = false;
 var bledy = "";

 with(document.com_add)
 {
  if(nick.value == "") bledy += "* Please fill in NICK field.\n";
  if(text.value == "") bledy += "* Please fill in COMMENT field.\n";

  if(bledy != "") error(bledy);
 }
return !errfound;
}


function validate_com_logout_ru()
{
 errfound = false;
 var bledy = "";

 with(document.com_add)
 {
  if(nick.value == "") bledy += "* Please fill in NICK field.\n";
  if(text.value == "") bledy += "* Please fill in COMMENT field.\n";

  if(bledy != "") error(bledy);
 }
return !errfound;
}



//************************************************************************
//************************* WALIDACJA ADRESU EMAIL

function clearField(fieldname) {
	setStatus(fieldname, 'clear');
	if (fieldname == "name") {
	var button = document.getElementById('checkname');
	button.value = "Check Availability";
	button.disabled = '';
	button.style.display = '';
	}
}


function checkEmail(name) {
	var input = document.getElementById(name);
	if (name != "email1") {
		var first = document.getElementById("email1");
		if (first.value == input.value && input.value != null && input.value.length > 4 && input.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
			setStatus(name, 'good');
		else if (input.value.length <= 4)
			clearField(name);
		else setStatus(name, 'bad');
	} else {
		if (input.value != null && input.value.length > 4 && input.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
			setStatus(name, 'good');
		else if (input.value.length <= 4)
			clearField(name);
		else setStatus(name, 'bad');
	}
}


function checkPass(name) {
	var input = document.getElementById(name);
	if (name != "pass1") {
		var first = document.getElementById("pass1");
		if (first.value == input.value && input.value != null && input.value.length >= 5)
			setStatus(name, 'good');
		else if (input.value.length < 1)
			clearField(name);
		else setStatus(name, 'bad');
	} else {
		if (input.value != null && input.value.length >= 5)
			setStatus(name, 'good');
		else if (input.value.length < 1)
			clearField(name);
		else setStatus(name, 'bad');
	}
}


function setStatus(inputid, status) {
	var indicator = document.getElementById(inputid+'_status');
	if (indicator) {
		if (status == "good") {
			indicator.style.backgroundImage = "url('gfx/correct.gif')";
			done[inputid] = true;
			}
	else if (status == "bad") {
	   indicator.style.backgroundImage = "url('gfx/incorrect.gif')";
	   done[inputid] = false;
	   }
	else {
	   indicator.style.backgroundImage = 'none';
	   }
	}
}


