var theRequired = 0;
var theType = "";
var theMinLength = "NaN";
var theMaxLength = "NaN";
var theMessage =""


// ----------------------------------------------------------------------------------------------------------------------
//   GoToPage
// ----------------------------------------------------------------------------------------------------------------------
function GoToPage(Course)
{
		if  (Course == 2)
		{
			theWidth=680
			theHeight=650
	
			theTop=(screen.availHeight - theHeight) / 2
			theLeft=(screen.availWidth - theWidth) / 2
			F1 = window.open("about:blank","Rechnung","scrollbars=yes,width=" + theWidth + ",height=" + theHeight + ",left="+theLeft +",top="+theTop)
	
			document.order.frm.GoTo.value = 2;
			document.order.frm.target="Rechnung"
			document.order.frm.submit();
		}
		else
		{
			if (Course==1)
			{
				switch (document.order.frm.Page.value)
				{
					case "1":
						if (CheckTicketData(document.order.frm)==false){return}
						break;
					case "3":
						if (document.getElementById("Accept").checked ==false)
						{	alert("Sie müßen unser AGbs akzeptierem.")
							return
						}
						break;
					default:
						if (CheckFormData(document.order.frm)==false){return}
						break;
				}
			}	
			
			
			lngPageCount = parseInt(document.order.frm.Page.value)
			lngPageCount = lngPageCount + Course

			for (var i=1; i<=3; i++) 
			{
				strItem = "ShopHeader" + i
				if (lngPageCount == i)
				{
					document.getElementById(strItem).style.display='block'
				}
				else
				{
					document.getElementById(strItem).style.display='none'
				}
			}
			
					
			document.order.frm.GoTo.value = Course;
			document.order.frm.target="_self"
			document.order.frm.submit();
			
			document.getElementById("gtb").disabled=true 
			document.getElementById("gtp").disabled=true
			document.getElementById("gtf").disabled=true
		}
}


// ----------------------------------------------------------------------------------------------------------------------
//   RefreshButton
// ----------------------------------------------------------------------------------------------------------------------
function RefreshButton(Page)
{
	//alert("RefreshButton: " + Page +"\nButton: " + parent.CheckForm.elements[0].name);

	// Button GoToBackward
	if (Page==1 || Page==4){tmp="hidden";} else {tmp="visible";}
 	parent.document.getElementById("gtb").style.visibility = tmp
	parent.document.getElementById("gtb").disabled=false

	// Button GoToPrint & Accept
	if (Page==3){tmp="visible";} else {tmp="hidden";}
 	parent.document.getElementById("gtp").style.visibility = tmp
	parent.document.getElementById("gtp").disabled=false
	
	parent.document.getElementById("Accept").checked=false
	parent.document.getElementById("at").style.visibility = tmp


	// Button GoToForward
	if (Page==4){tmp="hidden";} else {tmp="visible";}
	if (Page==3){ButtonName="Absenden";} else {ButtonName="Weiter >";}
 	parent.document.getElementById("gtf").style.visibility = tmp
	parent.document.getElementById("gtf").value = ButtonName
	//if (Page!=4){parent.document.getElementById("gtf").disabled=false}
	parent.document.getElementById("gtf").disabled=false
}

// ----------------------------------------------------------------------------------------------------------------------
//   CheckTicketData
// ----------------------------------------------------------------------------------------------------------------------
function ChangeData(ID)
{
	lngST = document.getElementById("ST" + ID).value
	strEP = document.getElementById("EP" + ID).firstChild.data
	
	strEP = strEP.replace('€','')
	lngEP = strEP.replace(',','.')
	lngGP = lngST * lngEP
	strGP = lngGP.toFixed(2)
	strGP = strGP.replace('.',',')
	document.getElementById("GP" + ID).firstChild.data = strGP +" €"
}
// ----------------------------------------------------------------------------------------------------------------------
//   CheckTicketData
// ----------------------------------------------------------------------------------------------------------------------
function CheckTicketData(CheckForm)
{
	if (CheckFormData(CheckForm)==false)
	{
		return false
	}
	else
	{
		ysnFlag=false
		for (var i=0;i<=CheckForm.length-1 ;i++ )
		{
			strTemp=CheckForm.elements[i].name
			strTemp=strTemp.substr(0,2)
			if (strTemp=='ST' && CheckForm.elements[i].value>0)
			{
				return true
				break
			}
		}

		alert("Sie müßen mindesten eine Artikel bestellen.")
		return false
	}
}

