/**
  Java Scripts for usage in comination with the web-forms.php script
  
  (c) Johannes Weberhofer, 2004, 2005
  
**/

/**
 ** checkDateField
 ** this function checks wheter a date entered into a field is a valid 
 ** date or not. After checking the date, a fully formatted data is set 
 ** back into the field again. Whenever values are invalid, a message 
 ** window will pop up.
 ** 
 ** @param text text-object containing the date that should be approved
 ** 		regular expression to transform a locally formatted date into an 
 ** 		ISO date (yyyy-mm-yy) format.
 ** @param boolean true whenever a text field is oblgatory
 ** @param string regex to compose a ISO data format from a local date format
 ** @param string message that should be printed. '' causes no text to be
 **			printed, null supresses any message. Text content must be 
 **			written in single quotes, because the string will be evaluated 
 **			by the interpretor to allow variables to be used.
 ** @param boolean set when the message should be 
 **/
function checkDateField (textfield, loc2isoRegex, obligatory, messagecontent) {

	// check, wheter emtpy dates are allowed
	is_ok = ( (textfield.value!= '') || !obligatory );
	
	if (is_ok) {
		// first step: bring the date in a nomalized form related 
		// to the given language and format
		//WEBform_dateFormat
		splitre = /([0-9]*).([0-9]*).([0-9]*)/
		ISOdate = textfield.value.replace( splitre, loc2isoRegex);
		
		dateParts = splitre.exec(ISOdate);
		is_ok = (dateParts != null);
		if (is_ok) {
			xyear = parseInt(dateParts[1]);
			xmonth = parseInt(dateParts[2]);
			xday = parseInt(dateParts[3]);
			is_ok = (!isNaN(xyear) && !isNaN(xmonth) && !isNaN(xday));
		}
		if (is_ok) {
			is_ok = ((xyear > -2000) && (xyear<3000) && (xmonth>0) && (xmonth<13) && (xday>0) && (xday<32));
		}
		
		if (is_ok) {
			if (xyear<10) {
				xyear += 2000;
			} else if (xyear<99) {
				xyear += 1900;
			}
			dateok = new Date();
			dateok.setFullYear( xyear, xmonth-1, xday);
			is_ok = true;
		}
	}
		
	if (is_ok) {
		// output the formatted date again
		if ( loc2isoRegex=='$3.$1.$2') {
			invRegex = '$2.$3.$1';
		} else if ( loc2isoRegex == '$2.$3.$1') {
			invRegex = '$3.$1.$2';
		} else {
			invRegex = loc2isoRegex;
		}
		ISOdate = dateok.getUTCFullYear().toString()+'-'+
				  (1+dateok.getUTCMonth()).toString()+'-'+dateok.getUTCDate().toString();
		textfield.value = ISOdate.replace( splitre, invRegex);
	} else {
		if (messagecontent != null) {
			msgcont = eval(messagecontent);
			alert(textfield.title+' - '+msgcont+' '+ textfield.value);
		}
	}
	return is_ok;
}
/**
 ** checkEmail
 ** this function controls wheter a e-Mail address in a given field is valid
 **/

function checkEmail(textfield, messageText) {
	var is_ok = false;
	if (messageText==null) {
		messageText = 'Wrong e-mail address';
	}	
	if (textfield.value != "")	{
	  var str = textfield.value;
	  var regWrong = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	  var regOk = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/;
	  if ( regWrong.test(str) || !regOk.test(str)) {
		  alert(textfield.title +' - '+ messageText);
		  is_ok = false;
		}
	};
  return(is_ok);
}