// Validate JS Functions
// Version 1.05
// copyright by Alexander Gingelmaier, team@celtisnet.de - www.celtisnet.de


function IsCharNum(theElem) {
	var valid = " abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var ok = "yes";
	var temp;
	for (var i=0; i<theElem.value.length; i++) {
	temp = "" + theElem.value.substring(i, i+1);

	if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert("Nur normale Zeichen und Zahlen verwenden!");
		theElem.focus();
		return true;
	}
	return false;
}

function IsPosNum(theElem) {
	var valid = "0123456789";
	var ok = "yes";
	var temp;
	for (var i=0; i<theElem.value.length; i++) {
	temp = "" + theElem.value.substring(i, i+1);

	if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert("Bitte nur positive Zahlen verwenden!");
		theElem.focus();
		return true;
	}
	return false;
}

function IsHTML(theElem) {
	HTMLToCheck = theElem.value;
	HTMLChecked = DeleteWord(HTMLToCheck);
	theElem.value = HTMLChecked;
	if(HTMLToCheck != HTMLChecked)
	{
		alert("Kein HTML verwenden. Dies wurde gerade entfernt!");
		theElem.focus();
		return true;
	}
	return false;
}

function DeleteWord(Word) {
	a = Word.indexOf("<");
	b = Word.indexOf(">");
	
	len = Word.length;

	c = Word.substring(0, a);
	if(b == -1)
	b = a;
	d = Word.substring((b + 1), len);
	Word = c + d;

	tagCheck = Word.indexOf("<");

	if(tagCheck != -1) 
	Word = DeleteWord(Word);
	return Word;
}

function CheckSubmit(frm,check1)
{ 
	if ((frm.disabled==true)&&(check1.checked==true)) 
	{ 
		frm.disabled = false; 
	} else { 
		frm.disabled = true; }
}

function CompareFields(theElem1,theElem2)
{ 
	if(theElem1.value != theElem2.value)
	{
		alert("Bestätigung stimmt nicht überein!");
		theElem2.focus();
		return true;
	}
	return false;
}

function IsDate(theElem)
{
	result = true;
	aStr = theElem.value;
	count = aStr.length;
	
	if( count!=0 )
	{
		result = false;
		var ar = aStr.split(".");
		
		if( ar.length==3 )
		{
			month = ar[1];
			day = ar[0];
			year = ar[2]	
		
			if( IsIntPriv(day) && IsIntPriv(month) && IsIntPriv(year) )
			{
				day = parseInt(RemoveLeadZeros(day));
				month = parseInt(RemoveLeadZeros(month))-1;
				year = parseInt(RemoveLeadZeros(year));

				// validate date
				mydate = new Date( year, month, day )
				if((day==mydate.getDate())&&(month==mydate.getMonth())&&(year==mydate.getFullYear())&&(year>1753))
					result = true;
			}
		}

		// show error message
		if( !result )
		{
			alert("Dieses Feld benötigt ein Datum (tt.mm.yyyy)!");
			theElem.focus();
		}
	}

	return result;
}

function IsEmail(theElem)
{
	result = true;
	aStr = theElem.value;
	count = aStr.length;
	
	if( count!=0 )
	{
		// get index of @
		inda = aStr.indexOf("@",0)
	
		// check for more @ and first/last @
		if( (aStr.indexOf("@",inda+1)!=-1)||(inda==0)||(inda==count-1))
			result = false;

		// check dot before @
		indd = aStr.indexOf(".",0)
		if(indd==inda-1)
			result = false;
		
		// get index of dot after @
		indd = aStr.indexOf(".",inda+1)
	
		// dot must be available. It can't be near and or @
		if( (indd==-1)||(indd==inda+1)||(indd==count-1))
			result = false;

		// show error message
		if( !result )
		{
			alert("e-mail- address is not valid!");
			theElem.focus();
		}
	}

	return result;
}

function IsEmpty(theElem)
{
	myValue = theElem.value
	while( myValue.charAt(0)==" " )
		myValue = myValue.substr( 1 );
	if(myValue == "")
	{
		alert("Please fill out all required * informations!");
		theElem.focus();
		return true;
	}
	return false;
}

function IsEmptyRadioButton(theElem,amount,fieldname)
{
  RadioChecked=false;
  for (ic=0;ic<amount;ic++) {
	  if (theElem[ic].checked) RadioChecked=true;
	  }
	  if (!RadioChecked) { 
		alert('Bitte unbedingt '+fieldname+' angeben.'); 
		return true;   
  }
  return false;
}

function IsEmptyThisRadioButton(theElem,fieldnumber,fieldname)
{
  RadioChecked=false;
  if (theElem[fieldnumber].checked) RadioChecked=true;
  if (!RadioChecked) { 
	alert('Bitte unbedingt '+fieldname+' angeben.'); 
	return true;   
  }
  return false;
}

function minLengthString(theElem, len) {
	if (theElem.value.length < len) {
	alert("Dieses Feld muss aus mindestens " + len + " Zeichen bestehen.");
	theElem.focus();
	return true;
	} 
	return false;
}

function fixedLengthString(theElem, len) {
	if (theElem.value.length != len) {
	alert("Dieses Feld muss unbedingt aus " + len + " Zeichen bestehen.");
	theElem.focus();
	return true;
	}
	return false;
}

function IsTime(theElem)
{
	result = true;
	aStr = theElem.value;
	count = aStr.length;	

	if( count!=0 )
	{
		result = false;
		
		firstInd = aStr.indexOf(":",0)
		if(firstInd!=-1)
		{
			if( IsIntPriv(aStr.substr(0,firstInd)) )
			{
				s1 = parseInt(RemoveLeadZeros(aStr.substr(0,firstInd)));	
				secInd = aStr.indexOf(":",firstInd+1)
				if(secInd!=-1)
				{
					if( (IsIntPriv(aStr.substr(firstInd+1,secInd-firstInd-1))) && (IsIntPriv(aStr.substr(secInd+1,count-secInd-1))) )
					{
						s2 = parseInt(RemoveLeadZeros(aStr.substr(firstInd+1,secInd-firstInd-1)));
						s3 = parseInt(RemoveLeadZeros(aStr.substr(secInd+1,count-secInd-1)));
						
						if ((s1 < 13)&&(s1>=0)&&(s2<60)&&(s2>=0)&&(s3<60)&&(s3>=0))
							result = true;
					}
				}
			}
		}

		// show error message
		if( !result )
		{
			alert("Dieses Feld benötigt eine Uhrzeit (hh:mm:ss)!");
			theElem.focus();
		}
	}

	return result;
}

function IsHourMinute(theElem)
{
	result = true;
	aStr = theElem.value;
	count = aStr.length;	

	if( count!=0 )
	{
		result = false;
		
		firstInd = aStr.indexOf(":",0)
		if(firstInd!=-1)
		{
			if( IsIntPriv(aStr.substr(0,firstInd)) )
			{
				s1 = parseInt(RemoveLeadZeros(aStr.substr(0,firstInd)));	
				secInd = aStr.indexOf(":",firstInd+1)
				if( (IsIntPriv(aStr.substr(firstInd+1,count-firstInd-1))) )
				{
					s2 = parseInt(RemoveLeadZeros(aStr.substr(firstInd+1,count-firstInd-1)));
						
					if ((s1 < 24)&&(s1>=0)&&(s2<60)&&(s2>=0))
						result = true;
				}
			}
		}

		// show error message
		if( !result )
		{
			alert("Dieses Feld benötigt eine Uhrzeit (hh:mm)!");
			theElem.focus();
		}
	}

	return result;
}