var OneClick_js = 1; // "register" this file with calling code
var PropertyTypeSelection = "";
var BoroughSelection = "";
function PickCity(widget,cityName,zipName){
  var selected = widget.selectedIndex,
	    form = widget.form,
			CityAry;
  if (selected == 0){
	alert("Please Select a City");
	ClearHeader();
	return false;
  }
  CityAry = GeoCapitalize(GeoCities[widget.options[selected].value]);
  if (CityAry[0][1] == '0')
		CityAry = CityAry.slice(1);
  //makeSelectList(document.getElementById('property_city'),CityAry,['Select One']);
  makeSelectList(form[cityName],CityAry,['Select One']);
  //makeSelectList(document.getElementById('property_zip'),[],['Zip Codes']);
	clearSelectList(form[zipName]);
}
function AutoPickCityList(state,borough){
  var state_code = GeoStates[state],CityAry;
  var county_ary = GeoCounties[state_code];
  var county_code = null;
  for (var i = 0; i < county_ary.length; i++){
	if(county_ary[i][0] == borough){
	  county_code = county_code[i][1];
	  break;
	}
  }
  if(!county_code){
	alert('AutoPickCityList: could not find city list for state:\n' +
		  state + ", borough: " + bourough);
	return false;
  }
  CityAry = GeoCities[county_code];
  makeSelectList(document.getElementById('property_city'),CityAry,['Select One']);
  makeSelectList(document.getElementById('property_zip'),[],['Zip Codes']);
  return true;
}
//========================================================================================
function BoroughList(slist,eleCounties,eleCities,eleZipCodes){
	var form = slist.form;
	var ndx = slist.selectedIndex,
			hdr = ["-------"],
			options = [];
	if(ndx != 0){
		options = GeoCapitalize(GeoCounties[slist.options[ndx].value]);
		hdr = ["Select A Borough"];
		}
	makeSelectList(form[eleCounties],options,hdr,-1);
	clearSelectList(form[eleCities]);
	clearSelectList(form[eleZipCodes]);
	}
//========================================================================================
// Use for MULTIPLE FORMS
//========================================================================================
function MLoadBoroughs(state,borough,form_number,max_form_ndx,expanded){
	var states,CityAry,code,ddStates,boroughs,val,ddCities,borough_code,zip_list,
			stateHdr=["Select A State"],
			elements = document.forms[form_number].elements;
	if(form_number > max_form_ndx){
		return true;   // debug if necessary
		}
	if(state){
		if(borough){
			borough_code = GetCountyID(state,borough);
			CityAry = GeoCapitalize(GeoCities[borough_code]);
			ddCities = elements["property_city"];
			if (CityAry[0][1] == '0')
				CityAry = CityAry.slice(1);
			makeSelectList(ddCities,CityAry,[['Select City','00']]);
			}
		else{
			code = GetStateID(state);
			ddStates = elements.property_state;
			states = [["Alaska","02"],["Washington","53"]]
			if(expanded)
				makeSelectList(ddStates,states,stateHdr,-1);
			else
				makeSelectList(ddStates,new Array(new Array(state,code)),stateHdr,-1);
			makeSelectList(elements.property_county,[],["-----------"])
			boroughs = GeoCapitalize(GeoCounties[code]);
			ddCities = elements.property_city;
			if (TypeOf(val) == 'undefined'){
				makeSelectList(ddCities,[],['Select One']);
				}
			else if(val){
				if (CityAry[0][1] == '0') CityAry = CityAry.slice(1);
				makeSelectList(ddCities,CityAry,['Select City']);
				}
			else{
				makeSelectList(ddCities,[],['------']);
				}
		zip_list = elements.property_zip;
		if(zip_list) makeSelectList(zip_list,['*****'],['Zip Codes']);
			return true;
			}
		}
	else{
		alert('no state argument. Need deployment for this!');
		}
	}
