
var iForm = 
{
    err_alert:   Array(
        "You must fill in all required fields", 
        "Invalid character inserted; please don't use any of the following character: @ = < > \"  % ; ) ( & + ",
        "Please provide a valid E-mail address",
        "Parent field value is not matching me",
        "The field contains an invalid numbers, only letters are allowed",
        "The field contains an invalid format, only number are allowed",
        "You must check at least one of the options",
	    "Invalid character inserted; please don't use any of the following character: @ = < > \"  % ; & "
    ),
    
    err_msg:    Array(
        "Required",
        "Invalid",
        "Invalid E-mail",
        "Not matching",
        "Invalid format",
        "Invalid format",
        "Required"
    ),
    
    non_numerics_fields: Array(
        "txtContactName_First",
        "txtContactName_Last",
        "txtContactTitle",
        "txtCity",
        "txtCountry"
    ),
    
    numerics_fields: Array(
        "txtZip"
    ),
    
    localValidate : function (obj)
    {
        // main node
        global_cell = $(obj);
        while (global_cell.nodeName.toLowerCase() != "tr") {
        	global_cell = $(global_cell.parentNode);
        }
        
        //  check if it's mandatory (if input is blank)
        if (global_cell.children[0].children.length && global_cell.children[0].children[0].className == "mandatory")
        {
            if (obj.value == "")
            {
                this.printError(0, global_cell);
                return;
            }
        } else {
        	return; // not mandatory, nothing to check
        }
        
        //  check for invalid character within input string (if type == input)
        if (obj.type == "text" && obj.name.toLowerCase().indexOf("email") == -1 && obj.name.toLowerCase().indexOf("phone") == -1)
        {
            invalidPattern = /^[\w\s\'\-\.]*$/;
            if (!invalidPattern.test(obj.value))
            {
                this.printError(1, global_cell);
                return;
            }
        }

	    if (obj.type == "text" && obj.name.toLowerCase().indexOf("phone") > -1)
	    {
	        invalidPattern = /^[\w\s\+\(\)\-]*$/;
                if (!invalidPattern.test(obj.value))
                {
                    this.printError(7, global_cell);
                    return;
                }
	    }

        
        //  check for at least one checkbox selected
        if (obj.type == "checkbox")
        {
        	var oneChecked = false;
        	var items = global_cell.select("input");
        	for (var i=0; i<items.length; i++) {
        		oneChecked = oneChecked || items[i].checked; 
        	} 
            
            if (!oneChecked)
            {
                this.printError(6, global_cell);
                return;
            }
        }
            
        //  email data check
        if (obj.name.toLowerCase().indexOf("email") > -1)
        {
            emailPattern = /^([a-zA-Z0-9_\.\-\'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (!emailPattern.test(obj.value)) 
            {
                this.printError(2, global_cell);
                return;
            }
        }
        
        // matching data to its sibling
        if (obj.name.toLowerCase().indexOf("_confirm") > -1)
        {
            if (!(document.getElementById(obj.name.split("_")[0]).value == obj.value))
            {
                this.printError(3, global_cell);    
                return;
            }
        }
        
        // strict non-numeric
        if (!isNaN(obj.value) && this.non_numerics_fields.indexOf(obj.name) > -1)
        {
            this.printError(4, global_cell);    
            return;
        }
        
        // strict numeric
        if (isNaN(obj.value) && this.numerics_fields.indexOf(obj.name) > -1)
        {
            this.printError(5, global_cell);
            return;
        }
        
        // we clear the error cell if everything goes well
        global_cell.children[2].innerHTML = "";
    },
    
    hasError: false,
    alertOnError: true,
    errorMsg: "",
    lastAdded: "",
    
    printError : function (index, report) 
    {
    	if (iForm.alertOnError) {
        	//alert(this.err_alert[index]);
        } else {
        	var fldName = $(report.children[0]).innerHTML;
        	fldName = fldName.replace('<span class="mandatory">*</span>', "");
        	fldName = fldName.replace('<SPAN class="mandatory">*</SPAN>', "");
        	fldName = fldName.replace('<SPAN class=mandatory>*</SPAN>', "");
        	fldName = fldName.replace(': ', "");
        	fldName = fldName.replace(':', "");
        	
        	if (iForm.lastAdded != fldName) {
        		iForm.errorMsg += ((iForm.errorMsg.length > 0) ? "\n" : "Please review your input:\n\n") + " - " + fldName + ": " + this.err_alert[index];
        		iForm.lastAdded = fldName;
        	} 
        }
        
        iForm.hasError = true;
        report.children[2].innerHTML = this.err_msg[index];
        return;
    },
    
    globalCheck : function ()
    {  
    	iForm.hasError = false;
    	iForm.alertOnError = false;
    	iForm.errorMsg = "";
    	iForm.lastAdded = "";
    	
    	$$("Form")[0].select("input").each(function(item) {
    		if (item.onblur) {
    			item.onblur.apply(item, [item]);
    		} else if (item.onclick) {
    			item.onclick.apply(item, [item]);
    		}
    	});
    	
    	iForm.alertOnError = true;
    	
    	if (iForm.hasError) {
    		alert(iForm.errorMsg);
    	}
    	
    	return !iForm.hasError;
    }
    
     
};