// SET THIS TO 'false' TO TURN OFF DEBUGGING.

var DEBUG = false;

// ------------------------------------------------------------------------------------------
// Splits "Payment Amount" values into two separate fields.
function splitPaymentAmount()
{
	var frm = document.getElementById("payForm");
	var selectValue = frm.paymentAmount.value;
	var stringParts = new Array();
	stringParts = selectValue.split('|');
	if (stringParts.length == 2)
	{
		frm.amount.value = stringParts[0];
		frm.currency.value = stringParts[1];
		if (DEBUG)
		{
			alert("Amount: " + frm.amount.value + "\nCurrency: " + frm.currency.value);
		}
	}
	else 
	{
		frm.amount.value = "EMPTY";
		frm.currency.value = "";
		if (DEBUG)
		{
			alert("Payment Amount not set.");
		}
	}									
}

// ------------------------------------------------------------------------------------------
// Redirects the user to the chosen form.
function formRedirect()
{
	var found = false;
	for(var i = 0; i < document.form.URL.length; i++)
	{
		if(document.form.URL[i].checked == true)
		{
			var redirect = document.form.URL[i].value;
			found = true;
			if (DEBUG)
			{
				alert("Redirecting to:\n" + redirect);
			}
			window.location = redirect;
		}
	}
	if (! found)
	{
		alert("You need to choose an option");
	}	
}

// ------------------------------------------------------------------------------------------
// Validates a form
function validateForm(frm)
{
	var isValid = true;
	for (var i = 0; i < frm.elements.length; i++)
	{
		var element = frm.elements[i];
		if (element.title && element.title.length > 0)
		{
			var elementOk = true;
			if (element.title.indexOf("required") > -1 && elementOk)
			{
				elementOk = (element.value.length > 0)
			}
			if (element.title.indexOf("email") > -1 && elementOk)
			{
				elementOk = validEmail(element.value);
			}
			if (element.title.indexOf("phone") > -1 && elementOk)
			{
				elementOk = validPhone(element.value);	
			}
			if (element.title.indexOf("number") > -1 && elementOk)
			{
				elementOk = isNumeric(element.value);
			}
			if (elementOk) 
			{
				element.style.border = "";	
			}
			else
			{
				element.style.border = "1px solid #FF0000";
				isValid = false;
			}
		}
	}
	if (! isValid)
	{
		alert("Please correct the highlighted fields before continuing.");
	}
	return isValid;
}

// ------------------------------------------------------------------------------------------
// Validates an email address
function validEmail(email)
{
	var atPos = email.indexOf('@');
	var dotPos = email.lastIndexOf('.');
	return ((email.length > 4) && (atPos > 0) && (dotPos > (atPos + 1)));
}

// ------------------------------------------------------------------------------------------
// Validates a phone number
function validPhone(phone)
{
	var validChars = "1234567890().-";
	for (var i = 0; i < (phone.length - 1); i++)
	{
		var char = phone.substr(i,1);
		if (validChars.indexOf(char) < 0)
		{
			return false;	
		}
	}
	return true;
}

// ------------------------------------------------------------------------------------------
// Checks a value to see if it is numeric.
function isNumeric(text)
{
   var validChars = "0123456789.";
   var isNum = true;
   for (var i = 0; i < text.length; i++) 
   { 
      var char = text.charAt(i); 
      if (validChars.indexOf(char) < 0) 
      {
         return false;
      }
   }
   return true;
}
