﻿/****************************************************************************************************************************
 functions: (1) removing whitespace (2) make short alias of id value (3) check whether checksum and typed names are correct 
*****************************************************************************************************************************/

function remove_XS_whitespace(item) {
 		var tmp = "";
 		var item_length = item.value.length;
 		var item_length_minus_1 = item.value.length - 1;
 			for (index = 0; index < item_length; index++) {
   			if (item.value.charAt(index) != ' ') {
     				tmp += item.value.charAt(index); }
   			else {
     				if (tmp.length > 0) {
       				if (item.value.charAt(index+1) != ' ' && index != item_length_minus_1) {
         					tmp += item.value.charAt(index); }
       			}
       		}
   		}
 		item.value = tmp;
}
  	

// just to shorten the id calling
// elval .. element value */ 
function elval(id) {
	return document.getElementById(id).value;
}

/* 	.: chkForm() first does check the small math task sum 
	.: if correct it calls twice the function to remove whitespace before and after the typed first- and lastname
	.: afterwards it checks whether the typed names are empty [""] - with the logical OR "&&" it is allowed to leave one name
	   blank but not both - so if both are empty the alert() forces the user to type in again, that would be also the case if
	   only whitespaces are typed cause of the remove function 
	.: after the alert() the focus is set on the name input field and the "return false" is to avoid performing the form action
 */	
function chkForm() {
	if(elval('sum') != elval('mchk')) {
		alert("Feil sum! Vennligst prøv igjen!");
		// -- refresh to create a new task --
		location.href = '/index.php?newtask=1';
		return false; 
	}
	remove_XS_whitespace(document.getElementById('fname'));
	if(	(elval('fname') == "") || (elval('fname') == "[navn]") ) {
		document.getElementById('fname').focus();
		document.getElementById('fname').select();
		alert("Vennligst skriv inn et navn");
		return false; 
	}

// if form correct...
  self.name = "hovedvindu";
// -- open new window and center it -- 
  var hpos = (screen.availWidth - 600) / 2;
  var vpos = (screen.availHeight - 200) / 2;
  
  nyttVindu = window.open("./wp-content/plugins/mps-1st-widgets/birth4s/birth-to-db.php?"
  						+ "fname="+elval('fname')+"&bday_day="+elval('bday_day')+"&bday_month="+elval('bday_month')+"&bday_year="+elval('bday_year'),
  						  "nyttVindu",
  						  "location=0,status=0,toolbar=0,scrollbars=0,resizable=1,width=600,height=200,"
  						+ "screenX="+hpos+",screenY="+vpos+",left="+hpos+",top="+vpos);
  nyttVindu.focus();
}


/*************************************************************************************************************************************
 script functions for showBirthList() in birth4s-functions.php
**************************************************************************************************************************************/

function showImg(ix, bw, bh) {
var img_b = document.getElementsByTagName('img')[ix];
	img_b.style.position = 'absolute';
	img_b.style.maxWidth = bw + 'px';
	img_b.style.height = bh + 'px';
	img_b.style.width = bw + 'px';
	img_b.style.border = '3px solid #000'; 
	img_b.style.background = '#000';
	img_b.style.marginLeft = '-3px';
	img_b.style.marginTop = '-3px';
}
function hideImg(ix, sw, sh, bw, bh) {
var img_s = document.getElementsByTagName('img')[ix];
	img_s.style.position = 'static';
	img_s.style.height = sh + 'px';
	img_s.style.maxWidth = sw + 'px';
	img_s.style.width = sw + 'px';
	img_s.style.border = 'none'; 
	img_s.style.background = 'none';
	img_s.style.marginLeft = '0px';
	img_s.style.marginTop = '0px';
}