<?php
add_action( 'add_meta_boxes', 'add_custom_page_attributes_meta_box' );
function add_custom_page_attributes_meta_box(){
global $post;
    if ( 'page' != $post->post_type && post_type_supports($post->post_type, 'page-attributes') ) {
        add_meta_box( 'custompageparentdiv', __('Template'), 'custom_page_attributes_meta_box', NULL, 'side', 'core');
    }
}

function custom_page_attributes_meta_box($post) {
    $template = get_post_meta( $post->ID, '_wp_page_template', 1 ); ?>
    <select name="page_template" id="page_template">
        <?php $default_title = apply_filters( 'default_page_template_title',  __( 'Default Template' ), 'meta-box' ); ?>
        <option value="default"><?php echo esc_html( $default_title ); ?></option>
        <?php page_template_dropdown($template); ?>
    </select><?php
}

add_action( 'save_post', 'save_custom_page_attributes_meta_box' );
function save_custom_page_attributes_meta_box( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) return;
    if ( ! current_user_can( 'edit_post', $post_id ) ) return;
    if ( ! empty( $_POST['page_template'] ) && get_post_type( $post_id ) != 'page' ) {
        update_post_meta( $post_id, '_wp_page_template', $_POST['page_template'] );
    }
}

 function my_theme_enqueue_styles() {

	$parent_style = 'parent-style';

	wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css'     );
	wp_enqueue_style( 'child-style',get_stylesheet_directory_uri() . '/custom.css',array( $parent_style ));
	wp_enqueue_style( 'theatre-style',get_stylesheet_directory_uri() . '/css/theatre.css',array( $parent_style ));
	wp_enqueue_style( 'fancybox-style',get_stylesheet_directory_uri() . '/css/jquery.fancybox.css',array( $parent_style ));
	wp_enqueue_style( 'asAccordion-style',get_stylesheet_directory_uri() . '/css/asAccordion.css',array( $parent_style ));
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

add_action( 'wp_enqueue_scripts', 'menu_scripts' );
function menu_scripts() {
	wp_enqueue_script('infochart', get_stylesheet_directory_uri() . '/js/infochart.js', array('jquery'));
	wp_enqueue_script('speclist', get_stylesheet_directory_uri() . '/js/speclist.js', array('jquery'));
	wp_enqueue_script('theatre', get_stylesheet_directory_uri() . '/js/jquery.theatre.min.js', array('jquery'));
	wp_enqueue_script('fancybox', get_stylesheet_directory_uri() . '/js/jquery.fancybox.min.js', array('jquery'));
	wp_enqueue_script('custom', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'));
	wp_enqueue_script('toc', get_stylesheet_directory_uri() . '/js/jquery.toc.min.js', array('jquery'));
	wp_enqueue_script('asAccordion', get_stylesheet_directory_uri() . '/js/jquery-asAccordion.js', array('jquery'));
	wp_enqueue_script('justgage', get_stylesheet_directory_uri() . '/js/justgage.js', array('jquery'));
	wp_enqueue_script('raphael', get_stylesheet_directory_uri() . '/js/raphael-2.1.4.min.js', array('jquery'));
	wp_enqueue_script('slick', get_stylesheet_directory_uri() . '/js/slick.min.js', array('jquery'));
	wp_enqueue_script('canvasjs', get_stylesheet_directory_uri() . '/js/canvasjs.min.js', array('jquery'));
	//wp_enqueue_script('productinfo', get_stylesheet_directory_uri() . '/js/productinfo.js', array('jquery'));
	wp_localize_script( 'speclist', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
 }

 
function getQuantities($id){ 

     global $wpdb;
	 $tmpArray = array();
		if($id != 0 ) {
			$query = "SELECT * FROM wp_woodlister_products WHERE id = '".$id."'";
			
			$prod = $wpdb->get_results( $query);
			
			$i = 1;
			for($i=1; $i<=10; $i++){
				$breakd_ = 'breakd_'.$i;
				if($prod[0]->$breakd_ == 1){
					$opt = 'brkd_opt_'.$i;
					$query = "SELECT size_label FROM wp_woodlister_wood_ship_breakdown WHERE id = '".$prod[0]->$opt."'";
					$result = $wpdb->get_row( $query);
					array_push($tmpArray, $result->size_label);
				}
			}

			return $tmpArray;
		}
		return;
	}
	
 function getVariations($id){
    global $wpdb;
		$query = "SELECT * FROM wp_woodlister_product_variations WHERE avail_enable='1' AND prod_id = '".$id."' ORDER BY ordering";

		$data = $wpdb->get_results( $query);
		
		$data = getPriceList($data, $id);
		return $data;
	}
	
	function getPriceList($variations, $id){
		$i = 0;
		foreach($variations as $item){

			if($item->breakd_1 == 1 && $item->price_algor_1!='none'){
				$variations[$i]->price_1 = calcPrice($item->id, $item->price_algor_1, $id);
			}else{
				$variations[$i]->price_1 = 0;
			}
			
			if($item->breakd_2 == 1 && $item->price_algor_2!='none'){
				$variations[$i]->price_2 = calcPrice($item->id, $item->price_algor_2, $id);
			}else{
				$variations[$i]->price_2 = 0;
			}
			
			if($item->breakd_3 == 1 && $item->price_algor_3!='none'){
				$variations[$i]->price_3 = calcPrice($item->id, $item->price_algor_3, $id);
			}else{
				$variations[$i]->price_3 = 0;
			}
			
			if($item->breakd_4 == 1 && $item->price_algor_4!='none'){
				$variations[$i]->price_4 = calcPrice($item->id, $item->price_algor_4, $id);
			}else{
				$variations[$i]->price_4 = 0;
			}
			$i++;
		}
		return $variations;
	}
	
	function calcPrice($prod_id, $algor_id, $parentid){
		global $wpdb;

		$query = "SELECT * FROM wp_woodlister_markup_algorithms WHERE published='1' AND id = '".$algor_id."'";
		
		$algorithm = $wpdb->get_results( $query);
		
		$perc_add = 0;
		$perc_subtr = 0;
		$amt_add = 0;
		$amt_subtr = 0;
		
		if($algorithm[0]->perc_add > 0){
			$perc_add = $algorithm[0]->perc_add;
		}
		
		if($algorithm[0]->perc_subtr > 0){
			$perc_subtr = $algorithm[0]->perc_subtr;
		}
		
		if($algorithm[0]->amt_add > 0){
			$amt_add = $algorithm[0]->amt_add;
		}
		
		if($algorithm[0]->amt_subtr > 0){
			$amt_subtr = $algorithm[0]->amt_subtr;
		}

		$query = "SELECT * FROM wp_woodlister_product_variations WHERE avail_enable='1' AND id = '".$prod_id."' ORDER BY id DESC limit 0,1";
		$variation = $wpdb->get_results( $query);
		
		$query = "SELECT * FROM wp_woodlister_price_logs WHERE published='1' AND prod_id = '".$prod_id."' ORDER BY id DESC limit 0,1";
		$log = $wpdb->get_results( $query);
		
		$query = "SELECT roundup FROM wp_woodlister_settings ORDER BY id DESC limit 1";
		$roundup = $wpdb->get_results( $query);		
		
		if($log != '' && $variation[0]->base_price==0){
			$cur_price = $log[0]->price;
		}else if($variation[0]->base_price!=0){
			$cur_price = $variation[0]->base_price;
		}else{
			$cur_price = 0;
		}	
		
		$calc_price = 0;
		
		if($perc_add > 0){
			$perc = $perc_add/100;
			if($amt_add > 0){
				$calc_price = $cur_price + ($perc * $cur_price) + $amt_add;
			}else{
				$calc_price = $cur_price + ($perc * $cur_price) - $amt_subtr;
			}
		}else if($perc_subtr > 0){
			$perc = $perc_subtr/100;
			if($amt_add > 0){
				$calc_price = ($cur_price - ($perc * $cur_price)) + $amt_add;
			}else{
				$calc_price = ($cur_price - ($perc * $cur_price)) - $amt_subtr;
			}
		}else if($perc_add == 0 && $perc_subtr == 0){
			if($amt_add > 0){
				$calc_price = $cur_price + $amt_add;
			}else if($amt_subtr > 0){
				$calc_price = $cur_price - $amt_subtr;
			}else{
				$calc_price = $cur_price;
			}
		}
		
		//roundup to nearest nickel
		if($roundup[0]->roundup == 1){
			$x = 100 / 5;
			$calc_price = number_format(round($calc_price * $x) / $x, 2);
		}
		
		return $calc_price;
	}
	
	function getProductCat($id){
	 global $wpdb;
		
		$q = "SELECT cat_1 FROM wp_woodlister_product_cats WHERE prod_id = '".$id."'";
	    $data = $wpdb->get_results( $q);
		
		$q = "SELECT name FROM wp_k2_categories WHERE id = '".$data[0]->cat_1 ."'";
		$data = $wpdb->get_results( $q);

		return $data[0]->name ;
	}
	
	function getRate($userid, $password, $classof, $specificWeight)
	{
		global $query;
		$query = '<?xml version="1.0" encoding="utf-8" ?>
			<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
			 <soap:Body>
			 <GetQuote xmlns="http://www.echo.com/">
			 <echoRateRequest>
			 <AuthInfo>
			  <UserId>%1s</UserId>
			  <Password>%2s</Password>
			  </AuthInfo>
			 <Requests>
			 <Request>
			  <TotalWeight>%3$s</TotalWeight>
			 <Items>
			 <Item>
			  <Class>%4$s</Class>
			  <Weight>%3$s</Weight>
			  </Item>
			  </Items>
			 <Accessorials>
			 <Accessorial>
			  <AccessorialId>12</AccessorialId>
			  </Accessorial>
			 <Accessorial>
			  <AccessorialId>22</AccessorialId>
			  </Accessorial>
			  </Accessorials>
			 <Origin>
			  <Name />
			  <Address1 />
			  <Address2 />
			  <City />
			  <State />
			  <Zip>%5$s</Zip>
			  </Origin>
			 <Destination>
			  <Name />
			  <Address1 />
			  <Address2 />
			  <City />
			  <State />
			  <Zip>%6$s</Zip>
			  </Destination>
			  <PickupDate>'.date("Y-m-d").'</PickupDate>
			  <ShipmentType>Third Party</ShipmentType>
			  <PalletQty>0</PalletQty>
			  <ReturnMultipleCarriers>false</ReturnMultipleCarriers>
			  <SaveQuote>false</SaveQuote>
			  </Request>
			  </Requests>
			  </echoRateRequest>
			  </GetQuote>
			  </soap:Body>
			  </soap:Envelope>
			';

		if ($_POST){
			$skip = $_POST['skip'];
			if($skip != 1){
				if($_POST['dimen']){
					$bf = $_POST['bf'];
					$dimen = $_POST['dimen'];
					$bf = convertToBf($dimen, $bf);
				}else{
					$bf = $_POST['bf'];
				}
			}else{
				$bf = $_POST['bf'];
			}
			$destination = $_POST['destination'];
			$origin = $_POST['origin'];
			$specificWeight = $_POST['specificWeight'];
			$classof = $_POST['classof'];
			$markup = $_POST['markup'];
		}else{
			return;
		}
		
		$weight=$bf*$specificWeight;
		$formattedQuery = sprintf($query, $userid, $password, $weight, $classof, $origin, $destination);
		
		$ch = curl_init ();
		curl_setopt ($ch, CURLOPT_URL, "http://services.echo.com/Quote.asmx");
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_TIMEOUT, 20);
		curl_setopt ($ch, CURLOPT_HEADER, 1);
		curl_setopt ($ch, CURLOPT_POST, 1);
		curl_setopt ($ch, CURLOPT_POSTFIELDS,$formattedQuery);
		$header = array();
		$header[] = "Content-Type: text/xml";
		$header[] = 'SOAPAction: "http://www.echo.com/GetQuote"';
		$header[] = "User-Agent: windows";
		$header[] = "Host: services.echo.com";
		$header[] = "Content-Length: ".strlen($formattedQuery);
		$header[] = "Connection: close";
		curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);

		$result = curl_exec ($ch);
		$header_size = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
		$xml = substr($result, $header_size );
		curl_close($ch);
	

		return $xml;
	}
	
	function convertToBf($str, $amt){
		
		$q = "SELECT conversion FROM wp_er_ship_dimen_vals WHERE dimension = '".$str."'";
			
		$conv =$wpdb->get_results( $q);
		
		$new_bf = number_format($conv[0]->conversion, 2, '.', '')*$amt;
		return $new_bf;
	}
	
	function getLowestCost($rateXML){
		$xmlString = str_replace('xmlns="http://www.echo.com/"', '', $rateXML);
		$domObj = new DOMDocument();
		$domObj->loadXML($xmlString);
		$xPath = new DOMXPath($domObj);

		$retValues = array();

		$carrier = $xPath->query('//RateDetail[not(Charges/TotalCharge <= preceding-sibling::RateDetail/Charges/TotalCharge)and not(Charges/TotalCharge <= following-sibling::RateDetail/Charges/TotalCharge)]/Carrier/CarrierName');
		$charge = $xPath->query('//RateDetail[not(Charges/TotalCharge <= preceding-sibling::RateDetail/Charges/TotalCharge)and not(Charges/TotalCharge <= following-sibling::RateDetail/Charges/TotalCharge)]/Charges/TotalCharge');
		$errorMsgs=$xPath->query('//Messages/Errors');
		$errorMsgNode=$errorMsgs->item( 0 );
		if(isset($errorMsgNode->firstChild)){
			$errorMsg=$errorMsgNode->firstChild->nodeValue;
			if(isset($errorMsg)){
				$retValues["errorMsg"]=$errorMsg;
			}
		}
		$warningMsgs=$xPath->query('//Messages/Warnings');
		$warningMsgNode=$warningMsgs->item( 0 );
		if(isset($warningMsgNode->firstChild)){
			$warningMsg=$warningMsgNode->firstChild->nodeValue;
			if(isset($warningMsg)){
				$retValues["warningMsg"]=$warningMsg;
			}
		}
		$retValues["carrier"] = $carrier;
		$retValues["cost"] = $charge;
		
		return $retValues;
	}

	function getFinalCost($cost, $markup){
		$markupValue=$markup;
		if (is_numeric($markupValue)){
			$cost+=$markupValue;
		}
		return number_format ( $cost, 2 );
	}
	
	function calcSubtotal_callback()
	{
	   if(isset($_REQUEST['title'])){
	       $prodtitle = $_REQUEST['title'];
        }   
		if(isset($_REQUEST['id'])){
			$id = $_REQUEST['id'];
		}
		if(isset($_REQUEST['need'])){
			$need = $_REQUEST['need'];
		}
		if(isset($_REQUEST['pr1'])){
			$p1 = $_REQUEST['pr1'];
		}
		if(isset($_REQUEST['pr2'])){
			$p2 = $_REQUEST['pr2'];
		}
		if(isset($_REQUEST['pr3'])){
			$p3 = $_REQUEST['pr3'];
		}
		if(isset($_REQUEST['pr4'])){
			$p4 = $_REQUEST['pr4'];
		}
		if(isset($_REQUEST['pr5'])){
			$p5 = $_REQUEST['pr5'];
		}

		$brk_1 = 0;
		$brk_2 = 0;
		$brk_3 = 0;
		$brk_4 = 0;
		$brk_5 = 0;
		$subtot = 0;

		global $wpdb;
		$q  = "SELECT * FROM wp_woodlister_products WHERE id=".$id; 
		$row = $wpdb->get_results( $q);
		
		$brk_1 = $row[0]->brkd_opt_1;
		$brk_2 = $row[0]->brkd_opt_2;
		$brk_3 = $row[0]->brkd_opt_3;
		$brk_4 = $row[0]->brkd_opt_4;
		$brk_5 = $row[0]->brkd_opt_5;

		$q  = "SELECT * FROM wp_woodlister_wood_ship_breakdown"; 
		$result = $wpdb->get_results( $q);

			foreach($result as $row)
			{
			if($brk_1 == $row->id){
				$min_1 = $row->min_size;
				$max_1 = $row->max_size;
			}
			if($brk_2 == $row->id){
				$min_2 = $row->min_size;
				$max_2 = $row->max_size;
			}
			if($brk_3 == $row->id){
				$min_3 = $row->min_size;
				$max_3 = $row->max_size;
			}
			if($brk_4 == $row->id){
				$min_4 = $row->min_size;
				$max_4 = $row->max_size;
			}
			if($brk_5 == $row->id){
				$min_5 = $row->min_size;
				$max_5 = $row->max_size;
			}
		}

		switch($need){
			case ($need < $min_1):
									$subtot = 0;
									break;
			case ($need >= $min_1 && $need < $min_2):
			case ($need >= $min_1 && $min_2==0):
									$subtot = $p1*$need;
									break;
			case ($need >= $min_2 && $need < $min_3):
			case ($need >= $min_2 && $min_3==0):
									$subtot = $p2*$need;
									break;
			case ($need >= $min_3 && $need < $min_4):
			case ($need >= $min_3 && $min_4==0):
									$subtot = $p3*$need;
									break;
			case ($need >= $min_4 && $need < $min_5):
			case ($need >= $min_4 && $min_5==0):
									$subtot = $p4*$need;
									break;
			case ($need >= $min_5):
									$subtot = $p5*$need;
									break;
		}

		echo number_format($subtot, 2, '.', '');
		return;
	}
	
  add_action('wp_ajax_nopriv_calcSubtotal', 'calcSubtotal_callback');
  add_action('wp_ajax_calcSubtotal', 'calcSubtotal_callback');
  
  function convertMeas_callback()
	{
			if($_POST){
				$val = $_REQUEST['val'];
				$desc = $_REQUEST['desc'];
		    }
			$parts = explode(" ", $desc);

            global $wpdb;
			$list  = "SELECT * FROM wp_er_ship_dimen_vals "; 	
            $result = $wpdb->get_results($list);

			$convers_num = 1;

			foreach($result as $row) {
				$id = $row->id;
				$dimen = $row->dimension;
				$conv = $row->conversion;
				
				foreach($parts as $a=>$txt){
					if($txt === $dimen){
						$convers_num = $conv;
					}
				}
			}
			
			$newMeas = $convers_num*$val;
			echo $newMeas;	die();
	}
	
  add_action('wp_ajax_nopriv_convertMeas', 'convertMeas_callback');
  add_action('wp_ajax_convertMeas', 'convertMeas_callback');
  
  
    function quoteShipping_callback()
	{
			if ($_POST){
			$userid = $_REQUEST['userid'];
	        $password = $_REQUEST['password'];	
			$classof = $_REQUEST['classof'];
			$specificWeight = $_REQUEST['specificWeight'];
			$origin = $_REQUEST['origin'];
			$dest = $_REQUEST['dest'];
			$markup = $_REQUEST['markup'];
			//$productid = $_REQUEST['product'];
			
			$raterXML = getRate($userid, $password, $classof, $specificWeight);
			$lowestCarrier = getLowestCost($raterXML);
			
			echo '<div id="resDiv" style="margin: 5px; padding:0px; background-color:#efefef; color:#000080; text-shadow:none; ">';
			
			$carrier = $lowestCarrier['carrier']->item(0);
			$cost = $lowestCarrier['cost']->item(0);
			$err = 0;
			
			echo '<table style="width:100%;margin:10px 5px;">';
			
			if (isset($lowestCarrier["errorMsg"])){
				//echo '<br />' .'Error: <strong>' . $lowestCarrier["errorMsg"] . '</strong><br />';
				echo '<tr><td style="text-align:center;">';
				echo '<strong>No carriers found for this specific order. <br />Call WoodVendors for a price quote, 1-877-983-6367.</strong><br />';
				echo '</td></tr>';
				$err = 1;
			}
			if (isset($lowestCarrier["warningMsg"])){
				echo '<p><strong>No carriers found for this specific order. <br />Call WoodVendors for a price quote, 1-877-983-6367.</strong><br /></p>';
				$err = 1;
			}
				
			if($err != 1){	
				echo '<tr><td style="text-align:center;">';
				echo '<span id="delivMsg" style="font-size:16px;line-height:18px;font-weight:bold;">Your Full Delivered Price is... $<span id="totCost"></span></span>';
				echo '</td></tr>';
				
				for($i=1; $i<=12; $i++){
					echo '<tr><td><span id="purchase_'.$i.'"></span></td></tr>';
				}
				
				echo '<tr><td>';
				echo 'Delivery Charge to ';
				echo $dest;
				echo ' = $';
				if($markup){
					echo '<span id="delivCost">'.getFinalCost($cost->nodeValue,$markup).'</span>';
				}else{
					echo '<span id="delivCost">'.getFinalCost($cost->nodeValue,0).'</span>';
				}
				echo ' via ';
				if($carrier->nodeValue == ""){
					echo '<br /><strong>There is a problem with this shipping calculation. <br />Call WoodVendors for a price quote, 1-877-983-6367.</strong><br />';
				}else{
					echo $carrier->nodeValue;
				}
				echo '</td></tr>';
				$imageUrl = get_stylesheet_directory_uri();
				echo '<tr>
						<td style="float:left;padding:15px 15px 0 2px;"><em>Questions?  Call us 877.983.6367 </em></td>
						<td style="float:left;padding:15px 15px 3px 0;"><div id="quick_click2" style="display:none;"><a href="tel:8779836367"><img src="'.$imageUrl.'/images/click-to-call.gif" alt="Click to Call" /></a></div></td>
						<td style="float:right;padding:15px 15px 0 15px;"><em>Residential Deliveries add $100</em></td>
					</tr>';
			}
				
			echo '</table>';
			echo '</div>';
		}	
		die();
	}
	
  add_action('wp_ajax_nopriv_quoteShipping', 'quoteShipping_callback');
  add_action('wp_ajax_quoteShipping', 'quoteShipping_callback');
  
  function getInfo_callback()
	{
				$id = $_REQUEST["id"];
				$html = '';
				global $wpdb;
				$sql = "SELECT * FROM wp_er_ship_products WHERE id=".$id; 
				$row = $wpdb->get_results($sql);

				$i = 1;
				
				$html .= "<br />";
				
				//set up a max of four different zip codes and descriptions. Must have both a zip code and a label.
				while($i<5){
					$origin = "origin_".$i;
					$originLabel = "originLabel_".$i;
					
					if($row[0]->$origin==''||$row[0]->$origin==0){
						break;
					}
					
					$html .= '<input type="radio" name="origin" id="origin" value="'. $row[0]->$origin ;
					
					if($i==1){
						$html .= '" checked />'. $row[0]->$originLabel;
					}else{
						$html .= '" />'. $row[0]->$originLabel;
					}
					$html .= "<br />";
					$i++;
				}
				
				$html .= '<input type="hidden" name="markup" id="markup" value="'. $row[0]->markup .'" />';
				$html .= '<input type="hidden" name="specificWeight" id="specificWeight" value="'. $row[0]->specific_weight. '" />';
				$html .= '<input type="hidden" name="classof" id="classof" value="'. $row[0]->freight_class .'" />';
				
				die($html);
	}
	
  add_action('wp_ajax_nopriv_getInfo', 'getInfo_callback');
  add_action('wp_ajax_getInfo', 'getInfo_callback');
  
    function getSpecies_callback()
	{
		if(isset($_REQUEST['title'])){
			$prodtitle = $_REQUEST['title'];
		}

		if(isset($_REQUEST['id'])){
			$prod = $_REQUEST['id'];
		}
        global $wpdb;

		$html = '';
		
		switch($prod){
			case '0'://freight species rate category
				$list  = "SELECT * FROM wp_er_ship_products WHERE catid=8 AND published=1 ORDER BY title ASC "; 
				break;
			case '1'://freight moulding rate category
				$list  = "SELECT * FROM wp_er_ship_products WHERE catid=12 AND published=1 ORDER BY title ASC "; 
				break;
			case '2'://freight dimension rate category
				$list  = "SELECT * FROM wp_er_ship_products WHERE catid=14 AND published=1 ORDER BY title ASC "; 
				break;
			case '3'://freight decking rate category
				$list  = "SELECT * FROM wp_er_ship_products WHERE catid=13 AND published=1 ORDER BY title ASC "; 
				break;
			default:
				$list  = "SELECT * FROM wp_er_ship_products WHERE published=1 ORDER BY title ASC "; 
				break;
		}
		$result = $wpdb->get_results($list);

		$html .= '<select id="speciesSelector" style="width:180px; margin-left:3px;" class="inputbox" name="product" id="product" size="1" onchange="showProdInfo(this.value, 1);selValidation();" >';


		if (count($result) == 0) {
			$html .= '<option> ' ;
			$html .= 'No options available';
			$html .= '</option>';
		} else { 
			$html .= '<option value="-1">Select</option>';
			
			foreach ($result as $row) {
				$optionSel = $row->title;
				$html .= '<option value="'. $row->id.'" ' ;
				
				$substr = strpos($prodtitle, $optionSel);
				if($substr !== false){
					$html .= 'selected="selected" ';
				}
				
				$html .= '>'. $row->title;
				$html .= '</option>';
			}
		}

		$html .= '</select>';
		
		 die($html);
	}
	
  add_action('wp_ajax_nopriv_getSpecies', 'getSpecies_callback');
  add_action('wp_ajax_getSpecies', 'getSpecies_callback');
  
    
    function getDimens_callback()
	{
		global $wpdb;
		if(isset($_REQUEST['id'])){
			$prod = $_REQUEST['id'];
		}
		$list  = "SELECT chk_meas_group FROM wp_er_ship_products WHERE id=".$prod; 		
		
		$result = $wpdb->get_results($list);
		$boxes = explode(',', $result[0]->chk_meas_group);

		if($result){
			$i=1;
			foreach($boxes as $chkbox=>$value){
				echo '<input type="radio" id="dimen" name="dimen" value="'.$value.'" /><label for="dimen" style="padding-right:25px;">'.$value.'</label>';
				if($i==3){
					echo "<br />";
					$i = 0;
				}
				$i++;
			}
		}
	}
	
  add_action('wp_ajax_nopriv_getDimens', 'getDimens_callback');
  add_action('wp_ajax_getDimens', 'getDimens_callback');
?>