// Simple landing page form validation
// Allen Voelcker - Nov 7, 2006

String.prototype.trim = function() {
    // skip leading and trailing whitespace
    // and return everything in between
    var x=this;
    x=x.replace(/^\s*(.*)/, "$1");
    x=x.replace(/(.*?)\s*$/, "$1");
    return x;
}

function trimWS(item) {
    var text = item.value;
    text = text.replace(/^\s*(.*)/, "$1");
    text = text.replace(/(.*?)\s*$/, "$1");
    item.value = text;
}

function validate() {
    var fname = document.getElementById('firstname').value.trim();
    var lname = document.getElementById('lastname').value.trim();
    var email = document.getElementById('email').value.trim();
    var emailPattern = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
    var emailMatch = email.match(emailPattern);
    var phone1 = document.getElementById('phone1').value.trim();
    var state = document.getElementById('state').value.trim();
    var zip = document.getElementById('zip').value.trim();
    var password = document.getElementById('password').value.trim();
    var conf_password = document.getElementById('conf_password').value.trim();
//    var d_lead = document.getElementById('debt');
//    var t_lead = document.getElementById('tax');
    var terms = document.getElementById('terms');
    var message = "Please check the following:\n\n";
    var returnVal = true;
    
    if (fname == '') {
        message += "Broker's First Name is required\n";
        returnVal = false;
    }

    if (lname == '') {
        message += "Broker's Last Name is required\n";
        returnVal = false;
    }
    
    if (email == '') {
        message += "Broker's Email is required\n";
        returnVal = false;
    } else if (emailMatch == null) {
        message += "Invalid email address\n";
        returnVal = false;
    }
    
    if (phone1 == '') {
        message += "Broker's Phone Number is required\n";
        returnVal = false;
    } else if (phone1.length < 10) {
        message += "Primary phone is required and must contain the area code\n";
        returnVal = false;
    }
    
    if (state == '-1') {
        message += "Broker's State is required\n";
        returnVal = false;
    }
    
    if (zip == '') {
        message += "Broker's Zipcode is required\n";
        returnVal = false;
    }

    if (password == '') {
        message += "A password is required\n";
        returnVal = false;
    }

    if (password != conf_password) {
        message += "Password does not match confirmation\n";
        returnVal = false;
    }
    
//    if (!d_lead.checked && !t_lead.checked) {
//        message += "Please select the type of leads you want to refer\n";
//        returnVal = false;
//    }
    
    if (!terms.checked) {
        message += "You must agree to the Broker Agreement\n";
        returnVal = false;
    }
    
    if (!returnVal) {
        alert(message);
    }

    return returnVal;
}

function stringFilter (input) {
    s = input.value;
    filteredValues = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz()- !@#$%^&*\"?/><,.~_+=|\\'`{}[]:;";     // Characters stripped out
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
        var c = s.charAt(i);
        if (filteredValues.indexOf(c) == -1) returnString += c;
    }
    input.value = returnString;
}

function stringFilter2 (input) {
    s = input.value;
    filteredValues = "\""     // Characters stripped out
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
        var c = s.charAt(i);
        if (filteredValues.indexOf(c) == -1) returnString += c;
    }
    input.value = returnString;
}

function checkHours(){

	var t = new Date();

	// GMT version of total minutes today
	var utc_total_min_today = (t.getUTCHours()*60) + t.getUTCMinutes();
	var est_timezone_offset = 240;

	/*
	Hours of Operation - TCs
	Monday - Thursday:  9:15 AM - 9:45 PM
	Friday:             9:15 AM - 5:45 PM
	Friday:             9:15 AM - 4:15 PM (summer hours) June 21 - Sept 23
	*/

	var mon_to_thr_open = (9*60) + 15;
	var mon_to_thr_close = (21*60) + 45;

	var fri_open = (9*60) + 15;
	var fri_close = (17*60) + 45;

	var fri_open_summer = (9*60) + 15;
	var fri_close_summer = (16*60) + 15;

	// mmdd format used in summer date comparison
	var today   = (t.getMonth()+1)*100 + t.getDate();
	var june_21 = 621;
	var sept_23 = 923;

	if(t.getDay() >= 1 && t.getDay() <= 4){  // Monday - Thursday
		if(utc_total_min_today >= mon_to_thr_open + est_timezone_offset &&
		   utc_total_min_today <= mon_to_thr_close + est_timezone_offset){

			return true;
		}
	}

	if(t.getDay() == 5){  // Friday
		if(today < june_21 || today > sept_23){  // not summer
			if(utc_total_min_today >= fri_open + est_timezone_offset &&
			   utc_total_min_today <= fri_close + est_timezone_offset){

				return true;
			}
		}else{  // summer
			if(utc_total_min_today >= fri_open_summer + est_timezone_offset &&
			   utc_total_min_today <= fri_close_summer + est_timezone_offset){

				return true;
			}
		}
	}
	return false;
}
