// formvalidate.js
// lre 2001

function StripChrs(s, chrs)
{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (chrs.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}
function StripDigits(s)
{
    return StripChrs(s, "0123456789");
}
function IsCharInString(c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}
function EndsWith( s, e )
{
    var end = s.substr(s.length-e.length, e.length);
    if ( end.toLowerCase() == e.toLowerCase() )
        return true;
    return false;
}
function BeginsWith( s, e )
{
    var beg = s.substr(0, e.length);
    if ( beg.toLowerCase() == e.toLowerCase() )
        return true;
    return false;
}
function getFront(mainStr,searchStr){
    foundOffset = mainStr.indexOf(searchStr)
    if (foundOffset == -1) {
        return null
    }
    return mainStr.substring(0,foundOffset)
}
function getEnd(mainStr,searchStr) {
    foundOffset = mainStr.indexOf(searchStr)
    if (foundOffset == -1) {
        return null
    }
    return mainStr.substring(foundOffset+searchStr.length,mainStr.length)
}
function replaceString(mainStr,searchStr,replaceStr)
{
    var front = getFront(mainStr,searchStr);
    var end = getEnd(mainStr,searchStr);
    if (front != null && end != null)
    {
        return front + replaceStr + end;
    }
    return mainStr;
}
function IsLetter(c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}
function IsDigit(c)
{   return ((c >= "0") && (c <= "9"))
}
function IsLetterOrDigit(c)
{   return (IsLetter(c) || IsDigit(c))
}
function IsAlpha( str )
{    if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
        return false;
    str += "";
      for (i = 0; i < str.length; i++)
    {    if ( !IsLetter(str.charAt(i)) && str.charAt(i)!=' ' )
            return false;
    }
    return true;
}
function IsAlpha2( str )
{    if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
        return false;
    str += "";
      for (i = 0; i < str.length; i++)
    {    if ( !IsLetterOrDigit(str.charAt(i)) && str.charAt(i)!=' ' )
            return false;
    }
    return true;
}
function ContainsAlpha( str )
{   if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
        return false;
    str += "";
    for (i = 0; i < str.length; i++)
    {   if ( IsLetter(str.charAt(i)) )
            return true;
    }
    return false;
}
function IsMatch( str1, str2 )
{    if (str1+"" == "undefined" || str1+"" == "null" || str1+"" == "")
        return false;
    if (str2+"" == "undefined" || str2+"" == "null" || str2+"" == "")
        return false;
    if (str1.length != str2.length)
        return false;

    var isValid = true;

    str1 += "";
    str2 += "";
      for (i = 0; i < str1.length; i++)
      {
        if ( !(str1.charAt(i) == str2.charAt(i)) )
        {
         isValid = false;
         break;
      }
   }
    return isValid;
}
function IsNull( val )
{     if (val+"" == "null")
         return true;
    return false;
}
function IsUndef( val )
{     if (val+"" == "undefined")
         return true;
    return false;
}
function IsBlank( str )
{     if ( IsNull(str) || IsUndef(str) || (str == "") )
         return true;
    return false;
}
function IsValidEmail( str ) {
    if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
        return false;

    var isValid = true;

    str += "";

    namestr = str.substring(0, str.indexOf("@"));
    domainstr = str.substring(str.indexOf("@")+1, str.length);
      if (IsBlank(str) || (namestr.length == 0) ||
            (domainstr.indexOf(".") <= 0) ||
            (domainstr.indexOf("@") != -1) ||
            !IsAlpha(str.charAt(str.length-1)))
        isValid = false;

       return isValid;
}
function IsChecked( thegroup )
{
    if ( thegroup.length > 0)
    {
        for (i=0; i<thegroup.length; i++)
        {   if ( thegroup[i].checked  )
                return true;
        }
    }
    else
        if ( thegroup.checked )
            return true;
     return false;
}
function IsURL( str ) {
    if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
        return false;

    str += "";
    var validchrs = ":./+-_?#=%&$@~";

    for (x = 0; x < str.length; x++)
    {
        if ( !IsLetterOrDigit(str.charAt(x)) &&
             !IsCharInString(str.charAt(x),validchrs) )
        {
            return false;
        }
    }
    if ( BeginsWith( str, 'http://' ) || BeginsWith( str, 'https://' ) || BeginsWith( str, 'ftp://' ) )
        return true;

    return false;
}
function IsImage( name )
{
    if ( IsBlank(name) )
        return true;
    if ( EndsWith(name,'.jpg') ||
         EndsWith(name,'.gif') )
        return true;
    return false;
}

var errstring;
var errselect;
var doformvalidate = true;
function AddError(errstr, errsel) {
    if ( doformvalidate==false ) return;
    if ( IsBlank(errstring) && !IsNull(errstr) )
        errstring = errstr;
    else
        errstring += "\n\n" + errstr;
    if ( IsBlank(errselect) && !IsNull(errsel) )
        errselect = errsel;
}

function FormSubmit( comval, myform )
{
    if (IsBlank(myform))
        myform=document.forms[0];
    else if ( !myform.submit && myform.length )
        myform=eval("document."+myform);

    if (!IsBlank(comval))
    {
        if ( IsCharInString('.', comval) ) // Is set
            myform.action = comval;
        else
        {    // Is append
            if (IsCharInString('?', myform.action))
                myform.action = getFront(myform.action,'?');
            myform.action += '?'+comval;
        }
    }
    myform.submit();
}

function FormReset( )
{
    document.forms[0].reset();
}

function SetFormValidate( onoff )
{
    doformvalidate = onoff;
}

function RedirectURL( url )
{
    window.location = url;
    return false;
}
