
var VALID = "1"
var NOTVALID = "0"

function trim(strText) {
while (strText.substring(0,1) == ' ')
strText = strText.substring(1, strText.length);
while (strText.substring(strText.length-1,strText.length) == ' ')
strText = strText.substring(0, strText.length-1);
return strText;
}

function SearchAndReplace(Content, SearchFor, ReplaceWith) {

   var tmpContent = Content;
   var tmpBefore = new String();   
   var tmpAfter = new String();
   var tmpOutput = new String();
   var intBefore = 0;
   var intAfter = 0;

   if (SearchFor.length == 0)
      return;


   while (tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase()) > -1) {
   
      // Get all content before the match
      intBefore = tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase());
      tmpBefore = tmpContent.substring(0, intBefore);
      tmpOutput = tmpOutput + tmpBefore;

      // Get the string to replace
      tmpOutput = tmpOutput + ReplaceWith;


      // Get the rest of the content after the match until
      // the next match or the end of the content
      intAfter = tmpContent.length - SearchFor.length + 1;
      tmpContent = tmpContent.substring(intBefore + SearchFor.length);

   }

   return tmpOutput + tmpContent;

}

function validate(sObj, sItemName, sCSS1, sCSS2, sDisplayName)
{
   var oObj = document.getElementById(sObj);
   var oParent = document.getElementById(sItemName);
   var oError = document.getElementById("Error");
   var oErrorList = document.getElementById("ErrorList");
   if ((trim(oObj.value) == "")||(oObj.value == null))
   {
      displayError(true,oParent,oErrorList,oError,sCSS1,sCSS2, sItemName, sDisplayName);
      return NOTVALID;
   }
   else
   {
      displayError(false,oParent,oErrorList,oError,sCSS1,sCSS2, sItemName, sDisplayName);
      return VALID;
   }
}

function validateAsEmail(sObj, sItemName, sCSS1, sCSS2, sDisplayName)
{
   var oObj = document.getElementById(sObj);
   var sOK = validate(sObj,sItemName,sCSS1,sCSS2, sDisplayName);
   if (sOK == VALID)
   {
      var emailPattern = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
      var bEmail = emailPattern.test(oObj.value);

      if (!bEmail)
      {      
         var oParent = document.getElementById(sItemName);
         var oError = document.getElementById("Error");
         var oErrorList = document.getElementById("ErrorList");
         displayError(true,oParent,oErrorList,oError,sCSS1,sCSS2, sItemName, sDisplayName, "Please enter in valid email address"); 
         return NOTVALID;
      }
   }   
   return sOK;
}

function validateAsPhone(sObj, sItemName, sCSS1, sCSS2, sDisplayName)
{
   var oObj = document.getElementById(sObj);
   var sOK = validate(sObj,sItemName,sCSS1,sCSS2, sDisplayName);
   if (sOK == VALID)
   {     
      sObj = SearchAndReplace(oObj.value," ","");
      if ((trim(sObj).length < 8)||(oObj.value == null))
      {      
         var oParent = document.getElementById(sItemName);
         var oError = document.getElementById("Error");
         var oErrorList = document.getElementById("ErrorList");
         displayError(true,oParent,oErrorList,oError,sCSS1,sCSS2, sItemName, sDisplayName, "Please enter in valid phone number with 8 or more digits"); 
         return NOTVALID;
      }
   }   
   return sOK;
}

function validateCheckBox(sObj, sItemName, sCSS1, sCSS2, sDisplayName, bVerbose)
{
   var oObj = document.getElementById(sObj);
   var oParent = document.getElementById(sItemName);
   var oError = document.getElementById("Error");
   var oErrorList = document.getElementById("ErrorList");   
   if (!oObj.checked)
   {
      if (bVerbose)
      {
         displayError(true,oParent,oErrorList,oError,sCSS1,sCSS2, sItemName, sDisplayName);
      }
      return NOTVALID;
   }
   else
   {
      if (bVerbose)
      {
         displayError(false,oParent,oErrorList,oError,sCSS1,sCSS2, sItemName, sDisplayName);      
      }
      return VALID;
   }
}

function displayError(bError, oParent, oErrorList, oError, sCSS1, sCSS2, sItemName, sDisplayName, sErrorMessage)
{
    if (!sErrorMessage)
    {
       sErrorMessage = "please fill in this field";
    }    
    if (bError)
    {
      if (oParent)
      {
         oParent.className = sCSS2;
      }
      if (oErrorList)
      {
         if ((sDisplayName != "")&&(sDisplayName != null)&&(sItemName != "")&&(sItemName != null))
         {
            oErrorList.innerHTML = oErrorList.innerHTML + '<li><b><a href="#' + sItemName + '">' + sDisplayName + '</a></b> - ' + sErrorMessage + '</li>';
         }
      }
      if (oError)
      {
         oError.style.display = 'block';
      }      
    }
    else
    {
       if (oParent)
       {
         oParent.className = sCSS1;      
       }
    }
}

