
// NOTE* include formValidation.js prior to this file

// Use multiple case statements where applicable.
function ValidateField(field)
{
	if(!gbl_isKeyPressed)
		return false;

	var str_errorMsg = "";

	switch(field.id)
	{
		case "nameTextBox"         :
		case "contactPhoneTextBox" :
		case "noOfGuestsTextBox" :
		case "prefMonthTextBox" :
			str_errorMsg += ValidateREQ(field);
			str_errorMsg += ValidateLEN(field, 100);
			break;
		case "partnersNameTextBox" :
		case "bestTimeToCallTextBox" :
		case "countryTextBox" :
		case "themeTextBox" :
		case "denominationTextBox" :
		case "favFoodTextBox" :
		case "favFlowerTextBox" :
			str_errorMsg += ValidateLEN(field, 100);
			break;
		case "estBudgetDDL" :
			str_errorMsg += ValidateDDLREQ(field);
			break; 
		case "emailTextBox" :
			str_errorMsg += ValidateREQ(field);
			if(str_errorMsg.length == 0)
				str_errorMsg += ValidateREG(field, /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/);
			str_errorMsg += ValidateLEN(field, 100);
			break;
		case "mustHaveTextBox" :
		case "wedVenueOtherTextBox" :
		case "recVenueOtherTextBox" :
		case "accommodOtherTextBox" :
		case "specialDietTextBox" :
		case "cakeOtherTextBox" :
		case "typeMusicOtherTextBox" :
		case "liveBandOtherTextBox" :
		case "transportOtherTextBox" :
		case "otherReqOtherTextBox" :
			str_errorMsg += ValidateLEN(field, 2000);
			break;
	}

	return str_errorMsg;
}

function ShowFormErrors(obj_Form)
{
	var str_errorMsgs = "";
	var bln_isFocus   = false;
	var obj_field;
	var obj_fields    = new Array();
	var obj_estBudget = null;
	var obj_prototype = null;
	var str_phototype = "";
	var obj_flowers	  = null;
	var str_flowers   = "";
	var obj_otherReq  = null;
	var str_otherReq  = "";
	var index         = 0;
	
	gbl_isKeyPressed = true;

	// Find input fields
	obj_fields = obj_Form.getElementsByTagName('input');

	for(obj_field in obj_fields)
	{
		if(obj_fields[obj_field].type == "checkbox" && obj_fields[obj_field].checked)
		{
			// Create photo type list
			if(obj_fields[obj_field].id.substring(0, 9) == "phototype")
			{
				str_phototype += obj_fields[obj_field].value + " ; ";
				continue;
			}
			
			// Create flowers list
			if(obj_fields[obj_field].id.substring(0, 7) == "flowers")
			{
				str_flowers += obj_fields[obj_field].value + " ; ";
				continue;
			}
			
			// Create Other Requirements list
			if(obj_fields[obj_field].id.substring(0, 8) == "otherReq")
			{
				str_otherReq += obj_fields[obj_field].value + " ; ";
				continue;
			}
		}
	
		str_errorMsgs += ValidateField(obj_fields[obj_field]);
		if(str_errorMsgs.length > 0 && !bln_isFocus)
		{
			ValidateFocus(obj_fields[obj_field]);
			bln_isFocus = true;
		}
	}
	
	// Find textarea fields
	obj_fields = null;
	obj_field  = null;
	obj_fields = obj_Form.getElementsByTagName('textarea');

	for(obj_field in obj_fields)
	{
		if(index == 0)
		{
			index++;
			continue;
		}
		
		str_errorMsgs += ValidateField(obj_fields[obj_field]);
		if(str_errorMsgs.length > 0 && !bln_isFocus)
		{
			ValidateFocus(obj_fields[obj_field]);
			bln_isFocus = true;
		}
	}
	
	obj_estBudget = document.getElementById("estBudgetDDL");
	
	if(obj_estBudget)
		str_errorMsgs += ValidateField(obj_estBudget);
	
	if(str_errorMsgs.length > 0)
	{
		alert(str_errorMsgs);
	}
	else
	{
		// if form validation is successful add lists to hidden fields
		// Photography type
		obj_prototype = document.getElementById("photoTypeTextBox");
		if(obj_prototype)
			obj_prototype.value = str_phototype;

		// Flowers
		obj_flowers = document.getElementById("flowersTextBox");
		if(obj_flowers)
			obj_flowers.value = str_flowers;

		// Other Requirements
		obj_otherReq = document.getElementById("otherReqTextBox");
		if(obj_otherReq)
			obj_otherReq.value = str_otherReq;
	}
	
	return str_errorMsgs.length > 0 ? false : true;
}

function SubmitForm(obj_Form, obj_button)
{
	var bln_isValid = ShowFormErrors(obj_Form);
	
	if(bln_isValid)
	{
		if(CaptchaCheck(obj_Form))
		{
			LoadingBar(true, obj_button);
			obj_Form.submit();
		}
	}

	return bln_isValid;
}
