!function() {
     $.backToDash.hide();
     $.backToDash.height = 0;
     var angle, grade, shortener, end, upper, Database = require("databaseObj"), database = new Database("SlingDB.sqlite"), db = database.openDb(), legLength = Math.ceil(Alloy.Globals.sling.nominalLength);
     Ti.API.info(Alloy.Globals.sling);
     Alloy.Globals.wllAngle = null;
     Alloy.Globals.wllId = null;
     Alloy.Globals.slingId = null;
     if ("Chain" === Alloy.Globals.sling.type) {
         var angle, grade, shortener, end, upper;
         if (45 === Alloy.Globals.sling.angle) {
             angle = "wll.limit45";
             Alloy.Globals.wllAngle = "limit45";
         } else {
             angle = "wll.limit60";
             Alloy.Globals.wllAngle = "limit60";
         }
         shortener = "NONE" === Alloy.Globals.sling.shorteningDeviceCode ? 'AND s.shortener LIKE ""' : 'AND s.shortener LIKE "' + Alloy.Globals.sling.shorteningDeviceCode + '"';
         end = "NONE" === Alloy.Globals.sling.lowerTerminationCode ? 'AND end = ""' : 'AND end = "' + Alloy.Globals.sling.lowerTerminationCode + '"';
         upper = "NONE" === Alloy.Globals.sling.upperTerminationCode ? 'AND end = ""' : 'AND end = "' + Alloy.Globals.sling.upperTerminationCode + '"';
         if (8 === Alloy.Globals.sling.grade || 10 === Alloy.Globals.sling.grade) {
             grade = Alloy.Globals.sling.grade;
             var sql = "SELECT *, wll.id AS wllId, s.id AS slingId FROM WorkingLoadLimits AS wll, Slings AS s WHERE " + angle + " >= " + Alloy.Globals.sling.load + ' AND wll.type="c" AND wll.legs = ' + Alloy.Globals.sling.legs + " AND s.legs=wll.legs AND (wll.grade = " + grade + ") AND (s.size=wll.size AND s.grade=wll.grade " + end + " " + shortener + ' AND s.length = "' + legLength + '") GROUP BY wll.id ORDER BY ' + angle + " LIMIT 1";
             var row = db.execute(sql);
             Ti.API.info(sql);
             if (row.isValidRow()) {
                 Alloy.Globals.wllId = row.fieldByName("wllId");
                 Alloy.Globals.slingId = row.fieldByName("slingId");
                 Alloy.Globals.sling.partCode = row.fieldByName("code");
                 Alloy.Globals.sling.quotedPrice = row.fieldByName("price");
                 Alloy.Globals.sling.slingDescription = row.fieldByName("description");
             } else {
                 Ti.API.info("row 10 create part code");
                 createPartcode();
             }
         } else if ("Auto" === Alloy.Globals.sling.grade) {
             var price8, code8, description8, wllId8, slingId8, price10, code10, description10, wllId10, slingId10, angle, grade, shortener, end, upper;
             if (45 === Alloy.Globals.sling.angle) {
                 angle = "wll.limit45";
                 Alloy.Globals.wllAngle = "limit45";
             } else {
                 angle = "wll.limit60";
                 Alloy.Globals.wllAngle = "limit60";
             }
             shortener = "NONE" === Alloy.Globals.sling.shorteningDeviceCode ? "AND s.shortener IS NULL" : 'AND s.shortener LIKE "' + Alloy.Globals.sling.shorteningDeviceCode + '"';
             end = "NONE" === Alloy.Globals.sling.lowerTerminationCode ? "AND s.end IS NULL" : 'AND s.end = "' + Alloy.Globals.sling.lowerTerminationCode + '"';
             upper = "NONE" === Alloy.Globals.sling.upperTerminationCode ? "AND s.end IS NULL" : 'AND s.end = "' + Alloy.Globals.sling.upperTerminationCode + '"';
             var sql = "SELECT *, wll.id AS wllId, s.id AS slingId FROM WorkingLoadLimits AS wll, Slings AS s WHERE " + angle + " >= " + Alloy.Globals.sling.load + ' AND wll.type="c" AND wll.legs = ' + Alloy.Globals.sling.legs + ' AND s.legs = wll.legs AND (wll.grade = "8" ) AND (s.size=wll.size AND s.grade=wll.grade ' + end + " " + shortener + " AND s.length = " + legLength + ") GROUP BY wll.id ORDER BY " + angle + " ASC";
             var row8 = db.execute(sql);
             if (row8.isValidRow()) {
                 price8 = row8.fieldByName("price");
                 code8 = row8.fieldByName("code");
                 description8 = row8.fieldByName("description");
                 wllId8 = row8.fieldByName("wllId");
                 slingId8 = row8.fieldByName("slingId");
             } else Ti.API.info("Row 8 not valid");
             var sql = "SELECT *, wll.id AS wllId, s.id AS slingId FROM WorkingLoadLimits AS wll, Slings AS s WHERE " + angle + " >= " + Alloy.Globals.sling.load + ' AND wll.type="c" AND wll.legs = ' + Alloy.Globals.sling.legs + ' AND s.legs=wll.legs AND (wll.grade = "10" ) AND (s.size=wll.size AND s.grade=wll.grade ' + end + " " + shortener + " AND s.length = " + legLength + ") GROUP BY wll.id ORDER BY " + angle + " ASC";
             var row10 = db.execute(sql);
             if (row10.isValidRow()) {
                 price10 = row10.fieldByName("price");
                 code10 = row10.fieldByName("code");
                 description10 = row10.fieldByName("description");
                 wllId10 = row10.fieldByName("wllId");
                 slingId10 = row10.fieldByName("slingId");
             } else Ti.API.info("Row 10 not valid");
             if (void 0 !== price8 && null !== price8 || void 0 !== price10 && null !== price10) {
                 if (void 0 === price10 || null === price10) {
                     Alloy.Globals.sling.partCode = code8;
                     Alloy.Globals.sling.quotedPrice = price8;
                     Alloy.Globals.sling.slingDescription = description8;
                     Alloy.Globals.wllId = wllId8;
                     Alloy.Globals.slingId = slingId8;
                 } else if (void 0 === price8 || null === price8) {
                     Alloy.Globals.sling.partCode = code10;
                     Alloy.Globals.sling.quotedPrice = price10;
                     Alloy.Globals.sling.slingDescription = description10;
                     Alloy.Globals.wllId = wllId10;
                     Alloy.Globals.slingId = slingId10;
                 } else if (parseInt(price8) < parseInt(price10)) {
                     Alloy.Globals.sling.partCode = code8;
                     Alloy.Globals.sling.quotedPrice = price8;
                     Alloy.Globals.sling.slingDescription = description8;
                     Alloy.Globals.sling.gradeCode = 8;
                     Alloy.Globals.wllId = wllId8;
                     Alloy.Globals.slingId = slingId8;
                 } else if (parseInt(price8) > parseInt(price10)) {
                     Alloy.Globals.sling.partCode = code10;
                     Alloy.Globals.sling.quotedPrice = price10;
                     Alloy.Globals.sling.slingDescription = description10;
                     Alloy.Globals.sling.gradeCode = 10;
                     Alloy.Globals.wllId = wllId10;
                     Alloy.Globals.slingId = slingId10;
                 } else if (parseInt(price8) === parseInt(price10)) {
                     Alloy.Globals.sling.partCode = code10;
                     Alloy.Globals.sling.quotedPrice = price10;
                     Alloy.Globals.sling.slingDescription = description10;
                     Alloy.Globals.sling.gradeCode = 10;
                     Alloy.Globals.wllId = wllId10;
                     Alloy.Globals.slingId = slingId10;
                 }
             } else {
                 Alloy.Globals.sling.quotedPrice = null;
                 Alloy.Globals.sling.slingDescription = null;
                 createPartcode();
             }
         }
     } else if ("Wire Rope" === Alloy.Globals.sling.type) {
         var angle, grade, shortener, end, upper;
         if (45 === Alloy.Globals.sling.angle) {
             angle = "wll.limit45";
             Alloy.Globals.wllAngle = "limit45";
         } else {
             angle = "wll.limit60";
             Alloy.Globals.wllAngle = "limit60";
         }
         end = "NONE" === Alloy.Globals.sling.lowerTerminationCode ? ' AND end = "" ' : ' AND end = "' + Alloy.Globals.sling.lowerTerminationCode + '" ';
         upper = "NONE" === Alloy.Globals.sling.upperTerminationCode || null === Alloy.Globals.sling.upperTerminationCode ? ' AND end_b = "" ' : ' AND end_b = "' + Alloy.Globals.sling.upperTerminationCode + '" ';
         var sql = "SELECT *, wll.id AS wllId, s.id AS slingId FROM WorkingLoadLimits AS wll, Slings AS s WHERE " + angle + " >= " + Alloy.Globals.sling.load + " AND wll.type='r' AND wll.legs = " + Alloy.Globals.sling.legs + " AND s.grade = '' AND s.legs = wll.legs AND s.size = wll.size AND s.length = " + legLength + " " + end + upper + "GROUP BY wll.id ORDER BY " + angle + " ASC LIMIT 1";
         Ti.API.info(sql);
         var row = db.execute(sql);
         if (row.isValidRow()) {
             Ti.API.info("DB part code: " + row.fieldByName("code"));
             Alloy.Globals.sling.partCode = row.fieldByName("code");
             Alloy.Globals.sling.quotedPrice = row.fieldByName("price");
             Alloy.Globals.sling.slingDescription = row.fieldByName("description");
             Alloy.Globals.wllId = row.fieldByName("wllId");
             Alloy.Globals.slingId = row.fieldByName("slingId");
         } else {
             Ti.API.info("wire rope create part code");
             createPartcode();
             Alloy.Globals.sling.grade = null;
             Alloy.Globals.sling.quotedPrice = null;
             Alloy.Globals.sling.slingDescription = null;
         }
     }
     outputDetails(Alloy.Globals.sling.type, Alloy.Globals.sling.grade, Alloy.Globals.sling.legs, Alloy.Globals.sling.load, Alloy.Globals.sling.nominalLength, Alloy.Globals.sling.slingDescription, Alloy.Globals.sling.partCode, Alloy.Globals.sling.quotedPrice);
     database.closeDb(db);
     checkImage(Alloy.Globals.sling.partCode);
     checkSpec();
 }();
