// BROKER OFFICE LOGIN
var bo_login = 'tnefouse@nefouse.com';
var depQty=0;

/* Function called when home page quote form is submitted */
function quoteFormSubmit() {

	// checks that the zip is five digits and begins with 46 or 47
	// if you want to check for different two digit prefixes, just replace the "(46|47)" with e.g. "(12|13|14|15)"
	reZip = new RegExp(/^(46|47)(\d{3}$)|(^\d{3}$)/);
	
	f = document.quoteForm;
	// if the group was selected then send to group.html page with query variable for us on the page
	if (f.quoteType[1].checked) {
			var loc = "quotes/group.html?tfa_ZIPCode=" + f.quoteZip.value;
			window.location = loc;
	}
	// make sure the zip is one of the allowed types
	else if(!reZip.test(f.quoteZip.value)) {
		//if not show an error
		document.getElementById('zipError').innerHTML = "**Please use a valid Indiana ZIP code.";
		return;
	}
	// if the zip was OK, see if Family was checked, and redirect to the individual.html page, adding the zip code as a query variable for use on the page
	else if (f.quoteType[0].checked) {
		var loc = "quotes/individual.php?zip=" + f.quoteZip.value;
		window.location = loc;
	}

}

/* Function called when group mini quote form is submitted */
function quoteGroupFormSubmit() {

	// checks that the zip is five digits and begins with 46 or 47
	// if you want to check for different two digit prefixes, just replace the "(46|47)" with e.g. "(12|13|14|15)"
	regzip = new RegExp(/^(46|47)(\d{3}$)|(^\d{3}$)/);
	
	f = document.groupQuote;
	// make sure the zip is one of the allowed types
	if(!regzip.test(f.quoteZip.value)) {
		//if not show an error
		document.getElementById('zipError').innerHTML = "**Please use a valid Indiana ZIP code.";
		$("#zipError").css("display","block");
		return;
	}
	// if the zip was OK, redirect to the group page, adding the zip code as a query variable for use on the page
	else {
		var loc = "/quotes/group.html?tfa_ZIPCode=" + f.quoteZip.value;
		window.location = loc;
	}

}

/* Function called when individual mini quote form is submitted */
function quoteIndivFormSubmit() {

	// checks that the zip is five digits and begins with 46 or 47
	// if you want to check for different two digit prefixes, just replace the "(46|47)" with e.g. "(12|13|14|15)"
	regzip = new RegExp(/^(46|47)(\d{3}$)|(^\d{3}$)/);
	
	f = document.indivQuote;
	// make sure the zip is one of the allowed types
	if(!regzip.test(f.quoteZip.value)) {
		//if not show an error
		document.getElementById('zipError').innerHTML = "**Please use a valid Indiana ZIP code.";
		$("#zipError").css("display","block");
		return;
	}
	// if the zip was OK, redirect to the individual.html page, adding the zip code as a query variable for use on the page
	else {
		var loc = "/quotes/individual.php?zip=" + f.quoteZip.value;
		window.location = loc;
	}

}

