//----------------------------------------------------------------
//  Author:			Matt Kelsey
//  Procedure Name:	GetCountField
//  Description:	Gets the hidden Count Field that is laid down for Multi Select Checkboxes and Radio Buttons
//  Arguments:		field   	- field
//  Return Value:	Hidden Field
//  Comments:		(none)
//----------------------------------------------------------------
function GetCountField(Field)
{
	var CountField = Field.id;
	var tempIndex = CountField.lastIndexOf("_");
	CountField = CountField.substring(0,tempIndex);
	var CountFieldHiddenField = CountField + "__Count";
	return document.getElementById(CountFieldHiddenField);
}
//----------------------------------------------------------------
//  Author:			Matt Kelsey
//  Procedure Name:	VerifyOneChecked
//  Description:	Verifies that at least one checkbox or radio button is checked
//  Arguments:		CountField   	- Hidden Count Field
//  Return Value:	Bool indicating whether at least one option has been selected
//  Comments:		(none)
//----------------------------------------------------------------
function VerifyOneChecked(CountField)
{
	var Checked = false;
	var tempIndex = CountField.id.lastIndexOf("_");
	FieldToVerify = CountField.id.substring(0,tempIndex);

	for (var x=0;x<CountField.value;x++)
	{
		var tempField = FieldToVerify + x;
		if(document.getElementById(tempField).checked)
			Checked = true;								
	}							

	return Checked;
}
//----------------------------------------------------------------
//  Author:			Matt Kelsey
//  Procedure Name:	ValidateFormat
//  Description:	Validates the format of Date/Time/Number Text boxes for InboundForms
//  Arguments:		(none)
//  Return Value:	
//  Comments:		(none)
//----------------------------------------------------------------
function ValidateFormat()
{
	var blnValid = false;
	for (var FieldCounter=0; FieldCounter < document.forms[0].length; FieldCounter++) 
	{
		var formField = document.forms[0].elements[FieldCounter];
		if (formField.name.substring(0,5) == "field")
		{
			//Only Text And Text Areas can have format
			if(formField.name.search('ROWYYY')<0)
			{
				if((formField.type == "text") || (formField.type == "textarea"))
				{
					switch(formField.subtype)
					{
						case "atDateOnly":
							blnValid = Date_Validate(formField);
							break;
						case "atTimeOnly":
							blnValid = Time_Validate(formField);
							break;
						case "dtSingleDouble":
							blnValid = Number_Validate(formField);
							break;		
						case "Email":
							blnValid = Email_Validate(formField);
							break;	
						case "atNone":
							blnValid = true;
							break;
					}
					if(!blnValid)
					{
						return false;
					}
				}
			}
		}
	}
	return true;
}
function Email_Validate(formField)
{
	if (formField.value.length > 0) 
	{
		var expSyn = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var exp = new RegExp(expSyn);
		if ( exp.exec(formField.value) != null)
		{
			return (true)
		}
		else
		{
			popMessage(ResourceID541,formField);
			return (false)
		}
	}
	return (true)
}
//----------------------------------------------------------------
//  Author:			Matt Kelsey
//  Procedure Name:	ValidateRequired
//  Description:	Validates the Required fields
//  Arguments:		(none)
//  Return Value:	
//  Comments:		(none)
//----------------------------------------------------------------
function ValidateRequired()
{
	var CountField;
	var blnOneChecked = false;
	
	for (var i=0; i < document.forms[0].length; i++) 
		{
			var formField = document.forms[0].elements[i];
			
			if(formField.validity != null)
			{
				if(!formField.validity.valid)
				{
					document.forms[0].elements[i].required = null;
				}
			}
			
			if (formField.name.substring(0,5) == "field")
			{
				var temp = "req" + formField.name;
				
				try{
				if(eval(temp) != null)
					{
						if(formField.name.search('ROWYYY')<0)
						{
							if((formField.type == "text" && trim(formField.value) == "") || 
							(formField.type == "textarea" && trim(formField.value) == "") ||
							(formField.type == "select-one" && formField.length == 0)||
							(formField.type == "select-one" && formField.options[formField.selectedIndex].value == ""))
							{
								sMsg = ResourceID520;
								popMessage(sMsg,formField);
								return false;
							}
							else if(formField.type == "radio")
							{
								CountField = GetCountField(formField);
								
								if(eval(CountField)!=null)
									blnOneChecked = VerifyOneChecked(CountField);
									
								if(!blnOneChecked)
								{
									sMsg = ResourceID520;
									popMessage(sMsg,formField);
									return false;
								}
									
							}
							else if(formField.type == "checkbox")
							{
								
								CountField = GetCountField(formField);
						
								if(eval(CountField) != null)//Could be MultiSelect, Checkbox
								{
									blnOneChecked = VerifyOneChecked(CountField);	
									if(!blnOneChecked)
									{
										sMsg = ResourceID520;
										popMessage(sMsg,formField);
										return false;
									}
								}		
								else
								{
									if(!formField.checked)
									{
										sMsg = ResourceID520;
										popMessage(sMsg,formField);
										return false;
									}						
								}									
							}
						}
					}
				}
				catch (err) {	
				}			
			}
		}
		
		return true;
}

