var submitted = false;

function togleContactType( type, form_name ) {

	document.getElementById( 'radio_' + type ).checked = true;

	var i = 0;
	var list_retail = getElementsByStyleClass( 'retail_options' );
	var list_online = getElementsByStyleClass( 'online_options' );

	if( type == 'retail' ) {

		if( list_retail.length > 0 ) { for( var y = 0; y < list_retail.length; y++ ) { list_retail[y].style.display = "block"; } }
		if( list_online.length > 0 ) { for( var y = 0; y < list_online.length; y++ ) { list_online[y].style.display = "none"; } }

		var retail_sel = document.getElementById( 'regarding_field' );
		if( retail_sel && online_reasons != "" ) {
			var retail_ops = retail_reasons.split( '|' );
			retail_sel.options.length = 0;
			retail_sel.options[0] = new Option( "- select -", "" );
			for( i = 0; i < retail_ops.length; i++ ) {
				retail_sel.options[(i + 1 )] = new Option( retail_ops[i].split( '^' )[1], retail_ops[i].split( '^' )[0] );
			}
		}

	 } else {

		if( list_retail.length > 0 ) { for( var y = 0; y < list_retail.length; y++ ) { list_retail[y].style.display = "none"; } }
		if( list_online.length > 0 ) { for( var y = 0; y < list_online.length; y++ ) { list_online[y].style.display = "block"; } }

		var online_sel = document.getElementById( 'regarding_field' );
		if( online_sel && online_reasons != "" ) {
			var online_ops = online_reasons.split( '|' );
			online_sel.options.length = 0;
			online_sel.options[0] = new Option( "- select -", "" );
			for( i = 0; i < online_ops.length; i++ ) {
				online_sel.options[(i + 1 )] = new Option( online_ops[i].split( '^' )[1], online_ops[i].split( '^' )[0] );
			}
		}
	}
}

// XML HTTPS REQUEST STUFF
var req;
var data;

// malls
function updateMalls() {
	var select = document.getElementById('state');
	var state = select.options[select.selectedIndex].value;

	var store = document.getElementById('concepts');
	var concept = store.options[store.selectedIndex].value;

	if (state == '') return;
	var url = "";
	url = "/contact/malls.php?state=" + state + "&concept=" + concept;
	req = create_XMLHttpRequest_object()
	if(!req) return;
	perform_xml_request(url, data, mall_callback );
}
function mall_callback() {
	if( check_req_status() ) 	{
		var theDiv = document.getElementById('malls');
		var response= req.responseXML.getElementsByTagName('malls').item(0).firstChild.data;
		theDiv.innerHTML = response;
	}
}

// products
function updateProducts() {
	// if the store changed, reset the flavors
	resetFlavors();

	var select = document.getElementById('concepts');
	var concept = select.options[select.selectedIndex].value;
	if (concept == '') return;
	var url = "";
	url="/contact/products.php?concept=" + concept;
	req = create_XMLHttpRequest_object()
	if(!req) return;
	perform_xml_request(url, data, product_callback );
}
function product_callback() {
	if( check_req_status() ) 	{
		var theDiv = document.getElementById('products');
		if(req && req.responseXML && req.responseXML.getElementsByTagName('products') && req.responseXML.getElementsByTagName('products').item(0)){
			var response= req.responseXML.getElementsByTagName('products').item(0).firstChild.data;
			theDiv.innerHTML = response;
			// after a product change, if the box only has one item with a value, update the flavors
			var select = document.getElementById('product_type');
			if( select.options.length == 1 && select.options[0].value != '' ) {
				updateFlavors();
			}

		}
	}
}

// flavors
function updateFlavors() {
	var select = document.getElementById('product_type');
	var product = select.options[select.selectedIndex].value;

	var select2 = document.getElementById('concepts');
	var concept = select2.options[select2.selectedIndex].value;

	if (concept == '') return;
	if (product == '') return;
	var url = "";
	url="/contact/flavors.php?prod=" + product + "&concept=" + concept;
	req = create_XMLHttpRequest_object()
	if(!req) return;
	perform_xml_request(url, data, flavor_callback );
}

function flavor_callback() {
	if( check_req_status() ) 	{
		var theDiv = document.getElementById('flavor');
		var response= req.responseXML.getElementsByTagName('flavors').item(0).firstChild.data;
		theDiv.innerHTML = response;
	}
}

function resetFlavors() {
		var theDiv = document.getElementById('flavor');
		theDiv.innerHTML = '<select name="form[flavor]"><option value="">- select a flavor - </option></select>';
}