/* Function called when individual quote form is submitted */
function famFormSubmit() {
	
	var failMsg = '';
	var qryStr = 'https://www.brokeroffice.com/quote/prepopulation?login=' + bo_login;

	var zip = window.location.search.substring(1).split("=")[1];
	
	if(!zip)
	{
		if(document.getElementById('app_zip').value!='')
		{
			zip=document.getElementById('app_zip').value;
		}
	}
	qryStr += "&zip=" + zip;
	var d = new Date();
	var curr_date = d.getDate();
	var curr_month = d.getMonth();
	var curr_year = d.getFullYear();

	f = document.famForm;

		
	if(document.getElementById('app_fname').value!='')
	{qryStr += "&firstName=" + removeChars(document.getElementById('app_fname').value)}
	else { failMsg += "<div>Please enter your first name</div>";}
	
	if(document.getElementById('app_lname').value!='')
	{qryStr += "&lastName=" + removeChars(document.getElementById('app_lname').value)}
	else { failMsg += "<div>Please enter your last name</div>"; }
	
	if(document.getElementById('app_email').value!='')
	{qryStr += "&email=" + removeChars(document.getElementById('app_email').value)}
	else { failMsg += "<div>Please enter your email address</div>"; }
	
	if(document.getElementById('app_phone').value!='')
	{qryStr += "&work_phone=" + removeChars(document.getElementById('app_phone').value)}
	else { failMsg += "<div>Please enter your phone number</div>"; }
	
	if(document.getElementById('app_address').value!='')
	{qryStr += "&address1=" + removeChars(document.getElementById('app_address').value)}
	
	if(document.getElementById('app_address2').value!='')
	{qryStr += "&address2=" + removeChars(document.getElementById('app_address2').value)}
		
	if(document.getElementById('app_city').value!='')
	{qryStr += "&city=" + removeChars(document.getElementById('app_city').value)}
		
	if(document.getElementById('app_state').value!='')
	{qryStr += "&state=" + removeChars(document.getElementById('app_state').value)}
	
	if(document.getElementById('app_zip').value=='')
	{ failMsg += "<div>Please enter your zip code</div>"; }
	
	if (f.applicant_gender.selectedIndex != 0) {
					
		var msg = isDate(f.applicant_dobMM.value + "/" + f.applicant_dobDD.value + "/" + f.applicant_dobYYYY.value);
		if (msg != "true") {
			failMsg += "<div>Applicant: " + msg + "</div>";
		}
		
		qryStr += "&applicant_dob=" + f.applicant_dobMM.value + "/" + f.applicant_dobDD.value + "/" + f.applicant_dobYYYY.value;
		qryStr += "&applicant_gender=" + (f.applicant_gender.selectedIndex == 1 ? "Male" : "Female");
		qryStr += "&applicant_smoker=" + (f.applicant_smoker.checked ? "YES" : "");
	}
	else
	{
		if(failMsg=='')
		{
				failMsg = '<div>Please enter applicant information</div>';
		}
	}

	if(f.spouse_gender.selectedIndex != 0) {
		var msg = isDate(f.spouse_dobMM.value + "/" + f.spouse_dobDD.value + "/" + f.spouse_dobYYYY.value);
		if (msg != "true") {
			failMsg += "<div>Spouse: " + msg + "</div>";
		}
		
		qryStr += "&spouse_dob=" + f.spouse_dobMM.value + "/" + f.spouse_dobDD.value + "/" + f.spouse_dobYYYY.value;
		qryStr += "&spouse_gender=" + (f.spouse_gender.selectedIndex == 1 ? "Male" : "Female");
		qryStr += "&spouse_smoker=" + (f.spouse_smoker.checked ? "YES" : "");
	}


	// Calling it dependent2 because of the following instructions, which are confusing
		/*
		 For dependents, you must always start with dependent2, and go up from there, without skipping any.  You don’t have to always include the spouse info. 
		Note:  For dependents, you need to always start with "dependent2" and increase from there to "dependent3, 4, etc.".  
		1 and 2 are always reserved for applicant and spouse even though they are titled "applicant" and "spouse" instead of "dependent1" and "dependent2".*/
		
	var dependentTotal=Number(depQty)+2;
	//alert('depQty: '+depQty+'\n: '+dependentTotal);
	
	for(i=2; i<dependentTotal; i++)
	{
		if(document.getElementById('dependent'+i+'_gender').value!='') {
			var msg = isDate(document.getElementById('dependent'+i+'_dobMM').value + "/" + document.getElementById('dependent'+i+'_dobDD').value + "/" + document.getElementById('dependent'+i+'_dobYYYY').value);
			
			if (msg != "true") {
				failMsg += "<div>Child "+i+": " + msg + "</div>";
			}
			
			
			qryStr += "&dependent"+i+"_dob=" + document.getElementById('dependent'+i+'_dobMM').value + "/" + document.getElementById('dependent'+i+'_dobDD').value + "/" + document.getElementById('dependent'+i+'_dobYYYY').value;
			qryStr += "&dependent"+i+"_gender=" + document.getElementById('dependent'+i+'_gender').value;
		
		}
	}

    $("#errorTD").fadeIn("slow");
	
	document.getElementById('errorTD').innerHTML = failMsg;
	if (failMsg == '') {
		contentHeight = $("#content").height();
		$("#content-loading").css("height",contentHeight/2);
		$("#content-loading").css("padding-top",contentHeight/2);
		document.getElementById('content-loading').style.display='block';
		$(".form-gender").addClass("hideMe");
		//alert(qryStr);
		window.location = qryStr;
	}

}

/* DO NOT CHANGE ANYTHING BELOW --- FUNCTIONS THAT HELP OTHER FUNCTIONS */

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;
	
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return "The date format should be : mm/dd/yyyy";
	}
	if (strMonth.length<1 || month<1 || month>12){
		return "Please enter a valid month";
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return "Please enter a valid day";
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return "Please enter a valid 4 digit year between "+minYear+" and "+maxYear;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return "Please enter a valid date";

	}

	var bDate = new Date();
	bDate.setFullYear(strYear, strMonth, strDay);
	var longTimeAgo = new Date();
	longTimeAgo.setDate(longTimeAgo.getDate() - (365 * 64.5));

	if (longTimeAgo > bDate)
		return "Must be younger than 64 1/2 years old.";
	else
		return "true";


}

function showMoreKids() {

	if(document.getElementById('dependent_div_'+depQty).style.display)
	{
		document.getElementById('dependent_div_'+depQty).style.display='block';
	}
	depQty+=1;
	
	
	if(depQty==childMax)
	{
		document.getElementById('child-div-button').style.display='none';
	}
}

function removeKid() {
    num = depQty - 1;
    $('#dependent_div_'+num).css("display","none");
	$('#dependent_div_'+num+" :input").val("");
    $("#child-div-button").css("display","inline");
    //$("#form-remove-title").text(depQty);
}

function removeChars(str) {
	
	str=str.replace(/#/, "No.");
	return str;
}