function PageQuery(q) {
  if (q.length > 1) this.q = q.substring(1, q.length);
  else this.q = null;

  this.keyValuePairs = new Array();

  if (q) {
    for (var i = 0; i < this.q.split("&").length; i++) {
      this.keyValuePairs[i] = this.q.split("&")[i];
    }
  }

  this.getKeyValuePairs = function() {
    return this.keyValuePairs;
  }

  this.getValue = function(s) {
    for (var j = 0; j < this.keyValuePairs.length; j++) {
      if (this.keyValuePairs[j].split("=")[0] == s) return this.keyValuePairs[j].split("=")[1];
    }
    return false;
  }

  this.getParameters = function() {
    var a = new Array(this.getLength());
    for (var j = 0; j < this.keyValuePairs.length; j++) {
      a[j] = this.keyValuePairs[j].split("=")[0];
    }
    return a;
  }

  this.getLength = function() {
    return this.keyValuePairs.length;
  }
}

function queryString(key) {
  var page = new PageQuery(window.location.search);
  return unescape(page.getValue(key));
}

function hideTopstory() {
}

function hideTopstory() {
  var page = queryString('pg');
  if ((page == 'false') || (page == 1)) {
    document.write(' <div id="topstory">');
  }
  else {
    document.write(' <div id="topstory" class="hidden">');
  }
}

function hideH2() {
  var page = queryString('pg');
  if ((page == 'false') || (page == 1)) {
    document.write(' <h2>');
  }
  else {
    document.write(' <h2 class="hidden">');
  }
}

function isValidDate(dateStr) {
  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

  var matchArray = dateStr.match(datePat);
  if (matchArray == null) {
    return false;
  }

  month = matchArray[1];
  day = matchArray[3];
  year = matchArray[4];

  if (month < 1 || month > 12) {
    return false;
  }

  if (day < 1 || day > 31) {
    return false;
  }

  if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
    return false;
  }

  if (month == 2) {
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day == 29 && !isleap)) {
      return false;
    }
  }

  return true;
}


function dispDate(dateObj) {
  month = dateObj.getMonth() + 1;
  day = dateObj.getDate();
  year = dateObj.getYear();
  if (year < 2000) year += 1900;

  return(day + '. ' + month + '. ' + year);
}


function calcPregnancy(pregform) {
	menstrual = new Date();
	ovulation = new Date();
	duedate = new Date();
	today = new Date();
	cycle = 0, luteal = 0;

	menstrualvalue = pregform.month.options[pregform.month.selectedIndex].value + '/' + pregform.day.options[pregform.day.selectedIndex].value + '/' + pregform.year.value;

	if (isValidDate(menstrualvalue)) {
		menstrualinput = new Date(menstrualvalue);
		menstrual.setTime(menstrualinput.getTime())
	}
	else {
		new Effect.Pulsate('delivery-date');
		return false;
  }

	cycle = (pregform.cycle.value == '' ? 28 : pregform.cycle.value);
	if (pregform.cycle.value != '' && (pregform.cycle.value < 22 || pregform.cycle.value > 45)) {
		return false;
	}

	luteal = (pregform.luteal.value == '' ? 14 : pregform.luteal.value);
	if (pregform.luteal.value != '' && (pregform.luteal.value < 9 || pregform.luteal.value > 16)) {
		return false;
	}

	ovulation.setTime(menstrual.getTime() + (cycle * 86400000) - (luteal * 86400000));
	document.getElementById('delivery-conception').innerHTML = '<strong>Odhadované datum početí:</strong><br /> ' + dispDate(ovulation);

	duedate.setTime(ovulation.getTime() + 266 * 86400000);
	document.getElementById('delivery-birth').innerHTML = '<strong>Odhadované datum narození:</strong><br /> ' + dispDate(duedate);

	var fetalage = 14 + 266 - ((duedate - today) / 86400000);
	weeks = parseInt(fetalage / 7);
	days = Math.floor(fetalage % 7);

	fetalage = weeks + (weeks > 1 ? (weeks > 4 ? ' týdnů' : ' týdny') : ' týden') + ' a ' + days + (days > 1 ? (days > 4 ? ' dnů' : ' dny') : ' den');
	document.getElementById('delivery-age').innerHTML = '<strong>Odhadované stáří plodu:</strong><br /> ' + fetalage;

	new Effect.SlideUp('delivery-input');
	window.setTimeout('Effect.SlideDown(\'delivery-output\')', 1000);
	return false;
}