function createPartcode(){
	
	Ti.API.info("createPartcode call");
	
	var Database = require('databaseObj'),
		database = new Database('SlingDB.sqlite'),
		db = database.openDb(),
		gradeCode,
		limit,
		type,
		slingSize,
		slingSizeAuto8,
		slingSizeAuto10,
		legLength = Math.ceil(parseFloat(Alloy.Globals.sling.nominalLength));
	
	// sort Angle
	if( Alloy.Globals.sling.angle === 45 ){
		limit = 'limit45';
	}else{	
		limit = 'limit60';
	}
	
	if( Alloy.Globals.sling.type === 'Chain'){
	// Configure a part code for a Chain Sling
		
		// sort Code	
		if( Alloy.Globals.sling.grade === 8 ){
			
			// 'A' represents Grade 8 in the finished part code
			gradeCode = 'A';
			
			var newRow = db.execute('SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "8" AND ' + limit + ' >= ' + Alloy.Globals.sling.load + ' AND type = "c" ORDER BY ' +  limit + ' ASC LIMIT 1');
			
			// Store Sling size
			if( newRow.isValidRow() ){	
				slingSize = newRow.fieldByName('size');
			
			}else{
				
				Ti.API.info('Grade 8 Limited Exceeded');
				
				Alloy.Globals.sling.limitExceeded = true;
			}
			
		}else if( Alloy.Globals.sling.grade === 10){
			
			// 'X' represents Grade 10 in the finished part code
			gradeCode = 'X';
			
			var newRow = db.execute('SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "10" AND ' + limit + ' >= ' + Alloy.Globals.sling.load + ' AND type = "c" ORDER BY ' +  limit + ' ASC LIMIT 1');
			
			// Store Sling size
			if( newRow.isValidRow() ){	
				slingSize = newRow.fieldByName('size');
			
			}else{
				
				Ti.API.info('Grade 10 Limited Exceeded');
				
				Alloy.Globals.sling.limitExceeded = true;
			}
			
		}else if( Alloy.Globals.sling.grade === 'Auto'){
			// Query DB with known info to get sling size
			var row8 = db.execute('SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "8" AND ' + limit + ' >= ' + Alloy.Globals.sling.load + ' AND type = "c" ORDER BY ' +  limit + ' ASC LIMIT 1');
			
			Ti.API.info('Query: SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "8" AND ' + limit + ' >= ' + Alloy.Globals.sling.load + ' AND type = "' + type + '" ORDER BY ' +  limit + ' ASC LIMIT 1' );
			
			// Store Sling size
			if( row8.isValidRow() ){	
				slingSizeAuto8 = row8.fieldByName('size');
				
				Ti.API.info('sling size 8: ' + slingSizeAuto8 );
			}else{
				
				Ti.API.info('Auto 8 Limited Exceeded');
				
				Alloy.Globals.sling.limitExceeded = true;
			}
			
			var row10 = db.execute('SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "10" AND ' + limit + ' >= ' + Alloy.Globals.sling.load + ' AND type = "c" ORDER BY ' +  limit + ' ASC LIMIT 1');
			
			Ti.API.info('Query: SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "10" AND ' + limit + ' >= ' + Alloy.Globals.sling.load + ' AND type = "' + type + '" ORDER BY ' +  limit + ' ASC LIMIT 1' );
			
			// Store Sling size
			if( row10.isValidRow() ){	
				slingSizeAuto10 = row10.fieldByName('size');
				
				Ti.API.info('sling size 10: ' + slingSizeAuto10 );
			}else{
				
				Ti.API.info('Auto 10 Limited Exceeded');
				
				Alloy.Globals.sling.limitExceeded = true;
			}
		}
		
	}else{
	// Configure a part code for a Wire Rope Sling
		
		type = 'r';
		
		// Query DB with known info to get sling size
		var newRow = db.execute('SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND ' + limit + ' >= ' + Alloy.Globals.sling.load + ' AND type = "' + type + '" ORDER BY ' +  limit + ' ASC LIMIT 1');
		
		// Store Sling size
		if( newRow.isValidRow() ){	
			
			slingSize = newRow.fieldByName('size');
		
		}else{
				
				Ti.API.info('Limited Exceeded');
				
				Alloy.Globals.sling.limitExceeded = true;
			}
	}
	
	if( Alloy.Globals.sling.shorteningDeviceCode === 'NONE' ){
		Alloy.Globals.sling.shorteningDeviceCode = 0;
	}
	if( Alloy.Globals.sling.lowerTerminationCode === 'NONE' ){
		Alloy.Globals.sling.lowerTerminationCode = 0;
	}
	if( Alloy.Globals.sling.upperTerminationCode === 'NONE' ){
		Alloy.Globals.sling.upperTerminationCode = 0;
	}
	
	// If the sling load limit has not been exceeded
	// Continue to format the sling size
	if( !Alloy.Globals.sling.limitExceeded ){
		// Pad sling size if neccessary
		if( slingSize < 10 ){
			slingSize = '0' + slingSize;
		}
		
		if( slingSizeAuto8 < 10 ){
			slingSizeAuto8 = '0' + slingSizeAuto8;
		}
		
		if( slingSizeAuto10 < 10 ){
			slingSizeAuto10 = '0' + slingSizeAuto10;
		}
		
		// Pad nominal length if neccessary
		if( legLength < 10 ){
			legLength = '0' + legLength;
		}
	}
	
	// Create a Chain Sling part code
	if( Alloy.Globals.sling.type === 'Chain' ){
		
		if( Alloy.Globals.sling.grade !== 'Auto'){
			
			Alloy.Globals.sling.partCode = gradeCode + slingSize + '.' + Alloy.Globals.sling.legs + legLength + '.' + Alloy.Globals.sling.lowerTerminationCode + Alloy.Globals.sling.shorteningDeviceCode;
			
		}else{
			
			Alloy.Globals.sling.partCode = 'A' + slingSizeAuto8 + '.' + Alloy.Globals.sling.legs + legLength + '.' + Alloy.Globals.sling.lowerTerminationCode + Alloy.Globals.sling.shorteningDeviceCode
											+ ' or ' +
											'X' + slingSizeAuto10 + '.' + Alloy.Globals.sling.legs + legLength + '.' + Alloy.Globals.sling.lowerTerminationCode + Alloy.Globals.sling.shorteningDeviceCode;
		}
	
	
	// Create a Wire Rope sling part code	
	}else{
		
		// If legs === 1 add upper temination
		if( Alloy.Globals.sling.legs === 1){

			if(Alloy.Globals.sling.upperTerminationCode === 'NONE'){
				
				Alloy.Globals.sling.partCode = 'RS0' + slingSize + '.' + Alloy.Globals.sling.legs + legLength + '.' + Alloy.Globals.sling.lowerTerminationCode;
			}else{
				
				Alloy.Globals.sling.partCode = 'RS0' + slingSize + '.' + Alloy.Globals.sling.legs + legLength + '.' + Alloy.Globals.sling.lowerTerminationCode + Alloy.Globals.sling.upperTerminationCode;
			}
		}else{
			
			Alloy.Globals.sling.partCode = 'RS0' + slingSize + '.' + Alloy.Globals.sling.legs + legLength + '.' + Alloy.Globals.sling.lowerTerminationCode;
		}
	}
	
	database.closeDb(db);
	
	Ti.API.info("createPartcode generated code: " + Alloy.Globals.sling.partCode);
	
	return Alloy.Globals.sling.partCode;
}
 function createPartcode() {
     Ti.API.info("createPartcode call");
     var gradeCode, limit, type, slingSize, slingSizeAuto8, slingSizeAuto10, Database = require("databaseObj"), database = new Database("SlingDB.sqlite"), db = database.openDb(), legLength = Math.ceil(parseFloat(Alloy.Globals.sling.nominalLength));
     limit = 45 === Alloy.Globals.sling.angle ? "limit45" : "limit60";
     if ("Chain" === Alloy.Globals.sling.type) {
         if (8 === Alloy.Globals.sling.grade) {
             gradeCode = "A";
             var newRow = db.execute('SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "8" AND ' + limit + " >= " + Alloy.Globals.sling.load + ' AND type = "c" ORDER BY ' + limit + " ASC LIMIT 1");
             if (newRow.isValidRow()) slingSize = newRow.fieldByName("size"); else {
                 Ti.API.info("Grade 8 Limited Exceeded");
                 Alloy.Globals.sling.limitExceeded = true;
             }
         } else if (10 === Alloy.Globals.sling.grade) {
             gradeCode = "X";
             var newRow = db.execute('SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "10" AND ' + limit + " >= " + Alloy.Globals.sling.load + ' AND type = "c" ORDER BY ' + limit + " ASC LIMIT 1");
             if (newRow.isValidRow()) slingSize = newRow.fieldByName("size"); else {
                 Ti.API.info("Grade 10 Limited Exceeded");
                 Alloy.Globals.sling.limitExceeded = true;
             }
         } else if ("Auto" === Alloy.Globals.sling.grade) {
             var row8 = db.execute('SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "8" AND ' + limit + " >= " + Alloy.Globals.sling.load + ' AND type = "c" ORDER BY ' + limit + " ASC LIMIT 1");
             Ti.API.info('Query: SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "8" AND ' + limit + " >= " + Alloy.Globals.sling.load + ' AND type = "' + type + '" ORDER BY ' + limit + " ASC LIMIT 1");
             if (row8.isValidRow()) {
                 slingSizeAuto8 = row8.fieldByName("size");
                 Ti.API.info("sling size 8: " + slingSizeAuto8);
             } else {
                 Ti.API.info("Auto 8 Limited Exceeded");
                 Alloy.Globals.sling.limitExceeded = true;
             }
             var row10 = db.execute('SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "10" AND ' + limit + " >= " + Alloy.Globals.sling.load + ' AND type = "c" ORDER BY ' + limit + " ASC LIMIT 1");
             Ti.API.info('Query: SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND grade = "10" AND ' + limit + " >= " + Alloy.Globals.sling.load + ' AND type = "' + type + '" ORDER BY ' + limit + " ASC LIMIT 1");
             if (row10.isValidRow()) {
                 slingSizeAuto10 = row10.fieldByName("size");
                 Ti.API.info("sling size 10: " + slingSizeAuto10);
             } else {
                 Ti.API.info("Auto 10 Limited Exceeded");
                 Alloy.Globals.sling.limitExceeded = true;
             }
         }
     } else {
         type = "r";
         var newRow = db.execute('SELECT * FROM WorkingLoadLimits WHERE legs = "' + Alloy.Globals.sling.legs + '" AND ' + limit + " >= " + Alloy.Globals.sling.load + ' AND type = "' + type + '" ORDER BY ' + limit + " ASC LIMIT 1");
         if (newRow.isValidRow()) slingSize = newRow.fieldByName("size"); else {
             Ti.API.info("Limited Exceeded");
             Alloy.Globals.sling.limitExceeded = true;
         }
     }
     "NONE" === Alloy.Globals.sling.shorteningDeviceCode && (Alloy.Globals.sling.shorteningDeviceCode = 0);
     "NONE" === Alloy.Globals.sling.lowerTerminationCode && (Alloy.Globals.sling.lowerTerminationCode = 0);
     "NONE" === Alloy.Globals.sling.upperTerminationCode && (Alloy.Globals.sling.upperTerminationCode = 0);
     if (!Alloy.Globals.sling.limitExceeded) {
         10 > slingSize && (slingSize = "0" + slingSize);
         10 > slingSizeAuto8 && (slingSizeAuto8 = "0" + slingSizeAuto8);
         10 > slingSizeAuto10 && (slingSizeAuto10 = "0" + slingSizeAuto10);
         10 > legLength && (legLength = "0" + legLength);
     }
     Alloy.Globals.sling.partCode = "Chain" === Alloy.Globals.sling.type ? "Auto" !== Alloy.Globals.sling.grade ? gradeCode + slingSize + "." + Alloy.Globals.sling.legs + legLength + "." + Alloy.Globals.sling.lowerTerminationCode + Alloy.Globals.sling.shorteningDeviceCode : "A" + slingSizeAuto8 + "." + Alloy.Globals.sling.legs + legLength + "." + Alloy.Globals.sling.lowerTerminationCode + Alloy.Globals.sling.shorteningDeviceCode + " or X" + slingSizeAuto10 + "." + Alloy.Globals.sling.legs + legLength + "." + Alloy.Globals.sling.lowerTerminationCode + Alloy.Globals.sling.shorteningDeviceCode : 1 === Alloy.Globals.sling.legs ? "NONE" === Alloy.Globals.sling.upperTerminationCode ? "RS0" + slingSize + "." + Alloy.Globals.sling.legs + legLength + "." + Alloy.Globals.sling.lowerTerminationCode : "RS0" + slingSize + "." + Alloy.Globals.sling.legs + legLength + "." + Alloy.Globals.sling.lowerTerminationCode + Alloy.Globals.sling.upperTerminationCode : "RS0" + slingSize + "." + Alloy.Globals.sling.legs + legLength + "." + Alloy.Globals.sling.lowerTerminationCode;
     database.closeDb(db);
     Ti.API.info("createPartcode generated code: " + Alloy.Globals.sling.partCode);
     return Alloy.Globals.sling.partCode;
 }
