function copyAddress(formName) {	
	var c = eval('document.' + formName + '.copy_address');	
	var ba1 = eval('document.' + formName + '.BILLING_ADDRESS_1');
	var ba2 = eval('document.' + formName + '.BILLING_ADDRESS_2');
	var bc = eval('document.' + formName + '.BILLING_CITY');
	var bz = eval('document.' + formName + '.BILLING_ZIP_CODE');
	var bsi = eval('document.' + formName + '.BILLING_STATE_ID');
	var bp1 = eval('document.' + formName + '.BILLING_PHONE_NUMBER_PART_1');
	var bp2 = eval('document.' + formName + '.BILLING_PHONE_NUMBER_PART_2');
	var bp3 = eval('document.' + formName + '.BILLING_PHONE_NUMBER_PART_3');
	var sa1 = eval('document.' + formName + '.shipping_address_1');
	var sa2 = eval('document.' + formName + '.shipping_address_2');
	var sc = eval('document.' + formName + '.shipping_city');
	var szc = eval('document.' + formName + '.shipping_zip_code');
	var ssi = eval('document.' + formName + '.shipping_state_id');
	var sp1 = eval('document.' + formName + '.shipping_phone_1');
	var sp2 = eval('document.' + formName + '.shipping_phone_2');
	var sp3 = eval('document.' + formName + '.shipping_phone_3');
	
	if (c.checked) {
		ba1.value = sa1.value;
				
		if (ba2) {
			ba2.value = sa2.value;
		}
		
		bc.value = sc.value;
		bz.value = szc.value;
		bsi.value = ssi.value;
		bp1.value = sp1.value;
		bp2.value = sp2.value;
		bp3.value = sp3.value;
	} else {
		ba1.value = '';
		
		if (ba2) {
			ba2.value = '';
		}
		
		bc.value = '';
		bz.value = '';
		bsi.value = 12;
		bp1.value = '';
		bp2.value = '';
		bp3.value = '';
	}
}	

function copyAddress2(formName) {
	var c = eval('document.' + formName + '.copy_address');
	var ba1 = eval('document.' + formName + '.address');
	var ba2 = eval('document.' + formName + '.address2');
	var bc = eval('document.' + formName + '.city');
	var bz = eval('document.' + formName + '.zip_code');
	var bsi = eval('document.' + formName + '.state_id');
	var sp1 = eval('document.' + formName + '.phone_1');
	var sp2 = eval('document.' + formName + '.phone_2');
	var sp3 = eval('document.' + formName + '.phone_3');
	var sa1 = eval('document.' + formName + '.from_address_1');
	var sa2 = eval('document.' + formName + '.from_address_2');
	var sc = eval('document.' + formName + '.from_city');
	var szc = eval('document.' + formName + '.from_zip_code');
	var ssi = eval('document.' + formName + '.from_state_id');
	var sf1 = eval('document.' + formName + '.from_phone_1');
	var sf2 = eval('document.' + formName + '.from_phone_2');
	var sf3 = eval('document.' + formName + '.from_phone_3');
	
	if (c.checked) {
		ba1.value = sa1.value;
		
		if (ba2) {
			ba2.value = sa2.value;
		}
		
		bc.value = sc.value;
		bz.value = szc.value;
		bsi.value = ssi.value;
		sp1.value = sf1.value;
		sp2.value = sf2.value;
		sp3.value = sf3.value;
	} else {
		ba1.value = '';
		
		if (ba2) {
			ba2.value = '';
		}
		
		bc.value = '';
		bz.value = '';
		bsi.value = 12;
		sp1.value = '';
		sp2.value = '';
		sp3.value = '';
	}
}

function copyAddress3(formName) {	
	var c = eval('document.' + formName + '.copy_address');	
	var ba1 = eval('document.' + formName + '.BILLING_ADDRESS_1');
	var ba2 = eval('document.' + formName + '.BILLING_ADDRESS_2');
	var bc = eval('document.' + formName + '.BILLING_CITY');
	var bz = eval('document.' + formName + '.BILLING_ZIP_CODE');
	var bsi = eval('document.' + formName + '.BILLING_STATE_ID');
	var bp1 = eval('document.' + formName + '.BILLING_PHONE_NUMBER_PART_1');
	var bp2 = eval('document.' + formName + '.BILLING_PHONE_NUMBER_PART_2');
	var bp3 = eval('document.' + formName + '.BILLING_PHONE_NUMBER_PART_3');
	var sa1 = eval('document.' + formName + '.SHIPPING_ADDRESS_1');
	var sa2 = eval('document.' + formName + '.SHIPPING_ADDRESS_2');
	var sc = eval('document.' + formName + '.SHIPPING_CITY');
	var szc = eval('document.' + formName + '.SHIPPING_ZIP_CODE');
	var ssi = eval('document.' + formName + '.SHIPPING_STATE_ID');
	var sp1 = eval('document.' + formName + '.SHIPPING_PHONE_NUMBER_PART_1');
	var sp2 = eval('document.' + formName + '.SHIPPING_PHONE_NUMBER_PART_2');
	var sp3 = eval('document.' + formName + '.SHIPPING_PHONE_NUMBER_PART_3');
	
	if (c.checked) {
		ba1.value = sa1.value;		
		
		if (ba2) {
			ba2.value = sa2.value;
		}
		
		bc.value = sc.value;
		bz.value = szc.value;
		bsi.value = ssi.value;
		bp1.value = sp1.value;
		bp2.value = sp2.value;
		bp3.value = sp3.value;
	} else {
		ba1.value = '';
		
		if (ba2) {
			ba2.value = '';
		}
		
		bc.value = '';
		bz.value = '';
		bsi.value = 12;
		bp1.value = '';
		bp2.value = '';
		bp3.value = '';
	}
}

