///////////////////////////////////////////////////////////////////////////////////////////////////


/// function checkForm: validates the form fields according hidden field with ID : - required - /// 
       
function checkForm(e)
{
var of=document.webform;
var t=0; 
var focusOnFirstItem=true;


/* No copymail to client
if (of.email)
{
of.send_cc.value=of.email.value;
}
*/


// Define cross browser Event object.
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // avoid Safari bug
targ = targ.parentNode; //
                 
		
// Test if DOM is available and there is an element called required
if(!document.getElementById || !document.createTextNode){return;}
if(!document.getElementById('required')){return;}


// Define error messages and split the required fields
var errorID='errormsg';
var errorClass='error';
var errorClassxl='errorxl';
var errorClassTextArea='error2';
var errorMsg='Niet alle velden werden correct ingevuld. Gelieve het nodige te corrigeren:';
if (window.location.href.indexOf('/hb/')!=-1)
{var errorImg='../../img/alert.gif';}
else
{var errorImg='../img/alert.gif';}
var errorAlt='Fout';
var errorTitle='Dit veld werd niet correct ingevuld!';
var reqfields=document.getElementById('required').value.split(',');
    reqfields=reqfields.reverse();


// Cleanup old mess
// if there is an old errormessage field, delete it
   if(document.getElementById(errorID))
   {
   var em=document.getElementById(errorID);
   em.parentNode.removeChild(em);
   }


// remove old images and classes from the required fields
for(var i=0;i<reqfields.length;i++)
{
 var f=document.getElementById(reqfields[i]);
 if(!f)
 {
 continue;
 }
		
 if(f.previousSibling && /img/i.test(f.previousSibling.nodeName))
 {
 f.parentNode.removeChild(f.previousSibling);
 }
 if(f.nextSibling)
 {
 f.parentNode.removeChild(f.nextSibling);
 }
 if(f.nextSibling)
 {
 f.parentNode.removeChild(f.nextSibling);
 }
 switch(f.type.toLowerCase())
 {
  case 'textarea':
  {
  f.className='textareafield';
  break;
  }
  default:
  {
 
   // style exception for xl drop down in HB edesk form
 
    if (f.name=='VARAT_probleem') 
    {
    f.className='formtextfieldxl'; 
    }
    else
    {  
    f.className='formtextfield';
    }
  }
 }



} // end for 


// loop over required fields
		
for(var i=(reqfields.length)-1;i>=0;i--)
{
// check if required field is there
var f=document.getElementById(reqfields[i]);
if(!f)
{
continue;
}

// test if the required field has an error, according to its type
switch(f.type.toLowerCase())
{
case 'text':      if (f.value=='' && f.name!='email')
                  {
                  cf_adderr(f,0);
                  }							
	          // email is a special field and needs checking
	          if (f.name=='email' && !cf_isEmailAddr(f.value))
                  {
                  cf_adderr(f,2);
                  }	

                  // check confirmation email 
                  if ( (f.name=='email2') && (f.value!='') && (f.value!=document.getElementById('E-mail').value)  )
                  {
                  cf_adderr(f,5);
                  }

                  // numeric
                  if ((f.isNumber=='true') && (isNaN(f.value)==true))
                  {
                  cf_adderr(f,4);
                  }
                  
   	          break;
case 'textarea':  if (f.value=='' && f.name!='VARBA_commentaar')
                  {
                  cf_adderr(f,1);
                  }							
                  if ((f.value.indexOf('<a href')!=-1) || (f.value.indexOf('<A HREF')!=-1))
                  {
                  cf_adderr(f,6);
                  }							
		  break;
                     
case 'checkbox':  if (!f.checked)
                  {
                  cf_adderr(f,0)
                  }							
		  break;
case 'select-one':if (!f.selectedIndex || f.selectedIndex==0)
                  {
                  cf_adderr(f,3);
                  }							  
                  break;
}
}

			
if ((!document.getElementById(errorID)) && (targ.value=="Versturen"))
{
of.submit();
}

/* Tool methods */
function cf_adderr(o,fieldType)
{
 // create image, add to and colourise the error fields
 var errorIndicator=document.createElement('img');
 errorIndicator.alt=errorAlt;
 errorIndicator.src=errorImg;
 errorIndicator.title=errorTitle;
 errorIndicator.className="form_alert";
 if (fieldType==1)
 {
 o.className=errorClassTextArea;
 }

 // style exception for xl drop down in HB edesk form  
 else if ((fieldType==3) && (o.name=='VARAT_probleem'))
 { 
 o.className=errorClassxl; 
 }
 else  
 {
 o.className=errorClass;
 }

 var theText1='';				
 //prepare the errortext node

 if (fieldType==2)
 {
 theText1 = document.createTextNode(' Gelieve een geldig e-mail adres in te geven !');
 }
 else if (fieldType==3)
 {
 theText1 = document.createTextNode(' Gelieve uw \''+o.id+'\' te selecteren !');
 }  
 else if (fieldType==4)
 {
 theText1 = document.createTextNode(' Gelieve een numerieke waarde in te geven !');
 } 
 else if (fieldType==5)
 {
 theText1 = document.createTextNode(' Het e-mail adres dat u ter bevestiging hebt ingegeven stemt niet overeen !');
 }
 else if (fieldType==6)
 {
 theText1 = document.createTextNode(' Uw invoer is niet toegestaan !');
 }
 else
 {
 theText1 = document.createTextNode(' Het veld \''+o.id+'\' is verplicht !');
 }
 var spanel=document.createElement('span');
 spanel.id="spanel"+t;
 o.parentNode.appendChild(spanel);
 x=document.getElementById('spanel'+t);
 x.setAttribute('class','table_main');
 x.appendChild(theText1);
 o.parentNode.className='table_main';		
 o.onblur=checkForm;
 t++;
				
 // Check if there is no error message
 if(!document.getElementById(errorID))
 {
  // create errormessage and insert before submit button
  var em=document.createElement('div');
  em.id=errorID;
  var newp=document.createElement('p');
  newp.className='errortekst';
  newp.appendChild(document.createTextNode(errorMsg))
  // clone and insert the error image
  newp.appendChild(errorIndicator.cloneNode(true));
  em.appendChild(newp);
  var newul=document.createElement('ul');		
  em.appendChild(newul);
  // find the submit button 
  sb=document.getElementById('errorDiv');
  if(sb)
  {
  sb.parentNode.insertBefore(em,sb);
  }// end if	
 } // end if 
}  // end function cf_aderr


function cf_isEmailAddr(str) 
{
return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
}  // end function cf_isEmailAddr

			
}  // end function checkForm