// ----------------------------------------------------------------------------------------------------------------------
//   CheckFormData
// ----------------------------------------------------------------------------------------------------------------------
function CheckFormData(CheckForm)
{
	var theMessageText =""
	for (var i=0;i<=CheckForm.length-1 ;i++ )
	{
		if (typeof ( CheckForm.elements[i].Verify) =='string')
		{
			ParserVerify ( CheckForm.elements[i].Verify);
			
			// Daten formatieren
			switch (theType.toLowerCase())
			{
				case "text":
					break;
				case "zip":
					break;
				case "email":
					theTemp = CheckForm.elements[i].value;
					CheckForm.elements[i].value=theTemp.toLowerCase();
					break;
			}

			switch ( CheckForm.elements[i].type)
			{
				case "text":
					theResult = CheckDataText(CheckForm.elements[i].value);
					break;
				case "select-one":
					theResult = CheckDataSelect(CheckForm.elements[i].selectedIndex);
					break;
				default:
					theResult = CheckDataText(CheckForm.elements[i].value);
					break;
			}


			//alert("Field: " +  CheckForm.elements[i].name + "\nType: " +  CheckForm.elements[i].type + "\nValue: " +  CheckForm.elements[i].value + "\n\nRequired: " + theRequired + "\nType: " + theType + "\nMinLength: " + theMinLength + "\nMaxLength: " + theMaxLength + "\nMessage: " + theMessage+ "\n\nResult: " + theResult);

			if (theResult==false)
			{
				if (theMessage == "")
				{
					theMessage = GetMessage(CheckForm.elements[i].name)
				}

				theMessageText = theMessageText + theMessage + "\n\n";
			}
		}
	}

	if (theMessageText !="")
	{
		alert(theMessageText);
		return false
	}
	else
	{
		return true
	}
}

// =========================================================================================================

// ----------------------------------------------------------------------------------------------------------------------
//   ParserVerify
// ----------------------------------------------------------------------------------------------------------------------
function ParserVerify(theString)
{
	theRequired = ParseArg(theString,"Required:")
	switch (theRequired)
	{
	case "True":
		theRequired=-1;
		break;
	case "-1":
		theRequired=-1;
		break;
	default:
		theRequired=0;
		break;
	}

	theType =Trim(ParseArg(theString, "Type:"));
	theMinLength = parseInt(ParseArg(theString, "MinLength:"));
	theMaxLength = parseInt(ParseArg(theString, "MaxLength:"));
	theMessage = ParseArg(theString, "Message:");
}

// ----------------------------------------------------------------------------------------------------------------------
// ParseArg
// ----------------------------------------------------------------------------------------------------------------------
function ParseArg(theString, strSearch)
{
	var lngPosStart=theString.indexOf(strSearch);

	if (lngPosStart !=-1)
	{
		lngPosStart = lngPosStart + strSearch.length;

		var strPosEnd=theString.indexOf(";",lngPosStart);
		if (strPosEnd !=-1)
		{
			theResult=theString.substring(lngPosStart,strPosEnd);
		}
		else
		{
			theResult=theResult=theString.slice(lngPosStart);
		}
	}
	else
	{
		theResult=""
	}
	
	//alert("String: " + theString + "\nSearch: " + strSearch + "\nPosStart: " + lngPosStart + "\nPosEnd: " + strPosEnd + "\nResult: " + theResult);
	return theResult
}

// ----------------------------------------------------------------------------------------------------------------------
// ParseArg
// ----------------------------------------------------------------------------------------------------------------------
function Trim(theString)
{
	if (theString != "")
	{
		while (theString.charCodeAt(0) == 32)
		{
			theString = theString.slice(1)
			alert(theString)
		}

		while (theString.charCodeAt(theString.length) == 32)
		{
			theString = theString.slice(1)
			alert(theString)
		}

	}
	return theString
}

