function agreement_onload() {

	var f;

	if(f = document.getElementById('fname')) {
	} else if((f = document.getElementById('s_fname')) && (!f.disabled) && (!document.forms.oa_form.spouse.checked)) {
	} else if((f = document.getElementById('st_addr')) && ((!f.value) || !(document.getElementById('mort_comp')))) {
	} else if(f = document.getElementById('mort_comp')) {
	}

	f.focus();

}

/* Billing to Shipping */
function copyInfo(frm,load) {

	frm.ship.checked = load?1:frm.ship.checked;

	var tog = frm.ship.checked?"none":"block";

	frm.st_addr_ship.style.display = tog;
	frm.city_name_ship.style.display = tog;
	frm.state_name_ship.style.display = tog;
	frm.zipcode_ship.style.display = tog;

	getLabelForId("st_addr_ship").style.display = tog;
	getLabelForId("city_name_ship").style.display = tog;
	getLabelForId("state_name_ship").style.display = tog;
	getLabelForId("zipcode_ship").style.display = tog;

	/*document.getElementById('ship_label').style.display = tog;*/

	if(frm.ship) {
		if(frm.ship.checked) {
			//frm.st_addr_ship.value = frm.st_addr.value;
			frm.st_addr_ship.disabled = frm.ship.checked;
			frm.st_addr_ship.style.backgroundColor='#ccc';
			//frm.city_name_ship.value = frm.city_name.value;
			frm.city_name_ship.disabled = frm.ship.checked;
			frm.city_name_ship.style.backgroundColor='#ccc';
			//frm.state_name_ship.value = frm.state_name.value;
			frm.state_name_ship.disabled = frm.ship.checked;
			frm.state_name_ship.style.backgroundColor='#ccc';
			//frm.zipcode_ship.value = frm.zipcode.value;
			frm.zipcode_ship.disabled = frm.ship.checked;
			frm.zipcode_ship.style.backgroundColor='#ccc';
		}
		if(!frm.ship.checked) {
			//frm.st_addr_ship.value = "";
			frm.st_addr_ship.disabled = frm.ship.checked;
			frm.st_addr_ship.style.backgroundColor='#fff';
			//frm.city_name_ship.value = "";
			frm.city_name_ship.disabled = frm.ship.checked;
			frm.city_name_ship.style.backgroundColor='#fff';
			//frm.state_name_ship.selectedIndex = 0;
			frm.state_name_ship.disabled = frm.ship.checked;
			frm.state_name_ship.style.backgroundColor='#fff';
			//frm.zipcode_ship.value = "";
			frm.zipcode_ship.disabled = frm.ship.checked;
			frm.zipcode_ship.style.backgroundColor='#fff';
		}
	}
}

function checkShip(frm, id) {
	if(frm.ship) {
		if (!frm.ship.checked) {
			checkRequired(id);
			if(checkRequired(id) == false) {
				return false;
			}
		}
	}
}

function checkSpouse(frm, id) {
	if(frm.spouse) {
		if (!frm.spouse.checked) {
			if(id == 's_ssn') {
				checkSSN(id);
				if(checkSSN(id) == false) {
					return false;
				}
			} else {
				checkRequired(id);
				if(checkRequired(id) == false) {
					return false;
				}
			}
		}
	} else {
		return true;
	}
}

function disabletx(frm,load) {

	var foo;

	if((foo = document.getElementById("errors"))) {
		if(foo.innerHTML.indexOf("Main app denied")>=0) {
			frm.spouse.checked = 0;
			frm.spouse.style.display = "none";
			getLabelForId("spouse").style.display="none";
		}
	}

	var tog = frm.spouse.checked?"none":"block";

	frm.s_fname.style.display = tog;
	frm.s_lname.style.display = tog;
	frm.s_ssn.style.display = tog;
	getLabelForId('s_fname').style.display = tog;
	getLabelForId('s_lname').style.display = tog;
	getLabelForId('s_ssn').style.display = tog;

	if(frm.spouse.checked) {

		frm.s_fname.disabled = frm.spouse.checked;
		frm.s_fname.value = " ";
		//checkRequired('s_fname');
		frm.s_fname.style.backgroundColor='#ccc';
		frm.s_lname.disabled = frm.spouse.checked;
		frm.s_lname.value = " ";
		//checkRequired('s_lname');
		frm.s_lname.style.backgroundColor='#ccc';
		frm.s_ssn.disabled = frm.spouse.checked;
		frm.s_ssn.value = " ";
		//checkRequired('s_ssn');
		frm.s_ssn.style.backgroundColor='#ccc';

	}
	if(!frm.spouse.checked) {
		frm.s_fname.disabled = frm.spouse.checked;
		frm.s_fname.value = "";
		frm.s_fname.style.backgroundColor='#fff';
		frm.s_lname.disabled = frm.spouse.checked;
		frm.s_lname.value = "";
		frm.s_lname.style.backgroundColor='#fff';
		frm.s_ssn.disabled = frm.spouse.checked;
		frm.s_ssn.value = "";
		frm.s_ssn.style.backgroundColor='#fff';
	}
}


