function addClass (elementName, newClass) {
  elementName.className = newClass + (elementName.className ? ' ' : '') + elementName.className;
}

function removeClass(elementName, oldClass) {
  var classesOld = elementName.className.split(' ');
  var classesNew = new Array();
  var i;

  for (i in classesOld) {
    if (classesOld[i] != oldClass) {
      classesNew.push(classesOld[i]);
    }
  }
  elementName.className = classesNew.join(' ');
}

function clickButton(button) {
	b = document.getElementById(button);
	b.click();
}

function mySubmit(button, nextActionVar, nextActionValue) {
  na = document.getElementById(nextActionVar);
  na.value = nextActionValue;
  b = document.getElementById(button);
  b.click();
  return false;
}

function myConfirmedSubmit(message, button, nextActionVar, nextActionValue) {
  if (confirm(message)) {
    mySubmit(button, nextActionVar, nextActionValue);
  }
  return false;
}

var jhodiny;

function j_hodiny() {
  var j_cas, j_datum;

  jhodiny.setTime(jhodiny.getTime()+1000);
  var datum = jhodiny;
  var hodiny=datum.getHours();
  var minuty=datum.getMinutes();
  var vteriny=datum.getSeconds();
  var den=datum.getDate();
  var mesic=datum.getMonth() + 1;
  var rok=datum.getFullYear();

  if (den<10) { den="0"+den; }
  if (mesic<10) { mesic="0"+mesic; }
  if (hodiny==0) { hodin=24; }
  if (vteriny<10) { vteriny="0"+vteriny; }
  if (minuty<10) { minuty="0"+minuty; }

  j_cas=document.getElementById('bottomLineTime');
  j_datum=document.getElementById('bottomLineDate');


  if (j_cas && j_cas.firstChild && j_cas.firstChild.nodeType == 3) {
    j_cas.firstChild.nodeValue=hodiny + ":" + minuty + ":" + vteriny;
  }

  if (j_datum && j_datum.firstChild && j_datum.firstChild.nodeType == 3) {
    j_datum.firstChild.nodeValue= den + "." + mesic + "." + rok;
  }

}

function checkAnchor() {
  if (document.location.hash) {
    var message = '';
    errorDiv = document.getElementsByName('error');
    for (i = 0;i < errorDiv.length;i++) {
      message += ' ' + errorDiv[i].innerHTML;
    }
    if (message) alert(message);
  }
}


String.prototype.lpad = function(padString, length) {
    var str = this;
    while (str.length < length) {
        str = padString + str;
    }
    return str;
}
 
//pads right
String.prototype.rpad = function(padString, length) {
    var str = this;
    while (str.length < length) {
        str = str + padString;
    }
    return str;
}

function j_hodiny_init(time) {
  jhodiny = new Date();
  jhodiny.setTime(time*1000);
  j_hodiny();
  setInterval('j_hodiny()',1000);
}

function sendMessage(textMessageFrom, textMessageText) {
  messageFrom = document.getElementById('messageFrom').value;
  if (messageFrom == '') {
    alert(textMessageFrom);
    return false;
  }
  messageText = document.getElementById('messageText').value;
  if (messageText == '') {
    alert(textMessageText);
    return false;
  }
  button = document.getElementById('buttonSubmit');
  button.click();
}

function clearInput(elementId, defaultText) {
  text = document.getElementById(elementId).value;
  if (text == defaultText) {
    document.getElementById(elementId).value = '';
  }
}

function alertContactMailSent(text) {
  if (text != '') {
    alert(text);
  }
}

function setContactPrevId () {
	pId = document.getElementById('photoId').getAttribute('value');
	if (pId == 0) {
		maxPId = document.getElementById('maxPhotoId').getAttribute('value');
		pId = maxPId - 1;
	} else {
		pId = pId - 1;
	}
	document.getElementById('photoId').setAttribute('value', pId);
	changeEmailUrl(pId);
	changeContactUrl(pId);
}

function setContactNextId () {
	pId = document.getElementById('photoId').getAttribute('value');
	maxPId = document.getElementById('maxPhotoId').getAttribute('value');
	if (pId == maxPId - 1) {
		pId = 0;
	} else {
		pId = pId - (-1);
	}
	document.getElementById('photoId').setAttribute('value', pId);
	changeEmailUrl(pId);
	changeContactUrl(pId);
}


function changeEmailUrl(pId) {
	mails = document.getElementById('emails').getAttribute('value');
	mailArray = new Array();
	mailArray = mails.split(',');

	document.getElementById('emailUrl').href = 'mailto:' + mailArray[pId];
}

function changeContactUrl(pId) {
	id = document.getElementById('ids').getAttribute('value');
	idArray = new Array();
	idArray = id.split(',');
	
	document.getElementById('contactUrl').href = document.getElementById('contactUrlBase').value + 'vContact/' + idArray[pId] + '.html';
}
