<?php
global $wpdb;
global $table_prefix;
if(!isset($wpdb)){
    require_once(ABSPATH .'wp-config.php');
    require_once(ABSPATH .'wp-includes/wp-db.php');
}

$arrvalues = array('','100%','fefaf1','1px solid #fff','ffffff','11px','47AFD4','333333','11px','DCF4F7','hide','hide','show','hide','hide','show','show','USD','$','Tax','0','','0','','0','','Online Quote','Price','Quantity','Discount','Amount','Submit an Order','Submit','4D97C2','ffffff','Additional Charge','Subtotal','Discount','Total before Tax','Total','Your Order has been Submitted!');

$order_form = array("Online Order","Name","E-mail","Address","Phone","When do You need the service?", "Time of arrival","1","Best time to contact you?","","","","Memo","Captcha",get_option('admin_email'));

$order_value = array("show","show (required field)","show (required field)","hide","hide","hide","hide","show","hide","hide","hide","hide","hide","hide","Automatically send an order to the customer.");

$calvalues = array('','January','February','March','April','May','June','July','August','September','October','November','December','Sun','Mon','Tue','Wed','Thu','Fri','Sat');

$arrfields = array('','Form width','Form background','Form border','Header text color','Header font size','Header background','Font color','Font size','Font background','Quantity Discounts <span style="font-size:10px;">(show or hide)</span>','Order Total Discount <span style="font-size:10px;">(show or hide)</span>','Description <span style="font-size:10px;">(show or hide)</span>','Price <span style="font-size:10px;">(show or hide)</span>','Subtotal Amount <span style="font-size:10px;">(show or hide)</span>','Total before tax <span style="font-size:10px;">(show or hide)</span>','Tax <span style="font-size:10px;">(show or hide)</span>','Currency Symbol','Currency Sign','Tax 1 <span style="font-size:10px;">(the name of a field)</span>','Tax 1 (%)','Tax 2 <span style="font-size:10px;">(the name of a field)</span>','Tax 2 (%)','Tax 3 <span style="font-size:10px;">(the name of a field)</span>','Tax 3 (%)','Header Name 1','Header Name 2','Header Name 3','Header Name 4','Header Name 5','Header Name 6','Button 1','Button 2','Buttons background','Buttons text color','Additional Charge <span style="font-size:10px;">(the name of a field)</span>','Subtotal <span style="font-size:10px;">(the name of a field)</span>','Discount <span style="font-size:10px;">(the name of a field)</span>','Total before Tax <span style="font-size:10px;">(the name of a field)</span>','Total <span style="font-size:10px;">(the name of a field)</span>','Final message');

	for($x=0; $x<=40; $x++){
		    if($x!=0){
			if ( get_option('setting_qg'.$x) == false ) {
			    add_option( 'setting_qg'.$x, $arrvalues[$x], '', 'yes' );
			}
		    }
			    if($x<count($order_form)){
				add_option( 'setting_qgname'.$x, $order_form[$x], '', 'yes' );
	                        add_option( 'setting_qgform'.$x, $order_value[$x], '', 'yes' );
			    }
			    if($x<=19){
    	                        if ( get_option('setting_qgc'.$x) == false ) {
                                     add_option( 'setting_qgc'.$x, $calvalues[$x], '', 'yes' );
	                        }
			    }
	}
                        
$tbname = $wpdb->prefix ."qg_itemx";
$tbname4 = $wpdb->prefix ."qg_calendar";
$tbname5 = $wpdb->prefix ."qg_orders";
$tbname2 = $wpdb->prefix ."qg_invoices";
$charset_collate = $wpdb->get_charset_collate();

			$ofields = '';
			for($ff=3;$ff<=40;$ff++){  $ofields .= 'field_'.$ff.' VARCHAR(256),';	}
			$sql = "CREATE TABLE IF NOT EXISTS $tbname ( id mediumint(9) NOT NULL AUTO_INCREMENT, idn mediumint(9), field_1 VARCHAR(256), field_2 text,".$ofields." visible VARCHAR(9), UNIQUE KEY id (id))$charset_collate;";
			require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
			dbDelta( $sql );
			
			$sql4 = "CREATE TABLE IF NOT EXISTS $tbname4 (id mediumint(9) NOT NULL AUTO_INCREMENT, field_1 VARCHAR(32),field_2 VARCHAR(32),field_3 VARCHAR(32),field_4 VARCHAR(32),field_5 text,visible VARCHAR(9), UNIQUE KEY id (id))$charset_collate;";
			dbDelta( $sql4 );
			
			$ofields = '';$ofields2 = '';
			for($ff=1;$ff<12;$ff++){$ofields .= 'field_'.$ff.' VARCHAR(64),';$ofields2 .= 'field_'.$ff.',';}
			$sql5 = "CREATE TABLE IF NOT EXISTS $tbname5 (id mediumint(9) NOT NULL AUTO_INCREMENT, order_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,".$ofields." field_12 VARCHAR(255), ip VARCHAR(32) DEFAULT '' NOT NULL, data_field text, subtotal VARCHAR(32), discount VARCHAR(32), total_before_tax VARCHAR(32), tax_1 VARCHAR(32), tax_2 VARCHAR(32), tax_3 VARCHAR(32), total VARCHAR(32), visible VARCHAR(9), UNIQUE KEY id (id))$charset_collate;";
			dbDelta( $sql5 );
			
			
			$sql2 = "CREATE TABLE IF NOT EXISTS $tbname2 (id mediumint(9) NOT NULL AUTO_INCREMENT, invoice_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, order_id VARCHAR(64), order_num VARCHAR(64), client VARCHAR(64), data_field text, visible VARCHAR(9), UNIQUE KEY id (id))$charset_collate;";
			dbDelta( $sql2 );