function OA() {
	if (
	checkRequired('fname') == false ||
	checkRequired('lname') == false ||
	checkSSN('ssn') == false ||
	checkSpouse(document.oa_form, 's_fname') == false ||
	checkSpouse(document.oa_form, 's_lname') == false ||
	checkSpouse(document.oa_form, 's_ssn') == false ||
	checkRequired('st_addr') == false ||
	checkRequired('city_name') == false ||
	checkRequired('state_name') == false ||
	checkRequired('zipcode') == false ||
	checkPhone('phone1') == false ||
	checkPhone('phone2') == false ||
	checkEmail('email_addr') == false ||
	checkShip(document.oa_form, 'st_addr_ship') == false ||
	checkShip(document.oa_form, 'city_name_ship') == false ||
	checkShip(document.oa_form, 'state_name_ship') == false ||
	checkShip(document.oa_form, 'zipcode_ship') == false ||
	checkRequired('police') == false ||
	checkPhone('police_phone') == false ||
	checkRequired('fire') == false ||
	checkPhone('fire_phone') == false ||
	checkRequired('cross_st') == false ||
	checkRequired('ec1_name') == false ||
	checkPhone('ec1_phone1') == false ||
	checkPhone('ec1_phone2') == false ||
	checkRequired('pass1') == false ) {
		return false;
	} else {
		return true;
	}
}

function SubmitAgreement() {
	if(OA()) {
		document.oa_form.submit();
	}
}

function OA_Ini() {
	if (
	checkCheckBoxes('read') == false ||

	checkRequired('ini_term') == false ||
	checkRequired('ini_renew') == false ||
	checkRequired('ini_mon_fee') == false ||
	checkRequired('ini_fin_dis') == false ||
	checkRequired('ini_contract') == false ||
	checkRequired('ini_backup') == false ||
	checkRequired('ini_cancel') == false ||
	checkRequired('ini_permit') == false ||
	checkRequired('ini_phone') == false ||
	checkRequired('ini_dsl') == false ||
	checkRequired('ini_liability') == false ||
	checkRequired('ini_rapid_act') == false ||
	document.oa_form.legal_name.disabled == true ||
	checkRequired('legal_name') == false ||
	checkRequired('threedayf') == false ||
	checkRequired('sign_floorplan') == false) {
		return false;
	} else {
		return true;
	}
}

function SubmitFinal() {
	if(OA_Ini()) {
		document.oa_form.submit();
	}
}

//FUNCTIONS FOR THE SMOKE STATE STUFF ON OA_FORM2.php

function checkSmoke() {

	var ele, smokebox;

	var smokeqty = document.forms.oa_form_tot.product_6;

	smokeqty = smokeqty.value?parseInt(smokeqty.value):0;

	//default heatqty to 0
	var heatqty = 0;
	if(document.forms.oa_form_tot.product_7) {
		heatqty = document.forms.oa_form_tot.product_7;
		heatqty = heatqty.value?parseInt(heatqty.value):0;
	}

	var val = smokeqty + heatqty;

	//alert(val);

	if(val >= 1) {
		if(!(smokebox = document.getElementById('smokebox'))) {
			smokebox = createSmokeBox();
		}
		//alert("Updating the box.");
		//updateSmokeBox();

	} else {
		if(smokebox = document.getElementById('smokebox')) {
			//alert("Hiding");
			$(smokebox).hide();
		} else {
			smokebox = createSmokeBox();
		}
	}

	updateSmokeBox();

}

