/**
 * Gestion de formulaires
 *
 * @package administration.formulaires
 * @copyright 2004 Activis
 */

function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function check_email(obj)
{
   var o = findObj(obj);
   return is_email(o.value);
}

function is_email(email)
{
   var reg = /^[\w.-]+@[\w.-]+\.\w{1,4}$/
   var reg2 = /[.@]{2,}/
   return ((reg.exec(email) != null) && (reg2.exec(email) == null))
}

function is_date(date)
{
   var reg = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/;
   return (reg.exec(date) != null);
}

function is_number(nb)
{
  var reg = /^[0-9,\.]*$/
  return (reg.exec(nb) != null);
}


function trim(str)
{
    var reg = new RegExp(" ", "mgi");
    return str.replace(reg, "");
}


function is_full(obj)
{
   var o = findObj(obj);
//   alert(o.type);

   if(o.type != "checkbox" && o.type != "radio" && o.value != undefined)
       return(trim(o.value) != '');
   else
   {
//       alert(o.length);
       if(o.length != undefined)
       	   for(var i = 0; i < o.length; ++i)
       	   {
//       	   	alert(o[i].checked);
       	   	if(o[i].checked)
                  return true;
           }
       else
           return o.checked;
   }

   return false;
}

function is_type(type, field)
{
   var obj = findObj(field);
   if(trim(obj.value) == '')
       return true;

   switch(type)
   {
        case "text_mail":
            return is_email(obj.value);

        case "text_date":
            return is_date(obj.value);

        case "text_number":
             return is_number(obj.value);
   
        default:
             return true;
   }


}

