/*
	This is a list of the Javascript functions in this file.
	Please keep it up to date.
	
	enableOnCheckCopyShippingNew - copies shipping info to billing info for the NEW shopping cart code
	resetForm - given the form name, it resets the fields (only certain ones)
	clearText - given the form name and field name, it clears the field
	setFocusOnSearchBox - gives the search field focus; used in onload; note that it looks for a specific form name
	enableOnCheckCopyShipping - (OLD WAY) copies shipping info to billing info in cart
*/









/*
	This uses the NEW form field names that work with the NEW shopping cart code.
*/
function enableOnCheckCopyShippingNew(formName) {
	var copy = eval('document.' + formName + '.copy_address');
	
	var billing_address_1 = eval('document.' + formName + '.BILLING_ADDRESS_1');
	var billing_address_2 =eval('document.' + formName + '.BILLING_ADDRESS_2');
	var billing_city = eval('document.' + formName + '.BILLING_CITY');
	var billing_country_id = eval('document.' + formName + '.BILLING_COUNTRY_ID');
	var billing_zip_code = eval('document.' + formName + '.BILLING_ZIP_CODE');
	var billing_state_id = eval('document.' + formName + '.BILLING_STATE_ID');
	var billing_company_name = eval('document.' + formName + '.BILLING_COMPANY_NAME');

	var shipping_address_1 = eval('document.' + formName + '.shipping_address_1');
	var shipping_address_2 = eval('document.' + formName + '.shipping_address_2');
	var shipping_city = eval('document.' + formName + '.shipping_city');
	var shipping_country_id = eval('document.' + formName + '.shipping_country_id');
	var shipping_zip_code = eval('document.' + formName + '.shipping_zip_code');
	var shipping_state_id = eval('document.' + formName + '.shipping_state_id');
	var shipping_company_name = eval('document.' + formName + '.shipping_company_name');
	
	var shipping_first_name = eval('document.' + formName + '.shipping_first_name');
	var shipping_last_name = eval('document.' + formName + '.shipping_last_name');
	
	var name_on_card = eval('document.' + formName + '.BILLING_NAME');
	
	if (copy.checked) {
		billing_address_1.value = shipping_address_1.value;
		if (billing_address_2) {
			billing_address_2.value = shipping_address_2.value;
		}
		billing_city.value = shipping_city.value;
		
		billing_zip_code.value = shipping_zip_code.value;
		billing_state_id.value = shipping_state_id.value;
		
		if (billing_country_id) {
			billing_country_id.value = shipping_country_id.value;
		}
		
		if (shipping_first_name && shipping_last_name){
			name_on_card.value = shipping_first_name.value + ' ' + shipping_last_name.value;
		} else {
			var noc = "";
			if (shipping_first_name.value != "")
				noc = noc + shipping_first_name.value + " ";
			if (shipping_middle_name.value != "")
				noc = noc + shipping_middle_name.value + " ";
			if (shipping_last_name.value != "")
				noc = noc + shipping_last_name.value + " ";
			name_on_card.value= noc;	
		}
		
		if (billing_company_name && shipping_company_name) {
			billing_company_name.value = shipping_company_name.value;
		}
	} else {
		billing_address_1.value = "";
		if (billing_address_2) {
			billing_address_2.value = "";
		}
		billing_city.value = "";
		
		billing_zip_code.value = "";
		billing_state_id.value = 0;
		
		if (billing_country_id) {
			billing_country_id.value = 0;
		}
		
		if (billing_company_name)	{
			billing_company_name.value="";
		}
		name_on_card.value = "";
	}
}	


/* Clears all values from a form except for few types (hidden, submit, button, etc). */
function resetForm(formName) {
	var form = eval('document.' + formName);
	var no_elements = form.elements.length;
	
	/* Select boxes start with "select"; seems select boxes are named "select-one", etc. */
	var select_pattern = /select.*/;
	
	for (var i = 0; i < no_elements; i++) {
		var element = form.elements[i];
		if (element.type != 'hidden' && element.type != 'submit' && element.type != 'button') {
			if (element.type == 'checkbox') {
				element.checked = false;
			} else if (element.type == 'radio') {
				var group = form[element.name];
				
				if (group) {
					var group_count = group.length;
					for (var e = 0; e < group_count; e++) {
						group[e].checked = false;
					}
				}
			} else if (element.type.match(select_pattern)) {
				element.value = 0;
			} else {
				element.value = '';
			}
		}
	}
}

/* Clears a form's field. */
function clearText(formName, fieldName) {
	var field = eval('document.' + formName + '.' + fieldName);
	if (field) {
		field.value = '';
	}
}

/* Gives focus to the search text box - used in body.onload for constructioncomplete */
function setFocusOnSearchBox() {
	var textbox = eval('document.dosearch.searchterm');
	
	if (textbox) {
		textbox.focus();
	}
}

