function change_keep_file(newFile, keep_file)
{
	if (newFile == ""){
		document.getElementById(keep_file).checked = true;
	}
	else{
		document.getElementById(keep_file).checked = false;
	}
}

function delete_new_image(keep_image, image)
{
	if (keep_image){
		document.getElementById('logo').value = "";
	}
}


function make_price(form_name)
{
	var sum = 0;
	var price = 0;
	the_form = document.getElementById(form_name);
	inputs = the_form.getElementsByTagName('input');
	for (var i = 0; i < inputs.length-3; i++){
		if(inputs[i].checked){
			price = inputs[i].value;
			if(!isNaN(price))
				sum += price * 1;
		}
	}
	
	document.getElementById('total').value = sum;
	document.getElementById('totaltva').value = (sum * 119 / 100).toFixed(2);
}


function change_elements(form_name)
{
	the_form = document.getElementById(form_name);
	
	els = the_form.elements; 
	for (var i = 0; i < els.length; i++) {
		switch (els[i].type) {
			case "text":
			case "textarea":
				if (els[i].disabled == false && els[i].value != '') {
//					els[i].name = document.getElementById(els[i].name).innerHTML;
				}
				break;
			case "radio":
			case "checkbox":
				if (els[i].checked) {
					els[i].value = (els[i].id).replace(els[i].name, '');
//					els[i].name = document.getElementById(els[i].name).innerHTML;
//					els[i].value = document.getElementById('label' + els[i].id).innerHTML;
				}
				break;
			default:
				break;
		}
	}
}


function validate_form(form_name, changeelements)
{
	valid = true;
	field = '';
	the_form = document.getElementById(form_name);
	
	els = the_form.elements; 
	for (var i = 0; i < els.length; i++) {
		if(els[i].className.indexOf('mandatory') > 0){
			switch (els[i].type) {
				case "text":
				case "textarea":
					if(!valid){
						field_text = document.getElementById(field).innerHTML;
						alert('Va rugam sa completati campul < ' + field_text + ' > pentru a continua.');
						return false;
					}
					if (els[i].value == '') {
						field_text = document.getElementById(els[i].name).innerHTML;
						alert('Va rugam sa completati campul < ' + field_text + ' > pentru a continua.');
						return false;
					}
					break;
				case "radio":
				case "checkbox":
					if(els[i].name != field){
						if(!valid){
							field_text = document.getElementById(field).innerHTML;
							alert('Va rugam sa completati campul < ' + field_text + ' > pentru a continua.');
							return false;
						}
						field = els[i].name;
						valid = false;
					}
					if (els[i].checked) valid = true;
					break;
			}
		}
	}
	if(!valid){
		field_text = document.getElementById(field).innerHTML;
		alert('Va rugam sa completati campul < ' + field_text + ' > pentru a continua.');
		return false;
	}
	
	if(changeelements){
		change_elements(form_name);
	}

	return true;
}



function delete_image(val, pos){
	var c=confirm('Doriti sa stergeti imaginea ' + pos + '?');
	if (c == true) {
		document.getElementById('delete_img').value = val;
		document.getElementById('images_form').submit();
	}
}


function check_newsletter()
{

	if (document.newsletter.email_one.value=="") {
		alert("Va rugam adaugati adresa de e-mail pentru a continua.");
		document.newsletter.email_one.focus();
		return false;
	} else {
		ast=document.newsletter.email_one.value.indexOf("@");
		dot=document.newsletter.email_one.value.lastIndexOf(".");
		if(!(ast>0 && dot>0 && dot>ast)){
			alert("Va rugam adaugati o adresa de e-mail valida pentru a continua.");
			document.newsletter.email_one.focus();
			return false;
	}}


	if (document.newsletter.email_two.value=="") {
		alert("Va rugam adaugati adresa de e-mail pentru a continua.");
		document.newsletter.email_two.focus();
		return false;
	} else {
		ast=document.newsletter.email_two.value.indexOf("@");
		dot=document.newsletter.email_two.value.lastIndexOf(".");
		if(!(ast>0 && dot>0 && dot>ast)){
			alert("Va rugam adaugati o adresa de e-mail valida pentru a continua.");
			document.newsletter.email_two.focus();
			return false;
	}}

	if (document.newsletter.email_one.value!=document.newsletter.email_two.value) {
		alert("La rescriere adresa de email nu se potriveste.");
		document.newsletter.email_two.focus();
		return false;
	}

	return true;
}

function movepic(img_name,img_src) {
	document[img_name].src=img_src;
	var frame_pic = document.getElementById('frame_pic');
	if (frame_pic != null)
		document.getElementById('frame_pic').value = img_src;
}

function commentThis()
{
var cmtForm = document.getElementById("comment_form");
cmtForm.style.display = ''; 
var cmtForm = document.getElementById("greeding");
cmtForm.style.display = 'none'; 
var cmtBtn = document.getElementById("add_comment");
cmtBtn.style.display = 'none';
var cmtBtn = document.getElementById("send_greeting");
cmtBtn.style.display = '';
return false;
}

