
// This file is used in conjunction with common.js
// Must be loaded after the common.js file.

function ValidateFocus(o_field)
{
	var s_originalValue = "";
	
	if(o_field)
	{
		with(o_field)
		{
			s_originalValue = value;
			value = "";
			focus();
			value = s_originalValue;
		}
	}
}

function ValidateTrim(s_value)
{
    var s_match = s_value.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (s_match == null) ? "" : s_match[1];
}

// Validate Required Field.
function ValidateREQ(o_field)
{
	var b_valid		= true;
	var o_validator	= document.getElementById(o_field.id + "REQ");

	if(o_validator && o_field)
	{
		with(o_field)
		{
			if(value == null || (ValidateTrim(value)).length == 0)
				b_valid = false;
			
			o_validator.style.display = b_valid ? "none" : "block";
			return b_valid ? "" : (o_validator.innerHTML + g_NEW_LINE);
		}
	}
	else
	{
		return "Invalid Validator REQ FIELD : " + o_field.id;
	}
}

// Validate Length.
function ValidateLEN(o_field, param)
{
	var b_valid		= true;
	var o_validator	= document.getElementById(o_field.id + "LEN");

	if(o_validator && o_field)
	{	
		with(o_field)
		{
			if(value == null || (ValidateTrim(value)).length > param)
				b_valid = false;
			
			o_validator.style.display = b_valid ? "none" : "block";
			return b_valid ? "" : (o_validator.innerHTML + g_NEW_LINE);
		}
	}
	else
	{
		return "Invalid Validator LENGTH : " + o_field.id;
	}
}

// Validate Regular Expression.
function ValidateREG(o_field, param)
{
	var b_valid		= true;
	var o_validator	= document.getElementById(o_field.id + "REG");

	if(o_validator && o_field)
	{	
		with(o_field)
		{
			if(value == null || !param.test((ValidateTrim(value))))
				b_valid = false;
			
			o_validator.style.display = b_valid ? "none" : "block";
			return b_valid ? "" : (o_validator.innerHTML + g_NEW_LINE);
		}
	}
	else
	{
		return "Invalid Validator REG EXP : " + o_field.id;
	}
}

// Validate Date
function ValidateDate(o_field, param)
{
	var b_valid		= true;
	var o_validator	= document.getElementById(o_field.id + "Date");

	if(o_validator && o_field)
	{
		with(o_field)
		{
			if(value == null || !IsValidDate(ValidateTrim(value)))
				b_valid = false;
			
			if(ValidateTrim(value).length == 0 && !param)
				b_valid = true;
	
			o_validator.style.display = b_valid ? "none" : "block";
			return b_valid ? "" : (o_validator.innerHTML + g_NEW_LINE);
		}
	}
	else
	{
		return "Invalid Validator Date : " + o_field.id;
	}
}


function IsValidDate(date)
{
	var o_myDate	= new Date();
	var o_dateArray	= date.split("/");
	var s_year		= o_dateArray[2];
	var i_month		= -1;
	var i_monthTemp	= 0;
	var s_day		= o_dateArray[0];
	
	// javascript months start at 0 (0-11 instead of 1-12)
	try
	{
		i_month = parseInt(o_dateArray[1], 10);
		i_month--;
	}
	catch(e){}

	// setFullYear(yyyy, mm, dd);
	o_myDate.setFullYear(s_year, i_month ,s_day);
	
	try
	{
		i_monthTemp = o_myDate.getMonth();
	}
	catch(e){}	

	//alert(s_day);
	//alert(o_myDate.getDate());
	//alert(i_month);
	//alert(i_monthTemp);
	//alert(s_year);
	//alert(o_myDate.getFullYear());
	
	return ((s_day == o_myDate.getDate()) && (i_month == i_monthTemp) && (s_year == o_myDate.getFullYear()));
}

function ShowOther(o_source, s_itemID)
{
	var o_item = document.getElementById(s_itemID);
	
	if(o_item && o_source)
		o_item.style.display = o_source.options[o_source.selectedIndex].value == "other" ? "block" : "none";
}

function LoadingBar(b_Show, o_button)
{
	if(pg && o_button)
	{
		if(b_Show)
		{
			o_button.disabled = true;
			pg.show()
		}
		else
		{
			o_button.disabled = false;
			pg.hide();
		}
	}
}

//-------------------------------------------------
// Use multiple case statements where applicable.
function ValidateField(o_field)
{
    // See common.js file for variable declaration
	if(!g_b_keyPressed)
		return false;

	var s_errorMsg = "";

	switch(o_field.id)
	{
		case "nameTB" :
		case "phoneTB":
			s_errorMsg += ValidateREQ(o_field);
			s_errorMsg += ValidateLEN(o_field, 100);
			break;
		case "commentsTA" :
			s_errorMsg += ValidateREQ(o_field);
			s_errorMsg += ValidateLEN(o_field, 2000);
			break;
		case "emailTB" :
			s_errorMsg += ValidateREQ(o_field);
			if(s_errorMsg.length == 0)
				s_errorMsg += ValidateREG(o_field, /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/);
			s_errorMsg += ValidateLEN(o_field, 100);
			break;
	}

	return s_errorMsg;
}

function ShowFormErrors(o_Form)
{
	var s_errorMsgs = "";
	var b_isFocus   = false;
	var o_field;
	var o_fields    = new Array();
	var o_postal;
	var o_physical;
	var i_index = 0;
	
	// See common.js file for variable declaration
	g_b_keyPressed = true;

	// Find input fields
	o_fields = o_Form.getElementsByTagName('input');

	for(o_field in o_fields)
	{
		s_errorMsgs += ValidateField(o_fields[o_field]);
		if(s_errorMsgs.length > 0 && !b_isFocus)
		{
			ValidateFocus(o_fields[o_field]);
			b_isFocus = true;
		}
	}
	
	// Find textarea fields
	o_fields = null;
	o_field  = null;
	o_fields = o_Form.getElementsByTagName('textarea');

	for(o_field in o_fields)
	{
		if(i_index == 0)
		{
			i_index++;
			continue;
		}
		
		s_errorMsgs += ValidateField(o_fields[o_field]);
		if(s_errorMsgs.length > 0 && !b_isFocus)
		{
			ValidateFocus(o_fields[o_field]);
			b_isFocus = true;
		}
	}
	
	if(s_errorMsgs.length > 0)
		alert(s_errorMsgs);
	
	return s_errorMsgs.length > 0 ? false : true;
}

function SubmitForm(o_Form, o_button)
{
	var b_isValid = ShowFormErrors(o_Form);

	if(b_isValid)
	{
		if(CaptchaCheckCode(o_Form) == "Failed")
		{
			b_isValid = false;
			alert("Image text validation failed, please try again.");
		}
		else
		{
			LoadingBar(true, o_button);
			o_Form.submit();
		}
	}

	return b_isValid;
}