//-----------------------------------------------------------------------------------------------------------
//  Author:			Sally Xu
//  Procedure Name:	unformatfield
//  Description:	Unformats a given HTML form field
//  Arguments:		fieldname - the name of the form field to be unformatted
//  Return Value:	(none)
//  Comments:		This function assumes that the form field in question has
//					populated format, unformat and subtype attributes
//-----------------------------------------------------------------------------------------------------------
function unformatfield(fieldname) {
	var sInput = "";
	var format = "";
	var unformat = "";
	var subtype = "";

	//Read in the field value, format, unformat and subtype strings
	sInput = fieldname.value;
	format = fieldname.format;
	unformat = fieldname.unformat;
	subtype = fieldname.subtype;
	switch(subtype)
	{
		case "atDateOnly":
			fieldname.value = eval("Date_Unformat(sInput, format)");
			break;
		case "dtSingleDouble":
			fieldname.value = eval("Number_Unformat(sInput, format)");
			break;			
	}
	//Call the appropriate unformat function based on the field type
	

}


//-----------------------------------------------------------------------------------------------------------
//  Author:			Sally Xu
//  Procedure Name:	unformatform
//  Description:	Unformats the data in an HTML form
//  Arguments:		formname - the name of the form to be unformatted
//  Return Value:	(none)
//  Comments:		This function will unformat any form field that is populated 
//					and has a specified format attribute.  This function assumes
//					that any form field that has a populated format attribute will
//					also have populated unformat and subtype attributes.
//-----------------------------------------------------------------------------------------------------------
function unformatform(formname) {
	//Loop through the form elements and unformat those that have specified formats
	for (var z=0; z < document.forms[0].length; z++) {
		var field = document.forms[0].elements[z];
		if ((field.value != "") && (field.format)) {
			if (field.format != ""){
				unformatfield(field);
			}
		}
	}

}

//Used to validate inbound form pages
function validateIBFpage(SimpleBrowser)
{
	var required;
	var format;
	if(SimpleBrowser)
	{
	    required = ValidateRequired();
	    if(!required)
			return false;
		format = ValidateFormat();
		if(!format)
			return false
		
		if(required || format)
		{
			unformatform(document.forms[0]);
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return true;
	}
}

// VerifyForwardToAFriend
//
// Created By: Paul Burrous
//
// This function is used to verify that the friend name and the friend 
// email are both filled in.  
function VerifyForwardToAFriend(source, arguments) 
{
	RowNum = source.RowNum;
	
	NameControl = document.getElementById("fieldFriendName_jsID_" + RowNum);
	EMailControl = document.getElementById("fieldFriendEMail_jsID_" + RowNum);

	if ((NameControl.value.length > 0) ^ (EMailControl.value.length > 0)) {
		arguments.IsValid = false;
	} else {
		arguments.IsValid = true;
	}
}

// Verifies the inbound form
function verifyIBF(simpleBrowser)
{
	if (validateIBFpage(simpleBrowser)==true && Page_ClientValidate()==true)
	{
		document.forms[0].submit();
	}
}
