onload: function onLoad() {
		        Ti.API.info("Loaded: " + this.status + ": " + this.responseText);
		        
		        var json_resp = this.responseText;
		        var JSONdata = JSON.parse(json_resp);
		        Ti.API.info(JSONdata.devices.length);
		        if(JSONdata.devices.length==0){
		        	Ti.API.info(tagid+' does not exist****************');
		        	toast.show_toast('Device does not exists in DB.',Ti.UI.NOTIFICATION_DURATION_SHORT);
		        	Ti.App.removeEventListener('checkin code scanned device info',callback2);
		        	//scan_done_tick2.show();
		        }
		        else{
		        	//If it exists in DB
		        	Ti.API.info(tagid+' exist in DB******************');
		        	for (var i=0;i<JSONdata.devices.length;i++){
		        		var devices = JSONdata.devices[i];
		        		//Assigning variables values which are not going to change to original values
		        		update_name=devices.name;
		        		update_platform=devices.platform;
		        		update_os_ver=devices.os_ver;
		        		update_make=devices.make;
		        		update_model=devices.model;
		        		update_serial_number=devices.serial_number;
		        		update_IMEI=devices.IMEI;
		        		update_phone_no=devices.phone_no;
		        		update_notes=devices.notes;
		        		update_network=devices.network;
		        		update_arch=devices.arch;
		        		update_registered=devices.registered;
		        		update_inventoried=devices.inventoried;
		        		update_devicetype=devices.devicetype;
		        		update_tag_id=devices.tag_id;
		        		update_user_email=devices.user_email;
		        		
		        		DBID=devices.id;
		        		//Checking if the device is already checked out by checking the checked out field.
		        		if(devices.checkedin=='true'){
		        			toast.show_toast('You can not checkin this device as it is already checked in.',Ti.UI.NOTIFICATION_DURATION_SHORT);
		        			Ti.App.removeEventListener('checkin code scanned device info',callback2);
		        		}
		        		//If the device is not checked out then do this
		        		else{     			
		        			checked_out = 'false';
		        			user = '******';
		        			checkout_date = 'NA';
		        			email = 'NA';
		        			checked_in = 'true';
		        			checkedin_on = date.getDate();
							//Calling the update device function
		        			update_device(DBID,update_name,update_platform,update_os_ver,update_make,update_model,update_serial_number,update_IMEI,update_phone_no,update_notes,update_network,update_arch,update_registered,update_inventoried,update_devicetype,update_tag_id,checked_out,user,checkout_date,email,checked_in,checkedin_on);
		        			//Sending check in email to user who checked out the device
		        			checkin_email('checkin_template',update_user_email,update_checkedout_by,update_name,update_model,update_platform,update_os_ver);
		        			//Sending check in email to administrator
		        			checkin_email('checkin_template_admin',links_keys.admin_email,links_keys.admin_name,update_name,update_model,update_platform,update_os_ver);
		        			Ti.App.removeEventListener('checkin code scanned device info',callback2);
		        		}
		        	}    
		        }
		    },
		    onload: function onLoad() {
		        Ti.API.info("Loaded: " + this.status + ": " + this.responseText);
		  
		        var json_resp = this.responseText;
		        var JSONdata = JSON.parse(json_resp);
		        Ti.API.info("Device Count: "+JSONdata.devices.length);
		        for (var i=0;i<JSONdata.devices.length;i++){
		        	var devices = JSONdata.devices[i];    	
		        	
		        	platform_lbl.text = "PLATFORM:    "+devices.platform;
		        	os_ver_lbl.text = "OS VER:    "+devices.os_ver;
		        	model_lbl.text = "MODEL:    "+devices.model;
		        	name_lbl.text = "NAME:    "+devices.name;
		        	
		        	listDataSet.push({ platform: {text: platform_lbl.text,height:50}, 
						os_ver: {text: os_ver_lbl.text,height:46}, 
						model: {text: model_lbl.text,height:45}, 
						name: {text: name_lbl.text,height:45}, 
						pic: {image: platform_imageview.backgroundImage},
						editpic: {image: edit_imageview.backgroundImage},
						deletepic: {image: delete_imageview.backgroundImage},
						properties:{title:devices.name,itemID:devices.name,searchableText:devices.platform+devices.os_ver+devices.model+devices.name,height:140}
						});
					
					listSection.setItems(listDataSet);
					sections.push(listSection);
					listView.setSections(sections);
		        	
		        	//Change the header of the table view	
		        	listSection.headerTitle=JSONdata.devices.length+" iOS device/s found";
		        }   
		        toast.show_toast("Found "+JSONdata.devices.length+" device/s.",Ti.UI.NOTIFICATION_DURATION_SHORT);
		        
		    },
	        }, function (e) {
	            if (e.success) {
	                var user = e.users[0];
	                //checking if user is an admin to login if not then don't allow login
	                if(user.admin == 'true'){
		                username_txt.value = password_txt.value = '';
						landing_pg_admin.landing();
						//close the admin login window so that it does not show up when logging out.
						main_login_win.close();
					}
					else{
						//Logging out if user is not an admin
						Cloud.Users.logout(function (e) {
					        if (e.success) {
					            Ti.API.info('Blocked nonadmin access');
					        }
					        else {
					            alert((e.error && e.message) || e);
					        }
					    });
						username_txt.value = password_txt.value = '';
						toast.show_toast('Only admin has access.\nYou are not an admin.',Ti.UI.NOTIFICATION_DURATION_SHORT);
					}
	            }
	            else {
	                 alert('Error:\n' +((e.error && e.message) || JSON.stringify(e)));
	            }
	        });
		    onload: function onLoad() {
		        Ti.API.info("Loaded: " + this.status + ": " + this.responseText);
		        if((this.status) == 201){
		        	//alert("Device added successfully");
		        	toast.show_toast("Device added successfully",Ti.UI.NOTIFICATION_DURATION_SHORT);
		        	device_add_win.close();
		        }
		    },
		    onload: function onLoad() {
		        Ti.API.info("Loaded: " + this.status + ": " + this.responseText);
		        if(this.status=='204'){
		        	toast.show_toast("Device "+name+" checked in successfully.",Ti.UI.NOTIFICATION_DURATION_SHORT);
		        }
		        else{
		        	alert('Please check for Status: '+this.status);
		        }
		    },
	Cloud.Users.logout(function (e) {
        if (e.success) {
            toast.show_toast('Logged Out Successfully',Ti.UI.NOTIFICATION_DURATION_SHORT);
			win.close();
        }
        else {
            alert((e.error && e.message) || e);

        }
    });
