/*
Script to Validate Form Submit
Author: Abdullah Ibne Alam. Shohag
E-mail: abdullah.alam@soltiud.com.bd
*/

function validateForm(mfrm, style, bkcol, defbkcol)
{
	var me = mfrm.elements;
	var r = new RegExp("[\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-]", "i");
	var errorMSG = '';
	var iserror = 0;

	var regemail = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/

	bkcol = bkcol || "#FF0044";

	defbkcol = defbkcol || "#FFFFFF";

	for (var i=0; i < me.length; i++)
	{
		if ( (typeof(me[i].getAttribute('cmsReq')) != "undefined") && ( me[i].getAttribute('cmsReq') == 1) )
		{
			if (me[i].type == 'radio' || me[i].type == 'checkbox')
			{
				var rOptions = me[me[i].getAttribute('name')];
				var rChecked = 0;
				if (rOptions.length > 1)
				{
					for (var r = 0; r < rOptions.length; r++)
					{
						if (rOptions[r].checked)
						{
							rChecked = 1;
						}
					}
				}
				else
				{
					if (me[i].checked)
					{
						rChecked = 1;
					}
				}

				if (rChecked == 0)
				{
					errorMSG += me[i].getAttribute('cmsLabel') + ' : This field is required!\n';
					me[i].style.backgroundColor = bkcol;
					iserror = 1;
				}
				else if (me[i].style.backgroundColor == bkcol)
				{
					me[i].style.backgroundColor = defbkcol;
				}
			}
			if (me[i].value == '')
			{
				errorMSG += me[i].getAttribute('cmsLabel') + ' : This field is required!\n';
				me[i].focus();
				me[i].style.backgroundColor = bkcol;
				iserror = 1;
			}
			else if (me[i].getAttribute('cmsType') == "email" && !me[i].value.match(regemail))
			{
				errorMSG += me[i].getAttribute('cmsLabel') + ' : This field is required! Must be Valid Email Address.\n';
				me[i].focus();
				me[i].style.backgroundColor = bkcol;
				iserror = 1;
			}
			else // if (me[i].style.backgroundColor == bkcol.toLowerCase())
			{
				me[i].style.backgroundColor = defbkcol;
			}

			if (style == 1 && iserror == 1)
			{
				alert(errorMSG);
				return false;
			}
		}
	}
	if (iserror == 1)
	{
		alert(errorMSG);
		return false;
	}
	else
	{
		return true;
	}
}