function disable_submit(button_id) {
	var el = document.getElementById(button_id);
	
	if (el) {
		el.disabled = true;
	}
}

function popupSeniorApplication(url) {
	window.open(url, '', 'height=800,width=870,scrollbars=yes').focus();
}

function fill_loginbox(val, id) {
	var el1 = document.getElementById('loginbox');
	
	if (el1) {
		if (undefined != val) {
			val = val.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			
			if (val != '') {
				el1.innerHTML = val;
				el1.className = 'loginbox';
				el1.style.display = 'block';
			} else {
				el1.className = '';
				el1.style.display = 'none';
			}
		}
	}
}

// Function to load the login/logout link on cms_pages
function load_login_logout_link(context_path) {
	new Ajax.Updater('loginlink', context_path + '/async/login_logout_link.jsp');
}

// Need to make sure that the survey is enabled and Agent is not logged in
function survey_popup_callback(resp, context_path) {
	if (resp) {
		var json = eval('(' + resp + ')');
		//alert('survey enabled: ' + json.enabled + ' user logged-in: ' + json.logged_in);
		
		if (json.enabled == 'true' && json.logged_in == 'false') {
			window.open(context_path + '/survey/index.jsp', '', 'height=800,width=870,scrollbars=yes').focus();
		}
	}
}

// We need to make sure that the survey hasn't already been shown
function survey_popup(context_path) {
	var surveyShown = readCookie('SURVEY');
	
	if (!surveyShown || surveyShown != 'YES') {
		asyncGet(context_path + '/async/survey_enabled.jsp', 'survey_popup_callback', context_path);
	}
}

// Tab auto move function for register a card
function moveOnMax(field,nextFieldID){
	  if(field.value.length >= field.maxLength){
	    document.getElementById(nextFieldID).focus();
	  }
}

function reload_captcha_image(context_path) {
	var now = new Date();
	if (document.images) {
		document.images.captcha_image.src = context_path + '/servlet/captcha.jpg?' + now.getTime();
	}
}

function product_image_swap(new_image_src) {
	if (document.images) {
		document.images.product_image.src = new_image_src;
	}
}

// This is called when a PDF is clicked on
function record_pdf_click(context_path, id) {
	var pic = new Image(1,1);
	pic.src = context_path + '/servlet/conversion?ref=PAGEVIEW&model_name=multimedia_file&model_id=' + id + '&ts=' + new Date().getTime();
}

// Fills in the time-based products for a card into an HTML element
function fill_time_based_value(context_path, card_id, el_id) {
	var url = context_path + '/async/card_product_state.jsp?id=' + card_id;
	new Ajax.Request(url, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			var balance = $(el_id);
			var json = transport.responseJSON;
			var inner = '';
			
			if (json.error) {
				balance.update('Currently Unavailable');
			} else {
				for (i = 0; i < json.products.length; i++) {
					if (i > 0) {
						inner = inner + '<br>';
					}
						
					inner = inner + json.products[i].fi_desc;
						
					if (json.products[i].used == 'true') {
						inner = inner + ' Exp. ' + json.products[i].expiry;
					} else {
						inner = inner + ' Unused';
					}
				}
				
				if (balance.innerHTML == 'Loading...' || balance.innerHTML == '') {
					balance.update(inner);
				} else {
					balance.update(balance.innerHTML + '<br>' + inner);
				}
			}
		},
		onFailure: function(response) {
			var balance = $(el_id);
			
			if (balance.innerHTML == 'Loading...') {
				
				// We don't display 'Currently Unavailable' b/c val may be empty b/c the card may not have time-based products
				balance.update('');
			}
		}
	});
}

// Fills in the remaining values for a card into an HTML element
function fill_remaining_value(context_path, card_id, el_id) {
	var url = context_path + '/async/card_state.jsp?id=' + card_id;
	new Ajax.Request(url, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			var balance = $(el_id);
			var json = transport.responseJSON;
			
			if (json.error) {
				balance.update('Currently Unavailable');
			} else {
				if (balance.innerHTML == 'Loading...' || balance.innerHTML == '') {
					if (json.remaining_value == '' || json.remaining_value == '0') {
						balance.update('');
					} else {
						balance.update(json.remaining_value);
					}
				} else {
					if (json.remaining_value == '' || json.remaining_value == '0') {
						
						// Do nothing
					} else {
						balance.update(balance.innerHTML + '<br>' + json.remaining_value);
					}				
				}
			}
		},
		onFailure: function(response) {
			var balance = $(el_id);
			
			if (balance.innerHTML == 'Loading...' || balance.innerHTML == '') {
				balance.update('Currently Unavailable');
			}
		}
	});
}

