﻿var db,edit_record=0,errormsg=[];errormsg[0]="Local Database error:";errormsg[1]="Failed to open the local database. There may not be enough space left in this domain's quota";errormsg[2]="Can't open a database.  Get a Browser that supports HTML 5 like Googe Chrome";errormsg[3]="Unable to get list of pages from your database";errormsg[4]="Unable to update record.";errormsg[5]="Unable to create new record";errormsg[6]="Unable to delete record";errormsg[7]="Unable to delete all records in table";var SQLtxt=[];SQLtxt[1]="DROP TABLE tblContacts";SQLtxt[2]="CREATE TABLE IF NOT EXISTS tblContacts (ContactId INTEGER PRIMARY KEY ASC, ContactName TEXT, ContactPhone TEXT)";SQLtxt[3]="INSERT INTO tblContacts (ContactId, ContactName, ContactPhone) VALUES (null,?,?)";SQLtxt[4]="SELECT ContactId, ContactName, ContactPhone FROM tblContacts";SQLtxt[5]="UPDATE tblContacts SET ContactName=?, ContactPhone=? WHERE ContactId=?";SQLtxt[6]="DELETE FROM tblContacts WHERE ContactId=?";SQLtxt[7]="DELETE FROM tblContacts";SQLtxt[8]="SELECT COUNT(*) AS TotCnt FROM tblContacts";var inetstatus="OFFLINE";function check_inet(){inetstatus=navigator.onLine?"ONLINE":"OFFLINE";document.addEventListener("offline",function(){inetstatus=navigator.onLine?"ONLINE":"OFFLINE"},false);document.addEventListener("online",function(){inetstatus=navigator.onLine?"ONLINE":"OFFLINE"},false)}function Error_DB(a,b){var c=errormsg[a]+"\nDetails:"+b.message;alert(c)}function Open_DB(b,d,a,c){try{if(window.openDatabase){db=openDatabase(b,d,a,c);if(!db)Error_DB(1,null)}else Error_DB(2,null)}catch(e){Error_DB(0,e)}}function build_db(){if(db)db.transaction(function(a){a.executeSql(SQLtxt[2])})}function adddata(){db.transaction(function(a){a.executeSql(SQLtxt[8],[],function(c,a){var b=a.rows.item(0);if(b["TotCnt"]<1)adddata_step2();get_list()},function(b,a){Error_DB(3,a)})})}function adddata_step2(){db.transaction(function(b){var a=["Walter Moore","727.827.9035"];b.executeSql(SQLtxt[3],a,function(){},function(b,a){Error_DB(0,a)});a=["Betty Smith","727.967.1111"];b.executeSql(SQLtxt[3],a,function(){},function(b,a){Error_DB(0,a)});a=["John Doe","416.927.1039"];b.executeSql(SQLtxt[3],a,function(){},function(b,a){Error_DB(0,a)});a=["Bent Elbow","888.642.8925"];b.executeSql(SQLtxt[3],a,function(){},function(b,a){Error_DB(0,a)});a=["Walter Moore","1.705.424.6722"];b.executeSql(SQLtxt[3],a,function(){},function(b,a){Error_DB(0,a)})})}function get_list(){db.transaction(function(a){a.executeSql(SQLtxt[4],[],function(b,a){loadlist(a)},function(b,a){Error_DB(3,a)})})}function loadlist(c){var d="";for(var b=0;b<c.rows.length;++b){var a=c.rows.item(b);d+="<br /><span style='cursor:pointer' onclick='return edit_contact(\""+a["ContactId"]+'","'+a["ContactName"]+'","'+a["ContactPhone"]+"\")'>Edit</span> / <span style='cursor:pointer' onclick='return delete_contact("+a["ContactId"]+")'>Delete</span> -- Name: "+a["ContactName"]+"-Phone: "+a["ContactPhone"]}document.getElementById("currentlist").innerHTML=d}function delete_contact(a){db.transaction(function(b){b.executeSql(SQLtxt[6],[a],get_list(),function(b,a){Error_DB(6,a)})})}function delete_alldata(){db.transaction(function(a){a.executeSql(SQLtxt[7],[],get_list(),function(b,a){Error_DB(7,a)})})}function store_local(){if(edit_record>0){var a=[document.getElementById("ContactName").value,document.getElementById("ContactPhone").value,edit_record];db.transaction(function(b){b.executeSql(SQLtxt[5],a,Update_successful(),function(b,a){Error_DB(4,a)})})}else{var a=[document.getElementById("ContactName").value,document.getElementById("ContactPhone").value];db.transaction(function(b){b.executeSql(SQLtxt[3],a,Update_successful(),function(b,a){Error_DB(5,a)})})}return false}function Update_successful(){document.getElementById("ContactName").value="";document.getElementById("ContactPhone").value="";edit_record=0;get_list()}function edit_contact(c,b,a){document.getElementById("ContactName").value=b;document.getElementById("ContactPhone").value=a;edit_record=c;return false}function show_demo(){document.getElementById("demo").style.display="";document.getElementById("DemoDesc").style.display="none"}function page_load(){check_inet();Open_DB("WMooreDB","1.0","WMooreDB",5e6);build_db();adddata()}document.onload=page_load()
