// JavaScript Document
// JS form field validation library developed by
// Robert Campbell - MRose Technologies, LLC - 2009
// program_functions()
// addSelectOption
// removeOptions
// isdefined
// validatePhone
// validateEmail
function remove_symbols_and_numbers(element){
	var boxText = document.getElementById(element).value;
	var origLength = boxText.length;
	var boxText = boxText.replace(/[^a-zA-Z]/g, '');
	//var boxText = boxText.replace(/[a-zA-Z0-9]/g, '');
	if (boxText.length < origLength)
		document.getElementById(element).value = Left(boxText,origLength - 1);
}
function checkIsCitizen(fld) {
    var error = false;
  	if (fld.value== "n") {
    	error = true;
	}
 	return error;
}
function program_functions()
{
	return true;	
}
function addSelectOption(fld, value, display) {
	if (display == null) {
		display = value;
	}
	var anOption = document.createElement('option');
	anOption.value = value;
	anOption.innerHTML = display;
	fld.appendChild(anOption);
	return anOption;
}
function removeOptions(fld) {
	for (var i = fld.length - 1; i >= 0; i--){
		fld.remove(i);
	}
}
function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function trim(s)
{
	return s.replace(/^\s+|\s+$/, '');
}
function Left(str, n){
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0,n);
}
function Right(str, n){
	if (n <= 0)
	   return "";
	else if (n > String(str).length)
	   return str;
	else {
	   var iLen = String(str).length;
	   return String(str).substring(iLen, iLen - n);
	}
}
function validateSelect(fld) {
    var error = false;
    if (fld.selectedIndex <= 0) {
        fld.style.background = 'Yellow'; 
		error = true;
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateEmpty(fld) {
    var error = false;
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
		error = true;
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateConfirm(fld1,fld2) {	//Two fields must match
    var error = false;
  		if (fld1.value != fld2.value)
    	{
        	fld1.style.background = 'Yellow'; 
        	fld2.style.background = 'Yellow'; 
        	error = true;
    	}
	return error;     
}
function remove_symbols(element){
	var boxText = document.getElementById(element).value;
	var origLength = boxText.length;
	var boxText = boxText.replace(/[^a-zA-Z0-9 ]/g, '');
	//var boxText = boxText.replace(/[a-zA-Z0-9]/g, '');
	if (boxText.length < origLength)
		document.getElementById(element).value = Left(boxText,origLength - 1);
}
function numerics_only(element){
	var boxText = document.getElementById(element).value;
	var origLength = boxText.length;
	var boxText = boxText.replace(/[^0-9]/g, '');
	//var boxText = boxText.replace(/[a-zA-Z0-9]/g, '');
	if (boxText.length < origLength)
		document.getElementById(element).value = Left(boxText,origLength - 1);
}
function setSelectedNone(fld) {
	fld.selectedIndex = 0;
}
function resetProgramsOfInterest() {
    setSelectedNone( document.getElementById('program_of_interest') );	
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');
	var pos = stripped.indexOf("123456");
	if (stripped.indexOf("123456") > 0){
		error = "Invalid phone number.\n";
		return error;
	}
	if (stripped.indexOf("4567890") > 0){
		error = "Invalid phone number.\n";
		return error;
	}
	if(Left(stripped.value,1) == "1" && stripped.length != 11){
		fld.style.background = 'Yellow';
		error = "Invalid phone number.\n";
		return error;
	} else if(Left(stripped.value,1) != "1" && stripped.length != 10){
		fld.style.background = 'Yellow';
		error = "Invalid phone number.\n";
		return error;
	}
	if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
		return error;
    }
	if (!(stripped.length == 10)){
        error = "There appears to be problem with your day time phone number wrong length.\n";
        fld.style.background = 'Yellow';
		return error;
    }
	var pos = stripped.indexOf("123456");
	if (stripped.indexOf("123456") > 0){
			error = "Invalid phone number.\n";
			return error;
	}
	if (stripped.indexOf("4567890") > 0){
			error = "Invalid phone number.\n";
			return error;
	}
	var area = Left(stripped,4);
	if (area.indexOf("111") > -1 ||  
		area.indexOf("123") > -1 || 
		area.indexOf("222") > -1 || 
		area.indexOf("333") > -1 ||
		area.indexOf("444") > -1 ||
		area.indexOf("555") > -1 ||
		area.indexOf("666") > -1 ||
		area.indexOf("777") > -1 ||
		area.indexOf("999") > -1 ||
		area.indexOf("911") > -1 ||
		area.indexOf("000") > -1 ||
		area.indexOf("098") > -1)
	{
			//alert("Problem With Area " + area);
			error = "Invalid phone number.\n";
			return error;
	}
	var main_phone = Right(stripped,7);
	var main_phone_prefix = Left(main_phone, 3);
	var main_phone_prefix_4 = Left(main_phone, 4);
	//alert(main_phone);
	//alert(main_phone_prefix);
	//alert(main_phone_prefix.indexOf("911"));
	if (main_phone_prefix.indexOf("000") > -1 ||  
		main_phone_prefix.indexOf("911") > -1 ||  
		main_phone_prefix.indexOf("555") > -1 ||
		main_phone_prefix_4.indexOf("1234") > -1 ||  
		main_phone_prefix_4.indexOf("0123") > -1 ||  
		main_phone.indexOf("1234567") > -1 || 
		main_phone.indexOf("4567890") > -1 || 
		main_phone.indexOf("0000000") > -1 || 
		main_phone.indexOf("1111111") > -1 ||
		main_phone.indexOf("3333333") > -1 ||
		main_phone.indexOf("4444444") > -1 ||
		main_phone.indexOf("5555555") > -1 ||
		main_phone.indexOf("6666666") > -1 ||
		main_phone.indexOf("7777777") > -1 ||
		main_phone.indexOf("8888888") > -1 ||
		main_phone.indexOf("9999999") > -1)
	{
			error = "Invalid phone number.\n";
			return error;
	}
	if (Left(stripped,1) == 1){
        error = "There appears to be problem with your day time phone number.\n";
        fld.style.background = 'Yellow';
		return error;
	}
	if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
		return error;
    }
    return error;
}
