/*
	This file contains all functions for the get listed subission page
		1. goToStep()			Proceed throught the steps of the form (show/hide) divs
		2. getplan()			Highlights the plan rates that you are on
		3. getCounties()		When a county is checked off, show it on the right
		4. changeMaxMin()		Updates two hidden fields that contain the min/max counties for a plan
		5. changeTerm()			Updates the hidden field length
		6. submitItOne()		Ensure STEP 1 is validated	

		
*/
// 1. Go to Step
//		Proceed throught the steps of the form (show/hide) divs
////////////////////////////////////////////////

function goToStep(step) {
	var i;
	for (i = 1; i <= 4; i++) {
		
		if (i == step) {
			document.getElementById('step'+i).style.display = 'block';
		} else {
			document.getElementById('step'+i).style.display = 'none';
		}
	}
	
}
// 2. Get relevant plan
//		Highlights the plan rates that you are on
////////////////////////////////////////////////
function getplan(id, total) {
	//alert(id);
	var i;
	document.getElementById('plan_row_'+id).style.backgroundColor = 'white';
	
	for (i = 1; i <= total; i++) {
		// If we find a plan element for this one and if this is not the ID we just turned
		// on, then set it back to blue colour
			if (i != id) {
				document.getElementById('plan_row_'+i).style.backgroundColor = '#e5f0f8';
			}
	}
}

// 3. Counties Listed
//		When a county is checked off, show it on the right
////////////////////////////////////////////////
// This global variable is used to keep track of the number of selected counties
var county_count = 0;
function getCounties(element, id) {
	
	// If this checkbox is checked show it on the list on the right
	// Else if it is not checked, hide is on the right
	if(element.checked)
	{
		document.getElementById('county_'+id).style.display = 'block';
		county_count++;
	} else {
		document.getElementById('county_'+id).style.display = 'none';
		county_count--;
	}
	
	// Validation: If they have exceeded their number of counties, remove it and let them know.
	if (county_count > document.getElementById('max').value) {
		//document.getElementById('county_'+id).style.display = 'none';
		//county_count--;
		//element.checked = false;
		if (county_count == 1) {
			alert('Please choose a subscription length before chosing counties');
		} else {
			alert('You have selected MORE counties than the current subscription plan will allow, please move up a plan to move to STEP 3.');
		}
		
	}
	// Set the text on the page to reflect the number of selected counties
	document.getElementById('count').innerHTML = county_count;
	//alert(county_count);

}

// 4. Change the Min Max counties
// 		Updates two hidden fields that contain the min/max counties for a plan
////////////////////////////////////////////////
function changeMaxMin(countymax, countymin, planid) {
	//alert('Max: ' + countymax + ' Count: ' + county_count);
	if (county_count > countymax) {
		var difference = county_count - countymax;
		alert('You have too many counties to go down a plan, please uncheck ' + difference + ' counties before going on');	
	}
	document.getElementById('max').value = countymax; 
	document.getElementById('min').value = countymin;
	document.getElementById('chosen_minmax').innerHTML = countymin + ' to ' + countymax;
}

// 5. Change the term in years
// 		Updates the hidden field length
////////////////////////////////////////////////
function changeTerm(num) {
	document.getElementById('term_in_years').value = num; 
}

// 6. get listed STEP 1
////////////////////////////////////////////////
function submitItOne() {
	
	if (!document.get_listed.agree_1.checked) {
		alert('You must agree to be a licensed appraiser and will only register for states where I hold current license(s)');
		return false;
	}
	if (!document.get_listed.agree_2.checked) {
		alert('You must posess a current errors and omissions insurance policy for the business being registered');
		return false;
	}
	if (!document.get_listed.agree_3.checked) {
		alert('You must have read and agreed with the AppraiserRegister.com (terms and conditions).');
		return false;
	}
		
	return true;

}


// 7. get listed STEP 2
////////////////////////////////////////////////
function submitItTwo() {

	
	plan_selected = false;
	// loop thorugh all state checkboxes
	for (i = 0; i < document.get_listed.plan.length; i++) {
		if (document.get_listed.plan[i].checked == true)
			plan_selected = true;
	}
	if (plan_selected == false) {
		alert("You must select a plan to continue to STEP 3.");
		return false;
	}
	
	
	county_selected = false;
	// loop thorugh all state checkboxes
	for (i = 0; i < document.get_listed.service_county.length; i++) {
		if (document.get_listed.service_county[i].checked == true)
			county_selected = true;
	}
	if (county_selected == false) {
		alert("You must select at least one county to continue to STEP 3.");
		return false;
	}
	
	var countymax = document.getElementById('max').value;
	if (county_count > countymax) {
		var difference = county_count - countymax;
		alert('You have too many counties to go down a plan, please uncheck ' + difference + ' counties before going on');	
		return false;
	}


	
	
	
	return true;

}