// ----------------------------------------------------------------------------------------------------------------------
// GetMessage
// ----------------------------------------------------------------------------------------------------------------------
function GetMessage(theFieldName)
{

	switch (theType.toLowerCase())
	{
	case "text":
		if (theRequired ==-1 && isNaN(theMinLength) == true && isNaN(theMaxLength) == true)
		{
			theResult = "Das Feld '" + theFieldName +"' muß ausgefült sein."
		}

		if (theRequired ==-1 && isNaN(theMinLength) == false && isNaN(theMaxLength) == true)
		{
			theResult = "Das Feld '" + theFieldName +"' muß ausgefült sein und mindestens " + theMinLength + " Zeichen haben."
		}
		
		if (theRequired ==-1 && isNaN(theMinLength) == true && isNaN(theMaxLength) == false)
		{
			theResult = "Das Feld '" + theFieldName +"' muß ausgefült sein und  maximal " + theMaxLength + " Zeichen haben."
		}

		if (theRequired ==-1 && isNaN(theMinLength) == false && isNaN(theMaxLength) == false)
		{
			theResult = "Das Feld '" + theFieldName +"' muß ausgefült sein und  mindestens " + theMinLength + ", maximal " + theMaxLength + " Zeichen haben."
		}

		if (theRequired ==0 && isNaN(theMinLength) == false && isNaN(theMaxLength) == true)
		{
			theResult = "Das Feld '" + theFieldName +"' muß mindestens " + theMinLength + " Zeichen haben oder leer sein."
		}
		
		if (theRequired ==0 && isNaN(theMinLength) == true && isNaN(theMaxLength) == false)
		{
			theResult = "Das Feld '" + theFieldName +"' darf maximal " + theMaxLength + " Zeichen haben oder leer sein."
		}

		if (theRequired ==0 && isNaN(theMinLength) == false && isNaN(theMaxLength) == false)
		{
			theResult = "Das Feld '" + theFieldName +"' muß mindestens " + theMinLength + " Zeichen und darf maximal " + theMaxLength + " Zeichen haben oder leer sein."
		}
		break;
	case "zip":
		if (theRequired ==-1)
		{
			theResult ="Das Feld '" + theFieldName +"' darf nicht leer sein, nur Ziffer enthalten und muß 5 Zeichen haben."
		}

		if (theRequired ==0)
		{
			theResult ="Das Feld '" + theFieldName +"'' darf nur Ziffer enthalten und muß 5 Zeichen haben."
		}

		break;
	case "num":
		if (theRequired ==-1)
		{
			theResult ="Das Feld '" + theFieldName +"' darf nicht leer sein, nur Ziffer enthalten."
		}

		if (theRequired ==0)
		{
			theResult ="Das Feld '" + theFieldName +"'' darf nur Ziffer enthalten."
		}

		break;
	case "email":
		if (theRequired ==-1)
		{
			theResult ="Das Feld '" + theFieldName +"'' darf nicht leer sein, oder die EMail-Adresse ist ungültig."
		}

		if (theRequired ==0)
		{
			theResult ="Die EMail-Adresse im Feld '" + theFieldName +"'' ist ungültig."
		}
		break;
	case "phone":
		if (theRequired ==-1)
		{
			theResult ="Das Feld '" + theFieldName +"'' darf nicht leer sein, oder die Telefonnr. ist ungültig.\nEs sind nur Ziffer und die Zeichen '+/() ' erlaubt."
		}

		if (theRequired ==0)
		{
			theResult ="Die Telefonnr. im Feld '" + theFieldName +"'' ist ungültig.\nEs sind nur Ziffer und die Zeichen '+/() ' erlaubt."
		}
		break;

	case "selectnofirst":
		if (theRequired ==-1)
		{
			theResult ="Sie müssene im Feld '" + theFieldName +"'' einen Eintrag auswählen."
		}

		if (theRequired ==0)
		{
			theResult ="Wählen Sie im Feld '" + theFieldName +"'' einen Eintrag aus."
		}
		break;

	default:
		theResult ="Wählen Sie im Feld '" + theFieldName +"'' einen Eintrag aus."
		break;
	}
	return theResult
}


// =========================================================================================================

// ----------------------------------------------------------------------------------------------------------------------
// CheckDataText
// ----------------------------------------------------------------------------------------------------------------------
function CheckDataText(theValue)
{
	// Eingabe notwendig
	if (theRequired ==-1 && theValue.length == 0) return false
	
	// Eingabe nicht notwendig und kein Inhalt
	if (theRequired ==0 && theValue.length == 0) return true

	// Type Prüfung
	switch (theType.toLowerCase())
	{
	case "text":
		 theResult = CheckText(theValue)
		break;
	case "zip":
		 theResult = CheckZIP(theValue)
		break;
	case "email":
		theResult = CheckEMail(theValue)
		break;
	case "phone":
		theResult = CheckPhone(theValue)
		break;
	case "num":
		theResult = CheckNum(theValue)
		break;
	default:
		theResult = true
		break;
	}

	return theResult
}


// ----------------------------------------------------------------------------------------------------------------------
// CheckDataSelect
// ----------------------------------------------------------------------------------------------------------------------

function CheckDataSelect(theValue)
{
	
	switch (theType.toLowerCase())
	{
	case "selectnofirst": // Erstes Wlement darfnicht ausgewählt sein
		// Eingabe notwendig
		if (theRequired ==-1 && (theValue == 0 || theValue == -1)) return false
		if (theRequired ==0 && theValue == 0) return false
		break;
	default:
		if (theRequired ==-1 && theValue == -1) return false
		break;
	}

	return true
}



// ----------------------------------------------------------------------------------------------------------------------
// CheckText
// ----------------------------------------------------------------------------------------------------------------------
function CheckText(theValue)
{

	if (isNaN(theMinLength) == false)
	{
		if (theValue.length < theMinLength)
		{
			return false
		}
	}

	if (isNaN(theMaxLength) == false)
	{
		if (theValue.length > theMaxLength)
		{
			return false
		}
	}
	return true
}

