function submit_form_for_custom_search( goto )
{    
	cbvalue = document.getElementById( 'cat_combo' ).value;
	url = goto.replace( '~name~', cbvalue );
	gets = '';
	
	if ( document.getElementsByName( 'searchdesc' ).length > 0 )
	{
		elem = document.getElementsByName( 'searchdesc' )[ 0 ];
		if ( elem.type == "checkbox" && elem.checked )
			gets += "?searchdesc=on";
		else if ( elem.type == "hidden" )
			gets += "?searchdesc=on";		
	}
	
	if ( document.getElementsByName( 'searchtext' ).length > 0 )
	{
		elem = document.getElementsByName( 'searchtext' )[ 0 ];
		st = $.trim( elem.value );
		if ( st.length > 0 ) 
		{
			if ( gets.length == 0 )
				gets += "?searchtext=" + st;
			else
				gets += "&searchtext=" + st;
		}
	}
	
	window.location = url + gets;
}

// used on the product detail page. this will get the text in
// the special instructions box.
function getSpecialInstructions()
{
	var tb = document.getElementById( 'descbox' );
	
	if ( tb == null ) return '';
	return tb.value;
}

// used on the product detail page. this gets all the options out
// of the options table.
function getOptions()
{
	var toReturn = new Object();
	
	// product option is the name of the class; this construction
	// returns all the select boxes that use this class.
	var o_list = $('.product_option');
	
	for ( i = 0; i < o_list.length; i++ )
	{
		var optionid = o_list[ i ].name;
		optionid = optionid.replace( "option_", "" );
		var value = o_list[ i ].value;
		toReturn[ optionid ] = value;
	}
	
	return toReturn;
}

// called from checkout page.
function hideNonDigi()
{
	if ( document.getElementById( 'digi_only' ).checked )
	{
		var start = '<div style="margin-left:175px;width:300px" class="generaltext"><i>';
		var msg = neucart_skipping_notice;
		var end = '</i></div>';
	
		$("#nondigifields").css( "display", "none" );
		$("#digimessage").html( start.concat( msg, end ) );
	}
	else
	{
		$("#nondigifields").css( "display", "inline" );
		$("#digimessage").html( "" );
	}
	
	if ( document.getElementById( 'email' ) != null )
		document.getElementById( 'email' ).focus();
	else if ( document.getElementById( 'first' ) != null )
		document.getElementById( 'first' ).focus();
}

// called from checkout page.
function hideAddressFieldsForTakeOut()
{
	if ( document.getElementById( 'hide_optional_address_fields' ).checked )
		$("#hideableaddressfields").css( "display", "none" );
	else
		$("#hideableaddressfields").css( "display", "inline" );
		
	if ( document.getElementById( 'email' ) != null )
		document.getElementById( 'email' ).focus();
	else if ( document.getElementById( 'first' ) != null )
		document.getElementById( 'first' ).focus();
}

function toggleDelivTakeOutOptionalBox()
{
	if ( document.getElementById( 'this_is_take_out_order' ).checked )
	{
		document.getElementById( 'because_take_out' ).style.display = "inline";
	}
	else
	{
		document.getElementById( 'because_take_out' ).style.display = "none";
		document.getElementById( 'hide_optional_address_fields' ).checked = false;
		hideAddressFieldsForTakeOut();
	}
}

function checkPhysicalFields()
{
	if ( document.getElementById( 'digi_only' ) == null )
		return false;
		
	return document.getElementById( 'digi_only' ).checked;
}

// called from the user-pw-change screen
function validateAccountInfoPwFields()
{
	if ( fieldIsBlank( document.getElementById( 'pw' ) ) || fieldIsBlank( document.getElementById( 'pw2' ) ) )
	{
		alert( neucart_pw_blank );
	}
	else if ( !passwordsMatch( document.getElementById( 'pw' ), document.getElementById( 'pw2' ) ) )
	{
		alert( neucart_pw_diff );
	}
	else
	{
		// everything is good!
		return true;
	}
	
	return false;
}

// called from the user-edit screen.
function validateAccountInfoUpdateFields()
{
	if ( fieldIsBlank( document.getElementById( 'firstname' ) ) )
	{
		alert( neucart_blank_first );
	}
	else if ( fieldIsBlank( document.getElementById( 'lastname' ) ) )
	{
		alert( neucart_blank_last );
	}
	else if ( fieldIsBlank( document.getElementById( 'email' ) ) )
	{
		alert( neucart_bad_email );
	}
	else if ( !emailIsValid( document.getElementById( 'email' ) ) )
	{
		alert( neucart_bad_email );
	}
	else
	{
		//everything is good!
		return true;
	}
	
	return false;
}

