/*
 JS-FORM-CHECKER
 ===============
 $Id$
 Author: nilsandrey@gmail.com
 
 Assumed Document Object Model described in:
 "Document Object Model (DOM) Level 2 Specification Version 1.0"
 (http://www.w3.org/TR/1999/CR-DOM-Level-2-19991210)
 Latest version: (http://www.w3.org/TR/DOM-Level-2)

 Successfully tested on:
  - Miscrosoft Internet Explorer 6
  - Mozilla Firefox 1.0.7
  - Opera 8.5
 All in platform Win32, Operating System Windows XP SP2.
 
 Description:
 ============
 Script to check values in a form based on regular expression objects. To validate a form call the function
 checkForm(Names, Exprs, EMsgs). MSG_INCORRECT_FORM is the message to show when the form is incorrect.
 The incorrect fields, based on regular expressions, will be signaled changing the border and background styles.
 The two parameters are arrays with the names and regular expressions correspondients to each field
 in the form. For each name in the array Names, will be a matching regular expression at the same position in the
 array RegExprs.
 An example of declaration is commented out below. Note that there are some regular expressions already defined for
 both required and not required fields.

 Use Example:
 ============
	<script type="text/javascript" src="validateform.js"></script>
	<script type="text/javascript">
		MSG_INCORRECT_FORM = "Error in form! Check signaled fields.";

		var Names = new Array();
		var Exprs = new Array(); 
		var EMsgs = new Array();

		Names[0] = "company_name";
		EMsgs[0] = "Error in company name"
		Exprs[0] = RE_Req_NoTags;

		Names[1] = "phone";
		EMsgs[1] = "Error in phone"
		Exprs[1] = "([\\d\\s\\(\\)\\+\\-])*"; // Phone number: ( ) + - 0 1 2 3 4 5 6 7 8 9 and whitespace chars

		Names[2] = "description";
		EMsgs[2] = "Error in description"
		Exprs[2] = RE_Req_NoTags;

		Names[3] = "email";
		EMsgs[3] = "Error in email"
		Exprs[3] = RE_NonReq_EmailAddress;

		Names[4] = "webpage";
		EMsgs[4] = "Error in webpage"
		Exprs[4] = RE_NonReq_WebURL;

		Names[5] = "address";
		EMsgs[5] = "Error in address"
		Exprs[5] = RE_NonReq_NoTags;

	</script>
	....
	<form ... onsubmit="return checkForm(Names, Exprs, EMsgs, MSG_INCORRECT_FORM)"...>
	...
*/

var RE_Req_DecimalNumber    = "\\d+(\\.\\d+){0,1}";
var RE_NonReq_DecimalNumber = "(\\d+(\\.\\d+){0,1}){0,1}";

var RE_Required             = ".+";

var RE_Req_EmailAddress     = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)*$";
var RE_NonReq_EmailAddress  = "^([\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)*){0,1}$";

var RE_Req_WebURL           = "^(http|ftp|https|ghoper)://[\\w-]+(\\.[\\w-]+)*(:\\d+){0,1}(/\\S*)*$";
var RE_NonReq_WebURL        = "^((http|ftp|https|ghoper)://[\\w-]+(\\.[\\w-]+)*(:\\d+){0,1}(/\\S*)*){0,1}$";

var RE_Req_NoTags           = "^([^<>])+$";
var RE_NonReq_NoTags        = "^([^<>])*$"; 

var CurrentMessages         = "";

function checkElements( elements, Names, Exprs, EMsgs )
{
    result = true;
    
    for (i=0; i<elements.length; i++)
    {
        vpos = 0;
        // Finds the correspondient regular expression...
        while ((vpos < Names.length) && (elements.item(i).name != Names[vpos]))
        {
            vpos++;
        }
        // If found checks for matching...
        if (elements.item(i).name == Names[vpos])
        {
            var RExpr   = new RegExp(Exprs[vpos]);
            var Str     = new String(elements.item(i).value);
            var matches = Str.match(RExpr);
            if (matches == null)
            {
            	elements.item(i).style.backgroundColor = "#F0A9A9";
            	//elements.item(i).style.border 		   = ""; 
            	CurrentMessages                        = CurrentMessages+EMsgs[vpos]+"\n";
                result = false;
            }
            else
            {
            	elements.item(i).style.backgroundColor = "#FFFFFF";
            	//elements.item(i).style.border 		  = "solid 1px #666666";
            }
        }
    }
    return result;
}

function checkForm( Names, Exprs, EMsgs, MSG_INCORRECT_FORM, destId )
{

    var result = true;
    
    var elements  = document.getElementsByTagName("input");
    var elements2 = document.getElementsByTagName("textarea");
    
    CurrentMessages = "";
    
    result1 = checkElements(elements, Names, Exprs, EMsgs);
    result2 = checkElements(elements2, Names, Exprs, EMsgs);
    result  = result1 && result2;
    
    if (!result)
    {
		if (destId != "") {
			var ele = document.getElementById(destId);
			ele.innerHTML = MSG_INCORRECT_FORM+"\n"+CurrentMessages;
		}
		else
			alert(MSG_INCORRECT_FORM+"\n"+CurrentMessages);
    }
    return result;
}

function wordCount( str )
{
    var RES     = "[^<>\\s]+(\\s)+|[^<>\\s]+$";
    var RExpr   = new RegExp(RES, "gim");
    var Str     = new String(str);
    var matches = Str.match(RExpr);
    if (matches != null)
        return matches.length;
    else
        return 0;
}