var pageForms = new Array();
var clearedTA= new Array();

function clearSpace(el){
  if(!clearedTA[el.id]){
    clearedTA[clearedTA.length] = el.id;
    clearedTA[el.id] = true; 
    el.value=''; 
  }  
}

function validateForm(strId) {

  for(var i=0;i<pageForms.length;i++) {
    
    if(pageForms[i] != null && pageForms[i].id == strId){

      fc = pageForms[i];
      if(fc.checkConstraints() == 0) {
        if(document.forms[fc.id].returnTo)
          document.forms[fc.id].returnTo.value = document.location.href;       
          document.forms[fc.id].submit();
      }  
      break;
    }
  }      
}

function formConstraints(formId) {
	 this.id = formId;
  this.constraints = new Array();		

	 this.addConstraint = function (fld,type,msg) {
  this.constraints[this.constraints.length] = new fieldValidation(fld, type, msg);	
	}		
	this.checkConstraints = function() {
		var cFail= 0;		 
	  for(var ii=0;ii<this.constraints.length;ii++) {
	    switch (this.constraints[ii].type) {
	      case 'req': { 
	        if(IsEmpty(this.id, this.constraints[ii].name)){
	          alert(this.constraints[ii].msg);
	          SetFocus(this.constraints[ii].name);
	          return ++cFail;
	        }else break;
	      }
	    }  
	  }
	  return cFail;
	}
}

function fieldValidation(name,type,msg) {
  this.name = name;
  this.type = type;
  this.msg = msg;
}

function IsEmpty(frmId,fld){
	var formEl = document.forms[frmId].elements[fld];
	var domEl = document.getElementById(fld);
	
	//mozilla doesnt do a name check in getById
	if(domEl == null)
	  domEl = document.getElementById(fld+'-1');
  
	switch(domEl.type) {
	  case 'text': {
	    if(domEl.value != '') 
	      return false;
	    break;  
	  }
	  case 'radio': {
	  	for(j=0;j<formEl.length;j++) {
	  	  if(formEl[j].checked)
	  	    return false;
	  	}
	  	break;
	  }
	  case 'checkbox': {
  	  if(formEl.checked)
  	    return false;
  	  break;
	  }
	  case 'select-one': {
	 // [JJ] .value is empty in MSIE but not in Mozilla when the value attribute is missing. 
	 // the value attribute is not set from the CMS so it is better to assume that the first element of the list is invalid
//	    if(formEl.options[formEl.selectedIndex].value != '')
        if(formEl.selectedIndex != 0)
	      return false;
	    break;  
	  }
	  case 'select-multiple': {
	    var totalChecked = 0;
	    
        for (i = 0; i < formEl.options.length; i++) {
          if (formEl.options[i].selected) {
             totalChecked++;
          }
        }
        if (totalChecked != 0) {
            return false;
        } 
        break;  
	  }
	  case 'textarea': {
	  	if(domEl.innerText != ' ' && domEl.innerText != '')  
	  	  return false;
	    break;  
	  }
	}
	return true;
}

function SetFocus(fieldName) {
  document.getElementById(fieldName).focus();
}

function change_visibility(id,id2,visible)
{ 
	 if(visible == 'visible' )
  {
		  document.getElementById(id).style.display = 'none';
		  document.getElementById(id2).style.display = '';
  }
	 if(visible == 'invisible' )
  {
		  document.getElementById(id2).style.display = 'none';
		  document.getElementById(id).style.display = '';
  }
  
}