// 8. get listed STEP 3
////////////////////////////////////////////////
function submitItThree() {


	if (document.get_listed.licence_typeid_f.value == "") {
		alert("A license type is required to continue.");
		document.get_listed.licence_typeid_f.focus();
		return false;
	}
	
	if (document.get_listed.license_number.value == "") {
		alert("A license number is required to continue.");
		document.get_listed.license_number.focus();
		document.get_listed.license_number.select();
		return false;
	}
	
	if (document.get_listed.licence_exp_date.value == "") {
		alert("A license expiry date is required to continue.");
		return false;
	}
	
	
	if (!check_expired(document.get_listed.licence_exp_date.value)) {
		
		alert("The date you entered has already expired.");
		document.get_listed.licence_exp_date.focus();
		document.get_listed.licence_exp_date.select();
		return false;
		
	}
	
	
	if (document.get_listed.insurance_carrier.value == "") {
		alert("An Insurance Carrier/Company is required to continue.");
		document.get_listed.insurance_carrier.focus();
		document.get_listed.insurance_carrier.select();
		return false;
	}
	
	
	
	if (document.get_listed.insurance_policy_number.value == "") {
		alert("An Insurance Policy Number is required to continue.");
		document.get_listed.insurance_policy_number.focus();
		document.get_listed.insurance_policy_number.select();
		return false;
	}
	
	if (document.get_listed.policy_amount.value == "") {
		alert("An Insurance Policy Amount is required to continue.");
		document.get_listed.policy_amount.focus();
		document.get_listed.policy_amount.select();
		return false;
	}
	
	
	
	if (document.get_listed.policy_expiration.value == "") {
		alert("An Insurance Policy Expriation Date is required to continue.");
		document.get_listed.policy_expiration.focus();
		document.get_listed.policy_expiration.select();
		return false;
	}
	
	if (!checkdate(document.get_listed.policy_expiration)) {
		document.get_listed.policy_expiration.focus();
		document.get_listed.policy_expiration.select();
		return false;
	
	}
	
	if (!check_expired(document.get_listed.policy_expiration.value)) {
		
		alert("The date you entered has already expired.");
		document.get_listed.policy_expiration.focus();
		document.get_listed.policy_expiration.select();
		return false;
		
	}
	
	
	
	if (document.get_listed.policy_type.value == "") {
		alert("An Insurance Policy Type is required to continue.");
		document.get_listed.policy_type.focus();
		document.get_listed.policy_type.select();
		return false;
	}
	

	if (document.get_listed.company_name.value == "") {
		alert("A Company Name is required to continue.");
		document.get_listed.company_name.focus();
		document.get_listed.company_name.select();
		return false;
	}
	
	if (document.get_listed.company_address.value == "") {
		alert("A Company Address is required to continue.");
		document.get_listed.company_address.focus();
		document.get_listed.company_address.select();
		return false;
	}
	if (document.get_listed.company_city.value == "") {
		alert("A Company City is required to continue.");
		document.get_listed.company_city.focus();
		document.get_listed.company_city.select();
		return false;
	}
	
	if (document.get_listed.company_state.value == "") {
		alert("A Company State is required to continue.");
		document.get_listed.company_state.focus();
		document.get_listed.company_state.select();
		return false;
	}
	
	if (document.get_listed.company_zip.value == "") {
		alert("A Company Zip Code is required to continue.");
		document.get_listed.company_zip.focus();
		document.get_listed.company_zip.select();
		return false;
	}
	
	
	if (document.get_listed.company_phone_area.value == "" || document.get_listed.company_phone_pre.value == "" || document.get_listed.company_phone_post.value == "") {
		alert("A Company Phone Number is required to continue.");
		document.get_listed.company_phone_area.focus();
		document.get_listed.company_phone_area.select();
		return false;
	}

	if (document.get_listed.company_email.value == "") {
		alert("A Company Email is required to continue.");
		document.get_listed.company_email.focus();
		document.get_listed.company_email.select();
		return false;
	}
	
	if (! validate_email(document.get_listed.company_email.value)) {
		document.get_listed.company_email.focus();
		document.get_listed.company_email.select();
		return false;
	}


	if (document.get_listed.contact_fname.value == "") {
		alert("A Contact First Name is required to continue.");
		document.get_listed.contact_fname.focus();
		document.get_listed.contact_fname.select();
		return false;
	}
	
	if (document.get_listed.contact_lname.value == "") {
		alert("A Contact Last Name is required to continue.");
		document.get_listed.contact_lname.focus();
		document.get_listed.contact_lname.select();
		return false;
	}
	
	if (document.get_listed.contact_email.value != "") {
		if (! validate_email(document.get_listed.contact_email.value)) {
			document.get_listed.contact_email.focus();
			document.get_listed.contact_email.select();
			return false;
		}
	}
	
	if (document.get_listed.contact_phone_area.value == "" || document.get_listed.contact_phone_pre.value == "" || document.get_listed.contact_phone_post.value == "") {
		alert("A Contact Phone Number is required to continue.");
		document.get_listed.contact_phone_area.focus();
		document.get_listed.contact_phone_area.select();
		return false;
	}
	
	
	
	service_cat_selected = false;
	// loop thorugh all state checkboxes
	for (i = 0; i < document.get_listed.service_categories.length; i++) {
		if (document.get_listed.service_categories[i].checked == true)
			service_cat_selected = true;
	}
	if (service_cat_selected == false) {
		alert("You must select at least one service category to continue to STEP 4.");
		return false;
	}
	
	service_selected = false;
	// loop thorugh all state checkboxes
	for (i = 0; i < document.get_listed.service_offered.length; i++) {
		if (document.get_listed.service_offered[i].checked == true)
			service_selected = true;
	}
	if (service_selected == false) {
		alert("You must select at least one service offered to continue to STEP 4.");
		return false;
	}
	
	payment_selected = false;
	// loop thorugh all state checkboxes
	for (i = 0; i < document.get_listed.accepted_payments.length; i++) {
		if (document.get_listed.accepted_payments[i].checked == true)
			payment_selected = true;
	}
	if (payment_selected == false) {
		alert("You must select at least type of payment accepted to continue to STEP 4.");
		return false;
	}
	
	
	
	
	if (document.get_listed.turn_around_time.value == "") {
		alert("A tournarond time is required to continue.");
		document.get_listed.turn_around_time.focus();
		return false;
	}
	
	return true;

}