function updateSmokeBox() {

	var smokeqty = document.forms.oa_form_tot.product_6;

	smokeqty = smokeqty.value?parseInt(smokeqty.value):0;

	//default heatqty to 0
	var heatqty = 0;
	if(document.forms.oa_form_tot.product_7) {
		heatqty = document.forms.oa_form_tot.product_7;
		heatqty = heatqty.value?parseInt(heatqty.value):0;
	}

	var val = smokeqty + heatqty;

	var ele = document.forms.oa_form_tot.product_6;
	var smokeform = document.getElementById('smokebox');
	var good = 0;

	var radios = document.getElementsByTagName('input');

	var splitval = -1;

	if(radios) {
		for(var i = 0; i < radios.length; i++) {
			if(radios[i].type == 'radio' && radios[i].checked && radios[i].name == 'smoke_split') {
				splitval = radios[i].value;
				break;
			}
		}
	}

	//var numval = document.forms.oa_form_tot.product_6.value + document.forms.oa_form_tot.product_7.value;
	var numval = val; //no need to repeat this, we get it earlier

	var floorval = ($("#smoke_floors").get(0) ? $("#smoke_floors").get(0).value : 0);

	var zipcode = $("#smoke_zip").get(0) ? $("#smoke_zip").get(0).value : 0;

	$.post('/scripts/smokeajax.php',
	{ smoke_num: numval
	, smoke_floors: floorval
	, smoke_split: splitval
	, smoke_zip: zipcode
	, profile: "agreement"
	, quantity: val
	},
	function(xml) {

		var error,msg;

		error = xml.split('|')[0];
		msg = xml.split('|')[1];

		//alert(xml + ":" + error + ":" + msg);

		if(msg == "clear") return;

		//alert(error + "\n" + msg);

		if(error) {
			$("#smokebox").hide();
			//alert("Error Code: " + error + "\nError Message: " + msg);
		} else {
			$("#smokebox").html("");
			$("#smokebox").show();
			$("#smokebox").append(msg);

			TB_init();

		}

	});

}

//END SMOKE STATE STUFF

function checkScrollPosition(div, offset) {
	var frm;
	if(frm = document.forms.oo_form) {
		frm = document.forms.oo_form;
	} else {
		frm = document.forms.oa_form;
	}

	if( div.scrollHeight < (div.scrollTop + div.offsetHeight)) {
		//div.style.scrollbarTrackColor="green";
		div.style.backgroundColor="#8dfc97";
		setInputs(div, false, offset); }
		if( div.scrollHeight > (div.scrollTop + div.offsetHeight)) {
			div.style.backgroundColor="pink";
			//div.style.scrollbarTrackColor="red";
			setInputs(div, true, offset);
			return true; }
}

function setInputs(div, val, offset) {
	var frm;
	if(frm = document.forms.oo_form) {
		frm = document.forms.oo_form;
	} else {
		frm = document.forms.oa_form;
	}
	if(document.getElementById('scrolldiv')) {
		div = (div ? div : document.getElementById('scrolldiv'));
		for(var i=offset; i<frm.elements.length; i++) {
			frm.elements[i].disabled=val;
			if(val == true) {
				div.style.backgroundColor="pink";
				//div.style.scrollbarTrackColor="red";
				frm.elements[i].style.backgroundColor='#ccc';
			} else {
				frm.elements[i].style.backgroundColor='#fff';
				//frm.elements[i].style.backgroundColor='';
			}
		}
		return val;
	}
}

function checkScrollPosition2(div, offset) {

	var frm;
	if(frm = document.forms.oo_form) {
		frm = document.forms.oo_form;
	} else {
		frm = document.forms.oa_form;
	}


	if(frm.read) {

		div = (div ? div : document.getElementById('scrolldiv'));
		checked = frm.read.checked;

		if( div.scrollHeight < (div.scrollTop + div.offsetHeight) || (checked)) {
			div.style.backgroundColor="#8dfc97";
			div.scrollTop = div.scrollHeight;
			setInputs(div, false, offset);
		}
		if( div.scrollHeight > (div.scrollTop + div.offsetHeight) && !(checked)) {
			div.style.backgroundColor="pink";
			//div.style.scrollbarTrackColor="red";
			setInputs(div, true, offset);
			return true;
		}
	}
}

function flagCheckbox(id, arg1) {  //function which sets hidden field 'flags' for the checkbox elements in order to correctly process the returns
	obj = document.getElementById(id);
	hidden = document.getElementById(id + "_flag");

	eval(arg1); //second parameter should be a function that will be called when the box is clicked

	if(hidden.value == 1) {
		hidden.value = 0;
	} else {
		hidden.value = 1;
	}
}

function none() {
	return 0;
	//dummy function to be used as a placeholder
}

function ContactFormValidation()
{
	if (checkRequired('c_name') == false ||
	checkEmail('c_email_addr') == false) {
		return false;
	} else {
		return true; }
}

function FeedbackSubmit() {
	if(ContactFormValidation()) {
		document.feedback.submit();
	}
}

function switchPaytype() {
	//payment_op_account
	//payment_op_credit

	var pay_type = document.forms['oa_form_type'].pay_type;

	var type = pay_type.options[pay_type.selectedIndex].value;

	switch(type) {
		case "checking":
		case "savings":
		$("#payment_op_account").show();
		$("#payment_op_credit").hide();
		break;
		case "credit":
		$("#payment_op_account").hide();
		$("#payment_op_credit").show();
		break;
		default:
		$("#payment_op_account").hide();
		$("#payment_op_credit").hide();
		break;
	}

}

