function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function cycle_home_picture() {
  var current = get_current_home_picture();
  if (current == '3') {
    set_home_picture('1');
  } else if (current == '2') {
    set_home_picture('3');
  } else {
    set_home_picture('2');
  }
}


function get_current_home_picture() {
  return $('[kl_hpa=1]').attr('kl_hpn');
}

function set_home_picture(n) {
  var current = get_current_home_picture();
  $('[kl_hpn='+current+']').attr('kl_hpa', '0');
  $('[kl_hpn='+n+']').attr('kl_hpa', '1');  
  $('[kl_hpin='+current+']').attr('src', 'images/hem/dot_inactive.png');
  $('[kl_hpin='+n+']').attr('src', 'images/hem/dot_active.png');  
  
  $('#home_main_picture').addClass('home_main_picture_'+n);
  $('#home_main_picture').removeClass('home_main_picture_'+current);  
}


$(document).everyTime(5000, function(i) {
  cycle_home_picture();
});

function isValidEmail(str) {
	   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

$(document).ready(function(){
	$(document).pngFix();
		if ($('#input_for_first_name').length != 0) {
			var check_for_first_name = function() {
				if ($('#input_for_first_name').val().length < 2 || $('#input_for_first_name').val().length > 80) {
					$('#answer_for_first_name').addClass('notok');
					$('#answer_for_first_name').removeClass('ok');
				} else {
					$('#answer_for_first_name').addClass('ok');
					$('#answer_for_first_name').removeClass('notok');				
				}
			};
			//$('#input_for_first_name').keyup(check_for_first_name);
			$('#input_for_first_name').change(check_for_first_name);
		}
	
		if ($('#input_for_last_name').length != 0) {
			var check_for_last_name = function() {
				if ($('#input_for_last_name').val().length < 2 || $('#input_for_last_name').val().length > 80) {
					$('#answer_for_last_name').addClass('notok');
					$('#answer_for_last_name').removeClass('ok');
				} else {
					$('#answer_for_last_name').addClass('ok');
					$('#answer_for_last_name').removeClass('notok');				
				}
			};
			//$('#input_for_last_name').keyup(check_for_last_name);
			$('#input_for_last_name').change(check_for_last_name);
		}
		
		if ($('#input_for_birthdate').length != 0) {
			var check_for_birthdate = function() {
				var birthdate = $('#input_for_birthdate').val();
				if (birthdate.length != 8 && birthdate.length != 10) {
					$('#answer_for_birthdate').addClass('notok');
					$('#answer_for_birthdate').removeClass('ok');				
				} else if (birthdate.length == 10) {
					var pieces = birthdate.split('-');
					if (pieces.length == 3 && pieces[0] > 1900 && pieces[0] < 2010 && pieces[1] > 0 && pieces[1] < 13 && pieces[2] > 0 && pieces[2] < 32) {
						$('#answer_for_birthdate').addClass('ok');
						$('#answer_for_birthdate').removeClass('notok');								
					} else {
						$('#answer_for_birthdate').addClass('notok');
						$('#answer_for_birthdate').removeClass('ok');						
					}
				} else if (birthdate.length == 8) {
					var y = birthdate.substring(0,4);
					var m = birthdate.substring(4,6);
					var d = birthdate.substring(6);
					if (y > 1900 && y < 2000 && m > 0 && m < 13 && d > 0 && d < 32) {
						$('#answer_for_birthdate').addClass('ok');
						$('#answer_for_birthdate').removeClass('notok');								
					} else {
						$('#answer_for_birthdate').addClass('notok');
						$('#answer_for_birthdate').removeClass('ok');						
					}	
				}
			};
			//$('#input_for_birthdate').keyup(check_for_birthdate);
			$('#input_for_birthdate').change(check_for_birthdate);
		}
		
		if ($('#input_for_club').length != 0) {
			var check_for_club = function() {
				$('#answer_for_club').addClass('ok');
				$('#answer_for_club').removeClass('notok');
			};
			$('#input_for_club').keyup(check_for_club);
			$('#input_for_club').change(check_for_club);
		}
	
		if ($('#input_for_streetadress').length != 0) {
			var check_for_streetadress = function() {
				var streetadress = $('#input_for_streetadress').val();
				if (streetadress.length > 0) {
					$('#answer_for_streetadress').addClass('ok');
					$('#answer_for_streetadress').removeClass('notok');
				} else {
					$('#answer_for_streetadress').addClass('notok');
					$('#answer_for_streetadress').removeClass('ok');				
				}
			};
			//$('#input_for_streetadress').keyup(check_for_streetadress);
			$('#input_for_streetadress').change(check_for_streetadress);
		}
		
		if ($('#input_for_co_adress').length != 0) {
			var check_for_co_adress = function() {
				$('#answer_for_co_adress').addClass('ok');
				$('#answer_for_co_adress').removeClass('notok');
			};
			//$('#input_for_co_adress').keyup(check_for_co_adress);
			$('#input_for_co_adress').change(check_for_co_adress);
		}
		
		if ($('#input_for_zipcode').length != 0) {
			var check_for_zipcode = function() {
				var zipcode = $('#input_for_zipcode').val();
				while (zipcode.indexOf(' ') != -1) {
					zipcode = zipcode.replace(' ', '');
				}
				console.log(zipcode);
				if (zipcode.length == 5 && zipcode > 9999 && zipcode <= 99999) {
					$('#answer_for_zipcode').addClass('ok');
					$('#answer_for_zipcode').removeClass('notok');
				} else {
					$('#answer_for_zipcode').addClass('notok');
					$('#answer_for_zipcode').removeClass('ok');				
				}
			};
			//$('#input_for_zipcode').keyup(check_for_zipcode);
			$('#input_for_zipcode').change(check_for_zipcode);
		}
		
		if ($('#input_for_state').length != 0) {
			var check_for_state = function() {
				var state = $('#input_for_state').val();
				if (state.length > 0) {
					$('#answer_for_state').addClass('ok');
					$('#answer_for_state').removeClass('notok');
				} else {
					$('#answer_for_state').addClass('notok');
					$('#answer_for_state').removeClass('ok');				
				}
			};
			//$('#input_for_state').keyup(check_for_state);
			$('#input_for_state').change(check_for_state);
		}
	
		if ($('#input_for_email').length != 0) {
			var check_for_email = function() {
				var email = $('#input_for_email').val();
				var email2 = $('#input_for_email2').val();
	
				if (email.length > 0 && email == email2 && isValidEmail(email)) {
					$('#answer_for_email').addClass('ok');
					$('#answer_for_email').removeClass('notok');
					$('#answer_for_email2').addClass('ok');
					$('#answer_for_email2').removeClass('notok');
				} else {
					$('#answer_for_email').addClass('notok');
					$('#answer_for_email').removeClass('ok');
					$('#answer_for_email2').addClass('notok');
					$('#answer_for_email2').removeClass('ok');
				}
			};
			//$('#input_for_email').keyup(check_for_email);
			$('#input_for_email').change(check_for_email);
			//$('#input_for_email2').keyup(check_for_email);
			$('#input_for_email2').change(check_for_email);
		}
		
		if ($('#input_for_phonenumber').length != 0) {
			var check_for_phonenumber = function() {
				$('#answer_for_phonenumber').addClass('ok');
				$('#answer_for_phonenumber').removeClass('notok');
			};
			$('#input_for_phonenumber').keyup(check_for_phonenumber);
			$('#input_for_phonenumber').change(check_for_phonenumber);
		}
		
		if ($('#input_for_miletime').length != 0) {
			var check_for_miletime = function() {
				var pieces;
				var miletime = $('#input_for_miletime').val();
				if (miletime.length == 0) {
					$('#answer_for_miletime').addClass('ok');
					$('#answer_for_miletime').removeClass('notok');				
				} else if (miletime.split(':').length == 3) {
					pieces = miletime.split(':');
					if (pieces[0].length > 0 && pieces[1].length > 0 && pieces[2].length > 0 && pieces[0] >= 0 && pieces[0] < 60 && pieces[1] >= 0 && pieces[1] < 60 && pieces[2] >= 0 && pieces[2] < 60) {
						$('#answer_for_miletime').addClass('ok');
						$('#answer_for_miletime').removeClass('notok');								
					} else {
						$('#answer_for_miletime').addClass('notok');
						$('#answer_for_miletime').removeClass('ok');						
					}
				} else if (miletime.split(':').length == 2) {
					pieces = miletime.split(':');
					if (pieces[0].length > 0 && pieces[1].length > 0 && pieces[0] >= 0 && pieces[0] < 60 && pieces[1] >= 0 && pieces[1] < 60) {
						$('#answer_for_miletime').addClass('ok');
						$('#answer_for_miletime').removeClass('notok');								
					} else {
						$('#answer_for_miletime').addClass('notok');
						$('#answer_for_miletime').removeClass('ok');						
					}				
				} else if (miletime.split(',').length == 2) {
					pieces = miletime.split(',');
					if (pieces[0].length > 0 && pieces[1].length > 0 && pieces[0] >= 0 && pieces[0] < 60 && pieces[1] >= 0 && pieces[1] < 60) {
						$('#answer_for_miletime').addClass('ok');
						$('#answer_for_miletime').removeClass('notok');								
					} else {
						$('#answer_for_miletime').addClass('notok');
						$('#answer_for_miletime').removeClass('ok');						
					}				
				} else if (miletime.split('m').length == 2) {
					pieces = miletime.split('m');
					if (pieces[0].length > 0) {
						pieces[0] = trim(pieces[0]);
					}
					if (pieces[0] >= 0 && pieces[0] < 60 && (pieces[1] == '' || pieces[1] == 'in')) {
						$('#answer_for_miletime').addClass('ok');
						$('#answer_for_miletime').removeClass('notok');								
					} else {
						$('#answer_for_miletime').addClass('notok');
						$('#answer_for_miletime').removeClass('ok');						
					}				
				} else {
					$('#answer_for_miletime').addClass('notok');
					$('#answer_for_miletime').removeClass('ok');					
				}
			};
			//$('#input_for_miletime').keyup(check_for_miletime);
			$('#input_for_miletime').change(check_for_miletime);
		}
		
		if ($('#input_for_class').length != 0) {
			var check_for_class = function() {
				var cclass = $('#input_for_class').val();
				if (cclass.length == 1 || cclass.length == 2 || cclass.length == 3) {
					$('#answer_for_class').addClass('ok');
					$('#answer_for_class').removeClass('notok');				
				} else {
					$('#answer_for_class').addClass('notok');
					$('#answer_for_class').removeClass('ok');						
				}
			};
			//$('#input_for_class').keyup(check_for_class);
			$('#input_for_class').change(check_for_class);
		}
		
		if ($('#input_for_donate').length != 0) {
			var check_for_donate = function() {
				var donate = $('#input_for_donate').val();
				console.dir(donate);
				if (donate != 'null') {
					$('#answer_for_donate').addClass('ok');
					$('#answer_for_donate').removeClass('notok');				
				} else {
					$('#answer_for_donate').addClass('notok');
					$('#answer_for_donate').removeClass('ok');						
				}
			};
			//$('#input_for_donate').keyup(check_for_donate);
			$('#input_for_donate').change(check_for_donate);
		}

});