function calcBMI(bmiform) {
  var height = parseInt(bmiform.height.value);
  var weight = parseInt(bmiform.weight.value);

  if (!height || isNaN(height)) {
    new Effect.Pulsate('bmi-height');
    return false;
  }

  if (!weight || isNaN(weight)) {
    new Effect.Pulsate('bmi-weight');
    return false;
  }

  Element.hide('bmi-comment-low');
  Element.hide('bmi-comment-ideal');
  Element.hide('bmi-comment-over');
  Element.hide('bmi-comment-strong');
  Element.hide('bmi-comment-fatso');

  height = height / 100;
  var bmi = Math.round(weight / (height * height));
  var bmiindex = 'Váš BMI index je <strong>' + bmi + '</strong>, což je <strong>';

  if (bmi >= 40) {
    bmiindex += 'těžká obezita';
    Element.show('bmi-comment-fatso');
  }
  else if (bmi >= 30) {
    bmiindex += 'obezita';
    Element.show('bmi-comment-strong');
  }
  else if (bmi >= 25) {
    bmiindex += 'mírná nadváha';
    Element.show('bmi-comment-over');
  }
  else if (bmi >= 20) {
    bmiindex += 'v pořádku';
    Element.show('bmi-comment-ideal');
  }
  else {
    bmiindex += 'podváha';
    Element.show('bmi-comment-low');
  }
  bmiindex += '</strong>.';

  document.getElementById('bmi-index').innerHTML = bmiindex;

  new Effect.SlideUp('bmi-input');
  window.setTimeout('Effect.SlideDown(\'bmi-output\')', 1000);
  return false;
}


function change_page(select) {
  destination = select.options[select.selectedIndex].value;
  if (destination) location.href = destination;
}


function makeArray() {
  this[0] = makeArray.arguments.length;
  for (i = 0; i < makeArray.arguments.length; i++) this[i + 1] = makeArray.arguments[i];
}


function get_sign(day, month) {
  var horroscopes = new makeArray(20, 19, 21, 21, 21, 21, 22, 22, 22, 23, 22, 21);

  if (day < horroscopes[month]) return month;
  else if (month == 12) return 1;
  else return month + 1;
}


var horoscopechild_articles = new makeArray(205042, 211011, 210988, 204713, 204712, 204711, 205253, 205223, 205178, 205152, 211307, 211308);

var horoscope_articles = new makeArray(203309, 203310, 203311, 203300, 203301, 203302, 203303, 203304, 203305, 203306, 203307, 203308);

var horoscopeyear_articles = new makeArray(206403, 206402, 206401, 206412, 206411, 206410, 206409, 206408, 206407, 206406, 206405, 206404);

var horoscopemonth_articles = new makeArray(209310, 209311, 209312, 209301, 209302, 209303, 209304, 209305, 209306, 209307, 209308, 209309);


function change_page_horoscopechild(horoform) {
  datevalue = horoform.month.options[horoform.month.selectedIndex].value + '/' + horoform.day.options[horoform.day.selectedIndex].value + '/' + horoform.year.value;

  if (isValidDate(datevalue)) {
    article_id = horoscopechild_articles[get_sign(horoform.day.options[horoform.day.selectedIndex].value, horoform.month.options[horoform.month.selectedIndex].value)];
    location.href = '/scripts/detail.php?id=' + article_id;
  }
  else {
    new Effect.Pulsate('horoscopechild-date');
    return false;
  }
}


function change_page_hch(sign) {
  article_id = horoscopechild_articles[sign];
  location.href = '/scripts/detail.php?id=' + article_id;
}


function change_page_h(sign) {
  article_id = horoscope_articles[sign];
  location.href = '/scripts/detail.php?id=' + article_id;
}


function change_page_hy(sign) {
  article_id = horoscopeyear_articles[sign];
  location.href = '/scripts/detail.php?id=' + article_id;
}


function change_page_hm(sign) {
  article_id = horoscopemonth_articles[sign];
  location.href = '/scripts/detail.php?id=' + article_id;
}


function showPoll() {
  document.getElementById('poll-options').innerHTML = document.getElementById('poll-options-temp').innerHTML;
  if (Element.visible('poll-options')) {
    new Effect.Opacity('poll-options', {duration:0.5, from:0.1, to:1.0});
  }
  else {
    new Effect.Appear('poll-options', {duration:0.5});
  }
}


function changePoll(content) {
  document.getElementById('poll-options-temp').innerHTML = content;
  if (Element.visible('poll-options')) {
    new Effect.Opacity('poll-options', {duration:0.5, from:1.0, to:0.1});
  }
  window.setTimeout('showPoll()',1500);
}


function getCheckedValue(radioObj) {
	if(!radioObj)
		return '';
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return '';
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return '';
}


function changeBMI(radioobj) {
  radiovalue = getCheckedValue(radioobj);
  if (radiovalue == 'male') {
    document.getElementById('bmi').className = 'box male';
  }
  else if (radiovalue == 'female') {
    document.getElementById('bmi').className = 'box female';
  }

  return false;
}


function changeBMIsingle(radioobj) {
  radiovalue = getCheckedValue(radioobj);
  if (radiovalue == 'male') {
    document.getElementById('bmi-single').className = 'male';
  }
  else if (radiovalue == 'female') {
    document.getElementById('bmi-single').className = 'female';
  }

  return false;
}