// called from the user-create screen
function validateAccountInfoFields()
{
	if ( !validateAccountInfoUpdateFields() )
		return false;
		
	if( !validateAccountInfoPwFields() )
		return false;
		
	return true;
}

function validateAddressInfoFieldsForTakeOut()
{
	// email, first, last are required always, but the email
	// may not be there if someone is logged in.
	if ( document.getElementById( 'email' ) != null )
	{	
		if ( fieldIsBlank( document.getElementById( 'email' ) ) )
		{
			alert( neucart_bad_email );
			return false;
		}
	
		if ( !emailIsValid( document.getElementById( 'email' ) ) )
		{
			alert( neucart_bad_email );
			return false;
		}
	}
	
	if ( fieldIsBlank( document.getElementById( 'firstname' ) ) )
	{
		alert( neucart_blank_first );
		return false;
	}
	
	if ( fieldIsBlank( document.getElementById( 'lastname' ) ) )
	{
		alert( neucart_blank_last );
		return false;
	}
	
	// checkbox and phone are optional
	var cb = document.getElementById( 'hide_optional_address_fields' );
	var phone_req = document.getElementById( 'phone_required' ) != null;
	var check_csz = document.getElementById( 'delivery_location' ) == null;
	
	if ( phone_req && fieldIsBlank( document.getElementById( 'phone' ) ) )
	{
		alert( neucart_blank_phone );
		return false;
	}
	
	// if the checkbox isn't checked, then address, city, state, and zip are required.
	if ( !cb.checked )
	{
		// check the city/state/zip when there's no delivery_location item on
		// the page. (in that case, the city, state, zip fields DO NOT EXIST on
		// the page)
		var check_csz = document.getElementById( 'delivery_location' ) == null;
	
		if ( fieldIsBlank( document.getElementById( 'address' ) ) )
		{
			alert( neucart_blank_adrs );
			return false;
		}
		
		if ( check_csz && fieldIsBlank( document.getElementById( 'city' ) ) )
		{
			alert( neucart_blank_city );
			return false;
		}
		
		if ( check_csz && fieldIsBlank( document.getElementById( 'zip' ) ) )
		{
			alert( neucart_blank_zip );
			return false;
		}
		
		if ( check_csz && noSelection( document.getElementById( 'country' ) ) )
		{
			alert( neucart_select_country );
			return false;
		}
		
		if ( check_csz && document.getElementById( 'stateprov' ).value != "NONE" && noSelection( document.getElementById( 'stateprov' ) ) )
		{
			// if the value is "NONE", there's only one item in the box, so it is valid.
			alert( neucart_select_state );
			return false;
		}		
	}
	
	return true;

}

function validateAddressInfoFields( checkemail, adrsOptional )
{
	// if this is a take-out store, use the alternate function.
	if ( document.getElementById( 'hide_optional_address_fields' ) != null )
		return validateAddressInfoFieldsForTakeOut();

	// check the city/state/zip when there's no delivery_location item on
	// the page. (in that case, the city, state, zip fields DO NOT EXIST on
	// the page)
	var check_csz = document.getElementById( 'delivery_location' ) == null;
	var phone_req = document.getElementById( 'phone_required' ) != null;

	if ( checkemail )
	{
		if ( fieldIsBlank( document.getElementById( 'email' ) ) )
		{
			alert( neucart_bad_email );
			return false;
		}
	
		if ( !emailIsValid( document.getElementById( 'email' ) ) )
		{
			alert( neucart_bad_email );
			return false;
		}		
	}

	if ( fieldIsBlank( document.getElementById( 'firstname' ) ) )
	{
		alert( neucart_blank_first );
	}
	else if ( fieldIsBlank( document.getElementById( 'lastname' ) ) )
	{
		alert( neucart_blank_last );
	}
	else if ( !adrsOptional && fieldIsBlank( document.getElementById( 'address' ) ) )
	{
		alert( neucart_blank_adrs );
	}
	else if ( check_csz && !adrsOptional && fieldIsBlank( document.getElementById( 'city' ) ) )
	{
		alert( neucart_blank_city );
	}
	else if ( check_csz && !adrsOptional && fieldIsBlank( document.getElementById( 'zip' ) ) )
	{
		alert( neucart_blank_zip );
	}
	else if ( !adrsOptional && fieldIsBlank( document.getElementById( 'type' ) ) )
	{
		alert( neucart_blank_address_name );
	}
	else if ( check_csz && !adrsOptional && noSelection( document.getElementById( 'country' ) ) )
	{
		alert( neucart_select_country );
	}
	else if ( check_csz && !adrsOptional && document.getElementById( 'stateprov' ).value != "NONE" && noSelection( document.getElementById( 'stateprov' ) ) )
	{
		// if the value is "NONE", there's only one item in the box, so it is valid.
		alert( neucart_select_state );
	}
	else if ( phone_req && !adrsOptional && fieldIsBlank( document.getElementById( 'phone' ) ) )
	{
		alert( neucart_blank_phone );
	}
	else
	{
		// everything is good!
		return true;
	}
	
	return false;
}