//adds in asynchronous updating of products on oa_form1
$(document).ready(updateProductsOnBlur);

function updateProductsOnBlur() {
	$("input.product_quantity").each(
	function(i) {
		$(this).change(updateProducts);
	}
	);
}
function updateProducts(evt) {
	//evt is the event object.  we can get the element the event was bound to by getting evt.target
	//post the product ID and the quantity to /scripts/update_products.php

	var ele = evt.target;

	var prod_id = ele.name.substring("product_".length);
	var qty = ele.value;
	
	if(qty != '0') {
		$(ele).parent().parent().removeClass("not-selected");
	} else {
		$(ele).parent().parent().addClass("not-selected");
	}
	
	$.getJSON(
	"/agreement/scripts/update_products.php",
	{
		product_id: prod_id,
		qty: qty
	},
	processUpdate
	);

}

function processUpdate(data) {
	//data is a json object returned by updated_products.php

	//here we have to update the prices on our selected item, as well as the totals

	/*console.group("-- Product Update --");
	console.dir(data);
	console.groupEnd();
	
	$("#debug_info").html($("#debug_info").html() + "<hr/><b>Product Update</b><br/>" + data.messages);*/
	
	if (data.toomanyzones) {
		$('#zonemsg').show();
	} else {
		$('#zonemsg').hide();
	}
		
	//update the cost on the item we updated first
	$("#prod_tot_"+data.product.id).html(formatMoney(data.product.cost,1));

	//update the additional equipment total
	$("#eq_cost").html(formatMoney(data.equip_total,1));

	//add promo line items if applicable
	updatePromotionLineItems(data.promos);

	//update the final total. basically we take the number, subtract the previous extra equip total, then add the new extra equip total
	//var final_total = $("#final_total").html().trim().substring(1);

	/*var flmon, shipping, init_price, deposit, permit = 0;

	if($("#flmon").get(0)) {
		flmon = parseFloat($("#flmon").html().trim().substring(1));
	}

	if($("#shipping_div").get(0)) {
		shipping = parseFloat($("#shipping_div").html().trim().substring(1));
	}

	if($("#initprice").get(0)) {
		init_price = parseFloat($("#initprice").html().trim().substring(1));
	}
	
	if($("#deposit").get(0)) {
		deposit = parseFloat($("#deposit").html().trim().substring(1));
	}
	
	if($("#add_per").get(0)) {
		permit = parseFloat($("#add_per").html().trim().substring(1));
	}

	$("#final_total").html(formatMoney(flmon + shipping + init_price + data.equip_total + deposit + permit - parseFloat(data.promos.promotion_discount_total),1));*/
	
	$("#final_total").html(formatMoney(data.total));
	
}

function updatePromotionLineItems(list) {
		
	//first, remove all current line items
	$(".adj_line_item").remove();

	//now, add in all of the new ones
	if(list.count > 0) {
		list.adjustments.foreach(function(self) {
			$("#promo_anchor").before("<tr class=\"color3 adj_line_item\" id=\"adj_"+self.id+"\"><td headers=\"equipment\" colspan=\"3\" class=\"total red\">" + self.line_item + "</td><td headers='total' class='total red'>"+self.value+"</td></tr>");
		});
	}

}

//updates total on oa_form when shipping price is changed
$(document).ready(updateShipping);

function updateShipping() {
	$("#overnight").change(function() {
		var shipping = $(this).val();
		//shipping is a 0 or a 1
		shipping = 49.95 ? 49.95 : 19.95;

		var shipping_type = $(this).val() == 0 ? "standard" : "overnight";

		//let's post this to the update products script so we can set a session var for the shipping type
		$.getJSON(
		"/agreement/scripts/update_products.php",
		{
			shipping_type: shipping_type
		},
		function(data) {
			
			/*console.group("-- Shipping Update --");
			console.dir(data);
			console.groupEnd();
						
			$("#debug_info").html($("#debug_info").html() + "<hr/><b>Shipping Update</b><br/>" + data.messages);*/
			
			updatePromotionLineItems(data.promos);

			//update shipping cost
			$("#shipping_div").html(formatMoney(data.new_shipping));

			//update the final total
			$("#final_total").html(formatMoney(data.total));
			
		});

	});
}

function showHideCommercialAddress() {
	if ($("#ownership").val() == "Commercial") {
		$("#commercialAddress").show();
	} else {
		$("#commercialAddress").hide();
	}
}