var emailregexp = /^[\w\-\.]+@([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)*[a-zA-Z0-9]+-{0,1}[a-zA-Z0-9]+(-{0,1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,4}$/;
var daysInMonth = new Array();
	daysInMonth[1] = 31;
	daysInMonth[2] = 29;   // must programmatically check this
	daysInMonth[3] = 31;
	daysInMonth[4] = 30;
	daysInMonth[5] = 31;
	daysInMonth[6] = 30;
	daysInMonth[7] = 31;
	daysInMonth[8] = 31;
	daysInMonth[9] = 30;
	daysInMonth[10] = 31;
	daysInMonth[11] = 30;
	daysInMonth[12] = 31;

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;


function chkForm(frmname,lang) {
	return checkForm(frmname);
}

function checkForm(frmname){
	var frm = eval('document.'+frmname);
	if(frm.firstname){
		if (frm.firstname.value == "") {
			alert("Bitte Vornamen angeben!");
			highlight(frm.firstname);
			return false;
		}else removeHighlight(frm.firstname);
	}
	if(frm.lastname){
		if (frm.lastname.value == "") {
			alert("Bitte Nachnamen angeben!");
			highlight(frm.lastname);
			return false;
		}else removeHighlight(frm.lastname);
	}
	if(frm.email){
		if(frm.email.value == "" || !frm.email.value.match(emailregexp)) {
			alert("Bitte e-mail-Adresse angeben!");
			highlight(frm.email);
			return false;
		}else removeHighlight(frm.email);
	}
	if(frm.street){
		if(frm.street.value == "") {
			alert("Bitte geben Sie Ihre Strasse ein!");
			highlight(frm.street);
			return false;
		}else removeHighlight(frm.street);
	}
	if(frm.zipcode){
		if(frm.zipcode.value == "") {
			alert("Bitte geben Sie Ihre Postleitzahl ein!");
			highlight(frm.zipcode);
			return false;
		}else removeHighlight(frm.zipcode);
	}
	if(frm.city){
		if(frm.city.value == "") {
			alert("Bitte geben Sie Ihren Wohnort ein!");
			highlight(frm.city);
			return false;
		}else removeHighlight(frm.city);
	}
	if(frm.country){
		if(frm.country.value == "") {
			alert("Bitte Land angeben!");
			highlight(frm.country);
			return false;
		}else removeHighlight(frm.country);
	}
	if(frm.schoolname){
		if(frm.schoolname.value == "") {
			alert("Bitte trage den Namen Deiner Schule ein!");
			highlight(frm.schoolname);
			return false;
		}else removeHighlight(frm.schoolname);
	}
	if(frm.schooltype){
		if(frm.schooltype.value == "") {
			alert("Bitte trage die Art Deiner Schule ein!");
			highlight(frm.schooltype);
			return false;
		}else removeHighlight(frm.schooltype);
	}
	if(frm.product){
		if(frm.product.value == "") {
			alert("Please select a product!");
			highlight(frm.product);
			return false;
		}else removeHighlight(frm.product);
	}
	if(frm.number){
		if(frm.number.value == "" || !isNumber(frm.number.value)) {
			alert("Bitte geben Sie die Teilnehmerzahl ein!");
			highlight(frm.number);
			return false;
		}else removeHighlight(frm.number);
	}
	if(frm.date){
		if(frm.date.value != "" && !IsValidDate(frm.date.value)) {
			alert("Bitte geben Sie Ihren Wunschtermin ein (Format: tt.mm.jjjj)!");
			highlight(frm.date);
			return false;
		}else removeHighlight(frm.date);
	}
	if(frm.message){
		if(frm.message.value == "") {
			alert("Bitte Mitteilung eintragen!");
			highlight(frm.message);
			return false;
		}else removeHighlight(frm.message);
	}
	if(frm.photo){
		if(frm.photo.value == "") {
			alert("Please insert your photo!");
			highlight(frm.photo);
			return false;
		}else removeHighlight(frm.photo);
	}
	if(frm["FE[tt_address][tx_pritt_firstname]"]){
		if(frm["FE[tt_address][tx_pritt_firstname]"].value==""){
			alert("Please insert your first name!");
			highlight(frm["FE[tt_address][tx_pritt_firstname]"]);
			return false;
		}
		else removeHighlight(frm["FE[tt_address][tx_pritt_firstname]"]);
	}
	if(frm["FE[tt_address][name]"]){
		if(frm["FE[tt_address][name]"].value==""){
			alert("Please insert your last name!");
			highlight(frm["FE[tt_address][name]"]);
			return false;
		}
		else removeHighlight(frm["FE[tt_address][name]"]);
	}
	if(frm["FE[tt_address][email]"]){
		if(frm["FE[tt_address][email]"].value=="" || !frm["FE[tt_address][email]"].value.match(emailregexp)){
			alert("Please insert your email!");
			highlight(frm["FE[tt_address][email]"]);
			return false;
		}
		else removeHighlight(frm["FE[tt_address][email]"]);
	}
	if(frm["FE[tt_address][country]"]){
		if(frm["FE[tt_address][country]"].value==""){
			alert("Please select your country!");
			highlight(frm["FE[tt_address][country]"]);
			return false;
		}
		else removeHighlight(frm["FE[tt_address][country]"]);
	}
	if(frm.agree){
		if(!frm.agree.checked) {
			alert("Please confirm that you wish to receive the newsletter!");
			highlight(frm.agree);
			return false;
		}else removeHighlight(frm.agree);
	}
	if(frm.agree2){
		if(!frm.agree2.checked) {
			alert("Please accept our data protection declaration!");
			highlight(frm.agree2);
			return false;
		}else removeHighlight(frm.agree2);	
	}
	if(frm.termsofuse){
		if(!frm.termsofuse.checked) {
			alert("Bitte akzeptieren Sie die Nutzungsbedingungen!");
			highlight(frm.termsofuse);
			return false;
		}else removeHighlight(frm.termsofuse);
	}
	return true;
}

function checkTaf(){
	var frm = eval('document.toystory3taf');
	if(frm){
		if(frm["TIPFORM[name]"].value=="" || frm["TIPFORM[email]"].value=="" || frm["TIPFORM[recipient]"].value==""){
			alert(unescape("Ooops! Vergiss%20nicht%2C%20Name%20und%20E-Mail%20von%20Dir%20und%20mindestens%20einem%20Freund%20anzugeben%21"));
			if(frm["TIPFORM[name]"].value=="" ) highlight(frm["TIPFORM[name]"]);
			if(frm["TIPFORM[email]"].value=="" ) highlight(frm["TIPFORM[email]"]);
			if(frm["TIPFORM[recipient]"].value=="" ) highlight(frm["TIPFORM[recipient]"]);
			return false;
		}
		else{
			removeHighlight(frm["TIPFORM[name]"]);
			removeHighlight(frm["TIPFORM[email]"]);
			removeHighlight(frm["TIPFORM[recipient]"]);
		}
	}
	return true;
}
/* Validation of "Pritt City" and "Pritt JtfO" and "Pritt Bastelwettbewerb 2010" Upload Form */
function chkUploadForm(frmname){
	var frm = eval('document.'+frmname);
	if(frmname=="prittjtfo_form")	var errormsg = unescape("Beachte die roten Markierungen und f%FClle die Felder nochmal richtig aus.");
	else if(frmname=="form_bw_2010") var errormsg = unescape("Da fehlt noch etwas! Bitte die Pflichtfelder ausf%FCllen.");
	else if(frmname=="toystory3") var errormsg = unescape("Stopp%21%20Nicht%20so%20schnell%21%20Bitte%20pr%FCf%20noch%20mal%2C%20ob%20Du%20wirklich%20schon%20das%20ganze%20Formular%20ausgef%FCllt%20hast.");
	else var errormsg = unescape("Pfusch am Bau%21 Oder zumindest Pfusch am Formular. Beachte die roten Markierungen und f%FClle die Felder nochmal richtig aus.");

	 if(frmname=="toystory3"){
		var fields = new Array("image","firstname","lastname","age","email");
	}
	else var fields = new Array("image","fototitle","nickname","firstname","lastname","age","street","zipcode","city","email");
	for (var i = 0; i < fields.length; ++i){
		if(eval("frm."+fields[i])){
			if (eval("frm."+fields[i]).value == "") {
				alert(errormsg);
				highlight(eval("frm."+fields[i]));
				return false;
			}
			else{
				removeHighlight(eval("frm."+fields[i]));
			}
		}
	
	}
	if(frm.email){
		if(frm.email.value == "" || !frm.email.value.match(emailregexp)) {
			alert(errormsg);
			highlight(frm.email);
			return false;
		}else removeHighlight(frm.email);
	}
	if(frm.termsofuse){
		if(!frm.termsofuse.checked) {
			alert(errormsg);
			highlight(frm.termsofuse);
			return false;
		}else removeHighlight(frm.termsofuse);
	}
	if(frm.dataprotection){
		if(!frm.dataprotection.checked) {
			alert(errormsg);
			highlight(frm.dataprotection);
			return false;
		}else removeHighlight(frm.dataprotection);
	}
	if(document.getElementById("message"))
	document.getElementById("message").style.visibility = 'visible';
	return true;
}

function removeHighlight(el){
	el.style.background = '#ffffff';
	if(isIE) el.style.border = '1px solid #7F9DB9';
	if(document.getElementById('p'+el.name))
	document.getElementById('p'+el.name).style.color = '#000000';
}
function highlight(el){
	el.focus();
	el.style.background = '#EC641F';
	if(document.getElementById('p'+el.name))
	document.getElementById('p'+el.name).style.color = '#EC641F';
}

function checkFinder(){
	var frm = document.forms["productFinderForm"];
	if(frm.attach.value ==''){
		alert('Bitte wählen Sie das Material aus, das Sie verkleben möchten');
		return false;
	}
	if(frm.to.value ==''){
		alert('Bitte wählen Sie das Material aus, das Sie verkleben möchten');
		return false;
	}
	return true;
}
//##########################################
// Additional validation functions
//##########################################
function isNumber(val){
if (val == "") return false;
  for (var i = 0; i< val.length; i++) {
    if (val.charAt(i) < "0" || val.charAt(i) > "9")
      return false;
  }    
  return true;
}

//##########################################
function isIntegerInRange (intVar, intLow, intHigh){
    return ((intVar >= intLow) && (intVar <= intHigh));
}
//##########################################
function isDay (intDay){
	return isIntegerInRange (intDay, 1, 31);
}

function isMonth (intMonth){
	return isIntegerInRange (intMonth, 1, 12);
}

function isYear (intYear){
    return isIntegerInRange (intYear, 1900, 2009);
}

function daysInFebruary (intYear){		// February has 29 days in any year evenly divisible by four,
    								// EXCEPT for centurial years which are not also divisible by 400.
    return (  ((intYear % 4 == 0) && ( (!(intYear % 100 == 0)) || (intYear % 400 == 0) ) ) ? 29 : 28 );
}


//##########################################
function isDate (intYear, intMonth, intDay){
    if (! (isYear(intYear) && isMonth(intMonth) && isDay(intDay))) return false;

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}
//##########################################
function IsValidDate(strDate){
	var blnIsDate = false;
	
	var objRegExp = /^(\d{2})\.(\d{2})\.(\d{4})$/;
	blnIsDateFormat = objRegExp.test(strDate);
	if (blnIsDateFormat){
		objRegExp.exec(strDate);
		intDay 		= parseInt(RegExp.$1, 10);
		intMonth 	= parseInt(RegExp.$2, 10);
		
		if (RegExp.$3.length == 2){
			intYear = parseInt("19"+ RegExp.$3, 10);
		}
		else if(RegExp.$3.length == 4){
			intYear		= parseInt(RegExp.$3, 10);
		}
		else {
			return false;
		}
		blnIsDate = isDate(intYear,intMonth,intDay);
	}
	return blnIsDate;
}