


function contactFormHandler(formid, fullname, isolanguagecode) {
  this.mailws = new ITSWebService('cmsservice.ashx');
  this.isolanguagecode = isolanguagecode;
  this.formid = formid;
  this.fullname = fullname;

  this.onready = function(request) {
	  setVisibility(this.formid+'Progress',false);
	  setVisibility(this.formid+'Done',true);
	};
	
	this.onerror = function(request, errorCode, errorText) {
	  var form=document.getElementById(this.formid+'Form');
	  switch (parseInt(errorCode)) {
	    case 3: alert('Email-adressen du har angivet ser ikke ud til at være gyldig. \r\nKontrollér venligst at du har skrevet en email-adressen korrekt...'); this.restart(); form['email'].focus(); return; break; 
	  };
    setVisibility(this.formid+'Progress',false);
    setVisibility(this.formid+'Error',true);
	};

  this.send = function(contactid) {

    var err=false, form=document.getElementById(this.formid+'Form'); 

	  var cmm = 'Skriv venligst en kort besked til [name]...'; cmm = cmm.replace('[name]',fullname); 
	  if (!err) err=validateFieldNonEmpty(true,form['mailbody'],null,cmm, null, true);

    var phoneEmpty = false;
    if (form['phone']) phoneEmpty=validateFieldNonEmpty(false,form['phone'], null, null, null, true);
    var mailEmpty = (form['email'] ? validateFieldNonEmpty(false,form['email'], null, null, null, true) : true);
    phoneEmpty = (form['phone'] ? validateFieldNonEmpty(false,form['phone'], null, null, null, true) : true);
    var nameEmpty = (form['name'] ? validateFieldNonEmpty(false,form['name'], null, null, null, true) : true);
    
    if (!err) err=validateFieldNonEmpty(true,form['name'],'Navn', null, null, true);
    
    if (!err) {
      err=((mailEmpty) && (phoneEmpty));
      if (err) makeAlert(true,form['email'],'Du skal enten udfylde e-mail eller telefonnummer for at jeg kan kontakte dig...', null, null, true);   
    };
    
	  if ((!err) && (!mailEmpty)) err=validateFieldEmail(true,form['email'],'Email');
   
    if (!err) {
    
      request = new Request(), command = request.addCommand('contact','sendmail'); 
      command.setAttribute('contactid',contactid); 
      command.setAttribute('method','transform'); 
      command.setAttribute('mailsubject','Kundehenvendelse'); 
      command.setAttribute('xsl','contact.'+this.isolanguagecode+'.xsl'); 

      for (var i=0; i<form.elements.length; i++) 
        if ((form.elements[i].name!='') && (form.elements[i].name!='email') && (form.elements[i].name!='name')) 
          if (!validateFieldNonEmpty(false,form.elements[i],false,false,false,true)) 
            XMLAddElement(command.element, form.elements[i].name, form.elements[i].value);

      if (!mailEmpty) command.setAttribute('mailfrom',form['email'].value); 
      if (!nameEmpty) command.setAttribute('mailfromname',form['name'].value); 
   
      setVisibility(this.formid+'Form',false);
      setVisibility(this.formid+'Progress',true);
      
      this.mailws.Post(request, null, createDelegate(this, this.onready), createDelegate(this, this.onerror));
    };
    
  };
  
  this.restart = function() {
    this.reset(true);
  };
  
  this.reset = function(retry){
    if (!retry) {
      var form = document.forms[this.formid+'Form'];
      form.reset(); 
      for (var i=0;i<form.elements.length;i++) if (form.elements[i].initialValue) form.elements[i].style.color='';
    };

    setVisibility(this.formid+'Progress',false);
    setVisibility(this.formid+'Error',false);
    setVisibility(this.formid+'Done',false);
    setVisibility(this.formid+'Form',true);

  };
  
};


function contactFormHandlerDanfysik(formid,isolanguageid) {
  this.mailws = new ITSWebService('cmsservice.ashx');
  this.formid = formid;
  this.isolanguageid = isolanguageid;

  this.onready = function(request) {
	  setVisibility(this.formid+'Progress',false);
	  setVisibility(this.formid+'Done',true);
	};
	
	this.onerror = function(request, errorCode, errorText) {
    setVisibility(this.formid+'Progress',false);
    setVisibility(this.formid+'Error',true);
	};

  this.initialize = function() {
    var form = _(this.formid+'Form');
    for (var i=0; i<form.elements.length; i++) 
      if (form.elements[i].value) if (form.elements[i].value!='') form.elements[i].initialValue = form.elements[i].value;
  };

  this.send = function(contactid){
    
    var err=false, form=_(this.formid+'Form'); 

    if (form['phone']) phoneEmpty=validateFieldNonEmpty(false,form['phone']);
    var mailEmpty = (form['email'] ? validateFieldNonEmpty(false,form['email']) : true);
    var phoneEmpty = (form['phone'] ? validateFieldNonEmpty(false,form['phone']) : true);
    var nameEmpty = (form['name'] ? validateFieldNonEmpty(false,form['name']) : true);
    
    err=validateFieldNonEmpty(true,form['name'],'Navn');
    
    if (!err) {
      err=((mailEmpty) && (phoneEmpty));
      if (err) makeAlert(true,form['email'],'Du skal enten udfylde e-mail eller telefonnummer for at jeg kan kontakte dig...');   
    };
    
	  if ((!err) && (!mailEmpty)) err=validateFieldEmail(true,form['email'],'Email');
   
    if (!err) {
    
      request = new Request(), command = request.addCommand('contact','sendmail'); 
      command.setAttribute('contactid',contactid); 
      command.setAttribute('method','transform'); 
      command.setAttribute('mailsubject','Kundehenvendelse'); 
      command.setAttribute('xsl','contact.' + this.isolanguageid + '.xsl'); 

      for (var i=0; i<form.elements.length; i++) 
        if ((form.elements[i].name!='') && (form.elements[i].name!='email') && (form.elements[i].name!='name')) 
          if (!validateFieldNonEmpty(false,form.elements[i])) 
            XMLAddElement(command.element, form.elements[i].name, form.elements[i].value);

      if (!mailEmpty) command.setAttribute('mailfrom',form['email'].value); 
      if (!nameEmpty) command.setAttribute('mailfromname',form['name'].value); 
   
      setVisibility(this.formid+'Form',false);
      setVisibility(this.formid+'Progress',true);
      
      this.mailws.Post(request, null, createDelegate(this, this.onready), createDelegate(this, this.onerror));
    };
    
  };
  
  this.reset = function(){
    var form = document.forms[this.formid+'Form'];
    form.reset(); 
    for (var i=0;i<form.elements.length;i++) if (form.elements[i].initialValue) form.elements[i].style.color='';

    setVisibility(this.formid+'Progress',false);
    setVisibility(this.formid+'Error',false);
    setVisibility(this.formid+'Done',false);
    setVisibility(this.formid+'Form',true);

  };
  
  document.init.add(createDelegate(this,this.initialize));
  
};


