var create_submit = true;

function validateName(fld, what) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#FFD5D5'; 
        error = "The field " + what + " is mandatory.";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FFD5D5'; 
        error = "The field " + what + " contains invalid characters. Use only letters, numbers and underscores";
    } else {
        fld.style.background = 'White';
    }
	if(error != ""){
		alert(error + "\n\nIn order to continue please fix the error above.");
		create_submit = false;
		return false;
	}
	return true;
}

function validateName_new(fld, what) {
	var error = "";
	if (fld.value == "") {
        fld.style.background = '#FFD5D5'; 
        error = "The field " + what + " is mandatory.";
    } else {
        fld.style.background = 'White';
    }
	if(error != ""){
		alert(error + "\n\nIn order to continue please fix the error above.");
		create_submit = false;
		return false;
	}
	return true;
}

function validateDate(fld, what) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#FFD5D5'; 
        error = "The field " + what + " is mandatory.";
    } else {
        fld.style.background = 'White';
    }
	if(error != ""){
		alert(error + "\n\nIn order to continue please fix the error above.");		
		create_submit = false;
		return false;
	}
	return true;
}

function trim(s){
	return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#FFD5D5';
        error = "The email address is mandatory.";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FFD5D5';
        error = "The email address is invalid.";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFD5D5';
        error = "The email address contains invalid characters.";
    } else {
        fld.style.background = 'White';
    }
    if(error != ""){
		alert(error + "\n\nIn order to continue please fix the error above.");
		create_submit = false;
		return false;
	}
    return true;
}

function IsNumeric(sText, what){
	var ValidChars = "0123456789.";
	if(sText == ""){
		document.getElementById("nr_camere").style.background = '#FFD5D5';
		alert("The field " + what + " must be a number.\n\nIn order to continue please fix the error above.");
		return false;
	}
	for (i = 0; i < sText.length; i++) {
		if (ValidChars.indexOf(sText.charAt(i)) == -1) {
			document.getElementById("nr_camere").style.background = '#FFD5D5';
			alert("The field " + what + " must be a numbe.\n\nIn order to continue please fix the error above.");
			return false;
		}
	}
	return true;
}

function validateRoom(thisform){
	if(thisform.tip_camera.value == ''){
		document.getElementById('tip_camera').style.background = '#FFD5D5';
		create_submit = false;
		return false;
	}
	document.getElementById('tip_camera').style.background = 'white';
	return true;
}

function validate_form(){
	nr_camere         = document.getElementById("nr_camere").value;
	data_1            = document.getElementById("data_1");
	data_2			  = document.getElementById("data_2");
	nume			  = document.getElementById("nume");
	ema			      = document.getElementById("ema");
	telef			  = document.getElementById("telef");

	
	if(!validateRoom(document.create)){
		return;
	}if(!IsNumeric(nr_camere, "Number of rooms")){
		return;
	}if(!validateDate(data_1, "Check in date")){
		return;
	}if(!validateDate(data_2, "Check out date")){
		return;
	}if(!validateName_new(nume, "Name")){
		return;
	}if(!validateName_new(telef, "Telephone")){
		return;
	}if(!validateEmail(ema)){
		return;
	}

	document.create.submit();
}

function validate_contact(){
	nume			  = document.getElementById("nume");
	ema			      = document.getElementById("ema");
	mesaj			  = document.getElementById("mesaj");

	if(!validateName_new(nume, "Nume")){
		return;
	}if(!validateEmail(ema)){
		return;
	}
	if(mesaj.value == ''){
		document.getElementById("mesaj").style.background = '#FFD5D5';
		alert("The field Message is empty.\n\nIn order to continue please fix the error above.");
		return;
	}

	document.create.submit();
}

function add(url,x,y,info) {
  code = '<img src="../img/bullet1.gif">&nbsp;<b>Local hour</b><br/><br/>';
  code += '<object width='+x+' height='+y+'>';
  code += '<param name=\'movie\' value='+url+'><param name=\'wmode\' value=\'transparent\'>';
  code += '<param name=\'allowfullscreen\' value=\'true\'><param name=\'flashvars\' value='+info+'>';
  code += '<embed width='+x+' height='+y+' src='+url+' wmode=transparent flashvars='+info+'></embed></object>';
  return code;
}

function init(){
	document.getElementById('watch').innerHTML = add('../img/watch.swf',100,100,'local=1&amp;smooth=1');
}

function goto(url){
	location.href = url;
}