/**
	These functions act as glue between the CMS and Admin DB site.  This is a temporary situation.

*/

var hErrorMessages =
	{
	phone 						: "Phone number should only contain '+', numbers or spaces.",
	business_started 	: "Please tell us when you started your business.",
	state							: "Either select a State or fill in the Other State field, please",
	find_site					: "Please tell us how you found us."
	};

function ValidateICInput()
	{
	// all fields are required!
	// some can be handled here -- special cases handled below

	// questions about: im_handle
	var aRequiredFields = new Array
		(
		{ field_name : 'fname', 				field_string : 'First Name' },
		{ field_name : 'lname', 				field_string : 'Last Name' },
		{ field_name : 'email', 				field_string : 'Email' },
		{ field_name : 'address1', 			field_string : 'Street Address' },
		{ field_name : 'city', 					field_string : 'City' },
		{ field_name : 'zip', 					field_string : 'Postal Code' },
		{ field_name : 'dob_day', 			field_string : 'Day of birth' },
		{ field_name : 'dob_month', 		field_string : 'Month of birth' },
		{ field_name : 'dob_year', 			field_string : 'Year of birth' }
		);

	if(!RequiredFieldsOK(aRequiredFields))
		return false;

	if($('phone_number').value == "" && $('cell_number').value == "")
		{
		alert("Please enter a Phone or Cell number.");
		return false;
		}

	if($('country').selectedIndex == 0)
		{
		alert("Please select a country.");
		return false;
		}

	if(!SelectFieldOK('state', 'other_state', true))
		{
		alert(hErrorMessages.state);
		return false;
		}

	if(!SelectFieldOK('find_site_ic', 'other_find_site', true))
		{
		alert("Please tell us how you found the announcement for this position.");
		return false;
		}

	if(!SelectFieldOK('job', 'comments', true))
		{
		alert("Either select a Job or fill in the Comments field, please");
		return false;
		}

	if(!PhoneValueOK($('phone_number').value))
		{
		alert(hErrorMessages.phone);
		return false;
		}

	if(!PhoneValueOK($('cell_number').value))
		{
		alert(hErrorMessages.phone);
		return false;
		}

	if($('timezone').selectedIndex == 0)
		{
		alert("Please select a time zone offset.");
		return false;
		}

	return true;
	}

function ValidateSurveyInput()
	{
	var aRequiredFields = new Array
		(
		{ field_name : 'fname', 						field_string : 'First Name' },
		{ field_name : 'lname', 						field_string : 'Last Name' },
		{ field_name : 'telephone', 				field_string : 'Phone' },
		{ field_name : 'email', 						field_string : 'Email' },
// 		{ field_name : 'address_1', 				field_string : 'Street Address' },
		{ field_name : 'city', 							field_string : 'City' },
		{ field_name : 'zip', 							field_string : 'Postal Code' },
		{ field_name : 'credit_card', 			field_string : 'Payment Method' },
		{ field_name : 'current_business',	field_string : 'Current Business' }
		);

	var aNumberFields = new Array
		(
		{ field_name : 'partnership_num', field_string : 'Number of partners' },
		{ field_name : 'open_days', field_string : 'Days open' },
		{ field_name : 'open_months', field_string : 'Months open' },
		{ field_name : 'open_years', field_string : 'Years open' }
		);

	if(!RequiredFieldsOK(aRequiredFields))
		return false;
	if(!NumberFieldsOK(aNumberFields))
		return false;

  if($('country').selectedIndex == 0)
    {
    alert("Please select a country.");
    return false;
    }

  if(!CheckboxFieldOK('started_business', 'open_duration'))
		{
		alert(hErrorMessages.business_started);
		return false;
		}

	if(!CheckboxFieldOK('start_business', 'when_start'))
		{
		alert("Please tell us when you plan to start your business.");
		return false;
		}

	if(!SelectFieldOK('state', 'other_state', true))
		{
		alert(hErrorMessages.state);
		return false;
		}

	if(!SelectFieldOK('find_site_survey', 'other_find_site', true))
		{
		alert(hErrorMessages.find_site);
		return false;
		}

	if(!ProductSelected())
		{
		alert("Please indicate which product you purchased.");
		return false;
		}

	if(!PhoneValueOK($('telephone').value))
		{
		alert(hErrorMessages.phone);
		return false;
		}

	if(!$('gender-m').checked && !$('gender-f').checked)
		{
		alert("Please tell us your gender");
		return false;
		}

	if($('age').selectedIndex < 1)
		{
		alert("Please tell us your age");
		return false;
		}

	return true;
	}