// 9. get listed STEP 4
////////////////////////////////////////////////
function submitIt(form) {


	if (form.billing_address.value == "") {
		alert("A Billing Address is required.");
		form.billing_address.focus();
		form.billing_address.select();
		return false;
	}
	
	if (form.billing_city.value == "") {
		alert("A Billing City is required.");
		form.billing_city.focus();
		form.billing_city.select();
		return false;
	}
	
	if (form.billing_state.value == "") {
		alert("A Billing State is required.");
		form.billing_state.focus();
		return false;
	}
	if (form.billing_zip.value == "") {
		alert("A Billing Zip Code is required.");
		form.billing_zip.focus();
		form.billing_zip.select();
		return false;
	}
	
	return true;
}



// 3. Counties Listed
//		When a county is checked off, show it on the right
////////////////////////////////////////////////
// This global variable is used to keep track of the number of selected counties
function setCost(element, id) {
	
	// If this checkbox is checked show it on the list on the right
	// Else if it is not checked, hide is on the right
	if(element.checked)
	{
		document.getElementById('service_'+id).style.display = 'block';

	} else {
		document.getElementById('service_'+id).style.display = 'none';
	}
	
	

}

// 3. Check Expired
//		Send in a date in at MM/DD/YYYY format at it will return false if it is expired or
//		true if it is valide
////////////////////////////////////////////////
function check_expired(check_date) {
		
		var currentTime = new Date()
		var month = currentTime.getMonth() + 1
		var day = currentTime.getDate()
		var year = currentTime.getFullYear()
		
		var mydate = check_date;
		var firstIndex = mydate.indexOf ("/");
		var lastIndex = mydate.lastIndexOf ("/");
		month1 = mydate.substring (0, firstIndex);
		date1 = mydate.substring (firstIndex+1, lastIndex);
		year1 = mydate.substring (lastIndex+1, mydate.length);
		
		if (month1.substring (0, 1) == 0) {
			month1 = month1.substring(1,2);
		}
		if (date1.substring (0, 1) == 0) {
			date1 = date1.substring(1,2);
		}
		//document.write(' -- ' + month1 + ' ' + date1 + ' ' + year1 + '<br/>');
		//document.write(year + ' vs ' + year1 + '<br/>');
		
		if (year1 < year) {
			//alert('Expired year');
			return false;
		
		} else {
			
			if( year1 <= year && month1 < month) {
				//alert('Expired month');
				return false;
			
			} else {
				if( (year1 <= year && month1 <= month) && date1 < day) {
					//alert('Expired day'+date1);
					return false;
				}
			}
		}
		return true;
	}