var custom_onload_func_exists = false;

function loadMain() {
  if( custom_onload_func_exists )
  {
      custom_onload_func();
  }
} // loadMain()

function place_order()
{
    var btn1 = dhtml_getElement( 'place1' );
    var btn2 = dhtml_getElement( 'place2' );

    btn1.disabled = true;
    btn2.disabled = true;

    
    btn1.src = '/images/buttons/button-place-order_off.gif';
    btn2.src = '/images/buttons/button-place-order_off.gif';
    
    return true;
} // place_order

function swap( img, isrc )
{
  if( !document.images ) return;
  document.images[img].src = isrc;
}

// this function shows/hides the text hints in form fields
function textHint( frm, element, event, text, hint )
{
    obj = document.forms[frm].elements[element];

    if( obj )
    {
	if( event == 'focus' )
	{
	    if( obj.value == text )
	    {
		obj.value = '';
	    }
	    
	    if( hint )
	    {
		window.status = hint;
	    }
	}
	else if( event == 'blur' )
	{
	    if( obj.value == '' )
	    {
	    	obj.value = text;
	    }

	    if( hint )
	    {
		window.status = '';
	    }
	}
    }
} // textHint()

function setWndLocation( wnd, url )
{
    wnd.location.href = url;		
} // dhtml_setWndLocation

function addToCartFromDropdown( master_product_id, dropdown_id )
{
    var product_id;
    var dd = dhtml_getElement( dropdown_id );

    // get item product_id
    product_id = dd.value;

    // attempt to add to cart
    if( product_id )
    {
	// add to cart
	addToCart( product_id );
    }
    else
    {
	alert( "Please select a Color+Size to add to your cart and try again." );
    }
} // addToCart()

function addToCart( product_id )
{
    // add item to cart
    // can't do this because cross-site posting is not allowed in Firefox, etc.
    //dhtml_xmlHttpPost( 'https://www.turntablelab.com/cart/add_item.xml',
    dhtml_xmlHttpPost( '/cart/add_item.xml',
		       'product_id=' + escape( product_id ),
		       'addToCart_ItemAdded' );
} // addToCart()

// AJAX callback function
function addToCart_ItemAdded( req )
{
    //alert( req.responseText );
    var resp, code, productList;
    code = 0;
    
    // Get data from response
    response = req.responseXML.documentElement;
    if( response ) {
	code = response.getAttribute( 'code' );
	productList = response.getElementsByTagName( 'product' );
    }
    //alert( 'Code = ' + code + ' product list = ' + productList.length );

    // If product was successfully added to cart
    if( code == 1 ) {

	if( productList != null ) {
	    
	    // Loop through each product added to cart (usually only one;more than one for cached category pages)
	    for( var prodIdx = 0; prodIdx < productList.length; prodIdx++ ) {
		
		// Get list of add and added buttons for this product
		var product_id   = productList[prodIdx].getAttribute( 'id' );
		var objAddList   = document.getElementsByName( 'atc_add_'   + product_id );
		var objAddedList = document.getElementsByName( 'atc_added_' + product_id );

		// Hide all of the add buttons
		if( objAddList != null ) {
		    for( var i = 0; i < objAddList.length; i++ ) {
			objAddList[i].style.display = 'none';
		    }
		}
		// Show all of the added buttons
		if( objAddedList != null ) {
		    for( var i = 0; i < objAddedList.length; i++ ) {
			objAddedList[i].style.display = 'block';
		    }
		}
	    } // for each product in cart
	} // if products returned by xml
    } // if code = 1
    else { // show alert
	alert( "We were unable to add this product to your cart. Please try again." );
    }
} // addToCart_ItemAdded

function swapFieldSets( idx, dropdownName, visibleStyle )
{
    if( !visibleStyle )
	visibleStyle = 'block';
    visibleStyle = dhtml_convertDisplayType( visibleStyle );
    
    // Get fieldset names
    var setNames = eval( dropdownName + "_fsets" );
    var defaultName = eval( dropdownName + "_default_fset" );
    var activated = false;
    
    // Loop through fieldset names
    //alert( 'idx = ' + idx + ' default = ' + defaultName );
    for( var i = 0; i < setNames.length; i++ ) {
	if( setNames[i] == null )
	    continue;
	
	// Activate
	if( i == idx || setNames[i] == setNames[idx] ) {
	    //alert( 'Activating ' + setNames[i] );
	    dhtml_getElement( setNames[i] ).style.display = visibleStyle;
	    activated = true;
	}
	else { // Deactivate
	    //alert( 'Hiding ' + setNames[i] + ' ELEMENT.style = ' + dhtml_getElement( setNames[i] ).style );
	    dhtml_getElement( setNames[i] ).style.display = 'none';
	}
	    
    } // for
    
    if( !activated && defaultName != null ) {
	//alert( 'Activating default' );
	dhtml_getElement( defaultName ).style.display = visibleStyle;
    }
    else if( defaultName != null ) {
	dhtml_getElement( defaultName ).style.display = 'none';
    }

}

function changeAction( formName, actionValue )
{
    document.forms[formName].elements['action'].value = actionValue;
} // changeAction

function popWindow( url, width, height )
{
    var wnd = window.open( url,
			   '_blank', 
			   'height=' + height + ',width=' + width + ',location=no,menubar=no,scrollbars=no,resizable=no,toolbar=no'
			   );
    wnd.focus();	
} // popWindow

function popWindowScroll( url, width, height )
{
    var wnd = window.open( url,
			   '_blank', 
			   'height=' + height + ',width=' + width + ',location=no,menubar=no,scrollbars=yes,resizable=no,toolbar=no'
			   );
    wnd.focus();	
} // popWindow

function popWindowCustom( url, width, height, features )
{
    var wnd = window.open( url,
			   '_blank', 
			   'height=' + height + ',width=' + width + ((features) ? (',' + features) : (''))
			   );
    wnd.focus();	
} // popWindow

function showDetails( pid, width, height )
{
    // set default width and height
    if( !width )
    {
	width = 425;
    }
    if( !height )
    {
	height = 500;
    }
    
    // popup window
    popWindow( "/products/details/" + pid + ".html", width, height )
} // showDetails()

function showImageDetail( numImage )
{
    var img, styleName;

    for( i = 0; i < 15; i++ ) // assume no more than 15 images/captions in popup
    {
	// select the new image, hide the rest
	img = dhtml_getElement( "img_" + i );
	if( img )
	{
	    if( i == numImage )
	    {
		styleName = 'block';
	    }
	    else
	    {
		styleName = 'none';
	    }
	    img.style.display = styleName;	    

	    // hilight the new caption, normalize the rest
	    //cap = dhtml_getElement( "cap_" + i );
	    //if( cap )
	    //{
	    //cap.style.font.decoration = 'none'; // this doesn't work
	    //}
	}
    }
} // showImageDetail

function customPkgInfo( section, ddName )
{
    var productId = dhtml_getFormValue( ddName );

    if( productId != null && productId != '' ) {
	popWindow( '/' + section + '/0/0/' + productId + '.html',
		   800,
		   600 );
    }
} // customPkgInfo

function showFullReview( id )
{
    var linkSpan   = dhtml_getElement( 'more_info_link_' + id );
    var reviewSpan = dhtml_getElement( 'rest_of_review_' + id );
    
    if( linkSpan != null && reviewSpan != null ) {
	
	dhtml_changeDisplayForElement( linkSpan, 'none' );
	dhtml_changeDisplayForElement( reviewSpan, 'inline' );
    }
}
