//pageValidator.AttachForm("frmPage");
//pageValidator.Forms["frmPage"].LoadConstraints("index.xml");
//pageValidator.Forms["frmPage"].Setup();
//pageValidator.DetachForm("frmPage");
//pageValidator.IsValidForm("frmPage") [boolean]

function PageValidator()
{
   this.Validate = true;
   this.Forms = new Object();
   this.Masks = new Object();
   this.PostValidationMethods = new Object();
}

var pageValidator = new PageValidator();

PageValidator.prototype.AttachForm = function( formId )
{
    try
    {
        this.Forms[formId] = this.SetupForm( document.forms[formId] );
   }
   catch(ex)
   {
       debugHelper.Exception("PageValidator.AttachForm " + ex.message, document.URL, document.lastModified, document.referrer);
   }
}

PageValidator.prototype.DetachForm = function( formId )
{
    this.Forms[formId] = null;
}

PageValidator.prototype.SetupForm = function( form )
{
    try
    {
        if( form == null ) return null;
        var newForm = new CustomHTMLForm( form );
        return newForm;
   }
   catch(ex)
   {
       debugHelper.Exception("PageValidator.SetupForm " + ex.message, document.URL, document.lastModified, document.referrer);
       return null;
   }
}



PageValidator.prototype.IsBlank = function( poElement )
{   
   if (poElement.value.length == 0) return true;
   var sTemp = poElement.value;
   sTemp = sTemp.replace(/^,+$/, ''); // Call 172031
   while (sTemp.substring(0,1) == ' ') {
      sTemp = sTemp.substring(1, sTemp.length);}
   if (sTemp.length == 0) return true;
   while (sTemp.substring(sTemp.length-1,sTemp.length) == ' ') {
       sTemp = sTemp.substring(0, sTemp.length-1);}
   if (sTemp.length == 0) return true;
   return false;
}

String.prototype.RTrim = function()
{
   //get current text
   var psString = this;

   if( psString == "" ) return psString;

   var sTemp = new String(psString);

   while (sTemp.substring(sTemp.length-1,sTemp.length) == ' ') {
       sTemp = sTemp.substring(0, sTemp.length-1);
   }
   return sTemp;

   return false;
}

PageValidator.prototype.HaveAtLastOneItemChecked = function( poElement )
{
   elements = document.getElementsByName(poElement.name);

   for (var z=elements.length-1; z >= 0; z--)
   {
      if (elements[z].checked == true)
      {
         return true;
      }
   }

   return false;
}

PageValidator.prototype.HaveMultiSelectedItem = function( poElement )
{
   var unselectedValue = poElement.getAttribute('ismUnselectedValue');

   if (poElement.options.length > 0)
   {
      if (poElement.value.length == 0)
      {
         return false;
      }
      else
      {
      	 if (unselectedValue == null) {unselectedValue = "-1";}

      	 var bValid = false;

      	 for (i = 0; i < poElement.options.length; i++)
      	 {
      	 	if (poElement.options[i].selected)
      	 	{
      	 		if (poElement.options[i].value == unselectedValue)
      	 		{
      	 			return false;
      	 		}
      	 		else
      	 		{
      	 			bValid = true;
      	 		}
      	 	}
      	 }
      	 return bValid;
     }

   }

   return false;
}

PageValidator.prototype.HaveSelectedItem = function( poElement )
{
   var unselectedValue = poElement.getAttribute('ismUnselectedValue');

   if (poElement.options.length > 0)
   {
      if (poElement.value.length == 0)
      {
         return false;
      }
      else if (unselectedValue == null)
      {
         return (poElement.value != "-1");
      }
      else if (poElement.value == unselectedValue)
      {
        return false;
      }
      return true;
   }

   return false;
}

PageValidator.prototype.CheckLengthConstraint = function( poElement, iMinimun, iMaximun )
{
   if( iMinimun >=0 && (iMinimun <= iMaximun || iMaximun == 0 || iMaximun == undefined))
   {
      if( poElement.value.length < iMinimun )
      {
         return false;
      }
   }

   if( iMaximun > 0 && (iMaximun >= iMinimun || iMinimun == 0 || iMinimun == undefined))
   {
      if( poElement.value.length > iMaximun )
      {
         return false;
      }
   }

   return true;
}