function validateTakeOutFields()
{
	var phone_req = document.getElementById( 'phone_required' ) != null;

	if ( fieldIsBlank( document.getElementById( 'email' ) ) )
	{
		alert( neucart_bad_email );
		return false;
	}

	if ( !emailIsValid( document.getElementById( 'email' ) ) )
	{
		alert( neucart_bad_email );
		return false;
	}

	if ( fieldIsBlank( document.getElementById( 'firstname' ) ) )
	{
		alert( neucart_blank_first );
		return false;
	}
	
	if ( fieldIsBlank( document.getElementById( 'lastname' ) ) )
	{
		alert( neucart_blank_last );
		return false;
	}
	
	if ( phone_req && fieldIsBlank( document.getElementById( 'phone' ) ) )
	{
		alert( neucart_blank_phone );
		return false;
	}
	
	return true;
}

function toggle_my_review()
{
	if ( document.getElementById( 'my_review' ).style.display == 'none' )
	{
		$( "#my_review" ).fadeIn( 500 );
		
		// now get the current ahref text from the element, and make it the full innerHTML
		// of the edit_your_review div. in short, remove the <a> through </a>, and replace
		// all that with what's currently the linkable text
		currentAtext = document.getElementById( 'edit_your_review' ).childNodes(0).innerHTML;
		$( "#edit_your_review" ).html( currentAtext );
	}
}

function show_leave_my_review()
{
	if ( document.getElementById( 'my_review' ).style.display == 'none' )
	{
		$( "#my_review" ).fadeIn( 500 );
		$( "#leave_a_review" ).remove();
	}
}

function validateResetPwFields()
{
	if ( fieldIsBlank( document.getElementById( 'pw' ) ) || fieldIsBlank( document.getElementById( 'pw2' ) ) )
	{
		alert( neucart_pw_blank );
	}
	else if ( !passwordsMatch( document.getElementById( 'pw' ), document.getElementById( 'pw2' ) ) )
	{
		alert( neucart_pw_diff );
	}
	else
	{
		// everything is good!
		return true;
	}
	
	return false;
}

function validateNewsletterTest()
{
	var testmails = document.getElementsByName( 'testmails' )[ 0 ];

	if ( fieldIsBlank( testmails ) )
	{
		alert( "Please enter one or more email addresses." );
		return false;
	}
	
	var s = testmails.value;
	
	if ( s.indexOf( ";" ) != -1 )
	{
		var arr = s.split( ";" );
		
		for ( var i = 0; i < arr.length; i++ )
		{
			if ( !emailIsValid( testmails ) )
			{
				alert( "At least one of the email addresses is not valid." );
				return false;
			}
		}
	}
	else
	{
		if ( !emailIsValid( testmails ) )
		{
			alert( "Please enter a valid email address." );
			return false;
		}
	}
	
	// everything is good!
	return true;
}


function noSelection( field )
{
	// in this case "no selection" means that the first 
	// item is selected. that item is "select a country" or
	// something like that
	return field.selectedIndex < 1;
}

function fieldIsBlank( field )
{
	if ( /\S+/.test( field.value ) == false )
		return true;
	return false;
}

function emailIsValid( field )
{
	if ( /^\S+@[a-z0-9_.-]+\.[a-z]{2,6}$/i.test( field.value ) == false )
		return false;
	return true;
}

function passwordsMatch( field1, field2 )
{
	if ( field1.value == field2.value )
		return true;
	return false;
}

function play_default_chrome_audio( playerid, filename, exts  )
{
	// this is called from chrome (and others?) where preload isn't 
	// recognized so we need to create the player first
	var player_name = 'audio_player_' + playerid;
	if ( document.getElementById( player_name ) == null )
	{
		var blank_div = document.getElementById( 'div_for_player_' + playerid );
		var player = document.createElement( 'audio' );
		player.setAttribute( 'id', 'audio_player_' + playerid );		
		blank_div.appendChild( player );
		
		var arrExts = exts.split( ',' );
		
		for ( i = 0; i < arrExts.length; i++ )
		{
			if ( arrExts[ i ].length == 0 )
				continue;
				
			var s = document.createElement( 'source' );
			s.setAttribute( 'src', filename + '.' + arrExts[ i ].toLowerCase() );
			player.appendChild( s );
		}
	}
	
	play_default_audio( playerid );
}

