var error_emptyfield = "This field is required and can't be empty";
var error_invalidemailmsg = "Email is invalid";
var error_notequalfield = "Fields are not equal";

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function validateForm( f, type )
{
    var elem, i = 0, attr = "alt";
    while ( elem = f.elements[i++] )
    {
        //  Skip fieldsets
        if ( elem.nodeName == "FIELDSET" ) continue;

        //  Does element have validator attribute? (short-circuit check)
        fvCode = ( elem[attr] ) ? elem[attr] : elem.getAttribute( attr );
        if ( !( typeof fvCode == 'undefined' || fvCode == null || fvCode == "" ) )
        {
            //  Set params, validation type, and validation state
            validcontrols = fvCode.split( "|" );

            vld = true;

            var spanobj = document.getElementById(elem.name + "_valid");

            var clsname = elem.className;

            var poserr = clsname.indexOf("_error");
            if (poserr != -1) // this is already error tag
            {
                clsname = clsname.substring(0, poserr);
            }
            elem.className = clsname;

            for (k=0; k<validcontrols.length; k++)
            {
                validcontrol = validcontrols[k];
                var params = validcontrol.split( "," );    
                switch( params[0] )
                {
                    case "equal" :
                        try
                        {
                            var element_to_equal = params[1];
                            var obj_to_equal = document.getElementById( element_to_equal );
                            if (obj_to_equal != null)
                            {
                                str1 = elem.value;
                                str2 = obj_to_equal.value;
                                if (str1 != str2)
                                {
                                    vld = false;
                                    if (type == 2)
                                    {
                                        var globalspanobj = document.getElementById("form_error_span");
                                        if (globalspanobj) 
                                        {
                                            elem.className = clsname + "_error";

                                            var objclsname = obj_to_equal.className;

                                            var poserr = objclsname.indexOf("_error");
                                            if (poserr != -1) // this is already error tag
                                            {
                                            }
                                            else
                                            {
                                                obj_to_equal.className = obj_to_equal.className + "_error";
                                            }

                                            globalspanobj.innerHTML = "&nbsp;<font color=red> " + error_notequalfield + " </font>";
                                        }
                                    }
                                    else
                                    {
                                        if (spanobj) { spanobj.innerHTML = "&nbsp;<font color=red> " + error_notequalfield + " </font>";}
                                    }

                                    elem.focus();
                                    return false;    

                                }
                            }

                        } 
                        catch(e) { }
                        break;

                    case "empty" :
                        try
                        {
                            var str = elem.value;
                            if(str == null || str.length == 0) 
                            {
                                vld = false;
                                if (type == 2)
                                {
                                    var globalspanobj = document.getElementById("form_error_span");
                                    if (globalspanobj) 
                                    {
                                        elem.className = clsname + "_error";
                                        globalspanobj.innerHTML = "&nbsp;<font color=red> " + error_emptyfield + " </font>";
                                    }
                                }
                                else
                                {
                                    if (spanobj) { spanobj.innerHTML = "&nbsp;<font color=red> " + error_emptyfield + " </font>";}
                                }

                                elem.focus();
                                return false;    
                            }
                        } 
                        catch(e) { }
                        break;

                    case "email" :
                        var bad = 0;
                        try
                        {

                            var str = elem.value;
                            var retValue = trim(str);
                            invalidChars = " /:,;";
                            if (retValue == "") {
                                bad = 1;
                            }
                            for (ii=0; ii < invalidChars.length; ii++) {
                                badChar = invalidChars.charAt(ii);
                                if (retValue.indexOf(badChar,0) > -1) {
                                    bad = 1;
                                }
                            }
                            atPos = retValue.indexOf("@",1);
                            if (atPos == -1) {
                                bad = 1;
                            }
                            if (retValue.indexOf("@",atPos+1) > -1) {
                                bad = 1;
                            }
                            periodPos = retValue.indexOf(".",atPos)
                            if (periodPos == -1) {
                                bad = 1;
                            }
                            if (periodPos+3 > retValue.length) {
                                bad = 1;
                            }

                        }
                        catch(e) {}

                        if (bad > 0 )
                        {                   
                            if (type == 2)
                            {
                                var globalspanobj = document.getElementById("form_error_span");
                                if (globalspanobj) 
                                {
                                    elem.className = clsname + "_error";
                                    globalspanobj.innerHTML = "&nbsp;<font color=red> " + error_invalidemailmsg + " </font>";
                                }
                            }
                            else
                            {
                                if (spanobj) { spanobj.innerHTML = "&nbsp;<font color=red> " + error_invalidemailmsg + " </font>"; }
                            }
                                
                            elem.focus();
                            return false;
                        }

                        break;
                        default: break;
                }
            }
        }
    } 
    f.form_completed.value = "ok";
    return true;
}