//========================================================================================
function LoadBoroughs(state,borough){
  var code = GetStateID(state);
  var boroughs = GeoCounties[code];
  var val = makeSelectList(document.getElementById('county'),boroughs,['Select One'],borough);
  if (TypeOf(val) == 'undefined'){
	makeSelectList(document.getElementById('property_city'),[],['Select One']);
  }
  else if(val){
	CityAry = GeoCities[val];
	if (CityAry[0][1] == '0')
	  CityAry = CityAry.slice(1);
	makeSelectList(document.getElementById('property_city'),CityAry,['Select City']);
  }
  else 
	makeSelectList(document.getElementById('property_city'),[],['------']);
  var zip_list = document.getElementById('property_zip');
  if(zip_list) makeSelectList(zip_list,['*****'],['Zip Codes']);
  return true;
}
function ListZipCodes(src_ID,dst_ID){
  var src = IDget(src_ID);
  var selected = src.selectedIndex;
  var ValueSelected = src.options[selected].value;
  makeSelectList(IDget(dst_ID),GeoCityCodes[ValueSelected],new Array('Zip Code'));
  return true;
}
// GetBorough, GetPropertyType, EstimatorAddon, should be wrapped into one object.
function GetBorough(slist){
  var selected = slist.selectedIndex;
  var ValueSelected = slist.options[selected].value;
  BoroughSelection = ValueSelected;
  EstimatorAddon();
}
// BoroughSelection = "170@02" PropertyTypeSelection = "SingleFamily"
function GetPropertyType(slist){
  var selected = slist.selectedIndex;
  var ValueSelected = slist.options[selected].value;
  PropertyTypeSelection = ValueSelected;
  EstimatorAddon();
}
function EstimatorAddon(){
  var chk = '<input type="checkbox"  name="ezEstimator" ' + 
            'value="49.00" default="49.00" onclick="SumPackageTotals(this,'
            + '\'form2\')" tabindex="20" ID="ezEstimator">$ 49.00&nbsp;' 
			+ '&nbsp;<font color="#333333" size="3">"1ezEstimator&trade;"' 
			+ '</font><br>';
  alert('Borough:' + BoroughSelection + " Property type: " + PropertyTypeSelection);
  if(PropertyTypeSelection == "SingleFamily" && BoroughSelection == "170@02"){
    document.getElementById('EstimatorAddon').innerHTML = chk;
  } else{
    document.getElementById('EstimatorAddon').innerHTML = "";
  }
}
function ListZipCodesForCity(slist,zipName){
	var selected = slist.selectedIndex,
			ValueSelected = slist.options[selected].value,
			form = slist.form;
  //makeSelectList(document.getElementById('property_zip'),GeoCityCodes[ValueSelected],new Array('Zip Code'));
  makeSelectList(form[zipName],GeoCityCodes[ValueSelected],new Array('Zip Code'));
  return true;
}
function GetStateID(state){
  for(var i = 0; i < GeoStates.length; i++){
	if (GeoStates[i][0] == state)
	  return GeoStates[i][1]
  }
  alert('No state ID was found in GetStateID!')
}
function GetCountyID(state,county_){
  var county = county_.toUpperCase();
  var statecode = GetStateID(state);
  var key;
  counties = GeoCounties[statecode];
  for(var i = 0; i < counties.length; i++){
	key = counties[i][0];
	if(key == county)
	  return counties[i][1];
  }
}
function ListCitiesInBorough(slist){
  //city1 
  var selected = slist.selectedIndex;
  if (selected == 0){
	alert("Please Select a City");
	return false;
  }
  //var ValueSelected = slist.options[selected].value;
  ary = GeoCities[slist.options[selected].value];
  if (ary[0][1] == '0')
	ary = ary.slice(1)
  makeSelectList(document.getElementById('city1'),ary,new Array('Select One'));
}
function SubscriptionInfo1(sel){
  var selected = sel.selectedIndex;
  var base_amount = '35.00';
  var ValueSelected = sel.options[selected].value;
  var TextSelected = sel.options[selected].text;
  if(ValueSelected){
	var target = document.getElementById('total_charge');
	var tmp = eval(base_amount + " + " + ValueSelected);
	target.value = tmp + '.00';
	tmp = TextSelected.split('...');
	tmp = tmp[1].split('=');
	tmp = tmp[0].split(',')
    tmp = tmp[0];
	target = IDget('subscript_len');
	target.removeAttribute('readOnly');
	target.value = trim(tmp);
	target.setAttribute('readOnly','true');
  }
}
// This function should be wrapped into a with TotalCharge attributes, widget arguments checked for types
//  and calculations based on the type of object. All widget arguments should be expected to have a 'value'
//  attribute and widget.checked should evaluate to false if not defined.
//  see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/in_Operator
//  for 'in' operator and 
function SumPackageTotals(widget,formName){
  //alert('widget name: [' + widget.name + "] value: [" + widget.value + "] Checked: [" + widget.checked + "]"); //debug
  //var _type = TypeOf(ischecked);
  //alert("_type: " + _type);  // DEBUG GLOBAL
  //if(widget.checked) alert('checked');
  //else alert('unchecked');
  var tbl = getParentTable(widget,"TABLE"); // outer table object
  var ShippingFee = '0';
  var SubscriptFee = '0';  // default if not checked
  var SubscriptLen = '';
  var checked;
  var form = document[formName];
  var Fees = form.subscript_fee;
  var Fee;
  SubscriptFee = Fees.options[Fees.selectedIndex].value;
  SubscriptLen = Fees.options[Fees.selectedIndex].text;
  SubscriptLen = SubscriptLen.split(' ');
  SubscriptLen = SubscriptLen[2];
  //  var tmp = SubscriptFee.split(' ');
  //  SubscriptFee = tmp[0];
  //  SubscriptLen = tmp[1];
  //  alert('SubscriptLen: ' + SubscriptLen); //debug
  // Find out if Featured Showcase is chosen
  var ShowCaseFee = '0';
  var FeaturedShowcase = form.featured_showcase;
  if(FeaturedShowcase.checked){
	ShowCaseFee = FeaturedShowcase.value;
  }
  // Find out if Custom Sign is chosen
  var CustomSignFee = '0';
  var CustomSign = form.custom_sign;
  // Find out if estimator is chosen
  var Estimator = form.ezEstimator;
  var EstimatorCharge = '0';
  if(Estimator.checked){
  	EstimatorCharge = Estimator.value;
	//alert("EstimatorCharge: " + EstimatorCharge);  // DEBUG GLOBAL
  	} 
  if(CustomSign.checked){
	CustomSignFee = CustomSign.value;
	ShippingFee = CustomSignFee;
	PrintTo('SignPhoneTargetLabel','<font color="red">*</font>Phone # for sign');
	PrintTo('SignPhoneTargetFormat','(555-234-4567)');
	}
  else{
    ShippingFee = '0';
	PrintTo('SignPhoneTargetLabel','');
	PrintTo('SignPhoneTargetFormat');
	}
  // shipping fee is a constant
	//     alert('SubscriptFee ' + SubscriptFee + "\nShowCaseFee " + ShowCaseFee + "\nCustomSignFee " + CustomSignFee
	//       	+ "\nShippingFee " + ShippingFee);
  //var TotalString = SubscriptFee + " + " + ShowCaseFee + " + " + CustomSignFee + " + " + ShippingFee + ";";
  var TotalString = SubscriptFee + " + " + ShowCaseFee + " + " + CustomSignFee + " + " + EstimatorCharge + ";";
  //alert('TotalString: ' + TotalString); //debug
  var Total = String(eval(TotalString));
  tmp = Total.split('.');
  if(tmp.length == 1){Total = Total + '.00'}
  //alert('Total: ' + Total); //debug
  // Write the subscription length
  var target = form.subscript_len;
  target.removeAttribute('readOnly');
  target.value = SubscriptLen;
  target.setAttribute('readOnly','true');
  // Write the Shipping fee
  target = form.shipping_charge;
  target.removeAttribute('readOnly');
  target.value = trim(String(ShippingFee));
  target.setAttribute('readOnly','true');
  // Write the total charg
  target = form.total_charge;
  target.removeAttribute('readOnly');
  target.value = Total;
  target.setAttribute('readOnly','true');
}
function SubscriptionInfo(sel){
  var selected = sel.selectedIndex;
  var base_amount = '00.00';
  var ValueSelected = sel.options[selected].value;
  var TextSelected = sel.options[selected].text;
  if(ValueSelected){
	var target = document.getElementById('total_charge');
	if(ValueSelected > 1){
		target.value = ValueSelected;
		tmp = TextSelected.split('.');
		target = IDget('subscript_len');
		target.removeAttribute('readOnly');
		target.value = trim(tmp[0]);
		target.setAttribute('readOnly','true');
	}else{
	  target.value = '00.00';
		target = IDget('subscript_len');
		target.removeAttribute('readOnly');
		target.value = '90 Days';
		target.setAttribute('readOnly','true');
	}
  }
}
function RenewalInfo(sel){
  var selected = sel.selectedIndex;
  var ValueSelected = sel.options[selected].value;
  var TextSelected = sel.options[selected].text;
  if(ValueSelected){
	tmp = TextSelected.split('--');
    tmp = tmp[0];
	target = IDget('subscript_len');
	target.value = trim(tmp);
  }
}
function AddInto(sel,val_target,text_target,base){
  var selected = sel.selectedIndex;
  var base_amount = base;
  var ValueSelected = sel.options[selected].value;
  var TextSelected = sel.options[selected].text;
  var tmp;
  if(ValueSelected){
	var target = document.getElementById(val_target);
	if(base){
	  tmp = eval(base_amount + " + " + ValueSelected);
	  target.value = tmp + '.00';
	}
	else target.value = ValueSelected;
	tmp = TextSelected.split('...');
	tmp = tmp[1].split('=');
	tmp = tmp[0].split(',')
    tmp = tmp[0];
	target = IDget(text_target);
	target.value = trim(tmp);
  }
}
function GetGoogleMapLatLong(form){
  form.LatLong.value = document.getElementById("map_location").innerHTML;
}