// ----------------------------------------------------------------------------------------------------------------------
// CheckNum
// ----------------------------------------------------------------------------------------------------------------------

function CheckNum(theValue)
{
	var CheckChrs = "0123456789";

	for (var i=0;i<=theValue.length; i++ )
	{
		if (CheckChrs.search(theValue.substr(i,1)) == -1)
		{
			return false;
		}
	}

	return true;
}


// ----------------------------------------------------------------------------------------------------------------------
// CheckZIP
// ----------------------------------------------------------------------------------------------------------------------

function CheckZIP(theValue)
{
	var CheckChrs = "0123456789";

	if (theValue.length != 5)
	{
		return false;
	}
	
	for (var i=0;i<=5 ; i++ )
	{
		if (CheckChrs.search(theValue.substr(i,1)) == -1)
		{
			return false;
		}
	}

	return true;
}



// ----------------------------------------------------------------------------------------------------------------------
// CheckPhone
// ----------------------------------------------------------------------------------------------------------------------
function CheckPhone(theValue)
{
	var CheckChrs = "0123456789+/() ";

	for (var i=0;i<=theValue.length ; i++ )
	{
		if (CheckChrs.search(theValue.substr(i,1)) == -1)
		{
			return false;
		}
	}
	
	return true;
}



// ----------------------------------------------------------------------------------------------------------------------
// CheckeEMail
// ----------------------------------------------------------------------------------------------------------------------

function CheckEMail(theValue)
{
	theValue = theValue.toLowerCase()

	var ShowAlert = false
	var checkTLD=1; 																					/* The following variable tells the rest of the function whether or not to verify that the address ends in a two-letter country or well-known TLD.  1 means check it, 0 means don't. */ 
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/; 		/* The following is the list of known TLDs that an e-mail address must end with. */ 
	var emailPat=/^(.+)@(.+)$/; 																		/* The following pattern is used to check if the entered e-mail address fits the user@domain format.  It also is used to separate the username from the domain. */ 
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; 													/* The following string represents the pattern for matching all special characters.  We don't want to allow special characters in the address. These characters include ( ) < > @ , ; : \ " . [ ] */ 
	var validChars="\[^\\s" + specialChars + "\]"; 														/* The following string represents the range of characters allowed in a username or domainname.  It really states which chars aren't allowed.*/ 
	var quotedUser="(\"[^\"]*\")"; 																		/* The following pattern applies if the "user" is a quoted string (in which case, there are no rules about which characters are allowed and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com is a legal e-mail address. */ 
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; 								/* The following pattern applies for domains that are IP addresses, rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal e-mail address. NOTE: The square brackets are required. */ 
	var atom=validChars + '+'; 																			/* The following string represents an atom (basically a series of non-special characters.) */ 
	var word="(" + atom + "|" + quotedUser + ")"; 														/* The following string represents one word in the typical username. For example, in john.doe@somewhere.com, john and doe are words. Basically, a word is either an atom or quoted string. */ 
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");											/* The following pattern describes the structure of the user */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); 										/* The following pattern describes the structure of a normal symbolic domain, as opposed to ipDomainPat, shown above. */ 


	// EMail in ihre Struktur zerlegen. Trenner ist '@'.
	var theResult=theValue.match(emailPat); 
	if (theResult==null) return false

	var theUser=theResult[1]; 
	var theDomain=theResult[2]; 


	// Prüfen, ob nur ASCII-Zeichen  (0-127) verwendet wurden
	for (var i=0; i<theUser.length; i++)
	{
		if (theUser.charCodeAt(i)>127) return false; 
	}

	for (var i=0; i<theDomain.length; i++)
	{
		if (theDomain.charCodeAt(i)>127) return false; 
	}

	// Benutzername üperprüfen.
	if (theUser.match(userPat)==null)	return false; 


	// Üperprüfen ob die EMail-Addresse eine IP-Adresse ist und diese dann Prüfen
	var IPArray=theDomain.match(ipDomainPat); 
	if (IPArray != null) 
	{
		for (var i = 1;i<=4;i++) 
		{
			if (IPArray[i]>255) return false
		}
		return true;
	}


	// Domain ist ein symbolischer Name.  Überprüfen Sie, ob er gültig ist.
	var atomPat=new RegExp("^" + atom + "$"); 
	var domArr=theDomain.split("."); 
	var len=domArr.length; 

	for (var i=0;i<len;i++)
	{
		if (domArr[i].search(atomPat)==-1) return false;
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) return false; 
	if (len<2) return false; 

	return true
}