function play_default_audio( playerid )
{
	var button_name = 'play_button_' + playerid;
	var player_name = 'audio_player_' + playerid;
        var player = document.getElementById( player_name );
        var button = document.getElementById( button_name );
        
        player.addEventListener( 'ended', function() { 
            button.innerHTML = '<img style="border:0px" src="players/play.png"></img>';
            neucart_album_now_playing = 0;
            }, false);
        
        if ( player.paused || player.ended )
        {
            player.play();
	    button.innerHTML = '<img style="border:0px" src="players/pause.png"></img>';
        }
        else
        {
            player.pause();
            button.innerHTML = '<img style="border:0px" src="players/play.png"></img>';
        }
}

function show_track_menu( row )
{
	document.getElementById( "neucart_track_" + row + "_menu" ).style.display = "block";			
}

function hide_track_menu( row )
{
	document.getElementById( "neucart_track_" + row + "_menu" ).style.display = "none";
}

function show_volume( row )
{
	elem = document.getElementById( "neucart_track_" + row + "_volume" );
	elem.style.display = "block";		
}

function hide_volume( row )
{
	elem = document.getElementById( "neucart_track_" + row + "_volume" );
	elem.style.display = "none";		
}

function play_neucart_track_audio( playerid )
{
	// first, stop other audio.
	if ( neucart_album_now_playing != 0 )
	{
		player = document.getElementById( 'audio_player_tp_' + neucart_album_now_playing );
		play_default_audio( 'tp_' + neucart_album_now_playing );
	}
	
	player = document.getElementById( 'audio_player_tp_' + playerid );
				
	// now play it, but only if it is not the same track as before.
	if ( neucart_album_now_playing == playerid )
	{
		neucart_album_now_playing = 0;
		return;
	}
	else
	{
		neucart_album_now_playing = playerid;
		play_default_audio( 'tp_' + playerid );
	}			
}

function volume_changed( id, val )
{				
	document.getElementById( 'audio_player_tp_' + id ).volume = val / 100;
}

function volume_up( id )
{
	elem = document.getElementById( 'audio_player_tp_' + id );
	new_vol = elem.volume + .05;
	
	if ( new_vol > 1 )
		new_vol = 1;
	
	elem.volume = new_vol;
}

function volume_down( id )
{
	elem = document.getElementById( 'audio_player_tp_' + id );
	new_vol = elem.volume - .05;
	
	if ( new_vol < 0 )
		new_vol = 0;
	
	elem.volume = new_vol;
}

var neucart_album_now_playing = 0;

function set_mobile_param( type )
{
	var containsQ = window.location.href.indexOf( '?' ) > -1;
	var get = "";
	
	if ( containsQ )
		get = "&mobile_site_param=" + type;
	else
		get = "?mobile_site_param=" + type;
	
	window.location = window.location.href + get;
}

function toggle_order_memo()
{
	var elem = document.getElementById( "confirmpagememobox" );
	if ( elem.style.display == "none" || elem.style.display == "" )
		elem.style.display = "inline"
	else
		elem.style.display = "none";	
}

function toggle_memo_text()
{
	var tb = document.getElementById( 'memotext' );
	var btn = document.getElementById( 'editb' );
	
	if ( tb.disabled )
	{
		tb.disabled = false;
		btn.value = neucart_done;
	}
	else
	{
		tb.disabled = true;
		checkoutCartMemoChange( tb.value );
		btn.value = neucart_edit;
	}
}

function redo_sort()
{
   sel = document.getElementById( 'review_sort_box' ).value;
   hashes = window.location.href.slice( window.location.href.indexOf( '?' ) + 1 ).split( '&' );
   gets = '';
   found = false;
 
   for( var i = 0; i < hashes.length; i++ )
   {
        hash = hashes[i].split('=');
        pre_get = gets.length == 0 ? "?" : "&";
        
        if ( null == hash[1] )
        	hash[1] = '';
        if ( hash[0].length == 0 )
        	continue;

	if ( hash[0] == 'review_sort' )
	{
		gets = gets + pre_get + hash[0] + "=" + sel;
		found = true;
	}		
	else
		gets = gets + pre_get + hash[0] + "=" + hash[1];
   }
   
   if ( !found )
   	gets = gets + "&review_sort=" + sel;
   	
   url = window.location.href.slice( 0, window.location.href.indexOf( '?' ) );
   window.location = url + gets;
}
