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 = "Campul " + what + " este obligatoriu.";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FFD5D5'; 
        error = "Campul " + what + " contine caractere nepermise. Sunt permise numai litere, numere si underscore.";
    } else {
        fld.style.background = 'White';
    }
	if(error != ""){
		alert(error + "\n\nVa rugam corectati eroarea de mai sus pentru a putea continua.");
		create_submit = false;
		return false;
	}
	return true;
}

function validateName_new(fld, what) {
	var error = "";
	//alert(fld.value);
	if (fld.value == "") {
		//alert('1');
        fld.style.background = '#FFD5D5'; 
        error = "Campul " + what + " este obligatoriu.";
    } else {
		//alert('2');
        fld.style.background = 'White';
    }
	if(error != ""){
		//alert('3 ' + error);
		alert(error + "\n\nVa rugam corectati eroarea de mai sus pentru a putea continua.");
		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 = "Campul " + what + " este obligatoriu.";
    } else {
        fld.style.background = 'White';
    }
	if(error != ""){
		alert(error + "\n\nVa rugam corectati eroarea de mai sus pentru a putea continua.");
		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 = "Adresa de Email este obligatorie.";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FFD5D5';
        error = "Adresa de email introdusa este invalida";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFD5D5';
        error = "Adresa de email introdusa contine caractere nepermise.";
    } else {
        fld.style.background = 'White';
    }
    if(error != ""){
		alert(error + "\n\nVa rugam corectati eroarea de mai sus pentru a putea continua.");
		create_submit = false;
		return false;
	}
    return true;
}

function IsNumeric(sText, what){
	var ValidChars = "0123456789.";
	if(sText == ""){
		document.getElementById("nr_camere").style.background = '#FFD5D5';
		alert("Campul " + what + " trebuie sa fie o valoare numerica.\n\nVa rugam corectati eroarea de mai sus pentru a putea continua.");
		return false;
	}
	for (i = 0; i < sText.length; i++) {
		if (ValidChars.indexOf(sText.charAt(i)) == -1) {
			document.getElementById("nr_camere").style.background = '#FFD5D5';
			alert("Campul " + what + " trebuie sa fie o valoare numerica.\n\nVa rugam corectati eroarea de mai sus pentru a putea continua.");
			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");
	adr			      = document.getElementById("adr");
	ema			      = document.getElementById("ema");
	telef			  = document.getElementById("telef");

	
	if(!validateRoom(document.create)){
		return;
	}if(!IsNumeric(nr_camere, "Numar camere")){
		return;
	}if(!validateDate(data_1, "Data sosirii")){
		return;
	}if(!validateDate(data_2, "Data plecarii")){
		return;
	}if(!validateName_new(nume, "Nume")){
		return;
	}if(!validateName_new(telef, "Telefon")){
		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("Campul Mesaj este necompletat.\n\nVa rugam corectati eroarea de mai sus pentru a putea continua.");
		return;
	}

	document.create.submit();
}

function add(url,x,y,info) {
  code = '<img src="img/bullet1.gif">&nbsp;<b>Ora locala</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;
}