//CSValiViewdatosanunciante.js

//Clase CSValiViewdatosanunciante

function CSValiViewdatosanunciante(form,title,label_cif,label_empresa,label_direccion,label_cp,label_poblacion,
								   label_provincia,label_pais,label_nombre,label_apellidos,label_email,label_telefono) {

    this.form = form;
    this.title = title;

    this.cif = new CSAlfanum(form.cif.value,label_cif,1);	
	this.empresa = new CSAlfanum(form.empresa.value,label_empresa,1);
	this.direccion = new CSAlfanum(form.direccion.value,label_direccion,1);		
	this.cp = new CSCP(form.cp.value,label_cp,1);			
    this.poblacion = new CSAlfanum(form.poblacion.value,label_poblacion,1);
    this.provincia = new CSCombo(form.provincia,label_provincia);
    this.pais = new CSCombo(form.idpais,label_pais);

    this.nombre = new CSAlfanum(form.nombre.value,label_nombre,1);	
    this.apellidos = new CSAlfanum(form.apellidos.value,label_apellidos,1);	
	this.email = new CSAlfanum(form.email.value,label_email,1);	
	this.telefono = new CSTelefax(form.telefono.value,label_telefono,1);

    this.msg_error = new Array();
	
	this.validar = CSValiViewdatosanunciante_validar;
	this.show_bugs = CSValiViewdatosanunciante_show_bugs;
	this.submit_query = CSValiViewdatosanunciante_submit_query;
}


function CSValiViewdatosanunciante_validar() {

    if (!this.cif.validar()) { this.msg_error[this.msg_error.length] = this.cif.msg_error(); }
    if (!this.empresa.validar()) { this.msg_error[this.msg_error.length] = this.empresa.msg_error(); }
    if (!this.direccion.validar()) { this.msg_error[this.msg_error.length] = this.direccion.msg_error(); }
    if (!this.cp.validar()) { this.msg_error[this.msg_error.length] = this.cp.msg_error(); }		
    if (!this.poblacion.validar()) { this.msg_error[this.msg_error.length] = this.poblacion.msg_error(); }
    if (!this.provincia.validar()) { this.msg_error[this.msg_error.length] = this.provincia.msg_error(); }	
    if (!this.pais.validar()) { this.msg_error[this.msg_error.length] = this.pais.msg_error(); }
    if (!this.nombre.validar()) { this.msg_error[this.msg_error.length] = this.nombre.msg_error(); }			
    if (!this.apellidos.validar()) { this.msg_error[this.msg_error.length] = this.apellidos.msg_error(); }			
    if (!this.email.validar()) { this.msg_error[this.msg_error.length] = this.email.msg_error(); }
	if (!this.telefono.validar()) { this.msg_error[this.msg_error.length] = this.telefono.msg_error(); }

    return (this.msg_error.length == 0);
}
	
function CSValiViewdatosanunciante_show_bugs() {
     show_bugs(this.title,this.msg_error);
}

function CSValiViewdatosanunciante_submit_query() {
    this.form.submit();
}


function CSValiViewdatosanunciante_eval(form,title,label_cif,label_empresa,label_direccion,label_cp,label_poblacion,
								   label_provincia,label_pais,label_nombre,label_apellidos,label_email,label_telefono) {
  var obj1=new CSValiViewdatosanunciante(form,title,label_cif,label_empresa,label_direccion,label_cp,label_poblacion,
								   label_provincia,label_pais,label_nombre,label_apellidos,label_email,label_telefono);

  if (obj1.validar()) {
    saveCookie(form);
    obj1.submit_query();
  } else {
	 obj1.show_bugs();
  }
}


//Recupera el valor de un campo de la cookie.
function getCookie(campo) {
	var thiscookie = document.cookie.split("; ");
	
	for (i=0;i<thiscookie.length;i++) {
		if (campo == thiscookie[i].split("=")[0]) {
			 return thiscookie[i].split("=")[1];
		}
	}
	return "";
}


//Salvar campos del formulario en cookies
function saveCookie(form) {

    var expiredate = new Date;
    expiredate.setMonth(expiredate.getMonth()+12);
	var gmtstring = expiredate.toGMTString();
    
	document.cookie = "cif=" + form.cif.value + ";expires=" + gmtstring;
	document.cookie = "empresa=" + form.empresa.value + ";expires=" + gmtstring;
	document.cookie = "direccion=" + form.direccion.value + ";expires=" + gmtstring;
	document.cookie = "cp=" + form.cp.value + ";expires=" + gmtstring;
	document.cookie = "poblacion=" + form.poblacion.value + ";expires=" + gmtstring;
	document.cookie = "provincia=" + form.provincia.selectedIndex + ";expires=" + gmtstring;
	document.cookie = "idpais=" + form.idpais.selectedIndex + ";expires=" + gmtstring;
	document.cookie = "nombre=" + form.nombre.value + ";expires=" + gmtstring;
	document.cookie = "apellidos=" + form.apellidos.value + ";expires=" + gmtstring;
	document.cookie = "email=" + form.email.value + ";expires=" + gmtstring;
	document.cookie = "telefono=" + form.telefono.value + ";expires=" + gmtstring;
     
}

function loadDatosAnunciante(form) {

  var cif = getCookie("cif");
  var empresa = getCookie("empresa");
  var direccion = getCookie("direccion");
  var cp = getCookie("cp");
  var poblacion = getCookie("poblacion");
  var provincia = getCookie("provincia");
  var idpaisindex = getCookie("idpais");
  var nombre = getCookie("nombre");
  var apellidos = getCookie("apellidos");
  var email = getCookie("email");
  var telefono = getCookie("telefono");

  form.cif.value = cif;
  form.empresa.value = empresa;
  form.direccion.value = direccion;
  form.cp.value = cp;
  form.poblacion.value = poblacion;
  form.provincia.selectedIndex = provincia;
  form.idpais.selectedIndex = idpaisindex;
  form.nombre.value = nombre;
  form.apellidos.value = apellidos;
  form.email.value = email;
  form.telefono.value = telefono;

}