exports.nw_status=function(){
	if(!Ti.Network.getOnline()){
		toast.show_toast('Device is not connected to the internet.\nApp usage will be hampered',Ti.UI.NOTIFICATION_DURATION_LONG);
		Ti.API.warn('Device is not connected to the internet.\nApp usage will be hampered');
	}
	else{
		Ti.Network.addEventListener('change',function(){
			if(!Ti.Network.getOnline()){
				toast.show_toast('Device is not connected to the internet.\nApp usage will be hampered',Ti.UI.NOTIFICATION_DURATION_LONG);
				Ti.API.warn('Device is not connected to the internet.\nApp usage will be hampered');
			}
		});
	}
};
			    onload: function onLoad() {
			        //alert("Loaded: " + this.status + ": " + this.responseText);
			        Ti.API.info("Loaded: " + this.status + ": " + this.responseText);
			        
			        var json_resp = this.responseText;
			        var JSONdata = JSON.parse(json_resp);
			        Ti.API.info(JSONdata.devices.length);
			        if(JSONdata.devices.length==0){
			        	Ti.API.info(tagid+' does not exist****************');
			        	add();
			        }
			        else{
			        	Ti.API.info(tagid+' exist, skipped adding to DB******************');
			        	toast.show_toast('Device with given tag id already exists in DB. Please generate a different tag id.\nSkipping import',Ti.UI.NOTIFICATION_DURATION_SHORT);
			        }
			    },
			    onload: function onLoad() {
			        //alert("Loaded: " + this.status + ": " + this.responseText);
			        Ti.API.info("Loaded: " + this.status + ": " + this.responseText);
			        
			        var json_resp = this.responseText;
			        var JSONdata = JSON.parse(json_resp);
			        Ti.API.info(JSONdata.devices.length);
			        if(JSONdata.devices.length==0){
			        	Ti.API.info(serial+' does not exist****************');
			        	//If device with serial number does not exists already in the DB then check for tag ID
			        	check_exists_tagid(tagid_txtfld.value);
			        }
			        else{
			        	Ti.API.info(serial+' exist, skipped adding to DB******************');
			        	toast.show_toast('Device with the given serial already exists in DB.\nSkipping import',Ti.UI.NOTIFICATION_DURATION_SHORT);
			        }
			    },
			    onload: function onLoad() {
			        //alert("Loaded: " + this.status + ": " + this.responseText);
			        Ti.API.info("Loaded: " + this.status + ": " + this.responseText);
			        
			        var json_resp = this.responseText;
			        var JSONdata = JSON.parse(json_resp);
			        Ti.API.info(JSONdata.devices.length);
			        if(JSONdata.devices.length==0){
			        	Ti.API.info('Device '+name+' does not exist in DB****************');
			        	toast.show_toast('Device with name '+name+' does not exist in DB',Ti.UI.NOTIFICATION_DURATION_SHORT);
			        }
			        else{
			        	Ti.API.info('Device '+name+' exist in DB******************');
			        	for (var i=0;i<JSONdata.devices.length;i++){
		        			var devices = JSONdata.devices[i];
				        	//Filling the text fields with the device data we got after the search
				        	dbid = devices.id;
				        	tagid_txtfld.value = devices.tag_id;
				        	name_txtfld.value = devices.name;
				        	platform_txtfld.value = devices.platform;
				        	os_ver_txtfld.value = devices.os_ver;
				        	make_txtfld.value = devices.make;
				        	model_txtfld.value = devices.model;
				        	ser_no_txtfld.value = devices.serial_number;
				        	IMEI_txtfld.value  = devices.IMEI;
				        	phoneno_txtfld.value = devices.phone_no;
				        	notes_txtfld.value = devices.notes;
				        	network_txtfld.value = devices.network;
				        	arch_txtfld.value = devices.arch;
				        	registered_txtfld.value = devices.registered;
				        	devicetype_txtfld.value = devices.devicetype;
				        	checkedout_txtfld.value = devices.checkedout;
				        	checkedoutby_txtfld.value = devices.checkedout_by;
				        	checkedouton_txtfld.value = devices.checkedout_on;
				        	useremail_txtfld.value = devices.user_email;
				        	checkedin_txtfld.value = devices.checkedin;
				        	checkedinon_txtfld.value = devices.checkedin_on;			        	
				        }
			        }
			    },
		    onload: function onLoad() {
		        Ti.API.info("Loaded: " + this.status + ": " + this.responseText);
		        toast.show_toast('Device with name '+name+' edited successfully',Ti.UI.NOTIFICATION_DURATION_SHORT);
		    },