﻿function CheckField(field,previousFieldsAreOk,isDropDown)
{
	if(field.getAttribute("mandatory")==null)return true;
	var isEmpty;
	if(isDropDown)
		isEmpty=field.selectedIndex==0;
	else
		isEmpty=!field.value || field.value=="";
	if(!isEmpty && field.name && field.name.toUpperCase()=="EMAIL" && field.value.indexOf('@')<0)
		isEmpty=true;
	if(isEmpty)
	{
		if(!field.style.initBackgroundColor)
		{
			if(field.style.backgroundColor)
				field.style.initBackgroundColor=field.style.backgroundColor;
			else
				field.style.initBackgroundColor="transparent";
		}
		field.style.backgroundColor="#FFDDDD";
		if(!field.onchange)field.onchange=field.onkeypress=function(){if(field.style.initBackgroundColor)field.style.backgroundColor=field.style.initBackgroundColor;};
		try{if(previousFieldsAreOk)field.focus();}catch(ex){}
		return false;
	}
	else
	{
		if(field.style.initBackgroundColor)field.style.backgroundColor=field.style.initBackgroundColor;
		return true;
	}
}
function CheckForm()
{
	var result=true;

	var fields=document.getElementsByTagName("input");
	for (i=0;i<fields.length;i++)
	{
		if(!CheckField(fields[i],result,false))
			result=false;
	}
	var areas=document.getElementsByTagName("textarea");
	for (k=0;k<areas.length;k++)
	{
		if(!CheckField(areas[k],result,false))
			result=false;
	}
	var selects=document.getElementsByTagName("select");
	for (j=0;j<selects.length;j++)
	{
		if(!CheckField(selects[j],result,true))
			result=false;
	}
	
	return result;
}

function SubmitForm(formName)
{
	if(typeof(UpdateFormDigest)!="function")
		UpdateFormDigest=function(){};
	if(typeof(_spFormOnSubmitWrapper)!="function")
		_spFormOnSubmitWrapper=function(){};
	if(CheckForm())
	{
		var formUsed;
		if(typeof(formName)!="undefined")
		{
			formUsed= document.forms[formName];
			if (!formUsed)
			    formUsed = document.getElementById(formName);
			if(formUsed)formUsed.submit();
		}
		else
		{
			__doPostBack("CUSTOMFORM","");
		}
	}
}


