// JavaScript Document
	
function disableEnterKey(){
	if (window.event.keyCode == 13)
		window.event.keyCode = 0;
}

function toUpper(doctext) {
	var pattern = /(\w)(\w*)/; // a letter, and then one, none or more letters
	if (doctext.value.length > 0) {
	var a = doctext.value.split(/\s+/g); // split the sentence into an array of words
	
	for (i = 0 ; i < a.length ; i ++ ) {
	var parts = a[i].match(pattern); // just a temp variable to store the fragments in.
	
	var firstLetter = parts[1].toUpperCase();
	var restOfWord = parts[2].toLowerCase();
	
	a[i] = firstLetter + restOfWord; // re-assign it back to the array and move on
	}
	
	doctext.value = a.join(' '); // join it back together
	}
}

function toCaps(doctext) {
	doctext.value = doctext.value.toUpperCase();
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false
	 }

	 return true					
}

function autoComplete (field, select, property, forcematch) {
	var found = false;
	for (var i = 0; i < select.options.length; i++) {
	if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
		found=true; break;
		}
	}
	if (found) { select.selectedIndex = i; }
	else { select.selectedIndex = -1; }
	if (field.createTextRange) {
		if (forcematch && !found) {
			field.value=field.value.substring(0,field.value.length-1); 
			return;
			}
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[i][property] : oldValue;
			if (newValue != field.value) {
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
				}
			}
		}
}

function checkNumeric(objName,minval, maxval,comma,period,hyphen) {
	var numberfield = objName;
	if (chkNumeric(objName,minval,maxval,comma,period,hyphen) == false)
	{
		//numberfield.select();
		//numberfield.focus();
		return false;
	}
	else
	{
		return true;
	}
}

var monthname=new Array(11)
	monthname[4]="April"
	monthname[6]="June"
	monthname[9]="September"
	monthname[11]="November"

function isTrueDate(d,m,y)
	
{
	//alert("["+d+"]["+m+"]["+y+"]");
	//error caused in insert if not valid only check if one filled in as not mandatory
	if (d != "" || m != "" || y != "" )
	{
		if (d == "")
			{
				alert("Please choose the Day or Date");
				return false;
			}
		if (m == "")
			{
				alert("Please choose the Month");
				return false;
			}
		if (y == "")
			{
				alert("Please select the Year");
				return false;
			}
	
		if ((m==4 || m==6 || m==9 || m==11) && d==31)
			{
			alert(monthname[m]+" only has 30 days")
			return false;
			}
		
		if (m == 2) 
			{ // check for february 29th
			var isleap = (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));
			if (d > 29 || (d==29 && !isleap)) 
				{
				alert("February " + y + " doesn`t have " + d+ " days");
				return false;
				}
			}
	}
	//return true; // date is valid
}
function checkMinAge(cd,cm,cy)
{
//mina age is 18
	var dtoday = new Date();
	var thisyear = dtoday.getYear();
	var thismonth = dtoday.getMonth() +1;
	var thisday = dtoday.getDate();
	var nyears1 = thisyear - 18;
	var nyears2 = thisyear - 17;
	var nmonths2 = thismonth - cm;
	//alert("nyear1 "+nyears1 + ' nyear2 ' + nyears2 + ' nmonth2 ' + nmonths2);
	//alert("year["+thisyear+"]month["+thismonth+"]today["+dtoday+"] form month"+cm);
	if( nyears1 <= cy)
	{
		// play safe lets check month and date if 17
		//alert("nyear1 "+nyears1 + ' cy '+cy);
		if(nyears2 >= cy)
		{
			// check month 
			if(nmonths2 <= 0)
			{
				//alert(nmonths2+" here "+ cd +" > " +thisday);
				if(nmonths2 == 0 && cd > thisday )
				{
				// ok nearly but not quite
				alert("Minimum age is 18");
				return false;
				}
				
			}
		}
		else
		{
		alert("Minimum age is 18");
		return false;
		}
	}
}

function chkNumeric(objName,minval,maxval,comma,period,hyphen) {
	// only allow 0-9 be entered, plus any values passed
	// (can be in any order, and don't have to be comma, period, or hyphen)
	// if all numbers allow commas, periods, hyphens or whatever,
	// just hard code it here and take out the passed parameters
	var checkOK = "0123456789" + comma + period + hyphen;
	var checkStr = objName;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	
	for (i = 0;  i < checkStr.value.length;  i++)
	{
	ch = checkStr.value.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
	if (ch == checkOK.charAt(j))
	break;
	if (j == checkOK.length)
	{
	allValid = false;
	break;
	}
	if (ch != ",")
	allNum += ch;
	}
	if (!allValid)
	{	
	//alertsay = "Please enter only these values \""
	//alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
	//alert(alertsay);
	return (false);
}

// set the minimum and maximum
var chkVal = allNum;
var prsVal = parseInt(allNum);
if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))
{
//alertsay = "Please enter a value greater than or "
//alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
//alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
//alert(alertsay);
return (false);
}
}

function checkdate(field){
	var checkstr = "0123456789";
	var DateField = field;
	var Datevalue = "";
	var DateTemp = "";
	var seperator = "/";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
   err = 0;
   DateValue = DateField.value;
	if (DateValue == "") {  return true;
	}
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if ((DateValue.length == 6) && (DateValue.substr(4,2) <= 20)) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if ((DateValue.length == 6) && (DateValue.substr(4,2) > 20)) {
      DateValue = DateValue.substr(0,4) + '19' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 27; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
   }
   /* Error-message if err != 0 */
   else {
      alert("Please enter a valid date in the format DD/MM/YYYY!");
	  
     DateField.select();
	  DateField.focus();
	  return false;
   }
}

function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}
