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("Proszę wpisać swoje imię!");
			highlight(frm.firstname);
			return false;
		}else removeHighlight(frm.firstname);
	}
	if(frm.lastname){
		if (frm.lastname.value == "") {
			alert("Proszę wpisać swoje nazwisko!");
			highlight(frm.lastname);
			return false;
		}else removeHighlight(frm.lastname);
	}
	if(frm.email){
		if(frm.email.value == "" || !frm.email.value.match(emailregexp)) {
			alert("Proszę wpisać swój adres e-mail!");
			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("Proszę wpisać swój kraj!");
			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("Proszę wpisać swoją wiadomość!");
			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("Proszę wyrazić zgodę na warunki korzystania!");
			highlight(frm.termsofuse);
			return false;
		}else removeHighlight(frm.termsofuse);
	}
	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('Wybierz materiały, które chcesz skleić');
		return false;
	}
	if(frm.to.value ==''){
		alert('Wybierz materiały, które chcesz skleić');
		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;
}
