var dtCh = "/";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s) {
	var i;
	for (i = 0; i < s.length; i++) {
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag){
	var i;
	var returnString = '';
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

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

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	} 
	return this;
}

function isDate(dtStr) {
	var daysInMonth = DaysArray(12);
	var pos1 = dtStr.indexOf(dtCh);
	var pos2 = dtStr.indexOf(dtCh,pos1+1);
	var strMonth = dtStr.substring(0,pos1);
	var strDay = dtStr.substring(pos1+1,pos2);
	var strYear = dtStr.substring(pos2+1);
	strYr = strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month = parseInt(strMonth,10);
	day = parseInt(strDay,10);
	year = parseInt(strYr,10);
	if (pos1==-1 || pos2==-1) {
		//alert("The date format should be : mm/dd/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12) {
		//alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]) {
		//alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear) {
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false) {
		//alert("Please enter a valid date");
		return false;
	}
	return true;
}

function ValidateForm() {
	var dt=document.frmSample.txtDate;
	if (isDate(dt.value)==false){
		dt.focus();
		return false;
	}
    return true;
}


function clearField(which, preset) {
	if (which.value==preset) which.value = '';
}

function fillField(which, preset) {
	if (which.value=='') which.value = preset;
}

function checkBirthday() {
	var allowed = false;
	var error = false;
	document.getElementById('bottom_bar').className = 'bottom_bar';
"08", 10
	var day = parseInt(document.forms.birthcheck.day.value, 10);
	var month = parseInt(document.forms.birthcheck.month.value, 10);
	var year = document.forms.birthcheck.year.value;
	if (year.length==2) {
		year = '19'+year;
	}
	year = parseInt(year,10);
	
	if ( isNaN(day) || isNaN(month) || isNaN(year) ) {
		error = true;
	} else {
	//if ( !isNaN(day) && !isNaN(month) && !isNaN(year) ) {
		
		if ( !isDate(month+'/'+day+'/'+year) ) {
			error = true;
		}
		
		var now = new Date();
		var cutDate = new Date(year, month, day);
		var birthday = new Date(year, month, day);
		
		now.setHours(0);
		now.setMinutes(0);
		
		cutDate.setDate( birthday.getDate() );
		cutDate.setMonth( birthday.getMonth()-1 );
		
		cutDate.setFullYear( birthday.getFullYear() + parseInt(document.getElementById('age').value));
		
		if ( (parseInt(cutDate.getTime()/1000) - parseInt(now.getTime()/1000)) <= 0 ) {
			allowed = true;
		}
		
	}
	
	if (error) {
		alert( 'Bitte gib\' ein gültiges Datum ein!' );
	} else {
		if (allowed) {
			window.location.href = 'content.php'+URLparameters;
		} else {
			window.location.href = 'http://www.drink-smart.de';
			//document.getElementById('bottom_bar').className = 'bottom_bar bottom_bar_error';
		}
	}
}

function showSelection() {
	if (document.getElementById('selection').style.display == 'none') {
		document.getElementById('selection').style.display = 'block';
	} else {
		document.getElementById('selection').style.display = 'none';
	}
}

function changeCountry(country) {
	var de = '<p onclick="changeCountry(\'de\');">Deutschland</p>';
	var usa = '<p onclick="changeCountry(\'usa\');">USA</p>';
	var other = '<p onclick="changeCountry(\'other\');">anderes Land</p>';

	if (country=='de') {
		document.getElementById('age').value = 18;
		document.getElementById('selected').innerHTML = '<p onclick="changeCountry(\'de\');">Deutschland</p>';
		document.getElementById('selection').innerHTML = '<p onclick="changeCountry(\'usa\');">USA</p><p onclick="changeCountry(\'other\');">anderes Land</p>';
		document.getElementById('selection').className = 'not_displayed';
	}
	else if (country=='usa') {
		document.getElementById('age').value = 21;
		document.getElementById('selected').innerHTML = '<p onclick="changeCountry(\'usa\');">USA</p>';
		document.getElementById('selection').innerHTML = '<p onclick="changeCountry(\'de\');">Deutschland</p><p onclick="changeCountry(\'other\');">anderes Land</p>';
		document.getElementById('selection').className = 'not_displayed';
	}
	else if (country=='other') {
		document.getElementById('age').value = 18;
		document.getElementById('selected').innerHTML = '<p onclick="changeCountry(\'other\');">anderes Land</p>';
		document.getElementById('selection').innerHTML = '<p onclick="changeCountry(\'de\');">Deutschland</p><p onclick="changeCountry(\'usa\');">USA</p>';
		document.getElementById('selection').className = 'not_displayed';
	}
	showSelection();
}