///////////////////////////////////////////////////////////////////////////////////////////////////

/// function highlight: highlight the formfield which has focus ///

var highlightcolor='#EBEBEB'
var ns6=document.getElementById&&!document.all
var previous=''
var crossBrowserTargetElement

function initForm()
{
}


// temporarily disabled:  error in IE7
function initFormGo(){
 if (document.webform)
 {
 document.webform.onclick=highlight;
 document.webform.onkeyup=highlight;
 }
}


//Regular expression to select elements that will be highlighted
var intended=/INPUT|TEXTAREA|SELECT|OPTION/

function checkElement(which){
  if (which.style&&intended.test(which.tagName)){
  if (ns6&&crossBrowserTargetElement.nodeType==3)
      crossBrowserTargetElement=crossBrowserTargetElement.parentNode.parentNode
  return true
 }
else
return false
}

 
function highlight(e){
 
 // Define cross browser Event object and crossBrowserTargetElement.
 if (!e) var e = window.event;
 if (e.target) crossBrowserTargetElement = e.target;
 else if (e.srcElement) crossBrowserTargetElement = e.srcElement;
 
   
 if (previous!='')
 {
  if (checkElement(previous))
  previous.style.backgroundColor=''
  previous=crossBrowserTargetElement;
  if (checkElement(crossBrowserTargetElement))
  crossBrowserTargetElement.style.backgroundColor=highlightcolor
 }
 else
 {
  if (checkElement(crossBrowserTargetElement))
  crossBrowserTargetElement.style.backgroundColor=highlightcolor;
  previous=crossBrowserTargetElement;
  
 }
}
/// end fn highlight ///

///////////////////////////////////////////////////////////////////////////////////////////////////