function sendGreeting()
{
var cmtForm = document.getElementById("greeding");
cmtForm.style.display = ''; 
var cmtForm = document.getElementById("comment_form");
cmtForm.style.display = 'none'; 
var cmtBtn = document.getElementById("send_greeting");
cmtBtn.style.display = 'none';
var cmtBtn = document.getElementById("add_comment");
cmtBtn.style.display = '';
return false;
}


function view_greeting()
{
	if (document.greeting2.g_code.value=="") {
		alert("Va rugam adaugati codul pentru a continua.");
		document.greeting2.g_code.focus();
		return false;
	}
}


function clear_field(field,name)
{
		if(field.value==name){
			field.value="";
		}
}

function check_login()
{	
	if (document.login.email.value=="" || document.login.email.value=="adresa@email") {
		alert("Va rugam adaugati adresa de e-mail pentru a continua.");
		document.login.email.focus();
		return false;
	}
	
	if (document.login.password.value=="") {
		alert("Va rugam adaugati parola pentru a continua.");
		document.login.password.focus();
		return false;
	}
	return true;
}

function check_update()
{	
	if (document.profile.lname.value=="") {
		alert("Va rugam adaugati Numele pentru a continua.");
		document.profile.lname.focus();
		return false;
	}

	if (document.profile.fname.value=="") {
		alert("Va rugam adaugati Prenumele pentru a continua.");
		document.profile.fname.focus();
		return false;
	}

	if (document.profile.company.value=="") {
		alert("Va rugam adaugati Numele companiei pentru a continua.");
		document.profile.company.focus();
		return false;
	}

	if (document.profile.fiscal_code.value=="") {
		alert("Va rugam adaugati Codul fiscal pentru a continua.");
		document.profile.fiscal_code.focus();
		return false;
	}

	if (document.profile.reg_com.value=="") {
		alert("Va rugam adaugati Nr. Registrul Comertului pentru a continua.");
		document.profile.reg_com.focus();
		return false;
	}

	if (document.profile.bank_code.value=="") {
		alert("Va rugam adaugati Contul bancar pentru a continua.");
		document.profile.bank_code.focus();
		return false;
	}

	if (document.profile.name.value=="") {
		alert("Va rugam adaugati Numele pentru a continua.");
		document.profile.name.focus();
		return false;
	}

	if (document.profile.bank.value=="") {
		alert("Va rugam selectati Banca pentru a continua.");
		document.profile.bank.focus();
		return false;
	}

	if (document.profile.addr.value=="") {
		alert("Va rugam adaugati Adresa pentru a continua.");
		document.profile.addr.focus();
		return false;
	}

	if (document.profile.city.value=="") {
		alert("Va rugam selectati Localitatea pentru a continua.");
		document.profile.city.focus();
		return false;
	}

	if (document.profile.postal_code.value=="") {
		alert("Va rugam adaugati Codul postal pentru a continua.");
		document.profile.postal_code.focus();
		return false;
	}

	if (document.profile.phone.value=="") {
		alert("Va rugam adaugati Telefonul pentru a continua.");
		document.profile.phone.focus();
		return false;
	}
	
	if(!document.profile.type1.checked && !document.profile.type2.checked){
		alert("Va rugam selectati tipul de utilizator pentru a continua.");
		return false;
	}
	return true;
}

function check_new_pass()
{	
	if (document.new_pass.old_pass.value=="") {
		alert("Va rugam adaugati parola actuala pentru a continua.");
		document.new_pass.old_pass.focus();
		return false;
	}

	if (document.new_pass.password.value=="") {
		alert("Va rugam adaugati parola noua pentru a continua.");
		document.new_pass.password.focus();
		return false;
	}

	if (document.new_pass.password.value!=document.new_pass.password_again.value) {
		alert("La rescriere parola noua nu se potriveste.");
		document.new_pass.password_again.focus();
		return false;
	}
	return true;
}

