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("Please insert your first name!");
			highlight(frm.firstname);
			return false;
		}else removeHighlight(frm.firstname);
	}
	if(frm.lastname){
		if (frm.lastname.value == "") {
			alert("Please insert your last name!");
			highlight(frm.lastname);
			return false;
		}else removeHighlight(frm.lastname);
	}
	if(frm.email){
		if(frm.email.value == "" || !frm.email.value.match(emailregexp)) {
			alert("Please insert your email!");
			highlight(frm.email);
			return false;
		}else removeHighlight(frm.email);
	}
	if(frm.street){
		if(frm.street.value == "") {
			alert("Please insert your Street/PO Box!");
			highlight(frm.street);
			return false;
		}else removeHighlight(frm.street);
	}
	if(frm.zipcode){
		if(frm.zipcode.value == "") {
			alert("Please insert your zip code!");
			highlight(frm.zipcode);
			return false;
		}else removeHighlight(frm.zipcode);
	}
	if(frm.city){
		if(frm.city.value == "") {
			alert("Please insert your city!");
			highlight(frm.city);
			return false;
		}else removeHighlight(frm.city);
	}
	if(frm.country){
		if(frm.country.value == "") {
			alert("Please select your country!");
			highlight(frm.country);
			return false;
		}else removeHighlight(frm.country);
	}
	if(frm.schoolname){
		if(frm.schoolname.value == "") {
			alert("Please insert the name of your school!");
			highlight(frm.schoolname);
			return false;
		}else removeHighlight(frm.schoolname);
	}
	if(frm.schooltype){
		if(frm.schooltype.value == "") {
			alert("Please insert the type of your school!");
			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("Please insert the number of people!");
			highlight(frm.number);
			return false;
		}else removeHighlight(frm.number);
	}
	if(frm.date){
		if(frm.date.value != "" && !IsValidDate(frm.date.value)) {
			alert("Please insert the preferred date (Format: dd.mm.yyyy)!");
			highlight(frm.date);
			return false;
		}else removeHighlight(frm.date);
	}
	if(frm.message){
		if(frm.message.value == "") {
			alert("Please insert your message!");
			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("Please agree to our terms of use!");
			highlight(frm.termsofuse);
			return false;
		}else removeHighlight(frm.termsofuse);
	}
	return true;
}
/* Validation of "Pritt Toystory3" TAF Form */

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! Ne%20pozabi%20vpisati%20tvojega%20imena%20in%20elektronskega%20naslova%2C%20prav%20tako%20pa%20ne%20pozabi%20na%20ime%20in%20elektronski%20naslov%20vsaj%20enega%20prijatelja%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 Toystory3" Upload Form */
function chkUploadForm(frmname){
	var frm = eval('document.'+frmname);
	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.");
	

	var fields = new Array("image","firstname","lastname","age","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('Please select the material you want to attach');
		return false;
	}
	if(frm.to.value ==''){
		alert('Please select the material you want to attach');
		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;
}