(function(){
	
	// Hide the back to dash button so that it cannot be clicked
	// until the quote has been sent
	$.backToDash.hide();
	$.backToDash.height = 0;
	
	var Database = require('databaseObj'),
		database = new Database('SlingDB.sqlite'),
		db = database.openDb(),
		legLength = Math.ceil(Alloy.Globals.sling.nominalLength),
		angle,
		grade,
		shortener,
		end,
		upper;
		
	Ti.API.info(Alloy.Globals.sling);
	
	// Set the IDs of the items that make up the chosen sling spec.
	// We will set these, based on what we we can find based on requirements, 
	// and use them later for displaying sling specification along side requirements.
	// We just store the IDs so we can retrieve them later and pass them to the quoteRequest API call.
	Alloy.Globals.wllAngle = null;
	Alloy.Globals.wllId = null;
	Alloy.Globals.slingId = null;
	
	// Find a part code form the phone database
	if( Alloy.Globals.sling.type === 'Chain' ){
		
		var angle,
			grade,
			shortener,
			end,
			upper;
			
		// Perform all query string changes here
		// Query changes to "IS NULL" if no termination is selected
		// The checks below allow for this
		if( Alloy.Globals.sling.angle === 45 ){
			angle = 'wll.limit45';
			Alloy.Globals.wllAngle = "limit45";
		}else{	
			angle = 'wll.limit60';
			Alloy.Globals.wllAngle = "limit60";
		}
		
		if( Alloy.Globals.sling.shorteningDeviceCode === "NONE"){
			shortener = 'AND s.shortener LIKE ""';
		}else{
			shortener = 'AND s.shortener LIKE "' + Alloy.Globals.sling.shorteningDeviceCode + '"';
		}
		
		if( Alloy.Globals.sling.lowerTerminationCode === 'NONE'){
			end = 'AND end = ""';
		}else{
			end = 'AND end = "' + Alloy.Globals.sling.lowerTerminationCode + '"';
		}
		
		if( Alloy.Globals.sling.upperTerminationCode === 'NONE'){
			upper = 'AND end = ""';
		}else{
			upper = 'AND end = "' + Alloy.Globals.sling.upperTerminationCode + '"';
		}
		
		// If grade is equal to 8 or 10 perform the query below
		// the query changes vastly for auto
		if( Alloy.Globals.sling.grade === 8 || Alloy.Globals.sling.grade === 10 ){
			
			grade = Alloy.Globals.sling.grade;
			
			var sql = 'SELECT *, wll.id AS wllId, s.id AS slingId ' +
						'FROM WorkingLoadLimits AS wll, Slings AS s ' +
						'WHERE ' + angle + ' >= ' + Alloy.Globals.sling.load + ' ' + 
						'AND wll.type="c" ' + 
						'AND wll.legs = ' + Alloy.Globals.sling.legs + ' ' +
						'AND s.legs=wll.legs ' + 
						'AND (wll.grade = ' + grade + ') ' + 
						'AND (s.size=wll.size AND s.grade=wll.grade ' + end + ' ' + shortener + ' AND s.length = "' + legLength + '") ' + 
						'GROUP BY wll.id ' + 
						'ORDER BY ' + angle + ' ' + 
						'LIMIT 1';
			
			var row = db.execute(sql);
			
			Ti.API.info(sql);
			
			if( row.isValidRow() ){
				
				//Ti.API.info('part code: ' + row.fieldByName('code') );
				
				Alloy.Globals.wllId = row.fieldByName("wllId");
				Alloy.Globals.slingId = row.fieldByName("slingId");
				
				Alloy.Globals.sling.partCode = row.fieldByName('code');
				Alloy.Globals.sling.quotedPrice = row.fieldByName('price');
				Alloy.Globals.sling.slingDescription = row.fieldByName('description');
				
			}else{
				
				Ti.API.info('row 10 create part code');
				createPartcode();
			}
		
		// If the grade is "auto" we have to perform two querys and find
		// the best sling for the job out of the two
		//
		// If the price is the same for a grade 8 adn grade 10 sling
		// then always pick the grade 10 as it is better value for money
		}else if( Alloy.Globals.sling.grade === 'Auto' ){
			
			var price8,
				code8,
				description8,
				wllId8,
				slingId8,
				price10,
				code10,
				description10,
				wllId10,
				slingId10,
				angle,
				grade,
				shortener,
				end,
				upper;
			
			// Perform all query string changes here
			// Query changes to "IS NULL" if no termination is selected
			// The checks below allow for this
			if( Alloy.Globals.sling.angle === 45 ){
				angle = 'wll.limit45';
				Alloy.Globals.wllAngle = "limit45";
			}else{	
				angle = 'wll.limit60';
				Alloy.Globals.wllAngle = "limit60";
			}
			
			if( Alloy.Globals.sling.shorteningDeviceCode === "NONE"){
				shortener = 'AND s.shortener IS NULL';
			}else{
				shortener = 'AND s.shortener LIKE "' + Alloy.Globals.sling.shorteningDeviceCode + '"';
			}
			
			if( Alloy.Globals.sling.lowerTerminationCode === 'NONE'){
				end = 'AND s.end IS NULL';
			}else{
				end = 'AND s.end = "' + Alloy.Globals.sling.lowerTerminationCode + '"';
			}
			
			if( Alloy.Globals.sling.upperTerminationCode === 'NONE'){
				upper = 'AND s.end IS NULL';
			}else{
				upper = 'AND s.end = "' + Alloy.Globals.sling.upperTerminationCode + '"';
			}
			
			// Perform the query for a grade 8 sling
			var sql = 'SELECT *, wll.id AS wllId, s.id AS slingId ' + 
						'FROM WorkingLoadLimits AS wll, Slings AS s ' + 
						'WHERE ' + angle + ' >= ' +  Alloy.Globals.sling.load + ' ' +
						'AND wll.type="c" ' + 
						'AND wll.legs = ' + Alloy.Globals.sling.legs + ' ' + 
						'AND s.legs = wll.legs ' + 
						'AND (wll.grade = "8" ) ' + 
						'AND (s.size=wll.size AND s.grade=wll.grade ' + end + ' ' + shortener + ' AND s.length = ' + legLength + ') ' + 
						'GROUP BY wll.id ' + 
						'ORDER BY ' + angle +' ASC';
			
			var row8 = db.execute(sql);
			
			// Check to see if a chain was found
			if( row8.isValidRow() ){

				price8 = row8.fieldByName('price');
				code8 = row8.fieldByName('code');
				description8 = row8.fieldByName('description');
				wllId8 = row8.fieldByName("wllId");
				slingId8 = row8.fieldByName("slingId");
			
			}else{
				
				// This else is purely for dev purposes, can be removed
				// before final build	
				Ti.API.info('Row 8 not valid');
			}
			
			var sql = 'SELECT *, wll.id AS wllId, s.id AS slingId ' + 
						'FROM WorkingLoadLimits AS wll, Slings AS s ' + 
						'WHERE ' + angle + ' >= ' + Alloy.Globals.sling.load + ' ' + 
						'AND wll.type="c" ' + 
						'AND wll.legs = ' + Alloy.Globals.sling.legs + ' ' + 
						'AND s.legs=wll.legs ' + 
						'AND (wll.grade = "10" ) ' + 
						'AND (s.size=wll.size AND s.grade=wll.grade ' + end + ' ' + shortener + ' AND s.length = ' + legLength + ') ' + 
						'GROUP BY wll.id ' + 
						'ORDER BY ' + angle +' ASC';
			
			var row10 = db.execute(sql);

			if( row10.isValidRow() ){

				price10 = row10.fieldByName('price');
				code10 = row10.fieldByName('code');
				description10 = row10.fieldByName('description');
				wllId10 = row10.fieldByName("wllId");
				slingId10 = row10.fieldByName("slingId");
			
			}else{
				
				// This else is purely for dev purposes, can be removed
				// before final build
				Ti.API.info('Row 10 not valid');
			}
			
			// If price is undefined for both grade 8 and grade 10
			// build a part code.
			if( (price8 === undefined || price8 === null) && (price10 === undefined || price10 === null) ){
				
				Alloy.Globals.sling.quotedPrice = null;
				Alloy.Globals.sling.slingDescription = null;
				
				createPartcode();
			
			}else if( price10 === undefined || price10 === null ){
				
				Alloy.Globals.sling.partCode = code8;
				Alloy.Globals.sling.quotedPrice = price8;
				Alloy.Globals.sling.slingDescription = description8;
				Alloy.Globals.wllId = wllId8;
				Alloy.Globals.slingId = slingId8;
			
			}else if( price8 === undefined || price8 === null ){
				
				Alloy.Globals.sling.partCode = code10;
				Alloy.Globals.sling.quotedPrice = price10;
				Alloy.Globals.sling.slingDescription = description10;
				Alloy.Globals.wllId = wllId10;
				Alloy.Globals.slingId = slingId10;
			
			}else if( parseInt(price8) < parseInt(price10) ){
				
				Alloy.Globals.sling.partCode = code8;
				Alloy.Globals.sling.quotedPrice = price8;
				Alloy.Globals.sling.slingDescription = description8;
				Alloy.Globals.sling.gradeCode = 8;
				Alloy.Globals.wllId = wllId8;
				Alloy.Globals.slingId = slingId8;
			
			}else if( parseInt(price8) > parseInt(price10) ){
				
				Alloy.Globals.sling.partCode = code10;
				Alloy.Globals.sling.quotedPrice = price10;
				Alloy.Globals.sling.slingDescription = description10;
				Alloy.Globals.sling.gradeCode = 10;
				Alloy.Globals.wllId = wllId10;
				Alloy.Globals.slingId = slingId10;	
			
			}else if( parseInt(price8) === parseInt(price10) ){
				
				Alloy.Globals.sling.partCode = code10;
				Alloy.Globals.sling.quotedPrice = price10;
				Alloy.Globals.sling.slingDescription = description10;
				Alloy.Globals.sling.gradeCode = 10;
				Alloy.Globals.wllId = wllId10;
				Alloy.Globals.slingId = slingId10;
			}
		}
	
	// Currently William Hackett have no part code prices for Wire
	// Rope slings.
	}else if( Alloy.Globals.sling.type === 'Wire Rope' ){
		
		var angle,
			grade,
			shortener,
			end,
			upper;
			
		// Perform all query string changes here
		// Query changes to "IS NULL" if no termination is selected
		// The checks below allow for this
		if( Alloy.Globals.sling.angle === 45 ){
			angle = 'wll.limit45';
			Alloy.Globals.wllAngle = "limit45";
		}else{	
			angle = 'wll.limit60';
			Alloy.Globals.wllAngle = "limit60";
		}		
		
		if( Alloy.Globals.sling.lowerTerminationCode === 'NONE'){
			end = ' AND end = "" ';
		}else{
			end = ' AND end = "' + Alloy.Globals.sling.lowerTerminationCode + '" ';
		}
		
		if( Alloy.Globals.sling.upperTerminationCode === 'NONE' || Alloy.Globals.sling.upperTerminationCode === null){
			upper = ' AND end_b = "" ';
		}else{
			upper = ' AND end_b = "' + Alloy.Globals.sling.upperTerminationCode + '" ';
		}
		
		var sql = "SELECT *, wll.id AS wllId, s.id AS slingId " + 
				"FROM WorkingLoadLimits AS wll, Slings AS s " +
				"WHERE " + angle + " >= " + Alloy.Globals.sling.load + " " +
				"AND wll.type='r' " + 
				"AND wll.legs = " + Alloy.Globals.sling.legs + " " +
				"AND s.grade = '' " +
				"AND s.legs = wll.legs " +
				"AND s.size = wll.size " + 
				"AND s.length = " + legLength + " " +
				end + 
				upper + 
				"GROUP BY wll.id " +
				"ORDER BY " + angle + " ASC " +
				"LIMIT 1";
				
		Ti.API.info(sql);
		
		var row = db.execute(sql);
		
		if( row.isValidRow() ){
			
			Ti.API.info('DB part code: ' + row.fieldByName('code') );
			
			Alloy.Globals.sling.partCode = row.fieldByName('code');
			Alloy.Globals.sling.quotedPrice = row.fieldByName('price');
			Alloy.Globals.sling.slingDescription = row.fieldByName('description');
			Alloy.Globals.wllId = row.fieldByName("wllId");
			Alloy.Globals.slingId = row.fieldByName("slingId");
			
		}else{
			
			Ti.API.info('wire rope create part code');
			createPartcode();
			
			Alloy.Globals.sling.grade = null;
			Alloy.Globals.sling.quotedPrice = null;
			Alloy.Globals.sling.slingDescription = null;
			
		}
		
	}
	
	outputDetails( Alloy.Globals.sling.type, Alloy.Globals.sling.grade, Alloy.Globals.sling.legs, Alloy.Globals.sling.load, Alloy.Globals.sling.nominalLength, Alloy.Globals.sling.slingDescription, Alloy.Globals.sling.partCode, Alloy.Globals.sling.quotedPrice);
	
	database.closeDb(db);
	
	checkImage(Alloy.Globals.sling.partCode);
	checkSpec();
	
})();