/* Copies shipping address to billing address on check. */
function enableOnCheckCopyShipping(formName) {
	var copy = eval('document.' + formName + '.copy_address');
	
	var billing_address = eval('document.' + formName + '.billing_address');
	var billing_address2 =eval('document.' + formName + '.billing_address2');
	var billing_city = eval('document.' + formName + '.billing_city');
	var billing_country = eval('document.' + formName + '.billing_country');
	var billing_country_id = eval('document.' + formName + '.billing_country_id');
	var billing_zip_code = eval('document.' + formName + '.billing_zip_code');
	var billing_state_id = eval('document.' + formName + '.billing_state_id');
	var billing_company_name = eval('document.' + formName + '.billing_company_name');

	var shipping_address = eval('document.' + formName + '.shipping_address');
	var shipping_address2 = eval('document.' + formName + '.shipping_address2');
	var shipping_city = eval('document.' + formName + '.shipping_city');
	var shipping_country = eval('document.' + formName + '.shipping_country');
	var shipping_country_id = eval('document.' + formName + '.shipping_country_id');
	var shipping_zip_code = eval('document.' + formName + '.shipping_zip_code');
	var shipping_state_id = eval('document.' + formName + '.shipping_state_id');
	var shipping_company_name = eval('document.' + formName + '.shipping_company_name');
	
	var first_name = eval('document.' + formName + '.first_name');
	var last_name = eval('document.' + formName + '.last_name');

	var shipping_first_name = eval('document.' + formName + '.shipping_first_name');
	var shipping_middle_name = eval('document.' + formName + '.shipping_middle_name');
	var shipping_last_name = eval('document.' + formName + '.shipping_last_name');
	
	var name_on_card = eval('document.' + formName + '.name_on_card');
	if (copy.checked) {
	
		billing_address.value = shipping_address.value;
		if(billing_address2)
		{
			billing_address2.value = shipping_address2.value;
		}
		billing_city.value = shipping_city.value;
		
		if (billing_country) {
			billing_country.value = shipping_country.value;
		}
		
		billing_zip_code.value = shipping_zip_code.value;
		billing_state_id.value = shipping_state_id.value;
		
		if (billing_country_id) {
			billing_country_id.value = shipping_country_id.value;
		}
		
		if(first_name && last_name)
		{
			name_on_card.value = first_name.value + ' ' + last_name.value;
		}else if(shipping_first_name && shipping_last_name){
			name_on_card.value = shipping_first_name.value + ' ' + shipping_last_name.value;
		}else
		{
			var noc = "";
			if(shipping_first_name.value != "")
				noc = noc + shipping_first_name.value + " ";
			if(shipping_middle_name.value != "")
				noc = noc + shipping_middle_name.value + " ";
			if(shipping_last_name.value != "")
				noc = noc + shipping_last_name.value + " ";
			name_on_card.value= noc;	
		}
		
		if(billing_company_name && shipping_company_name)
		{
			billing_company_name.value = shipping_company_name.value;
		}
	} else {
		billing_address.value = "";
		if(billing_address2)
		{
			billing_address2.value = "";
		}
		billing_city.value = "";
		
		if (billing_country) {
			billing_country.value = "";
		}
		
		billing_zip_code.value = "";
		billing_state_id.value = 0;
		
		if (billing_country_id) {
			billing_country_id.value = 0;
		}
		
		if(billing_company_name)
		{
			billing_company_name.value="";
		}
		name_on_card.value = "";
	}
}	


function checkforcounty(formName, body_id) {
	var id = eval('document.' + formName + '.shipping_state_id.value');
	var county = eval('document.' + formName + '.shipping_county_id');
	var county_element = document.getElementById(body_id);
	if(id  == '37'){
		if (county_element) {
			county_element.className = "tbody_show";
		}
	}else{
		if (county_element) {
			county_element.className = "tbody_hide";
		}
		if (county) {
			county.value = "0";
		}
	}
}

/*
	this allows you to have a second submit button for a form that does something totally different by changing the
	form values onClick
*/
function changeForm(formName, new_action, new_action_call, new_success, new_failure) {
	var form = eval('document.' + formName);
	
	if (form) {
		form.action = new_action;
		form.action_call.value = new_action_call;
		form.success.value = new_success;
		form.failure.value = new_failure;
	}
}



function bookmark(url, description)
{
	msg ="Please hit CTRL+D to add a bookmark to this site."
	if (navigator.appName=='Microsoft Internet Explorer')
	{
		window.external.AddFavorite(url, description);
	}
	else if (navigator.appName=='Netscape')
	{
		alert(msg);
	}
}

function getQueryStringValue(field)
{
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for(var i=0; i < pairs.length; i++)
	{
		var pos = pairs[i].indexOf('=');
		if(pos == -1) continue;
		var argname = pairs[i].substring(0, pos)
		var value = pairs[i].substring(pos+1);
		if(argname == field)
			return value;
	}
	return "";
}
