/*

	Frank Marion, software@frankmarion.com
	http://www.frankmarion.com

	Date Created:	Thursday, January 15, 2004 7:20:50 PM
	Last Modified:	Thursday, January 15, 2004 7:20:52 PM
	Revision:		1.0

	Function: Various functions used to support the commerce engine

*/


// [Error Prevention] Client side validation of required fields and formatting
// for the shopping cart quantity input value. Called from dsp_cart_item_add.cfm
// Included in template_top.cfm
function validate_cart_quantity(form_name) 
{
	var acceptChars='0123456789';
	var charError = false;
	var errorString='';

	for (var i=0;i<form_name.product_quantity.value.length;i++) {
		temp=form_name.product_quantity.value.substring(i,i+1)
		if (acceptChars.indexOf(temp)==-1) {
			charError = true;
		}
	}
	if (form_name.product_quantity.value.length == 0) {
		errorString += 'Please ensure that you add the quantity\n';
	}
	if (charError == true)	{
		errorString += 'Quantities can only be numbers\n';
	}
	if (form_name.product_quantity.value <= 0)	{
		errorString += 'Product quantities must be greater than 0\n';
	}
	if (errorString != '')	{
		alert(errorString);
		form_name.product_quantity.value = 1;
		return false;
	} else {
		return true;
	}
}
/* ------------------------------------------------------------------------ */
/* Complete shipping address part of form with that of billing address		*/
// Set the user name field to the user's email (as a 'forced suggestion')

function MakeSameAddress() {
	bill_to_ship_copy	= document.getElementById("bill_to_ship_copy");
	
	// List all of the elements in the billing part of the form
	addr_name_first_1	= document.getElementById("addr_name_first_1");
	addr_name_last_1	= document.getElementById("addr_name_last_1");
	addr_company_1		= document.getElementById("addr_company_1");
	addr_phone_day_1	= document.getElementById("addr_phone_day");
	addr_phone_eve_1	= document.getElementById("addr_phone_eve");
	addr_line_1_1		= document.getElementById("addr_line_1");
	addr_line_2_1		= document.getElementById("addr_line_2_1");
	addr_city_1			= document.getElementById("addr_city");
	addr_prov_state_1	= document.getElementById("addr_prov_state_1");
	addr_postal_zip_1	= document.getElementById("addr_postal_zip");
	addr_country_1		= document.getElementById("addr_country_1");

	// List all of the elements in the shipping part of the form
	addr_name_first_2	= document.getElementById("addr_name_first_2");
	addr_name_last_2	= document.getElementById("addr_name_last_2");
	addr_name_company_2	= document.getElementById("addr_name_company_2");
	addr_phone_day_2	= document.getElementById("addr_phone_day_2");
	addr_phone_eve_2	= document.getElementById("addr_phone_eve_2");
	addr_line_1_2		= document.getElementById("addr_line_1_2");
	addr_line_2_2		= document.getElementById("addr_line_2_2");
	addr_city_2			= document.getElementById("addr_city_2");
	addr_prov_state_2	= document.getElementById("addr_prov_state_2");
	addr_postal_zip_2	= document.getElementById("addr_postal_zip_2");
	addr_country_2		= document.getElementById("addr_country_2");
	
	// Copy the values of part 1 to part 2 (convenience pre-filling)
	if(bill_to_ship_copy.checked) {
		addr_name_first_2.value		= addr_name_first_1.value;
		addr_name_last_2.value		= addr_name_last_1.value;
		addr_name_company_2.value	= addr_company_1.value;
		addr_phone_day_2.value		= addr_phone_day_1.value;
		addr_phone_eve_2.value		= addr_phone_eve_1.value;
		addr_line_1_2.value			= addr_line_1_1.value;
		addr_line_2_2.value			= addr_line_2_1.value;
		addr_city_2.value			= addr_city_1.value;
		addr_prov_state_2.selectedIndex	= addr_prov_state_1.selectedIndex;
		addr_postal_zip_2.value		= addr_postal_zip_1.value;
		addr_country_2.selectedIndex= addr_country_1.selectedIndex;
	} else {
		addr_name_first_2.value		= '';
		addr_name_last_2.value		= '';
		addr_name_company_2.value	= ''
		addr_phone_day_2.value		= '';
		addr_phone_eve_2.value		= '';
		addr_line_1_2.value			= '';
		addr_line_2_2.value			= '';
		addr_city_2.value			= '';
		addr_prov_state_2.selectedIndex	= '0';
		addr_postal_zip_2.value		= '';
		addr_country_2.selectedIndex= '0';
	}
}
/* ------------------------------------------------------------------------ */
/* Check for the existence of an account (remoting)		 					*/
// Set the user name field to the user's email (as a 'forced suggestion')
	function set_account_email (arg) {
		e = document.getElementById("client_account_email");
		nf = document.getElementById("addr_name_first_1");
		nl = document.getElementById("addr_name_last_1");
		if (e) {
			e.value = arg;
			// Hiding this function for now. opens iframe if account details match form entries
			//call_to_server();
		}
	}
	// In an iframe, load a document that queries the db for matches
	function call_to_server() {
		fr = document.getElementById("account_exists_test_frame");
		v = '&client_name_first='+nf.value+'&client_name_last='+nl.value+'&client_email='+e.value
		fr.src='index.cfm?fuseaction=account_exists_test' + v;
	}
	// If there's a match, the page calls this, and displays a login form in the iframe
	function show_login_form(cnt) {
		if (cnt != 0) {
			fr = document.getElementById("account_exists_test_frame");
			fr.style.display 	= 'block';
			fr.style.visibility = 'visible';
			fr.style.overflow	= 'auto';
			fr.style.height 	= '250px';
			fr.style.width 		= '475px';
		}
	}
	// if there's no match, make sure the iframe remains hidden
	function hide_login_form() {
		fr.style.display 		= 'none';
		fr.style.visibility 	= 'hidden';
		fr.style.height 		= '0px';
		fr.style.width 			= '0px';
	}

/* ------------------------------------------------------------------------ */
/* Toggle the visibilty of certain form and table elements 					*/

function toggleDisplay(class1,class2) {
	var el = getElementsByClassName(document, "tr", class1);
	if (el.length) {
		for (i = 0; i != el.length; i++) {
			cssjs('swap',el[i],class1,class2);
		}
	} else {
		var el = getElementsByClassName(document, "tr", class2);
		for (i = 0; i != el.length; i++) {
			cssjs('swap',el[i],class1,class2);
		}
	}
	return false;
}

function hideDisplay(class1,class2) {
	var el = getElementsByClassName(document, "tr", class1);
	if (el.length) {
		for (i = 0; i != el.length; i++) {
			cssjs('swap',el[i],class1,class2);
		}
	}
}

function resetSelect(sel) {
	var sel = document.getElementById(sel);
	sel.selectedIndex = 0;
}

function toggleAccountNewFlag(obj) {
	var obj = document.getElementById(obj);
	if (obj.value ==1) {
		obj.value = 0;
	} else {
		obj.value = 1;
	}
	//alert(obj.id + ' ' + obj.value);
}

function toggleElementDisplay(el) {
	if (el) {
		cssjs('swap',el,'hide','show');
	}
	return false;
}