// use this function to display threshold autoload information for a card.  this is better
// than simply looking at the database because the t/a in our database may not be in
// sync with the data from Cubic.  Cubic's data always takes precedence.
// to differentiate between t/a info from only our db and t/a info from both our db and Cubic, we'll
// prefix the combined info with a special character.  remove the character before we go live.
// if "show_link" is true, we'll create a link to add/edit a threshold autoload if possible.
function display_automatic_replenishment(context_path, card_id, el_id, hide_extra_info, show_link, ta_exists_msg, question_url) {
	var url = context_path + '/async/card_threshold_addable.jsp?id=' + card_id;
	var prefix = '';
	new Ajax.Request(url, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			var el = $(el_id);
			var json = transport.responseJSON;
			
			if (json.error) {
				el.update(prefix + 'Currently Unavailable');
			} else if (json.addable) {
				var inner = prefix + '';
				
				if (show_link) {
					inner = inner + '<a href="' + context_path + '/tandc/tandc_check.jsp?type=threshold&id=' + card_id 
						+ '"><img src="/images/threshold_button.gif" alt="Add Automatic Replenishment"></a>';
					inner = inner + '<a href="#" onclick="window.open(\'' + question_url 
						+ '\', \'_blank\', \'location=0,resizable=yes,scrollbars=yes,statusbar=0,menubar=0,width=330,height=400\'); return false;"><img src="/images/question.gif" alt="What\'s this?"></a>'
				}
				
				el.update(inner);
			} else if (json.removalPending) {
				el.update(prefix + 'Pending Replenishment Removal');
			} else if (json.inSync) {
				var inner = prefix + json.acdcContent;
				
				if (ta_exists_msg != '') {
					inner = ta_exists_msg;
				} else {
					if (show_link) {
						inner = '<a href="' + context_path + '/threshold/index.jsp?id=' + card_id + '">' + inner + '</a>';
					}
				}
				
				el.update(inner);
			} else if (json.acdcContent == '') {
				if (ta_exists_msg != '') {
					el.update(ta_exists_msg);
				} else {
					el.update(prefix + json.cubicContent);
				}
			} else if (json.cubicContent == '') {
				var extra = '';
				var inner = prefix + json.acdcContent;
				
				if (ta_exists_msg != '') {
					inner = ta_exists_msg;
				} else {
					
					// if hide_extra_info is true, we don't show these extra information.
					if (!hide_extra_info) {
						if (show_link) {
							inner = '<a href="' + context_path + '/threshold/index.jsp?id=' + card_id + '">' + inner + '</a>';
						}
						
						if (json.pending) {
							extra = '<br> - Pending -';
						}
					}
				}
								
				el.update(inner + extra);
			} else if (json.acdcContent != json.cubicContent) {
				if (ta_exists_msg != '') {
					el.update(ta_exists_msg);
				} else {
					el.update(prefix + json.cubicContent);
				}
			}
		},
		onFailure: function(response) {
			var el = $(el_id);
			el.update(prefix + 'Currently Unavailable');
		}
	});
}

// Displays the last 4 digits of the credit card that's associated with a card (serial number)
function display_billing_pan_partial(context_path, card_id, el_id) {
	var url = context_path + '/async/card_billing_pan_partial.jsp?id=' + card_id;
	var prefix = '';
	new Ajax.Request(url, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			var el = $(el_id);
			var json = transport.responseJSON;
			
			if (json.error) {
				el.update(prefix + 'Currently Unavailable');
			} else {
				el.update(prefix + json.pan_partial);
			}
		}
	});
}

// "message_el_id" is the HTML element where any messages would be displayed.
// "da_el_id" is the HTML select where options would be added.
function show_direct_autoloads_if_possible(context_path, path, card_id, message_el_id, da_el_id, ta_exists_msg) {
	var url = path;
	new Ajax.Request(url, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			var da_el = $(da_el_id);
			var json = eval('(' + transport.responseText + ')');

			for (i = 0; i < json.length; i++) {
				var optionEl = document.createElement('option');
				optionEl.setAttribute('value', json[i].val);
				optionEl.innerHTML = json[i].string;
				da_el.appendChild(optionEl);
			}
		}
	});
	
	var url = context_path + '/async/card_directed_addable.jsp?id=' + card_id;
	new Ajax.Request(url, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			var da_el = $(da_el_id);
			var message_el = $(message_el_id);
			var json = transport.responseJSON;

			if (json.error) {
				message_el.update('Currently Unavailable');
			} else if (json.addable) {
				da_el.style.display = 'block';
			} else if (json.hasThresholdAutoloadLocally || json.activeEnrollment || json.pendingEnrollment) {
				message_el.update(ta_exists_msg);
			} else if (json.badCard) {
				
			} else if (json.removalPending) {
				message_el.update('Pending Replenishment Removal');
			}
		},
		onFailure: function(response) {
			var el2 = $(message_el_id);
			message_el.update('Currently Unavailable');
		}
	});
}

