/*

	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;
	}
}

/* ------------------------------------------------------------------------ */
/* 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;
}