function ValidateMerchantAccountInput()
	{
	var aRequiredFields = new Array
		(
		{ field_name : 'fname', 						field_string : 'First Name' },
		{ field_name : 'lname', 						field_string : 'Last Name' },
		{ field_name : 'phone_number', 			field_string : 'Phone' },
		{ field_name : 'email', 						field_string : 'Email' },
		{ field_name : 'email_check', 			field_string : 'Confirm Email' },
		{ field_name : 'business', 					field_string : 'Business for the Merchant Account' },
		{ field_name : 'credit_rating', 		field_string : 'Credit Rating' }
		);

	if(!RequiredFieldsOK(aRequiredFields))
		return false;

	if(!PhoneValueOK($('phone_number').value))
		{
		alert(hErrorMessages.phone);
		return false;
		}

	if($('email').value != $('email_check').value)
		{
		alert("Email addresses don't match!");
		return false;
		}

	return true;
	}

function ValidateContactInput()
	{
	var aRequiredFields = new Array
		(
		{ field_name : 'fname', 						field_string : 'First Name' },
		{ field_name : 'lname', 						field_string : 'Last Name' },
		{ field_name : 'phone_number', 			field_string : 'Phone' },
		{ field_name : 'email', 						field_string : 'Email' },
		{ field_name : 'email_check', 			field_string : 'Confirm Email' }
		);

	if(!RequiredFieldsOK(aRequiredFields))
		return false;

	if(!PhoneValueOK($('phone_number').value))
		{
		alert(hErrorMessages.phone);
		return false;
		}

	if($('email').value != $('email_check').value)
		{
		alert("Email addresses don't match!");
		return false;
		}

	return true;
	}

function ProductSelected()
	{
	var aProducts = document.getElementsByName('products[]');
	var bProductSelected = false;

	for(var i = 0; i < aProducts.length; i++)
		{
		if(aProducts[i].checked)
			return true;
		}

	return false;
	}

function RequiredFieldsOK(aRequiredFields)
	{
	for(var i = 0; i < aRequiredFields.length; i++)
		{
		var elm = $(aRequiredFields[i].field_name);
		if(!elm)
			return true;

		var s = elm.value.replace(/^\s+/, "");
		s = elm.value.replace(/\s+$/, "");

		if(s == "")
			{
			alert(aRequiredFields[i].field_string + " required");
			return false;
			}
		}

	return true;
	}

function NumberFieldsOK(aNumberFields)
	{
	for(var i = 0; i < aNumberFields.length; i++)
		{
		var elm = $(aNumberFields[i].field_name);
		if(!elm)
			return true;

		if(elm.value == "")
			return true;

		if(isNaN(elm.value))
			{
			alert("Only numbers in the " + aNumberFields[i].field_string + " field, thanks!");
			return false;
			}
		}

	return true;
	}

function SelectFieldOK(sSelectFieldName, sOtherFieldName, bRequiredField)
	{
	var sel = $(sSelectFieldName);

	// old JS?
	if(!sel)
		return true;

	// second element should always be 'other' selection
	if(sel.selectedIndex == 1
		&& $(sOtherFieldName).value == "")
		return false;

	// first element should always be blank
	if(bRequiredField
		&& (sel.selectedIndex < 2 && $(sOtherFieldName).value == ""))
		return false;

	return true;
	}

function CheckboxFieldOK(sCheckboxId, sTextFieldId)
	{
	if(!$(sCheckboxId).checked)
		return true;

	return ($(sTextFieldId).value != "");
	}

function PhoneValueOK(s)
	{
	if(!s)
		return true;

	if(s.match(/[^\s0-9\+]/))
		return false;

	return true;
	}

// shorten text area value to it's maxLength (case sensitive!) parameter length
// maxLength = 0 or missing maxLength means no limit
function MaxLength(txa)
	{
	var nMaxLength = txa.getAttribute('maxLength');
	if(!nMaxLength)
		return;

	if(txa.value.length <= nMaxLength)
		return;

	txa.value = txa.value.substr(0, nMaxLength);
	}

function SetAssociatedTextField(sTextFieldId)
	{
	$(sTextFieldId).value = "";
	$(sTextFieldId).disabled = false;
	$(sTextFieldId).focus();
	}

function SetAssociatedCheckbox(txt, sCheckboxId)
	{
	if(txt.value == "")
		{
		$(sCheckboxId).checked = false;
		txt.value = "n/a";
		txt.disabled = true;
		}
	}

function ActivateOtherState(selCountry)
  {
  var selState = document.getElementById('state');
  if(!selState)
    return;

  var txtState = document.getElementById('other_state');
  if(!txtState)
    return;

  if(selCountry.selectedIndex == 1 || selCountry.selectedIndex == 3)
    {
    selState.disabled = false;
    txtState.value = "";
    txtState.disabled = true;
    }
  else
    {
    selState.disabled = true;
    selState.selectedIndex = 0;
    txtState.disabled = false;
    }
  }

