function emptyField(s)
{
	if (s.value.length == 0) return true;
	for (var i=0; i < s.value.length; ++i)
	{
		if (s.value.charAt(i) != ' ') return false;
	}
	return true;
}

function emptyFieldValue(s)
{
	if (s.length == 0) return true;
	for (var i=0; i < s.length; ++i)
	{
		if (s.charAt(i) != ' ') return false;
	}
	return true;
}

function isIntegerValue (s)
{
    for (i = 0; i < s.length; i++)
    {
	var c = s.charAt(i);
	if (!isDigit(c)) return true;
    }
    return false;
}

function stringReplace(originalString, find, replace)
{
	var pos=0, preString, postString
	pos = originalString.indexOf(find)
	while (pos != -1)
	{
		preString = originalString.substring(0,pos)
		postString = originalString.substring(pos+1, originalString.length)
		originalString = preString + replace + postString
		pos = originalString.indexOf(find)
	}
	return originalString;
}

function validateForm(oForm)
{
	if (emptyField(oForm.sender_name))
	{
		alert("A name is required.");
		oForm.sender_name.focus();
		return false;
	}
	
	if (emptyField(oForm.from))
	{
		alert("An email address is required.");
		oForm.from.focus();
		return false;
	}
	
	if (!isEmail(oForm.from.value))
	{
		alert("The email address is invalid.");
		oForm.from.focus();
		return false;
	}
	
	if (emptyField(oForm.mobileNbr))
	{
		alert("Your i-wireless mobile number is required.");
		oForm.mobileNbr.focus();
		return false;
	}

	mobile_unformatted = stringReplace(oForm.mobileNbr.value, "-", "")
	if (emptyFieldValue(mobile_unformatted) || mobile_unformatted.length < 10 || mobile_unformatted.length > 10)
	{
		alert("Your i-wireless mobile number must be 10 digits long and only contain dashes (-) for formatting.");
		oForm.mobileNbr.focus();
		return false;
	}
	if (isIntegerValue(mobile_unformatted))
	{
		alert("Your i-wireless mobile number must not contain letters.");
		oForm.mobileNbr.focus();
		return false;
	}

	if (emptyField(oForm.dob))
	{
		alert("Date of birth is required.");
		oForm.dob.focus();
		return false;
	}
	if(!validateDate(oForm.dob.value))
	{
		alert("Birth Date is not valid. Please format it like 'mm/dd/yyyy'");
		oForm.dob.focus();
		return false;
	}
    
	if (emptyField(oForm.message))
	{
		alert("A message is required.");
		oForm.message.focus();
		return false;
	}	
	
	return true;
}

function formatNumber (tobject)
{
	var unformatted = stringReplace(tobject.value, "-", "")
	var formatted = unformatted.substring(0,3) + "-" + unformatted.substring(3,6) + "-" + unformatted.substring(6,10)
	tobject.value = formatted
}

function isEmail (s)
{
	if (!isvalidEmailChar(s))
		return false;

	atOffset = s.lastIndexOf('@');

	if ( atOffset < 1 )
		return false;
	else
	{
		dotOffset = s.indexOf('.', atOffset);
		if ( dotOffset < atOffset + 2 || dotOffset > s.length - 2 ) 
		{
			return false;
		}
	}
	return true;
}

function isvalidEmailChar (s)
{   
    for (var i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) || (c=='@') || (c=='.') || (c=='_') || (c=='-') || (c=='+')) ) {
       	return false;
		}
    }

    return true;
}

function isDigit (c)
{
        return ((c >= "0") && (c <= "9"))
}

function isLetter (c)
{
        return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function validateDate(field) 
  {
  var validAll = "0123456789-.\/";
  var validHyphens = "-.\/";
  var hyphencount = 0;
  for (var i=0; i < field.length; i++) 
    {
    temp = "" + field.substring(i, i+1);
    if (validHyphens.indexOf(temp) != "-1")
      {
      hyphencount++;
      }
    if (validAll.indexOf(temp) == "-1") 
      {
      return false;
      }
    }
  if (field.length != 10)
    {
    return false;
    }
  if (hyphencount != 2)
    {
    return false;
    }
  return true;
  }