function check_register()
{
	if (document.register.lname.value=="") {
		alert("Va rugam adaugati Numele pentru a continua.");
		document.register.lname.focus();
		return false;
	}

	if (document.register.fname.value=="") {
		alert("Va rugam adaugati Prenumele pentru a continua.");
		document.register.fname.focus();
		return false;
	}

	if (document.register.company.value=="") {
		alert("Va rugam adaugati Numele companiei pentru a continua.");
		document.register.company.focus();
		return false;
	}

	if (document.register.fiscal_code.value=="") {
		alert("Va rugam adaugati Codul fiscal pentru a continua.");
		document.register.fiscal_code.focus();
		return false;
	}

	if (document.register.reg_com.value=="") {
		alert("Va rugam adaugati Nr. Registrul Comertului pentru a continua.");
		document.register.reg_com.focus();
		return false;
	}

	if (document.register.bank_code.value=="") {
		alert("Va rugam adaugati Contul bancar pentru a continua.");
		document.register.bank_code.focus();
		return false;
	}

	if (document.register.name.value=="") {
		alert("Va rugam adaugati Numele pentru a continua.");
		document.register.name.focus();
		return false;
	}

	if (document.register.bank.value=="") {
		alert("Va rugam selectati Banca pentru a continua.");
		document.register.bank.focus();
		return false;
	}

	if (document.register.addr.value=="") {
		alert("Va rugam adaugati Adresa pentru a continua.");
		document.register.addr.focus();
		return false;
	}

	if (document.register.city.value=="") {
		alert("Va rugam selectati Localitatea pentru a continua.");
		document.register.city.focus();
		return false;
	}

	if (document.register.postal_code.value=="") {
		alert("Va rugam adaugati Codul postal pentru a continua.");
		document.register.postal_code.focus();
		return false;
	}

	if (document.register.phone.value=="") {
		alert("Va rugam adaugati Teelfonul pentru a continua.");
		document.register.phone.focus();
		return false;
	}

	if(!document.register.type1.checked && !document.register.type2.checked){
		alert("Va rugam selectati tipul de utilizator pentru a continua.");
		return false;
	}

	if (document.register.email.value=="") {
		alert("Va rugam adaugati adresa de e-mail pentru a continua.");
		document.register.email.value="";
		document.register.email.focus();
		return false;
	} else {
		ast=document.register.email.value.indexOf("@");
		dot=document.register.email.value.lastIndexOf(".");
		if(!(ast>0 && dot>0 && dot>ast)){
			alert("Va rugam adaugati o adresa de e-mail valida pentru a continua.");
			document.register.email.focus();
			return false;
	}}

	if (document.register.email_again.value!=document.register.email.value) {
		alert("Atentie, adresa de e-mail este gresita la rescriere.");
		document.register.email_again.focus();
		return false;
	}
	
	if (document.register.password.value=="") {
		alert("Va rugam adaugati parola pentru a continua.");
		document.register.password.focus();
		return false;
	}

	if (document.register.password_again.value!=document.register.password.value) {
		alert("Atentie, parola este gresita la rescriere.");
		document.register.password_again.focus();
		return false;
	}

	if (document.register.code.value==""){
		alert("Va rugam adaugati codul de siguranta din imaginea de mai sus pentru a continua.");
		document.register.code.focus();
		return false;
	}
/*	
	if (document.register.conditii.checked==false) {
		alert("Pentru a continua trebuie sa fiti de acord cu regulamentul.");
		document.register.conditii.focus();
		return false;
	}
*/
	return true;
}

function check_fpassword_form(){
	if (document.f_password.fpass_email.value=="") {
		alert("Va rugam adaugati adresa de e-mail pentru a continua.");
		document.f_password.fpass_email.focus();
		return false;
	}
	
	return true;	
}

function check_add()
{	
	if (document.post_add.title.value=="") {
		alert("Va rugam adaugati titlul anuntului.");
		document.post_add.title.focus();
		return false;
	}

	if (document.post_add.type.value=="0") {
		alert("Va rugam selectati o categorie.");
		document.post_add.type.focus();
		return false;
	}

	if (document.post_add.sub_type.value==""){
		alert("Va rugam selectati o subcategorie.");
		document.post_add.sub_type.focus();
		return false;
	}

	if (document.post_add.location.value=="") {
		alert("Va rugam selectati o localitate.");
		document.post_add.location.focus();
		return false;
	}

	if (document.post_add.op_type.value=="") {
		alert("Va rugam selectati tipul de anunt.");
		document.post_add.op_type.focus();
		return false;
	}

	if (document.post_add.delivery.value=="") {
		alert("Va rugam selectati modul de livrare.");
		document.post_add.delivery.focus();
		return false;
	}

	if (document.post_add.prod_state.value=="") {
		alert("Va rugam selectati starea produsului.");
		document.post_add.prod_state.focus();
		return false;
	}

	if (document.post_add.description.value=="") {
		alert("Va rugam adaugati descrierea.");
		document.post_add.description.focus();
		return false;
	}

	if ((document.post_add.phone.value!="")&&(document.post_add.phone.value.match(/[^0-9 ]/))) {
		alert("Va rugam adaugati un numar de telefon valabil.");
		document.post_add.phone.focus();
		return false;
	}

	return true;
}

function check_article()
{	
	if (document.post_article.title.value=="") {
		alert("Va rugam adaugati titlul articolului.");
		document.post_article.title.focus();
		return false;
	}

	if (document.post_article.catID.value=="0") {
		alert("Va rugam selectati o categorie.");
		document.post_article.catID.focus();
		return false;
	}

	if (document.post_article.text_zone.value=="") {
		alert("Va rugam adaugati textul articolului.");
		document.post_article.text_zone.focus();
		return false;
	}

	return true;
}


function check_message()
{
	if (document.msg_add.message.value=="") {
		alert("Va rugam adaugati textul mesajului.");
		document.msg_add.message.focus();
		return false;
	}	

	return true;
}

function change_keep_image(newImage, keep_image)
{
	if (newImage == ""){
		document.getElementById(keep_image).checked = true;
	}
	else{
		document.getElementById(keep_image).checked = false;
	}
}

function delete_new_image(keep_image, image)
{
	if (keep_image){
		document.getElementById(image).value = "";
	}
}

