// formcheck.js
// A set of routines used in webs created by D+DD
// 2001-12-28 (c) Docu + Design Daube, www.daube.ch
// 2007-03-13 check for e-mail extended and tested with RegExBuddy
// 2007-03-15 checkSelection added

// global variables to be defined _before_ including this file ! ----------------
// var language = "it"; // for the messages
// var _console = null; // for debugging output

switch (language) {
	case "de": var z_empty = "' muss angegeben werden.";
	           var z_field= "Feld '";
	           var z_zipcode="Ungültige PLZ für die Schweiz";
	           var z_email="Die e-mail Adresse scheint falsch zu sein ('@' und '.' prüfen)";
	           var z_ipaddress="IP Addresse ist ungültig - Werte > 255";
             var z_select="Nichts wurde gewählt in '";
	           break;
	case "fr": var z_empty = "' est necessaire.";
	           var z_field= "Champ '";
	           var z_zipcode="Erreur dans l'NPA Suisse";
	           var z_email="L'addresse e-mail nes pas correcte (contrôler les '@' et '.')";
	           var z_ipaddress="L'addresse IP nes pas correcte - valeurs > 255";
             var z_select="Nothing selected from the list '";
	           break;
	case "it": var z_empty = "' devono essere.";
	           var z_field= "Casallo '";
	           var z_zipcode="Errore nel NPA Svizzera";
	           var z_email="Il email e no vàlido (controllare il '@' et '.')";
	           var z_ipaddress="Il adresse IP e no vàlido!";
             var z_select="Nothing selected from the list '";
	           break;
	case "en": var z_empty = "' must be filled in.";
	           var z_field= "Field '";
	           var z_zipcode="Invalid ZIP code for Switzerland";
	           var z_email="The email address seems incorrect (check the '@' and '.'s)";
	           var z_ipaddress="Destination IP address is invalid - values > 255";
             var z_select="Nothing selected from the list '";
	           break;
  default:   var z_empty = "' must be filled in.";
	           var z_field= "Field '";
	           var z_zipcode="Invalid ZIP code for Switzerland";
	           var z_email="The email address seems incorrect (check the '@' and '.'s)";
	           var z_ipaddress="Destination IP address is invalid - values > 255";
             var z_select="Nothing selected from the list '";
}

/* ------- this function needs to be in the page -----------
   from.xxxx should be in the order of occurrance in the form
   plz assumes Swiss zip codes (4 digits between 1000 and 9999)
   
function checkForm(form) {
	return (checkString(form.name,   "Nachname")   &&
				 checkString(form.vorname, "Vorname")    &&	
				 checkString(form.strasse, "Strasse")    &&
				 checkString(form.strNr,   "Hausnummer") &&
				 checkString(form.ort,     "Ort")        &&
				 checkEmail (form.mail,    "E-mail")     &&
				 checkselected (form.wanted, "Operating System", 0) &&
				 checkZIP(form.plz)                    )
}
   ------- Pay special attention to the last paranthesis!
   for optional e-mail:
   (isEmpty(form.email) || checkEmail(form.email, "E-mail"))   )


*/

function isEmpty (inputvalue) {
// alert ("<" + inputvalue.value + "> " + inputvalue.value.length);
	if (inputvalue.value.lenght == 0) {
		return true;
	} 
	else {
		for ( var i=0; i<inputvalue.value.length; i++ ) {
			if (inputvalue.value.charAt(i) != " " ) {
				return false;
			}
		}
		return true;
	}
}

function checkEmpty(inputvalue) {
	if ( inputvalue.value.length == 0 ) {
   	return false;
	}
	else {
 	return spaceOnly(inputvalue);
	}
}

function spaceOnly(inputvalue) {
	for ( var i=0; i<inputvalue.value.length; i++ ) {
   	if ( inputvalue.value.charAt(i) != " " ) {
	   	return true;
		}
	}
	return false;
}

function countChars(inputvalue, number) {
	if ( inputvalue.value.length == number ) {
		return true;
	}
	else {
		return false;
	}
}

function isnumValue(inputvalue) {
	for ( var i=0; i<inputvalue.value.length; i++ ) {
		currChar = inputvalue.charAt(i);
		if ( currChar < "0" || currChar > "9" ) {
			return false;
		}
	}
	return true;
}

function numValueinRange(inputvalue, lower, upper) {
	var numValue = parseInt(inputvalue);
	return ((numValue >= lower) && (numValue <= upper))
}

function checkString(inputvalue, str) {
	if ( checkEmpty(inputvalue) && spaceOnly(inputvalue)) {
		return true;
	}	
	else {
		inputvalue.focus();
		alert(z_field + str + z_empty);
		return false;
	}
}

function checkZIP(inputvalue) {
	if ( isnumValue(inputvalue.value) && countChars(inputvalue.value, 4) && numValueinRange(inputvalue.value, 1000, 9999) ) {
		return true;
	}	
	else {
		inputvalue.focus();
		alert(z_zipcode);
		return false;
	}
}

function checkSelection (inputvalue, liststr, notselected) {
  // alert ("func checkSelection: " + inputvalue.value);
	if (inputvalue.value != notselected) {
		return true;
	}
	else {
		inputvalue.focus();
		alert(z_select + liststr + "'");
	  return false;
	}
}

function checkEmail(emailStr, str) {
	// checks if the e-mail address is syntactically valid
	// TLD may now be 2 (countries) or 3 (com) to 6 (museum) characters long
	// Special characters such as apostrophs or umlauts are not allowed by this RegEx
  var emailPat= /^([\w-]+(?:\.[\w-]+)*)@(((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2,4})?)|(\[([0-9]{1,3}\.){3}[0-9]{1,3}\]))$/i
	var matchArray = emailStr.value.match(emailPat);
	if (matchArray == null) {
		emailStr.focus();
		alert(z_field + str + "' : " + z_email);
		return false;
	}
	// alert (matchArray)
	// make sure the IP address domain is valid
	var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
	if (IPArray != null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				emailStr.focus();
				alert(z_field + str + "' : " + z_ipaddress);
				return false;
			}
		}
	}
	return true;
}


function debug(msg) {
  // open a window the first time we are called, or after an existing console window has been closed.
  if ((_console == null) || (_console.closed)) {
    _console = window.open("","console","width=600,height=200,resizable");
    _console.document.open("text/plain");
  }
    _console.document.writeln(msg);
}